1 | /* |
---|
2 | |
---|
3 | Copyright 2012-2015 Adam Reichold |
---|
4 | |
---|
5 | This file is part of qpdfview. |
---|
6 | |
---|
7 | qpdfview is free software: you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation, either version 2 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | qpdfview is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | #ifndef SEARCHTASK_H |
---|
23 | #define SEARCHTASK_H |
---|
24 | |
---|
25 | #include <QRectF> |
---|
26 | #include <QThread> |
---|
27 | #include <QVector> |
---|
28 | |
---|
29 | namespace qpdfview |
---|
30 | { |
---|
31 | |
---|
32 | namespace Model |
---|
33 | { |
---|
34 | class Page; |
---|
35 | } |
---|
36 | |
---|
37 | class SearchTask : public QThread |
---|
38 | { |
---|
39 | Q_OBJECT |
---|
40 | |
---|
41 | public: |
---|
42 | explicit SearchTask(QObject* parent = 0); |
---|
43 | |
---|
44 | bool wasCanceled() const { return loadCancellation() != NotCanceled; } |
---|
45 | int progress() const { return acquireProgress(); } |
---|
46 | |
---|
47 | const QString& text() const { return m_text; } |
---|
48 | bool matchCase() const { return m_matchCase; } |
---|
49 | bool wholeWords() const { return m_wholeWords; } |
---|
50 | |
---|
51 | void run(); |
---|
52 | |
---|
53 | signals: |
---|
54 | void progressChanged(int progress); |
---|
55 | |
---|
56 | void resultsReady(int index, const QList< QRectF >& results); |
---|
57 | |
---|
58 | public slots: |
---|
59 | void start(const QVector< Model::Page* >& pages, |
---|
60 | const QString& text, bool matchCase, bool wholeWords, int beginAtPage = 1); |
---|
61 | |
---|
62 | void cancel() { setCancellation(); } |
---|
63 | |
---|
64 | private: |
---|
65 | Q_DISABLE_COPY(SearchTask) |
---|
66 | |
---|
67 | QAtomicInt m_wasCanceled; |
---|
68 | mutable QAtomicInt m_progress; |
---|
69 | |
---|
70 | enum |
---|
71 | { |
---|
72 | NotCanceled = 0, |
---|
73 | Canceled = 1 |
---|
74 | }; |
---|
75 | |
---|
76 | void setCancellation(); |
---|
77 | void resetCancellation(); |
---|
78 | bool testCancellation(); |
---|
79 | int loadCancellation() const; |
---|
80 | |
---|
81 | void releaseProgress(int value); |
---|
82 | int acquireProgress() const; |
---|
83 | int loadProgress() const; |
---|
84 | |
---|
85 | |
---|
86 | QVector< Model::Page* > m_pages; |
---|
87 | |
---|
88 | QString m_text; |
---|
89 | bool m_matchCase; |
---|
90 | bool m_wholeWords; |
---|
91 | int m_beginAtPage; |
---|
92 | |
---|
93 | }; |
---|
94 | |
---|
95 | #if QT_VERSION > QT_VERSION_CHECK(5,0,0) |
---|
96 | |
---|
97 | inline void SearchTask::setCancellation() |
---|
98 | { |
---|
99 | m_wasCanceled.storeRelease(Canceled); |
---|
100 | } |
---|
101 | |
---|
102 | inline void SearchTask::resetCancellation() |
---|
103 | { |
---|
104 | m_wasCanceled.storeRelease(NotCanceled); |
---|
105 | } |
---|
106 | |
---|
107 | inline bool SearchTask::testCancellation() |
---|
108 | { |
---|
109 | return m_wasCanceled.load() != NotCanceled; |
---|
110 | } |
---|
111 | |
---|
112 | inline int SearchTask::loadCancellation() const |
---|
113 | { |
---|
114 | return m_wasCanceled.load(); |
---|
115 | } |
---|
116 | |
---|
117 | inline void SearchTask::releaseProgress(int value) |
---|
118 | { |
---|
119 | m_progress.storeRelease(value); |
---|
120 | } |
---|
121 | |
---|
122 | inline int SearchTask::acquireProgress() const |
---|
123 | { |
---|
124 | return m_progress.loadAcquire(); |
---|
125 | } |
---|
126 | |
---|
127 | inline int SearchTask::loadProgress() const |
---|
128 | { |
---|
129 | return m_progress.load(); |
---|
130 | } |
---|
131 | |
---|
132 | #else |
---|
133 | |
---|
134 | inline void SearchTask::setCancellation() |
---|
135 | { |
---|
136 | m_wasCanceled.fetchAndStoreRelease(Canceled); |
---|
137 | } |
---|
138 | |
---|
139 | inline void SearchTask::resetCancellation() |
---|
140 | { |
---|
141 | m_wasCanceled.fetchAndStoreRelease(NotCanceled); |
---|
142 | } |
---|
143 | |
---|
144 | inline bool SearchTask::testCancellation() |
---|
145 | { |
---|
146 | return !m_wasCanceled.testAndSetRelaxed(NotCanceled, NotCanceled); |
---|
147 | } |
---|
148 | |
---|
149 | inline int SearchTask::loadCancellation() const |
---|
150 | { |
---|
151 | return m_wasCanceled; |
---|
152 | } |
---|
153 | |
---|
154 | inline void SearchTask::releaseProgress(int value) |
---|
155 | { |
---|
156 | m_progress.fetchAndStoreRelease(value); |
---|
157 | } |
---|
158 | |
---|
159 | inline int SearchTask::acquireProgress() const |
---|
160 | { |
---|
161 | return m_progress.fetchAndAddAcquire(0); |
---|
162 | } |
---|
163 | |
---|
164 | inline int SearchTask::loadProgress() const |
---|
165 | { |
---|
166 | return m_progress; |
---|
167 | } |
---|
168 | |
---|
169 | #endif // QT_VERSION |
---|
170 | |
---|
171 | } // qpdfview |
---|
172 | |
---|
173 | #endif // SEARCHTHREAD_H |
---|