~ubuntu-branches/ubuntu/vivid/qpdfview/vivid

« back to all changes in this revision

Viewing changes to sources/searchtask.cpp

  • Committer: Package Import Robot
  • Author(s): Benjamin Eltzner
  • Date: 2014-10-22 21:49:15 UTC
  • mfrom: (1.2.15)
  • Revision ID: package-import@ubuntu.com-20141022214915-agqeoe318lzs2s4d
Tags: 0.4.12-1
* New upstream release.
* Fixed option to zoom to selection and implemented tiled rendering
  (Closes: #739554)
* Enable support for qt5 and poppler-qt5.
* Explicit dependence on hicolor-icon-theme.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#include "model.h"
25
25
 
 
26
namespace
 
27
{
 
28
 
 
29
using namespace qpdfview;
 
30
 
 
31
enum
 
32
{
 
33
    NotCanceled = 0,
 
34
    Canceled = 1
 
35
};
 
36
 
 
37
void setCancellation(QAtomicInt& wasCanceled)
 
38
{
 
39
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
 
40
 
 
41
    wasCanceled.storeRelease(Canceled);
 
42
 
 
43
#else
 
44
 
 
45
    wasCanceled.fetchAndStoreRelease(Canceled);
 
46
 
 
47
#endif // QT_VERSION
 
48
}
 
49
 
 
50
void resetCancellation(QAtomicInt& wasCanceled)
 
51
{
 
52
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
 
53
 
 
54
    wasCanceled.storeRelease(NotCanceled);
 
55
 
 
56
#else
 
57
 
 
58
    wasCanceled.fetchAndStoreRelease(NotCanceled);
 
59
 
 
60
#endif // QT_VERSION
 
61
}
 
62
 
 
63
bool testCancellation(QAtomicInt& wasCanceled)
 
64
{
 
65
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
66
 
 
67
    return wasCanceled.load() != NotCanceled;
 
68
 
 
69
#else
 
70
 
 
71
    return !wasCanceled.testAndSetRelaxed(NotCanceled, NotCanceled);
 
72
 
 
73
#endif // QT_VERSION
 
74
}
 
75
 
 
76
int loadWasCanceled(const QAtomicInt& wasCanceled)
 
77
{
 
78
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
79
 
 
80
    return wasCanceled.load();
 
81
 
 
82
#else
 
83
 
 
84
    return wasCanceled;
 
85
 
 
86
#endif // QT_VERSION
 
87
}
 
88
 
 
89
void releaseProgress(QAtomicInt& progress, int value)
 
90
{
 
91
#if QT_VERSION > QT_VERSION_CHECK(5,0,0)
 
92
 
 
93
    progress.storeRelease(value);
 
94
 
 
95
#else
 
96
 
 
97
    progress.fetchAndStoreRelease(value);
 
98
 
 
99
#endif // QT_VERSION
 
100
}
 
101
 
 
102
int acquireProgress(QAtomicInt& progress)
 
103
{
 
104
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
105
 
 
106
    return progress.loadAcquire();
 
107
 
 
108
#else
 
109
 
 
110
    return progress.fetchAndAddAcquire(0);
 
111
 
 
112
#endif // QT_VERSION
 
113
}
 
114
 
 
115
int loadProgress(const QAtomicInt& progress)
 
116
{
 
117
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
118
 
 
119
    return progress.load();
 
120
#else
 
121
 
 
122
    return progress;
 
123
 
 
124
#endif // QT_VERSION
 
125
}
 
126
 
 
127
} // anonymous
 
128
 
26
129
namespace qpdfview
27
130
{
28
131
 
29
132
SearchTask::SearchTask(QObject* parent) : QThread(parent),
30
 
    m_wasCanceled(false),
 
133
    m_wasCanceled(NotCanceled),
31
134
    m_progress(0),
32
135
    m_pages(),
33
136
    m_text(),
38
141
 
39
142
bool SearchTask::wasCanceled() const
40
143
{
41
 
    return m_wasCanceled;
 
144
    return loadWasCanceled(m_wasCanceled) != NotCanceled;
42
145
}
43
146
 
44
147
int SearchTask::progress() const
45
148
{
46
 
    return m_progress;
 
149
    return acquireProgress(m_progress);
47
150
}
48
151
 
49
152
void SearchTask::run()
50
153
{
51
154
    for(int index = m_beginAtPage - 1; index < m_pages.count() + m_beginAtPage - 1; ++index)
52
155
    {
53
 
        if(m_wasCanceled)
 
156
        if(testCancellation(m_wasCanceled))
54
157
        {
55
 
            m_progress = 0;
56
 
 
57
 
            return;
 
158
            break;
58
159
        }
59
160
 
60
161
        const QList< QRectF > results = m_pages.at(index % m_pages.count())->search(m_text, m_matchCase);
61
162
 
62
163
        emit resultsReady(index % m_pages.count(), results);
63
164
 
64
 
        m_progress = 100 * (index - m_beginAtPage)/ m_pages.count();
 
165
        releaseProgress(m_progress, 100 * (index + 1 - m_beginAtPage + 1) / m_pages.count());
65
166
 
66
 
        emit progressChanged(m_progress);
 
167
        emit progressChanged(loadProgress(m_progress));
67
168
    }
68
169
 
69
 
    m_progress = 0;
 
170
    releaseProgress(m_progress, 0);
70
171
}
71
172
 
72
 
void SearchTask::start(const QVector< Model::Page* >& pages, const QString& text, bool matchCase, int beginAtPage)
 
173
void SearchTask::start(const QVector< Model::Page* >& pages,
 
174
                       const QString& text, bool matchCase, int beginAtPage)
73
175
{
74
176
    m_pages = pages;
75
177
 
77
179
    m_matchCase = matchCase;
78
180
    m_beginAtPage = beginAtPage;
79
181
 
80
 
    m_wasCanceled = false;
81
 
    m_progress = 0;
 
182
    resetCancellation(m_wasCanceled);
 
183
    releaseProgress(m_progress, 0);
82
184
 
83
185
    QThread::start();
84
186
}
85
187
 
86
188
void SearchTask::cancel()
87
189
{
88
 
    m_wasCanceled = true;
 
190
    setCancellation(m_wasCanceled);
89
191
}
90
192
 
91
193
} // qpdfview