1 | /* |
---|
2 | |
---|
3 | Copyright 2014 S. Razi Alavizadeh |
---|
4 | Copyright 2012-2015 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 MISCELLANEOUS_H |
---|
25 | #define MISCELLANEOUS_H |
---|
26 | |
---|
27 | #include <QComboBox> |
---|
28 | #include <QGraphicsEffect> |
---|
29 | #include <QLineEdit> |
---|
30 | #include <QMenu> |
---|
31 | #include <QPainter> |
---|
32 | #include <QProxyStyle> |
---|
33 | #include <QSpinBox> |
---|
34 | #include <QTreeView> |
---|
35 | |
---|
36 | class QTextLayout; |
---|
37 | |
---|
38 | namespace qpdfview |
---|
39 | { |
---|
40 | |
---|
41 | // graphics composition mode effect |
---|
42 | |
---|
43 | class GraphicsCompositionModeEffect : public QGraphicsEffect |
---|
44 | { |
---|
45 | Q_OBJECT |
---|
46 | |
---|
47 | public: |
---|
48 | GraphicsCompositionModeEffect(QPainter::CompositionMode compositionMode, QObject* parent = 0); |
---|
49 | |
---|
50 | protected: |
---|
51 | void draw(QPainter* painter); |
---|
52 | |
---|
53 | private: |
---|
54 | QPainter::CompositionMode m_compositionMode; |
---|
55 | |
---|
56 | }; |
---|
57 | |
---|
58 | // proxy style |
---|
59 | |
---|
60 | class ProxyStyle : public QProxyStyle |
---|
61 | { |
---|
62 | Q_OBJECT |
---|
63 | |
---|
64 | public: |
---|
65 | ProxyStyle(); |
---|
66 | |
---|
67 | bool scrollableMenus() const; |
---|
68 | void setScrollableMenus(bool scrollableMenus); |
---|
69 | |
---|
70 | int styleHint(StyleHint hint, const QStyleOption* option, const QWidget* widget, QStyleHintReturn* returnData) const; |
---|
71 | |
---|
72 | private: |
---|
73 | Q_DISABLE_COPY(ProxyStyle) |
---|
74 | |
---|
75 | bool m_scrollableMenus; |
---|
76 | |
---|
77 | }; |
---|
78 | |
---|
79 | // searchable menu |
---|
80 | |
---|
81 | class SearchableMenu : public QMenu |
---|
82 | { |
---|
83 | Q_OBJECT |
---|
84 | |
---|
85 | public: |
---|
86 | SearchableMenu(const QString& title, QWidget* parent = 0); |
---|
87 | |
---|
88 | bool isSearchable() const; |
---|
89 | void setSearchable(bool searchable); |
---|
90 | |
---|
91 | protected: |
---|
92 | void hideEvent(QHideEvent* event); |
---|
93 | void keyPressEvent(QKeyEvent* event); |
---|
94 | |
---|
95 | private: |
---|
96 | bool m_searchable; |
---|
97 | QString m_text; |
---|
98 | |
---|
99 | }; |
---|
100 | |
---|
101 | // tab bar |
---|
102 | |
---|
103 | class TabBar : public QTabBar |
---|
104 | { |
---|
105 | Q_OBJECT |
---|
106 | |
---|
107 | public: |
---|
108 | explicit TabBar(QWidget* parent = 0); |
---|
109 | |
---|
110 | protected: |
---|
111 | QSize tabSizeHint(int index) const; |
---|
112 | |
---|
113 | void mousePressEvent(QMouseEvent* event); |
---|
114 | |
---|
115 | private: |
---|
116 | Q_DISABLE_COPY(TabBar) |
---|
117 | |
---|
118 | }; |
---|
119 | |
---|
120 | // tab widget |
---|
121 | |
---|
122 | class TabWidget : public QTabWidget |
---|
123 | { |
---|
124 | Q_OBJECT |
---|
125 | |
---|
126 | public: |
---|
127 | explicit TabWidget(QWidget* parent = 0); |
---|
128 | |
---|
129 | enum TabBarPolicy |
---|
130 | { |
---|
131 | TabBarAsNeeded = 0, |
---|
132 | TabBarAlwaysOn = 1, |
---|
133 | TabBarAlwaysOff = 2 |
---|
134 | }; |
---|
135 | |
---|
136 | TabBarPolicy tabBarPolicy() const; |
---|
137 | void setTabBarPolicy(TabBarPolicy tabBarPolicy); |
---|
138 | |
---|
139 | bool spreadTabs() const; |
---|
140 | void setSpreadTabs(bool spreadTabs); |
---|
141 | |
---|
142 | signals: |
---|
143 | void tabContextMenuRequested(const QPoint& globalPos, int index); |
---|
144 | |
---|
145 | protected slots: |
---|
146 | void on_tabBar_customContextMenuRequested(const QPoint& pos); |
---|
147 | |
---|
148 | protected: |
---|
149 | void tabInserted(int index); |
---|
150 | void tabRemoved(int index); |
---|
151 | |
---|
152 | private: |
---|
153 | Q_DISABLE_COPY(TabWidget) |
---|
154 | |
---|
155 | TabBarPolicy m_tabBarPolicy; |
---|
156 | bool m_spreadTabs; |
---|
157 | |
---|
158 | }; |
---|
159 | |
---|
160 | // tree view |
---|
161 | |
---|
162 | class TreeView : public QTreeView |
---|
163 | { |
---|
164 | Q_OBJECT |
---|
165 | |
---|
166 | public: |
---|
167 | explicit TreeView(int expansionRole, QWidget* parent = 0); |
---|
168 | |
---|
169 | public slots: |
---|
170 | void expandAbove(const QModelIndex& child); |
---|
171 | |
---|
172 | void expandAll(const QModelIndex& index = QModelIndex()); |
---|
173 | void collapseAll(const QModelIndex& index = QModelIndex()); |
---|
174 | |
---|
175 | int expandedDepth(const QModelIndex& index); |
---|
176 | |
---|
177 | void expandToDepth(const QModelIndex& index, int depth); |
---|
178 | void collapseFromDepth(const QModelIndex& index, int depth); |
---|
179 | |
---|
180 | void restoreExpansion(const QModelIndex& index = QModelIndex()); |
---|
181 | |
---|
182 | protected: |
---|
183 | void keyPressEvent(QKeyEvent* event); |
---|
184 | void wheelEvent(QWheelEvent* event); |
---|
185 | |
---|
186 | void contextMenuEvent(QContextMenuEvent* event); |
---|
187 | |
---|
188 | protected slots: |
---|
189 | void on_expanded(const QModelIndex& index); |
---|
190 | void on_collapsed(const QModelIndex& index); |
---|
191 | |
---|
192 | private: |
---|
193 | Q_DISABLE_COPY(TreeView) |
---|
194 | |
---|
195 | int m_expansionRole; |
---|
196 | |
---|
197 | }; |
---|
198 | |
---|
199 | // line edit |
---|
200 | |
---|
201 | class LineEdit : public QLineEdit |
---|
202 | { |
---|
203 | Q_OBJECT |
---|
204 | |
---|
205 | public: |
---|
206 | explicit LineEdit(QWidget* parent = 0); |
---|
207 | |
---|
208 | protected: |
---|
209 | void mousePressEvent(QMouseEvent* event); |
---|
210 | |
---|
211 | private: |
---|
212 | Q_DISABLE_COPY(LineEdit) |
---|
213 | |
---|
214 | }; |
---|
215 | |
---|
216 | // combo box |
---|
217 | |
---|
218 | class ComboBox : public QComboBox |
---|
219 | { |
---|
220 | Q_OBJECT |
---|
221 | |
---|
222 | public: |
---|
223 | explicit ComboBox(QWidget* parent = 0); |
---|
224 | |
---|
225 | private: |
---|
226 | Q_DISABLE_COPY(ComboBox) |
---|
227 | |
---|
228 | }; |
---|
229 | |
---|
230 | // spin box |
---|
231 | |
---|
232 | class SpinBox : public QSpinBox |
---|
233 | { |
---|
234 | Q_OBJECT |
---|
235 | |
---|
236 | public: |
---|
237 | explicit SpinBox(QWidget* parent = 0); |
---|
238 | |
---|
239 | signals: |
---|
240 | void returnPressed(); |
---|
241 | |
---|
242 | protected: |
---|
243 | void keyPressEvent(QKeyEvent* event); |
---|
244 | |
---|
245 | private: |
---|
246 | Q_DISABLE_COPY(SpinBox) |
---|
247 | |
---|
248 | }; |
---|
249 | |
---|
250 | // mapping spin box |
---|
251 | |
---|
252 | class MappingSpinBox : public SpinBox |
---|
253 | { |
---|
254 | Q_OBJECT |
---|
255 | |
---|
256 | public: |
---|
257 | struct TextValueMapper |
---|
258 | { |
---|
259 | virtual ~TextValueMapper() {} |
---|
260 | |
---|
261 | virtual QString textFromValue(int val, bool& ok) const = 0; |
---|
262 | virtual int valueFromText(const QString& text, bool& ok) const = 0; |
---|
263 | }; |
---|
264 | |
---|
265 | MappingSpinBox(TextValueMapper* mapper, QWidget* parent = 0); |
---|
266 | |
---|
267 | protected: |
---|
268 | QString textFromValue(int val) const; |
---|
269 | int valueFromText(const QString& text) const; |
---|
270 | |
---|
271 | QValidator::State validate(QString& input, int& pos) const; |
---|
272 | |
---|
273 | private: |
---|
274 | Q_DISABLE_COPY(MappingSpinBox) |
---|
275 | |
---|
276 | QScopedPointer< TextValueMapper > m_mapper; |
---|
277 | |
---|
278 | }; |
---|
279 | |
---|
280 | int getMappedNumber(MappingSpinBox::TextValueMapper* mapper, |
---|
281 | QWidget* parent, const QString& title, const QString& caption, |
---|
282 | int value = 0, int min = -2147483647, int max = 2147483647, |
---|
283 | bool* ok = 0, Qt::WindowFlags flags = 0); |
---|
284 | |
---|
285 | // progress line edit |
---|
286 | |
---|
287 | class ProgressLineEdit : public QLineEdit |
---|
288 | { |
---|
289 | Q_OBJECT |
---|
290 | |
---|
291 | public: |
---|
292 | explicit ProgressLineEdit(QWidget* parent = 0); |
---|
293 | |
---|
294 | int progress() const; |
---|
295 | void setProgress(int progress); |
---|
296 | |
---|
297 | signals: |
---|
298 | void returnPressed(const Qt::KeyboardModifiers& modifiers); |
---|
299 | |
---|
300 | protected: |
---|
301 | void paintEvent(QPaintEvent* event); |
---|
302 | void keyPressEvent(QKeyEvent* event); |
---|
303 | |
---|
304 | private: |
---|
305 | Q_DISABLE_COPY(ProgressLineEdit) |
---|
306 | |
---|
307 | int m_progress; |
---|
308 | |
---|
309 | }; |
---|
310 | |
---|
311 | // search line edit |
---|
312 | |
---|
313 | class SearchLineEdit : public ProgressLineEdit |
---|
314 | { |
---|
315 | Q_OBJECT |
---|
316 | |
---|
317 | public: |
---|
318 | explicit SearchLineEdit(QWidget* parent = 0); |
---|
319 | |
---|
320 | public slots: |
---|
321 | void startSearch(); |
---|
322 | |
---|
323 | void startTimer(); |
---|
324 | void stopTimer(); |
---|
325 | |
---|
326 | signals: |
---|
327 | void searchInitiated(const QString& text, bool modified = false); |
---|
328 | |
---|
329 | protected slots: |
---|
330 | void on_timeout(); |
---|
331 | void on_returnPressed(const Qt::KeyboardModifiers& modifiers); |
---|
332 | |
---|
333 | private: |
---|
334 | Q_DISABLE_COPY(SearchLineEdit) |
---|
335 | |
---|
336 | QTimer* m_timer; |
---|
337 | |
---|
338 | }; |
---|
339 | |
---|
340 | // fallback icons |
---|
341 | |
---|
342 | inline QIcon loadIconWithFallback(const QString& name) |
---|
343 | { |
---|
344 | QIcon icon = QIcon::fromTheme(name); |
---|
345 | |
---|
346 | if(icon.isNull()) |
---|
347 | { |
---|
348 | icon = QIcon(QLatin1String(":icons/") + name + QLatin1String(".svg")); |
---|
349 | } |
---|
350 | |
---|
351 | return icon; |
---|
352 | } |
---|
353 | |
---|
354 | } // qpdfview |
---|
355 | |
---|
356 | #endif // MISCELLANEOUS_H |
---|