1 | /* |
---|
2 | |
---|
3 | Copyright 2013-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 RENDERTASK_H |
---|
23 | #define RENDERTASK_H |
---|
24 | |
---|
25 | #include <QImage> |
---|
26 | #include <QMutex> |
---|
27 | #include <QRunnable> |
---|
28 | #include <QWaitCondition> |
---|
29 | |
---|
30 | #include "renderparam.h" |
---|
31 | |
---|
32 | namespace qpdfview |
---|
33 | { |
---|
34 | |
---|
35 | namespace Model |
---|
36 | { |
---|
37 | class Page; |
---|
38 | } |
---|
39 | |
---|
40 | class Settings; |
---|
41 | |
---|
42 | class RenderTask : public QObject, QRunnable |
---|
43 | { |
---|
44 | Q_OBJECT |
---|
45 | |
---|
46 | public: |
---|
47 | explicit RenderTask(Model::Page* page, QObject* parent = 0); |
---|
48 | |
---|
49 | void wait(); |
---|
50 | |
---|
51 | bool isRunning() const; |
---|
52 | |
---|
53 | bool wasCanceled() const { return loadCancellation() != NotCanceled; } |
---|
54 | bool wasCanceledNormally() const { return loadCancellation() == CanceledNormally; } |
---|
55 | bool wasCanceledForcibly() const { return loadCancellation() == CanceledForcibly; } |
---|
56 | |
---|
57 | void run(); |
---|
58 | |
---|
59 | signals: |
---|
60 | void finished(); |
---|
61 | |
---|
62 | void imageReady(const RenderParam& renderParam, |
---|
63 | const QRect& rect, bool prefetch, |
---|
64 | const QImage& image, const QRectF& cropRect); |
---|
65 | |
---|
66 | public slots: |
---|
67 | void start(const RenderParam& renderParam, |
---|
68 | const QRect& rect, bool prefetch); |
---|
69 | |
---|
70 | void cancel(bool force = false) { setCancellation(force); } |
---|
71 | |
---|
72 | private: |
---|
73 | Q_DISABLE_COPY(RenderTask) |
---|
74 | |
---|
75 | static Settings* s_settings; |
---|
76 | |
---|
77 | mutable QMutex m_mutex; |
---|
78 | QWaitCondition m_waitCondition; |
---|
79 | |
---|
80 | bool m_isRunning; |
---|
81 | QAtomicInt m_wasCanceled; |
---|
82 | |
---|
83 | enum |
---|
84 | { |
---|
85 | NotCanceled = 0, |
---|
86 | CanceledNormally = 1, |
---|
87 | CanceledForcibly = 2 |
---|
88 | }; |
---|
89 | |
---|
90 | void setCancellation(bool force); |
---|
91 | void resetCancellation(); |
---|
92 | bool testCancellation(); |
---|
93 | int loadCancellation() const; |
---|
94 | |
---|
95 | void finish(); |
---|
96 | |
---|
97 | |
---|
98 | Model::Page* m_page; |
---|
99 | |
---|
100 | static RenderParam s_defaultRenderParam; |
---|
101 | RenderParam m_renderParam; |
---|
102 | |
---|
103 | QRect m_rect; |
---|
104 | bool m_prefetch; |
---|
105 | |
---|
106 | }; |
---|
107 | |
---|
108 | #if QT_VERSION > QT_VERSION_CHECK(5,0,0) |
---|
109 | |
---|
110 | inline void RenderTask::setCancellation(bool force) |
---|
111 | { |
---|
112 | m_wasCanceled.storeRelease(force ? CanceledForcibly : CanceledNormally); |
---|
113 | } |
---|
114 | |
---|
115 | inline void RenderTask::resetCancellation() |
---|
116 | { |
---|
117 | m_wasCanceled.storeRelease(NotCanceled); |
---|
118 | } |
---|
119 | |
---|
120 | inline bool RenderTask::testCancellation() |
---|
121 | { |
---|
122 | return m_prefetch ? |
---|
123 | m_wasCanceled.load() == CanceledForcibly : |
---|
124 | m_wasCanceled.load() != NotCanceled; |
---|
125 | } |
---|
126 | |
---|
127 | inline int RenderTask::loadCancellation() const |
---|
128 | { |
---|
129 | return m_wasCanceled.load(); |
---|
130 | } |
---|
131 | |
---|
132 | #else |
---|
133 | |
---|
134 | inline void RenderTask::setCancellation(bool force) |
---|
135 | { |
---|
136 | m_wasCanceled.fetchAndStoreRelease(force ? CanceledForcibly : CanceledNormally); |
---|
137 | } |
---|
138 | |
---|
139 | inline void RenderTask::resetCancellation() |
---|
140 | { |
---|
141 | m_wasCanceled.fetchAndStoreRelease(NotCanceled); |
---|
142 | } |
---|
143 | |
---|
144 | inline bool RenderTask::testCancellation() |
---|
145 | { |
---|
146 | return m_prefetch ? |
---|
147 | m_wasCanceled.testAndSetRelaxed(CanceledForcibly, CanceledForcibly) : |
---|
148 | !m_wasCanceled.testAndSetRelaxed(NotCanceled, NotCanceled); |
---|
149 | } |
---|
150 | |
---|
151 | inline int RenderTask::loadCancellation() const |
---|
152 | { |
---|
153 | return m_wasCanceled; |
---|
154 | } |
---|
155 | |
---|
156 | #endif // QT_VERSION |
---|
157 | |
---|
158 | } // qpdfview |
---|
159 | |
---|
160 | #endif // RENDERTASK_H |
---|