1 | /* |
---|
2 | |
---|
3 | Copyright 2014 S. Razi Alavizadeh |
---|
4 | Copyright 2012-2014 Adam Reichold |
---|
5 | Copyright 2014 Dorian Scholz |
---|
6 | |
---|
7 | This file is part of qpdfview. |
---|
8 | |
---|
9 | qpdfview is free software: you can redistribute it and/or modify |
---|
10 | it under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation, either version 2 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | qpdfview is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef DOCUMENTVIEW_H |
---|
25 | #define DOCUMENTVIEW_H |
---|
26 | |
---|
27 | #include <QFileInfo> |
---|
28 | #include <QGraphicsView> |
---|
29 | #include <QMap> |
---|
30 | #include <QPersistentModelIndex> |
---|
31 | |
---|
32 | class QDomNode; |
---|
33 | class QFileSystemWatcher; |
---|
34 | class QPrinter; |
---|
35 | class QStandardItemModel; |
---|
36 | |
---|
37 | #include "renderparam.h" |
---|
38 | #include "printoptions.h" |
---|
39 | |
---|
40 | namespace qpdfview |
---|
41 | { |
---|
42 | |
---|
43 | namespace Model |
---|
44 | { |
---|
45 | class Annotation; |
---|
46 | class Page; |
---|
47 | class Document; |
---|
48 | } |
---|
49 | |
---|
50 | class Settings; |
---|
51 | class PageItem; |
---|
52 | class ThumbnailItem; |
---|
53 | class SearchModel; |
---|
54 | class SearchTask; |
---|
55 | class PresentationView; |
---|
56 | class ShortcutHandler; |
---|
57 | struct DocumentLayout; |
---|
58 | |
---|
59 | class DocumentView : public QGraphicsView |
---|
60 | { |
---|
61 | Q_OBJECT |
---|
62 | |
---|
63 | public: |
---|
64 | explicit DocumentView(QWidget* parent = 0); |
---|
65 | ~DocumentView(); |
---|
66 | |
---|
67 | const QFileInfo& fileInfo() const { return m_fileInfo; } |
---|
68 | bool wasModified() const { return m_wasModified; } |
---|
69 | |
---|
70 | int numberOfPages() const { return m_pages.count(); } |
---|
71 | int currentPage() const { return m_currentPage; } |
---|
72 | |
---|
73 | bool hasFrontMatter() const { return m_firstPage > 1; } |
---|
74 | |
---|
75 | int firstPage() const { return m_firstPage; } |
---|
76 | void setFirstPage(int firstPage); |
---|
77 | |
---|
78 | QString defaultPageLabelFromNumber(int number) const; |
---|
79 | QString pageLabelFromNumber(int number) const; |
---|
80 | int pageNumberFromLabel(const QString& label) const; |
---|
81 | |
---|
82 | QString title() const; |
---|
83 | |
---|
84 | static QStringList openFilter(); |
---|
85 | QStringList saveFilter() const; |
---|
86 | |
---|
87 | bool canSave() const; |
---|
88 | |
---|
89 | bool continuousMode() const { return m_continuousMode; } |
---|
90 | void setContinuousMode(bool continuousMode); |
---|
91 | |
---|
92 | LayoutMode layoutMode() const; |
---|
93 | void setLayoutMode(LayoutMode layoutMode); |
---|
94 | |
---|
95 | bool rightToLeftMode() const { return m_rightToLeftMode; } |
---|
96 | void setRightToLeftMode(bool rightToLeftMode); |
---|
97 | |
---|
98 | ScaleMode scaleMode() const { return m_scaleMode; } |
---|
99 | void setScaleMode(ScaleMode scaleMode); |
---|
100 | |
---|
101 | qreal scaleFactor() const { return m_scaleFactor; } |
---|
102 | void setScaleFactor(qreal scaleFactor); |
---|
103 | |
---|
104 | Rotation rotation() const { return m_rotation; } |
---|
105 | void setRotation(Rotation rotation); |
---|
106 | |
---|
107 | qpdfview::RenderFlags renderFlags() const { return m_renderFlags; } |
---|
108 | void setRenderFlags(qpdfview::RenderFlags renderFlags); |
---|
109 | void setRenderFlag(qpdfview::RenderFlag renderFlag, bool enabled = true); |
---|
110 | |
---|
111 | bool invertColors() const { return m_renderFlags.testFlag(InvertColors); } |
---|
112 | void setInvertColors(bool invertColors) { setRenderFlag(InvertColors, invertColors); } |
---|
113 | |
---|
114 | bool convertToGrayscale() const { return m_renderFlags.testFlag(ConvertToGrayscale); } |
---|
115 | void setConvertToGrayscale(bool convertToGrayscale) { setRenderFlag(ConvertToGrayscale, convertToGrayscale); } |
---|
116 | |
---|
117 | bool trimMargins() const { return m_renderFlags.testFlag(TrimMargins); } |
---|
118 | void setTrimMargins(bool trimMargins) { setRenderFlag(TrimMargins, trimMargins); } |
---|
119 | |
---|
120 | CompositionMode compositionMode() const; |
---|
121 | void setCompositionMode(CompositionMode compositionMode); |
---|
122 | |
---|
123 | bool highlightAll() const { return m_highlightAll; } |
---|
124 | void setHighlightAll(bool highlightAll); |
---|
125 | |
---|
126 | RubberBandMode rubberBandMode() const { return m_rubberBandMode; } |
---|
127 | void setRubberBandMode(RubberBandMode rubberBandMode); |
---|
128 | |
---|
129 | QSize thumbnailsViewportSize() const { return m_thumbnailsViewportSize; } |
---|
130 | void setThumbnailsViewportSize(const QSize& thumbnailsViewportSize); |
---|
131 | |
---|
132 | Qt::Orientation thumbnailsOrientation() const { return m_thumbnailsOrientation; } |
---|
133 | void setThumbnailsOrientation(Qt::Orientation thumbnailsOrientation); |
---|
134 | |
---|
135 | const QVector< ThumbnailItem* >& thumbnailItems() const { return m_thumbnailItems; } |
---|
136 | QGraphicsScene* thumbnailsScene() const { return m_thumbnailsScene; } |
---|
137 | |
---|
138 | QStandardItemModel* outlineModel() const { return m_outlineModel; } |
---|
139 | QStandardItemModel* propertiesModel() const { return m_propertiesModel; } |
---|
140 | |
---|
141 | QStandardItemModel* fontsModel() const; |
---|
142 | |
---|
143 | bool searchWasCanceled() const; |
---|
144 | int searchProgress() const; |
---|
145 | |
---|
146 | QString searchText() const; |
---|
147 | bool searchMatchCase() const; |
---|
148 | bool searchWholeWords() const; |
---|
149 | |
---|
150 | QPair< QString, QString > searchContext(int page, const QRectF& rect) const; |
---|
151 | |
---|
152 | struct SourceLink |
---|
153 | { |
---|
154 | QString name; |
---|
155 | int line; |
---|
156 | int column; |
---|
157 | |
---|
158 | operator bool() const { return !name.isNull(); } |
---|
159 | |
---|
160 | }; |
---|
161 | |
---|
162 | SourceLink sourceLink(const QPoint& pos); |
---|
163 | void openInSourceEditor(const SourceLink& sourceLink); |
---|
164 | |
---|
165 | signals: |
---|
166 | void documentChanged(); |
---|
167 | void documentModified(); |
---|
168 | |
---|
169 | void numberOfPagesChanged(int numberOfPages); |
---|
170 | void currentPageChanged(int currentPage, bool trackChange = false); |
---|
171 | |
---|
172 | void canJumpChanged(bool backward, bool forward); |
---|
173 | |
---|
174 | void continuousModeChanged(bool continuousMode); |
---|
175 | void layoutModeChanged(LayoutMode layoutMode); |
---|
176 | void rightToLeftModeChanged(bool rightToLeftMode); |
---|
177 | void scaleModeChanged(ScaleMode scaleMode); |
---|
178 | void scaleFactorChanged(qreal scaleFactor); |
---|
179 | void rotationChanged(Rotation rotation); |
---|
180 | |
---|
181 | void linkClicked(int page); |
---|
182 | void linkClicked(bool newTab, const QString& filePath, int page); |
---|
183 | |
---|
184 | void renderFlagsChanged(qpdfview::RenderFlags renderFlags); |
---|
185 | |
---|
186 | void invertColorsChanged(bool invertColors); |
---|
187 | void convertToGrayscaleChanged(bool convertToGrayscale); |
---|
188 | void trimMarginsChanged(bool trimMargins); |
---|
189 | |
---|
190 | void compositionModeChanged(CompositionMode compositionMode); |
---|
191 | |
---|
192 | void highlightAllChanged(bool highlightAll); |
---|
193 | void rubberBandModeChanged(RubberBandMode rubberBandMode); |
---|
194 | |
---|
195 | void searchFinished(); |
---|
196 | void searchProgressChanged(int progress); |
---|
197 | |
---|
198 | public slots: |
---|
199 | void show(); |
---|
200 | |
---|
201 | bool open(const QString& filePath); |
---|
202 | bool refresh(); |
---|
203 | bool save(const QString& filePath, bool withChanges); |
---|
204 | bool print(QPrinter* printer, const PrintOptions& printOptions = PrintOptions()); |
---|
205 | |
---|
206 | void previousPage(); |
---|
207 | void nextPage(); |
---|
208 | void firstPage(); |
---|
209 | void lastPage(); |
---|
210 | |
---|
211 | void jumpToPage(int page, bool trackChange = true, qreal newLeft = qQNaN(), qreal newTop = qQNaN()); |
---|
212 | |
---|
213 | bool canJumpBackward() const; |
---|
214 | void jumpBackward(); |
---|
215 | |
---|
216 | bool canJumpForward() const; |
---|
217 | void jumpForward(); |
---|
218 | |
---|
219 | void temporaryHighlight(int page, const QRectF& highlight); |
---|
220 | |
---|
221 | void startSearch(const QString& text, bool matchCase, bool wholeWords); |
---|
222 | void cancelSearch(); |
---|
223 | |
---|
224 | void clearResults(); |
---|
225 | |
---|
226 | void findPrevious(); |
---|
227 | void findNext(); |
---|
228 | void findResult(const QModelIndex& index); |
---|
229 | |
---|
230 | void zoomIn(); |
---|
231 | void zoomOut(); |
---|
232 | void originalSize(); |
---|
233 | |
---|
234 | void rotateLeft(); |
---|
235 | void rotateRight(); |
---|
236 | |
---|
237 | void startPresentation(); |
---|
238 | |
---|
239 | protected slots: |
---|
240 | void on_verticalScrollBar_valueChanged(); |
---|
241 | |
---|
242 | void on_autoRefresh_timeout(); |
---|
243 | void on_prefetch_timeout(); |
---|
244 | |
---|
245 | void on_temporaryHighlight_timeout(); |
---|
246 | |
---|
247 | void on_searchTask_progressChanged(int progress); |
---|
248 | void on_searchTask_resultsReady(int index, const QList< QRectF >& results); |
---|
249 | |
---|
250 | void on_pages_cropRectChanged(); |
---|
251 | void on_thumbnails_cropRectChanged(); |
---|
252 | |
---|
253 | void on_pages_linkClicked(bool newTab, int page, qreal left, qreal top); |
---|
254 | void on_pages_linkClicked(bool newTab, const QString& fileName, int page); |
---|
255 | void on_pages_linkClicked(const QString& url); |
---|
256 | |
---|
257 | void on_pages_rubberBandFinished(); |
---|
258 | |
---|
259 | void on_pages_zoomToSelection(int page, const QRectF& rect); |
---|
260 | |
---|
261 | void on_pages_wasModified(); |
---|
262 | |
---|
263 | protected: |
---|
264 | void resizeEvent(QResizeEvent* event); |
---|
265 | |
---|
266 | void keyPressEvent(QKeyEvent* event); |
---|
267 | void mousePressEvent(QMouseEvent* event); |
---|
268 | void wheelEvent(QWheelEvent* event); |
---|
269 | |
---|
270 | void contextMenuEvent(QContextMenuEvent* event); |
---|
271 | |
---|
272 | private: |
---|
273 | Q_DISABLE_COPY(DocumentView) |
---|
274 | |
---|
275 | static Settings* s_settings; |
---|
276 | static ShortcutHandler* s_shortcutHandler; |
---|
277 | |
---|
278 | QFileSystemWatcher* m_autoRefreshWatcher; |
---|
279 | QTimer* m_autoRefreshTimer; |
---|
280 | |
---|
281 | QTimer* m_prefetchTimer; |
---|
282 | |
---|
283 | Model::Document* m_document; |
---|
284 | QVector< Model::Page* > m_pages; |
---|
285 | |
---|
286 | QFileInfo m_fileInfo; |
---|
287 | bool m_wasModified; |
---|
288 | |
---|
289 | int m_currentPage; |
---|
290 | int m_firstPage; |
---|
291 | |
---|
292 | #ifdef WITH_CUPS |
---|
293 | |
---|
294 | bool printUsingCUPS(QPrinter* printer, const PrintOptions& printOptions, int fromPage, int toPage); |
---|
295 | |
---|
296 | #endif // WITH_CUPS |
---|
297 | |
---|
298 | bool printUsingQt(QPrinter* printer, const PrintOptions& printOptions, int fromPage, int toPage); |
---|
299 | |
---|
300 | struct Position |
---|
301 | { |
---|
302 | int page; |
---|
303 | qreal left; |
---|
304 | qreal top; |
---|
305 | |
---|
306 | Position(int page, qreal left, qreal top) : page(page), left(left), top(top) {} |
---|
307 | |
---|
308 | }; |
---|
309 | |
---|
310 | QList< Position > m_past; |
---|
311 | QList< Position > m_future; |
---|
312 | |
---|
313 | void saveLeftAndTop(qreal& left, qreal& top) const; |
---|
314 | |
---|
315 | QScopedPointer< DocumentLayout > m_layout; |
---|
316 | |
---|
317 | bool m_continuousMode; |
---|
318 | bool m_rightToLeftMode; |
---|
319 | ScaleMode m_scaleMode; |
---|
320 | qreal m_scaleFactor; |
---|
321 | Rotation m_rotation; |
---|
322 | |
---|
323 | qpdfview::RenderFlags m_renderFlags; |
---|
324 | |
---|
325 | bool m_highlightAll; |
---|
326 | RubberBandMode m_rubberBandMode; |
---|
327 | |
---|
328 | QVector< PageItem* > m_pageItems; |
---|
329 | QVector< ThumbnailItem* > m_thumbnailItems; |
---|
330 | |
---|
331 | QGraphicsRectItem* m_highlight; |
---|
332 | |
---|
333 | QSize m_thumbnailsViewportSize; |
---|
334 | Qt::Orientation m_thumbnailsOrientation; |
---|
335 | |
---|
336 | QGraphicsScene* m_thumbnailsScene; |
---|
337 | |
---|
338 | QStandardItemModel* m_outlineModel; |
---|
339 | QStandardItemModel* m_propertiesModel; |
---|
340 | |
---|
341 | bool checkDocument(const QString& filePath, Model::Document* document, QVector< Model::Page* >& pages); |
---|
342 | |
---|
343 | void loadFallbackOutline(); |
---|
344 | void loadDocumentDefaults(); |
---|
345 | |
---|
346 | void adjustScrollBarPolicy(); |
---|
347 | void disconnectVerticalScrollBar(); |
---|
348 | void reconnectVerticalScrollBar(); |
---|
349 | |
---|
350 | void prepareDocument(Model::Document* document, const QVector< Model::Page* >& pages); |
---|
351 | void preparePages(); |
---|
352 | void prepareThumbnails(); |
---|
353 | void prepareBackground(); |
---|
354 | |
---|
355 | void prepareScene(); |
---|
356 | void prepareView(qreal newLeft = 0.0, qreal newTop = 0.0, bool forceScroll = true, int scrollToPage = 0); |
---|
357 | |
---|
358 | void prepareThumbnailsScene(); |
---|
359 | |
---|
360 | void prepareHighlight(int index, const QRectF& highlight); |
---|
361 | |
---|
362 | // search |
---|
363 | |
---|
364 | static SearchModel* s_searchModel; |
---|
365 | |
---|
366 | QPersistentModelIndex m_currentResult; |
---|
367 | |
---|
368 | SearchTask* m_searchTask; |
---|
369 | |
---|
370 | void checkResult(); |
---|
371 | void applyResult(); |
---|
372 | |
---|
373 | }; |
---|
374 | |
---|
375 | } // qpdfview |
---|
376 | |
---|
377 | Q_DECLARE_METATYPE(qpdfview::DocumentView::SourceLink) |
---|
378 | |
---|
379 | #endif // DOCUMENTVIEW_H |
---|