1 | /* |
---|
2 | |
---|
3 | Copyright 2014 S. Razi Alavizadeh |
---|
4 | Copyright 2014 Adam Reichold |
---|
5 | |
---|
6 | This file is part of qpdfview. |
---|
7 | |
---|
8 | qpdfview is free software: you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation, either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | qpdfview is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | GNU General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef SEARCHMODEL_H |
---|
24 | #define SEARCHMODEL_H |
---|
25 | |
---|
26 | #include <QAbstractItemModel> |
---|
27 | #include <QCache> |
---|
28 | #include <QFutureWatcher> |
---|
29 | #include <QRectF> |
---|
30 | |
---|
31 | namespace qpdfview |
---|
32 | { |
---|
33 | |
---|
34 | class DocumentView; |
---|
35 | |
---|
36 | class SearchModel : public QAbstractItemModel |
---|
37 | { |
---|
38 | Q_OBJECT |
---|
39 | |
---|
40 | public: |
---|
41 | static SearchModel* instance(); |
---|
42 | ~SearchModel(); |
---|
43 | |
---|
44 | |
---|
45 | QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const; |
---|
46 | |
---|
47 | QModelIndex parent(const QModelIndex& child) const; |
---|
48 | |
---|
49 | int rowCount(const QModelIndex& parent = QModelIndex()) const; |
---|
50 | int columnCount(const QModelIndex& parent = QModelIndex()) const; |
---|
51 | |
---|
52 | enum |
---|
53 | { |
---|
54 | CountRole = Qt::UserRole + 1, |
---|
55 | ProgressRole, |
---|
56 | PageRole, |
---|
57 | RectRole, |
---|
58 | TextRole, |
---|
59 | MatchCaseRole, |
---|
60 | WholeWordsRole, |
---|
61 | MatchedTextRole, |
---|
62 | SurroundingTextRole |
---|
63 | }; |
---|
64 | |
---|
65 | QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; |
---|
66 | |
---|
67 | |
---|
68 | DocumentView* viewForIndex(const QModelIndex& index) const; |
---|
69 | |
---|
70 | |
---|
71 | bool hasResults(DocumentView* view) const; |
---|
72 | bool hasResultsOnPage(DocumentView* view, int page) const; |
---|
73 | int numberOfResultsOnPage(DocumentView* view, int page) const; |
---|
74 | QList< QRectF > resultsOnPage(DocumentView* view, int page) const; |
---|
75 | |
---|
76 | enum FindDirection |
---|
77 | { |
---|
78 | FindNext, |
---|
79 | FindPrevious |
---|
80 | }; |
---|
81 | |
---|
82 | QPersistentModelIndex findResult(DocumentView* view, const QPersistentModelIndex& currentResult, int currentPage, FindDirection direction) const; |
---|
83 | |
---|
84 | void insertResults(DocumentView* view, int page, const QList< QRectF >& resultsOnPage); |
---|
85 | void clearResults(DocumentView* view); |
---|
86 | |
---|
87 | void updateProgress(DocumentView* view); |
---|
88 | |
---|
89 | protected slots: |
---|
90 | void on_fetchSurroundingText_finished(); |
---|
91 | |
---|
92 | private: |
---|
93 | Q_DISABLE_COPY(SearchModel) |
---|
94 | |
---|
95 | static SearchModel* s_instance; |
---|
96 | SearchModel(QObject* parent = 0); |
---|
97 | |
---|
98 | QList< DocumentView* > m_views; |
---|
99 | |
---|
100 | QModelIndex findView(DocumentView* view) const; |
---|
101 | QModelIndex findOrInsertView(DocumentView* view); |
---|
102 | |
---|
103 | |
---|
104 | typedef QPair< int, QRectF > Result; |
---|
105 | typedef QList< Result > Results; |
---|
106 | |
---|
107 | QHash< DocumentView*, Results* > m_results; |
---|
108 | |
---|
109 | |
---|
110 | typedef QPair< DocumentView*, QByteArray > TextCacheKey; |
---|
111 | typedef QPair< QString, QString > TextCacheObject; |
---|
112 | |
---|
113 | struct TextJob |
---|
114 | { |
---|
115 | TextCacheKey key; |
---|
116 | TextCacheObject* object; |
---|
117 | |
---|
118 | TextJob() : key(), object(0) {} |
---|
119 | TextJob(const TextCacheKey& key, TextCacheObject* object) : key(key), object(object) {} |
---|
120 | |
---|
121 | }; |
---|
122 | |
---|
123 | typedef QFutureWatcher< TextJob > TextWatcher; |
---|
124 | |
---|
125 | mutable QCache< TextCacheKey, TextCacheObject > m_textCache; |
---|
126 | mutable QHash< TextCacheKey, TextWatcher* > m_textWatchers; |
---|
127 | |
---|
128 | QString fetchMatchedText(DocumentView* view, const Result& result) const; |
---|
129 | QString fetchSurroundingText(DocumentView* view, const Result& result) const; |
---|
130 | |
---|
131 | const TextCacheObject* fetchText(DocumentView* view, const Result& result) const; |
---|
132 | |
---|
133 | static TextCacheKey textCacheKey(DocumentView* view, const Result& result); |
---|
134 | static TextJob textJob(const TextCacheKey& key, const Result& result); |
---|
135 | |
---|
136 | }; |
---|
137 | |
---|
138 | } // qpdfview |
---|
139 | |
---|
140 | #endif // SEARCHMODEL_H |
---|