basic search support

This commit is contained in:
Marco Martin 2012-05-15 12:12:56 +02:00
parent c157463565
commit 97222282a9
3 changed files with 119 additions and 5 deletions

View file

@ -130,8 +130,9 @@ PlasmaComponents.Page {
GridView {
id: resultsGrid
anchors.fill: parent
clip: true
model: documentItem.pageCount
model: documentItem.matchingPages
cellWidth: theme.defaultFont.mSize.width * 14
cellHeight: theme.defaultFont.mSize.height * 12
@ -182,6 +183,31 @@ PlasmaComponents.Page {
}
}
highlight: PlasmaComponents.Highlight {}
header: PlasmaComponents.ToolBar {
width: resultsGrid.width
height: searchField.height + 10
MobileComponents.ViewSearch {
id: searchField
anchors.centerIn: parent
busy: documentItem.searchInProgress
onSearchQueryChanged: {
print(searchQuery)
if (searchQuery.length > 2) {
documentItem.searchText(searchQuery)
} else {
documentItem.resetSearch()
}
}
}
PlasmaComponents.Label {
anchors {
left: searchField.right
verticalCenter: searchField.verticalCenter
}
visible: documentItem.matchingPages.length == 0
text: i18n("No results found.")
}
}
}
}

View file

@ -19,15 +19,17 @@
#include "documentitem.h"
#include <okular/core/document.h>
#include <okular/core/page.h>
DocumentItem::DocumentItem(QObject *parent)
: QObject(parent)
: QObject(parent),
m_searchInProgress(false)
{
Okular::Settings::instance("okularproviderrc");
m_document = new Okular::Document(0);
connect(m_document, SIGNAL(searchFinished(int,Okular::Document::SearchStatus)),
this, SLOT(searchFinished(int,Okular::Document::SearchStatus)));
}
@ -41,6 +43,11 @@ void DocumentItem::setPath(const QString &path)
//TODO: remote urls
m_document->openDocument(path, KUrl(), KMimeType::findByUrl(KUrl(path)));
m_matchingPages.clear();
for (uint i = 0; i < m_document->pages(); ++i) {
m_matchingPages << (int)i;
}
emit matchingPagesChanged();
emit pathChanged();
emit pageCountChanged();
emit openedChanged();
@ -61,6 +68,47 @@ int DocumentItem::pageCount() const
return m_document->pages();
}
QList<int> DocumentItem::matchingPages() const
{
return m_matchingPages;
}
bool DocumentItem::isSearchInProgress() const
{
return m_searchInProgress;
}
void DocumentItem::searchText(const QString &text)
{
if (text.isEmpty()) {
resetSearch();
return;
}
m_document->resetSearch(PAGEVIEW_SEARCH_ID);
m_document->searchText(PAGEVIEW_SEARCH_ID, text, 1, Qt::CaseInsensitive,
Okular::Document::AllDocument, true, QColor(100,100,200,40), true);
if (!m_searchInProgress) {
m_searchInProgress = true;
emit searchInProgressChanged();
}
}
void DocumentItem::resetSearch()
{
m_document->resetSearch(PAGEVIEW_SEARCH_ID);
m_matchingPages.clear();
for (uint i = 0; i < m_document->pages(); ++i) {
m_matchingPages << (int)i;
}
if (m_searchInProgress) {
m_searchInProgress = false;
emit searchInProgressChanged();
}
emit matchingPagesChanged();
}
Okular::Document *DocumentItem::document()
{
return m_document;
@ -75,6 +123,29 @@ Observer *DocumentItem::observerFor(int id)
return m_observers.value(id);
}
void DocumentItem::searchFinished(int id, Okular::Document::SearchStatus endStatus)
{
Q_UNUSED(endStatus)
if (id != PAGEVIEW_SEARCH_ID) {
return;
}
m_matchingPages.clear();
for (uint i = 0; i < m_document->pages(); ++i) {
if (m_document->page(i)->hasHighlights(id)) {
m_matchingPages << (int)i;
}
}
if (m_searchInProgress) {
m_searchInProgress = false;
emit searchInProgressChanged();
}
emit matchingPagesChanged();
}
//Observer
Observer::Observer(DocumentItem *parent, int id)
@ -94,5 +165,4 @@ void Observer::notifyPageChanged(int page, int flags)
emit pageChanged(page, flags);
}
#include "documentitem.moc"

View file

@ -24,6 +24,7 @@
#include "settings.h"
#include <okular/core/document.h>
#include <okular/core/observer.h>
namespace Okular {
@ -39,6 +40,8 @@ class DocumentItem : public QObject
Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
Q_PROPERTY(bool opened READ isOpened NOTIFY openedChanged)
Q_PROPERTY(int pageCount READ pageCount NOTIFY pageCountChanged)
Q_PROPERTY(bool searchInProgress READ isSearchInProgress NOTIFY searchInProgressChanged)
Q_PROPERTY(QList<int> matchingPages READ matchingPages NOTIFY matchingPagesChanged)
public:
@ -52,6 +55,14 @@ public:
int pageCount() const;
bool isSearchInProgress() const;
QList<int> matchingPages() const;
//Those could be a property, but maybe we want to have parameter for searchText
Q_INVOKABLE void searchText(const QString &text);
Q_INVOKABLE void resetSearch();
//Internal, not binded to qml
Okular::Document *document();
Observer *observerFor(int id);
@ -60,10 +71,17 @@ Q_SIGNALS:
void pathChanged();
void pageCountChanged();
void openedChanged();
void searchInProgressChanged();
void matchingPagesChanged();
private Q_SLOTS:
void searchFinished(int id, Okular::Document::SearchStatus endStatus);
private:
Okular::Document *m_document;
QHash <int, Observer *> m_observers;
QList<int> m_matchingPages;
bool m_searchInProgress;
};
class Observer : public QObject, public Okular::DocumentObserver