Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/printsupport/dialogs/qprintpreviewdialog.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qprintpreviewdialog.h" | - | ||||||
41 | #include "qprintpreviewwidget.h" | - | ||||||
42 | #include <private/qprinter_p.h> | - | ||||||
43 | #include "private/qdialog_p.h" | - | ||||||
44 | #include "qprintdialog.h" | - | ||||||
45 | - | |||||||
46 | #include <QtWidgets/qaction.h> | - | ||||||
47 | #include <QtWidgets/qboxlayout.h> | - | ||||||
48 | #include <QtWidgets/qcombobox.h> | - | ||||||
49 | #include <QtWidgets/qlabel.h> | - | ||||||
50 | #include <QtWidgets/qlineedit.h> | - | ||||||
51 | #include <QtPrintSupport/qpagesetupdialog.h> | - | ||||||
52 | #include <QtPrintSupport/qprinter.h> | - | ||||||
53 | #include <QtWidgets/qstyle.h> | - | ||||||
54 | #include <QtWidgets/qtoolbutton.h> | - | ||||||
55 | #include <QtGui/qvalidator.h> | - | ||||||
56 | #include <QtWidgets/qfiledialog.h> | - | ||||||
57 | #include <QtWidgets/qmainwindow.h> | - | ||||||
58 | #include <QtWidgets/qtoolbar.h> | - | ||||||
59 | #include <QtWidgets/qformlayout.h> | - | ||||||
60 | #include <QtCore/QCoreApplication> | - | ||||||
61 | - | |||||||
62 | #ifndef QT_NO_PRINTPREVIEWDIALOG | - | ||||||
63 | - | |||||||
64 | static void initResources() | - | ||||||
65 | { | - | ||||||
66 | static bool resourcesInitialized = false; | - | ||||||
67 | if (!resourcesInitialized) { | - | ||||||
68 | Q_INIT_RESOURCE(qprintdialog); | - | ||||||
69 | resourcesInitialized = true; | - | ||||||
70 | } | - | ||||||
71 | } | - | ||||||
72 | - | |||||||
73 | QT_BEGIN_NAMESPACE | - | ||||||
74 | - | |||||||
75 | namespace { | - | ||||||
76 | class QPrintPreviewMainWindow : public QMainWindow | - | ||||||
77 | { | - | ||||||
78 | public: | - | ||||||
79 | QPrintPreviewMainWindow(QWidget *parent) : QMainWindow(parent) {} | - | ||||||
80 | QMenu *createPopupMenu() Q_DECL_OVERRIDE { return 0; } | - | ||||||
81 | }; | - | ||||||
82 | - | |||||||
83 | class ZoomFactorValidator : public QDoubleValidator | - | ||||||
84 | { | - | ||||||
85 | public: | - | ||||||
86 | ZoomFactorValidator(QObject* parent) | - | ||||||
87 | : QDoubleValidator(parent) {} | - | ||||||
88 | ZoomFactorValidator(qreal bottom, qreal top, int decimals, QObject *parent) | - | ||||||
89 | : QDoubleValidator(bottom, top, decimals, parent) {} | - | ||||||
90 | - | |||||||
91 | State validate(QString &input, int &pos) const Q_DECL_OVERRIDE | - | ||||||
92 | { | - | ||||||
93 | bool replacePercent = false; | - | ||||||
94 | if (input.endsWith(QLatin1Char('%'))) { | - | ||||||
95 | input = input.left(input.length() - 1); | - | ||||||
96 | replacePercent = true; | - | ||||||
97 | } | - | ||||||
98 | State state = QDoubleValidator::validate(input, pos); | - | ||||||
99 | if (replacePercent) | - | ||||||
100 | input += QLatin1Char('%'); | - | ||||||
101 | const int num_size = 4; | - | ||||||
102 | if (state == Intermediate) { | - | ||||||
103 | int i = input.indexOf(QLocale::system().decimalPoint()); | - | ||||||
104 | if ((i == -1 && input.size() > num_size) | - | ||||||
105 | || (i != -1 && i > num_size)) | - | ||||||
106 | return Invalid; | - | ||||||
107 | } | - | ||||||
108 | return state; | - | ||||||
109 | } | - | ||||||
110 | }; | - | ||||||
111 | - | |||||||
112 | class LineEdit : public QLineEdit | - | ||||||
113 | { | - | ||||||
114 | Q_OBJECT | - | ||||||
115 | public: | - | ||||||
116 | LineEdit(QWidget* parent = 0) | - | ||||||
117 | : QLineEdit(parent) | - | ||||||
118 | { | - | ||||||
119 | setContextMenuPolicy(Qt::NoContextMenu); | - | ||||||
120 | connect(this, SIGNAL(returnPressed()), SLOT(handleReturnPressed())); | - | ||||||
121 | } | - | ||||||
122 | - | |||||||
123 | protected: | - | ||||||
124 | void focusInEvent(QFocusEvent *e) Q_DECL_OVERRIDE | - | ||||||
125 | { | - | ||||||
126 | origText = text(); | - | ||||||
127 | QLineEdit::focusInEvent(e); | - | ||||||
128 | } | - | ||||||
129 | - | |||||||
130 | void focusOutEvent(QFocusEvent *e) Q_DECL_OVERRIDE | - | ||||||
131 | { | - | ||||||
132 | if (isModified() && !hasAcceptableInput()) | - | ||||||
133 | setText(origText); | - | ||||||
134 | QLineEdit::focusOutEvent(e); | - | ||||||
135 | } | - | ||||||
136 | - | |||||||
137 | private slots: | - | ||||||
138 | void handleReturnPressed() | - | ||||||
139 | { | - | ||||||
140 | origText = text(); | - | ||||||
141 | } | - | ||||||
142 | - | |||||||
143 | private: | - | ||||||
144 | QString origText; | - | ||||||
145 | }; | - | ||||||
146 | } // anonymous namespace | - | ||||||
147 | - | |||||||
148 | class QPrintPreviewDialogPrivate : public QDialogPrivate | - | ||||||
149 | { | - | ||||||
150 | Q_DECLARE_PUBLIC(QPrintPreviewDialog) | - | ||||||
151 | public: | - | ||||||
152 | QPrintPreviewDialogPrivate() | - | ||||||
153 | : printDialog(0), ownPrinter(false), | - | ||||||
154 | initialized(false) {} | - | ||||||
155 | - | |||||||
156 | // private slots | - | ||||||
157 | void _q_fit(QAction *action); | - | ||||||
158 | void _q_zoomIn(); | - | ||||||
159 | void _q_zoomOut(); | - | ||||||
160 | void _q_navigate(QAction *action); | - | ||||||
161 | void _q_setMode(QAction *action); | - | ||||||
162 | void _q_pageNumEdited(); | - | ||||||
163 | void _q_print(); | - | ||||||
164 | void _q_pageSetup(); | - | ||||||
165 | void _q_previewChanged(); | - | ||||||
166 | void _q_zoomFactorChanged(); | - | ||||||
167 | - | |||||||
168 | void init(QPrinter *printer = 0); | - | ||||||
169 | void populateScene(); | - | ||||||
170 | void layoutPages(); | - | ||||||
171 | void setupActions(); | - | ||||||
172 | void updateNavActions(); | - | ||||||
173 | void setFitting(bool on); | - | ||||||
174 | bool isFitting(); | - | ||||||
175 | void updatePageNumLabel(); | - | ||||||
176 | void updateZoomFactor(); | - | ||||||
177 | - | |||||||
178 | QPrintDialog *printDialog; | - | ||||||
179 | QPrintPreviewWidget *preview; | - | ||||||
180 | QPrinter *printer; | - | ||||||
181 | bool ownPrinter; | - | ||||||
182 | bool initialized; | - | ||||||
183 | - | |||||||
184 | // widgets: | - | ||||||
185 | QLineEdit *pageNumEdit; | - | ||||||
186 | QLabel *pageNumLabel; | - | ||||||
187 | QComboBox *zoomFactor; | - | ||||||
188 | - | |||||||
189 | // actions: | - | ||||||
190 | QActionGroup* navGroup; | - | ||||||
191 | QAction *nextPageAction; | - | ||||||
192 | QAction *prevPageAction; | - | ||||||
193 | QAction *firstPageAction; | - | ||||||
194 | QAction *lastPageAction; | - | ||||||
195 | - | |||||||
196 | QActionGroup* fitGroup; | - | ||||||
197 | QAction *fitWidthAction; | - | ||||||
198 | QAction *fitPageAction; | - | ||||||
199 | - | |||||||
200 | QActionGroup* zoomGroup; | - | ||||||
201 | QAction *zoomInAction; | - | ||||||
202 | QAction *zoomOutAction; | - | ||||||
203 | - | |||||||
204 | QActionGroup* orientationGroup; | - | ||||||
205 | QAction *portraitAction; | - | ||||||
206 | QAction *landscapeAction; | - | ||||||
207 | - | |||||||
208 | QActionGroup* modeGroup; | - | ||||||
209 | QAction *singleModeAction; | - | ||||||
210 | QAction *facingModeAction; | - | ||||||
211 | QAction *overviewModeAction; | - | ||||||
212 | - | |||||||
213 | QActionGroup *printerGroup; | - | ||||||
214 | QAction *printAction; | - | ||||||
215 | QAction *pageSetupAction; | - | ||||||
216 | - | |||||||
217 | QPointer<QObject> receiverToDisconnectOnClose; | - | ||||||
218 | QByteArray memberToDisconnectOnClose; | - | ||||||
219 | }; | - | ||||||
220 | - | |||||||
221 | void QPrintPreviewDialogPrivate::init(QPrinter *_printer) | - | ||||||
222 | { | - | ||||||
223 | Q_Q(QPrintPreviewDialog); | - | ||||||
224 | - | |||||||
225 | initResources(); | - | ||||||
226 | - | |||||||
227 | if (_printer) {
| 0-1 | ||||||
228 | preview = new QPrintPreviewWidget(_printer, q); | - | ||||||
229 | printer = _printer; | - | ||||||
230 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||
231 | ownPrinter = true; | - | ||||||
232 | printer = new QPrinter; | - | ||||||
233 | preview = new QPrintPreviewWidget(printer, q); | - | ||||||
234 | } never executed: end of block | 0 | ||||||
235 | QObject::connect(preview, SIGNAL(paintRequested(QPrinter*)), q, SIGNAL(paintRequested(QPrinter*))); | - | ||||||
236 | QObject::connect(preview, SIGNAL(previewChanged()), q, SLOT(_q_previewChanged())); | - | ||||||
237 | setupActions(); | - | ||||||
238 | - | |||||||
239 | pageNumEdit = new LineEdit; | - | ||||||
240 | pageNumEdit->setAlignment(Qt::AlignRight); | - | ||||||
241 | pageNumEdit->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); | - | ||||||
242 | pageNumLabel = new QLabel; | - | ||||||
243 | QObject::connect(pageNumEdit, SIGNAL(editingFinished()), q, SLOT(_q_pageNumEdited())); | - | ||||||
244 | - | |||||||
245 | zoomFactor = new QComboBox; | - | ||||||
246 | zoomFactor->setEditable(true); | - | ||||||
247 | zoomFactor->setMinimumContentsLength(7); | - | ||||||
248 | zoomFactor->setInsertPolicy(QComboBox::NoInsert); | - | ||||||
249 | LineEdit *zoomEditor = new LineEdit; | - | ||||||
250 | zoomEditor->setValidator(new ZoomFactorValidator(1, 1000, 1, zoomEditor)); | - | ||||||
251 | zoomFactor->setLineEdit(zoomEditor); | - | ||||||
252 | static const short factorsX2[] = { 25, 50, 100, 200, 250, 300, 400, 800, 1600 }; | - | ||||||
253 | for (int i = 0; i < int(sizeof(factorsX2) / sizeof(auto factorX2 : factorsX2[0])); ++i) | - | ||||||
254 | zoomFactor->addItem(QPrintPreviewDialog::tr("%1%").arg(factorsX2[i]factorX2 / 2.0)); executed 9 times by 1 test: zoomFactor->addItem(QPrintPreviewDialog::tr("%1%").arg(factorX2 / 2.0)); Executed by:
| 9 | ||||||
255 | QObject::connect(zoomFactor->lineEdit(), SIGNAL(editingFinished()), | - | ||||||
256 | q, SLOT(_q_zoomFactorChanged())); | - | ||||||
257 | QObject::connect(zoomFactor, SIGNAL(currentIndexChanged(int)), | - | ||||||
258 | q, SLOT(_q_zoomFactorChanged())); | - | ||||||
259 | - | |||||||
260 | QPrintPreviewMainWindow *mw = new QPrintPreviewMainWindow(q); | - | ||||||
261 | QToolBar *toolbar = new QToolBar(mw); | - | ||||||
262 | toolbar->addAction(fitWidthAction); | - | ||||||
263 | toolbar->addAction(fitPageAction); | - | ||||||
264 | toolbar->addSeparator(); | - | ||||||
265 | toolbar->addWidget(zoomFactor); | - | ||||||
266 | toolbar->addAction(zoomOutAction); | - | ||||||
267 | toolbar->addAction(zoomInAction); | - | ||||||
268 | toolbar->addSeparator(); | - | ||||||
269 | toolbar->addAction(portraitAction); | - | ||||||
270 | toolbar->addAction(landscapeAction); | - | ||||||
271 | toolbar->addSeparator(); | - | ||||||
272 | toolbar->addAction(firstPageAction); | - | ||||||
273 | toolbar->addAction(prevPageAction); | - | ||||||
274 | - | |||||||
275 | // this is to ensure the label text and the editor text are | - | ||||||
276 | // aligned in all styles - the extra QVBoxLayout is a workaround | - | ||||||
277 | // for bug in QFormLayout | - | ||||||
278 | QWidget *pageEdit = new QWidget(toolbar); | - | ||||||
279 | QVBoxLayout *vboxLayout = new QVBoxLayout; | - | ||||||
280 | vboxLayout->setContentsMargins(0, 0, 0, 0); | - | ||||||
281 | #ifdef Q_OS_MAC | - | ||||||
282 | // We query the widgets about their size and then we fix the size. | - | ||||||
283 | // This should do the trick for the laying out part... | - | ||||||
284 | QSize pageNumEditSize, pageNumLabelSize; | - | ||||||
285 | pageNumEditSize = pageNumEdit->minimumSizeHint(); | - | ||||||
286 | pageNumLabelSize = pageNumLabel->minimumSizeHint(); | - | ||||||
287 | pageNumEdit->resize(pageNumEditSize); | - | ||||||
288 | pageNumLabel->resize(pageNumLabelSize); | - | ||||||
289 | #endif | - | ||||||
290 | QFormLayout *formLayout = new QFormLayout; | - | ||||||
291 | #ifdef Q_OS_MAC | - | ||||||
292 | // We have to change the growth policy in Mac. | - | ||||||
293 | formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow); | - | ||||||
294 | #endif | - | ||||||
295 | formLayout->setWidget(0, QFormLayout::LabelRole, pageNumEdit); | - | ||||||
296 | formLayout->setWidget(0, QFormLayout::FieldRole, pageNumLabel); | - | ||||||
297 | vboxLayout->addLayout(formLayout); | - | ||||||
298 | vboxLayout->setAlignment(Qt::AlignVCenter); | - | ||||||
299 | pageEdit->setLayout(vboxLayout); | - | ||||||
300 | toolbar->addWidget(pageEdit); | - | ||||||
301 | - | |||||||
302 | toolbar->addAction(nextPageAction); | - | ||||||
303 | toolbar->addAction(lastPageAction); | - | ||||||
304 | toolbar->addSeparator(); | - | ||||||
305 | toolbar->addAction(singleModeAction); | - | ||||||
306 | toolbar->addAction(facingModeAction); | - | ||||||
307 | toolbar->addAction(overviewModeAction); | - | ||||||
308 | toolbar->addSeparator(); | - | ||||||
309 | toolbar->addAction(pageSetupAction); | - | ||||||
310 | toolbar->addAction(printAction); | - | ||||||
311 | - | |||||||
312 | // Cannot use the actions' triggered signal here, since it doesn't autorepeat | - | ||||||
313 | QToolButton *zoomInButton = static_cast<QToolButton *>(toolbar->widgetForAction(zoomInAction)); | - | ||||||
314 | QToolButton *zoomOutButton = static_cast<QToolButton *>(toolbar->widgetForAction(zoomOutAction)); | - | ||||||
315 | zoomInButton->setAutoRepeat(true); | - | ||||||
316 | zoomInButton->setAutoRepeatInterval(200); | - | ||||||
317 | zoomInButton->setAutoRepeatDelay(200); | - | ||||||
318 | zoomOutButton->setAutoRepeat(true); | - | ||||||
319 | zoomOutButton->setAutoRepeatInterval(200); | - | ||||||
320 | zoomOutButton->setAutoRepeatDelay(200); | - | ||||||
321 | QObject::connect(zoomInButton, SIGNAL(clicked()), q, SLOT(_q_zoomIn())); | - | ||||||
322 | QObject::connect(zoomOutButton, SIGNAL(clicked()), q, SLOT(_q_zoomOut())); | - | ||||||
323 | - | |||||||
324 | mw->addToolBar(toolbar); | - | ||||||
325 | mw->setCentralWidget(preview); | - | ||||||
326 | // QMainWindows are always created as top levels, force it to be a | - | ||||||
327 | // plain widget | - | ||||||
328 | mw->setParent(q, Qt::Widget); | - | ||||||
329 | - | |||||||
330 | QVBoxLayout *topLayout = new QVBoxLayout; | - | ||||||
331 | topLayout->addWidget(mw); | - | ||||||
332 | topLayout->setMargin(0); | - | ||||||
333 | q->setLayout(topLayout); | - | ||||||
334 | - | |||||||
335 | QString caption = QCoreApplication::translate("QPrintPreviewDialog", "Print Preview"); | - | ||||||
336 | if (!printer->docName().isEmpty())
| 0-1 | ||||||
337 | caption += QString::fromLatin1(": ") + printer->docName(); never executed: caption += QString::fromLatin1(": ") + printer->docName(); | 0 | ||||||
338 | q->setWindowTitle(caption); | - | ||||||
339 | - | |||||||
340 | if (!printer->isValid()
| 0-1 | ||||||
341 | #if defined(Q_OS_WIN) || defined(Q_OS_MAC) | - | ||||||
342 | || printer->outputFormat() != QPrinter::NativeFormat | - | ||||||
343 | #endif | - | ||||||
344 | ) | - | ||||||
345 | pageSetupAction->setEnabled(false); never executed: pageSetupAction->setEnabled(false); | 0 | ||||||
346 | preview->setFocus(); | - | ||||||
347 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||
348 | - | |||||||
349 | static inline void qt_setupActionIcon(QAction *action, QLatin1String name) | - | ||||||
350 | { | - | ||||||
351 | QLatin1String imagePrefix(":/qt-project.org/dialogs/qprintpreviewdialog/images/"); | - | ||||||
352 | QIcon icon; | - | ||||||
353 | icon.addFile(imagePrefix + name + QLatin1String("-24.png"), QSize(24, 24)); | - | ||||||
354 | icon.addFile(imagePrefix + name + QLatin1String("-32.png"), QSize(32, 32)); | - | ||||||
355 | action->setIcon(icon); | - | ||||||
356 | } | - | ||||||
357 | - | |||||||
358 | void QPrintPreviewDialogPrivate::setupActions() | - | ||||||
359 | { | - | ||||||
360 | Q_Q(QPrintPreviewDialog); | - | ||||||
361 | - | |||||||
362 | // Navigation | - | ||||||
363 | navGroup = new QActionGroup(q); | - | ||||||
364 | navGroup->setExclusive(false); | - | ||||||
365 | nextPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Next page")); | - | ||||||
366 | prevPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Previous page")); | - | ||||||
367 | firstPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "First page")); | - | ||||||
368 | lastPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Last page")); | - | ||||||
369 | qt_setupActionIcon(nextPageAction, QLatin1String("go-next")); | - | ||||||
370 | qt_setupActionIcon(prevPageAction, QLatin1String("go-previous")); | - | ||||||
371 | qt_setupActionIcon(firstPageAction, QLatin1String("go-first")); | - | ||||||
372 | qt_setupActionIcon(lastPageAction, QLatin1String("go-last")); | - | ||||||
373 | QObject::connect(navGroup, SIGNAL(triggered(QAction*)), q, SLOT(_q_navigate(QAction*))); | - | ||||||
374 | - | |||||||
375 | - | |||||||
376 | fitGroup = new QActionGroup(q); | - | ||||||
377 | fitWidthAction = fitGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Fit width")); | - | ||||||
378 | fitPageAction = fitGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Fit page")); | - | ||||||
379 | fitWidthAction->setObjectName(QLatin1String("fitWidthAction")); | - | ||||||
380 | fitPageAction->setObjectName(QLatin1String("fitPageAction")); | - | ||||||
381 | fitWidthAction->setCheckable(true); | - | ||||||
382 | fitPageAction->setCheckable(true); | - | ||||||
383 | qt_setupActionIcon(fitWidthAction, QLatin1String("fit-width")); | - | ||||||
384 | qt_setupActionIcon(fitPageAction, QLatin1String("fit-page")); | - | ||||||
385 | QObject::connect(fitGroup, SIGNAL(triggered(QAction*)), q, SLOT(_q_fit(QAction*))); | - | ||||||
386 | - | |||||||
387 | // Zoom | - | ||||||
388 | zoomGroup = new QActionGroup(q); | - | ||||||
389 | zoomInAction = zoomGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Zoom in")); | - | ||||||
390 | zoomOutAction = zoomGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Zoom out")); | - | ||||||
391 | qt_setupActionIcon(zoomInAction, QLatin1String("zoom-in")); | - | ||||||
392 | qt_setupActionIcon(zoomOutAction, QLatin1String("zoom-out")); | - | ||||||
393 | - | |||||||
394 | // Portrait/Landscape | - | ||||||
395 | orientationGroup = new QActionGroup(q); | - | ||||||
396 | portraitAction = orientationGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Portrait")); | - | ||||||
397 | landscapeAction = orientationGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Landscape")); | - | ||||||
398 | portraitAction->setCheckable(true); | - | ||||||
399 | landscapeAction->setCheckable(true); | - | ||||||
400 | qt_setupActionIcon(portraitAction, QLatin1String("layout-portrait")); | - | ||||||
401 | qt_setupActionIcon(landscapeAction, QLatin1String("layout-landscape")); | - | ||||||
402 | QObject::connect(portraitAction, SIGNAL(triggered(bool)), preview, SLOT(setPortraitOrientation())); | - | ||||||
403 | QObject::connect(landscapeAction, SIGNAL(triggered(bool)), preview, SLOT(setLandscapeOrientation())); | - | ||||||
404 | - | |||||||
405 | // Display mode | - | ||||||
406 | modeGroup = new QActionGroup(q); | - | ||||||
407 | singleModeAction = modeGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Show single page")); | - | ||||||
408 | facingModeAction = modeGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Show facing pages")); | - | ||||||
409 | overviewModeAction = modeGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Show overview of all pages")); | - | ||||||
410 | qt_setupActionIcon(singleModeAction, QLatin1String("view-page-one")); | - | ||||||
411 | qt_setupActionIcon(facingModeAction, QLatin1String("view-page-sided")); | - | ||||||
412 | qt_setupActionIcon(overviewModeAction, QLatin1String("view-page-multi")); | - | ||||||
413 | singleModeAction->setObjectName(QLatin1String("singleModeAction")); | - | ||||||
414 | facingModeAction->setObjectName(QLatin1String("facingModeAction")); | - | ||||||
415 | overviewModeAction->setObjectName(QLatin1String("overviewModeAction")); | - | ||||||
416 | - | |||||||
417 | singleModeAction->setCheckable(true); | - | ||||||
418 | facingModeAction->setCheckable(true); | - | ||||||
419 | overviewModeAction->setCheckable(true); | - | ||||||
420 | QObject::connect(modeGroup, SIGNAL(triggered(QAction*)), q, SLOT(_q_setMode(QAction*))); | - | ||||||
421 | - | |||||||
422 | - | |||||||
423 | printerGroup = new QActionGroup(q); | - | ||||||
424 | printAction = printerGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Print")); | - | ||||||
425 | pageSetupAction = printerGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Page setup")); | - | ||||||
426 | qt_setupActionIcon(printAction, QLatin1String("print")); | - | ||||||
427 | qt_setupActionIcon(pageSetupAction, QLatin1String("page-setup")); | - | ||||||
428 | QObject::connect(printAction, SIGNAL(triggered(bool)), q, SLOT(_q_print())); | - | ||||||
429 | QObject::connect(pageSetupAction, SIGNAL(triggered(bool)), q, SLOT(_q_pageSetup())); | - | ||||||
430 | - | |||||||
431 | // Initial state: | - | ||||||
432 | fitPageAction->setChecked(true); | - | ||||||
433 | singleModeAction->setChecked(true); | - | ||||||
434 | if (preview->orientation() == QPrinter::Portrait) | - | ||||||
435 | portraitAction->setChecked(true); | - | ||||||
436 | else | - | ||||||
437 | landscapeAction->setChecked(true); | - | ||||||
438 | } | - | ||||||
439 | - | |||||||
440 | - | |||||||
441 | bool QPrintPreviewDialogPrivate::isFitting() | - | ||||||
442 | { | - | ||||||
443 | return (fitGroup->isExclusive() | - | ||||||
444 | && (fitWidthAction->isChecked() || fitPageAction->isChecked())); | - | ||||||
445 | } | - | ||||||
446 | - | |||||||
447 | - | |||||||
448 | void QPrintPreviewDialogPrivate::setFitting(bool on) | - | ||||||
449 | { | - | ||||||
450 | if (isFitting() == on) | - | ||||||
451 | return; | - | ||||||
452 | fitGroup->setExclusive(on); | - | ||||||
453 | if (on) { | - | ||||||
454 | QAction* action = fitWidthAction->isChecked() ? fitWidthAction : fitPageAction; | - | ||||||
455 | action->setChecked(true); | - | ||||||
456 | if (fitGroup->checkedAction() != action) { | - | ||||||
457 | // work around exclusitivity problem | - | ||||||
458 | fitGroup->removeAction(action); | - | ||||||
459 | fitGroup->addAction(action); | - | ||||||
460 | } | - | ||||||
461 | } else { | - | ||||||
462 | fitWidthAction->setChecked(false); | - | ||||||
463 | fitPageAction->setChecked(false); | - | ||||||
464 | } | - | ||||||
465 | } | - | ||||||
466 | - | |||||||
467 | void QPrintPreviewDialogPrivate::updateNavActions() | - | ||||||
468 | { | - | ||||||
469 | int curPage = preview->currentPage(); | - | ||||||
470 | int numPages = preview->pageCount(); | - | ||||||
471 | nextPageAction->setEnabled(curPage < numPages); | - | ||||||
472 | prevPageAction->setEnabled(curPage > 1); | - | ||||||
473 | firstPageAction->setEnabled(curPage > 1); | - | ||||||
474 | lastPageAction->setEnabled(curPage < numPages); | - | ||||||
475 | pageNumEdit->setText(QString::number(curPage)); | - | ||||||
476 | } | - | ||||||
477 | - | |||||||
478 | void QPrintPreviewDialogPrivate::updatePageNumLabel() | - | ||||||
479 | { | - | ||||||
480 | Q_Q(QPrintPreviewDialog); | - | ||||||
481 | - | |||||||
482 | int numPages = preview->pageCount(); | - | ||||||
483 | int maxChars = QString::number(numPages).length(); | - | ||||||
484 | pageNumLabel->setText(QString::fromLatin1("/ %1").arg(numPages)); | - | ||||||
485 | int cyphersWidth = q->fontMetrics().width(QString().fill(QLatin1Char('8'), maxChars)); | - | ||||||
486 | int maxWidth = pageNumEdit->minimumSizeHint().width() + cyphersWidth; | - | ||||||
487 | pageNumEdit->setMinimumWidth(maxWidth); | - | ||||||
488 | pageNumEdit->setMaximumWidth(maxWidth); | - | ||||||
489 | pageNumEdit->setValidator(new QIntValidator(1, numPages, pageNumEdit)); | - | ||||||
490 | // any old one will be deleted later along with its parent pageNumEdit | - | ||||||
491 | } | - | ||||||
492 | - | |||||||
493 | void QPrintPreviewDialogPrivate::updateZoomFactor() | - | ||||||
494 | { | - | ||||||
495 | zoomFactor->lineEdit()->setText(QString().sprintf("%.1f%%", preview->zoomFactor()*100)); | - | ||||||
496 | } | - | ||||||
497 | - | |||||||
498 | void QPrintPreviewDialogPrivate::_q_fit(QAction* action) | - | ||||||
499 | { | - | ||||||
500 | setFitting(true); | - | ||||||
501 | if (action == fitPageAction) | - | ||||||
502 | preview->fitInView(); | - | ||||||
503 | else | - | ||||||
504 | preview->fitToWidth(); | - | ||||||
505 | } | - | ||||||
506 | - | |||||||
507 | void QPrintPreviewDialogPrivate::_q_zoomIn() | - | ||||||
508 | { | - | ||||||
509 | setFitting(false); | - | ||||||
510 | preview->zoomIn(); | - | ||||||
511 | updateZoomFactor(); | - | ||||||
512 | } | - | ||||||
513 | - | |||||||
514 | void QPrintPreviewDialogPrivate::_q_zoomOut() | - | ||||||
515 | { | - | ||||||
516 | setFitting(false); | - | ||||||
517 | preview->zoomOut(); | - | ||||||
518 | updateZoomFactor(); | - | ||||||
519 | } | - | ||||||
520 | - | |||||||
521 | void QPrintPreviewDialogPrivate::_q_pageNumEdited() | - | ||||||
522 | { | - | ||||||
523 | bool ok = false; | - | ||||||
524 | int res = pageNumEdit->text().toInt(&ok); | - | ||||||
525 | if (ok) | - | ||||||
526 | preview->setCurrentPage(res); | - | ||||||
527 | } | - | ||||||
528 | - | |||||||
529 | void QPrintPreviewDialogPrivate::_q_navigate(QAction* action) | - | ||||||
530 | { | - | ||||||
531 | int curPage = preview->currentPage(); | - | ||||||
532 | if (action == prevPageAction) | - | ||||||
533 | preview->setCurrentPage(curPage - 1); | - | ||||||
534 | else if (action == nextPageAction) | - | ||||||
535 | preview->setCurrentPage(curPage + 1); | - | ||||||
536 | else if (action == firstPageAction) | - | ||||||
537 | preview->setCurrentPage(1); | - | ||||||
538 | else if (action == lastPageAction) | - | ||||||
539 | preview->setCurrentPage(preview->pageCount()); | - | ||||||
540 | updateNavActions(); | - | ||||||
541 | } | - | ||||||
542 | - | |||||||
543 | void QPrintPreviewDialogPrivate::_q_setMode(QAction* action) | - | ||||||
544 | { | - | ||||||
545 | if (action == overviewModeAction) { | - | ||||||
546 | preview->setViewMode(QPrintPreviewWidget::AllPagesView); | - | ||||||
547 | setFitting(false); | - | ||||||
548 | fitGroup->setEnabled(false); | - | ||||||
549 | navGroup->setEnabled(false); | - | ||||||
550 | pageNumEdit->setEnabled(false); | - | ||||||
551 | pageNumLabel->setEnabled(false); | - | ||||||
552 | } else if (action == facingModeAction) { | - | ||||||
553 | preview->setViewMode(QPrintPreviewWidget::FacingPagesView); | - | ||||||
554 | } else { | - | ||||||
555 | preview->setViewMode(QPrintPreviewWidget::SinglePageView); | - | ||||||
556 | } | - | ||||||
557 | if (action == facingModeAction || action == singleModeAction) { | - | ||||||
558 | fitGroup->setEnabled(true); | - | ||||||
559 | navGroup->setEnabled(true); | - | ||||||
560 | pageNumEdit->setEnabled(true); | - | ||||||
561 | pageNumLabel->setEnabled(true); | - | ||||||
562 | setFitting(true); | - | ||||||
563 | } | - | ||||||
564 | } | - | ||||||
565 | - | |||||||
566 | void QPrintPreviewDialogPrivate::_q_print() | - | ||||||
567 | { | - | ||||||
568 | Q_Q(QPrintPreviewDialog); | - | ||||||
569 | - | |||||||
570 | #if defined(Q_OS_WIN) || defined(Q_OS_MAC) | - | ||||||
571 | if (printer->outputFormat() != QPrinter::NativeFormat) { | - | ||||||
572 | QString title = QCoreApplication::translate("QPrintPreviewDialog", "Export to PDF"); | - | ||||||
573 | QString suffix = QLatin1String(".pdf"); | - | ||||||
574 | QString fileName = QFileDialog::getSaveFileName(q, title, printer->outputFileName(), | - | ||||||
575 | QLatin1Char('*') + suffix); | - | ||||||
576 | if (!fileName.isEmpty()) { | - | ||||||
577 | if (QFileInfo(fileName).suffix().isEmpty()) | - | ||||||
578 | fileName.append(suffix); | - | ||||||
579 | printer->setOutputFileName(fileName); | - | ||||||
580 | } | - | ||||||
581 | if (!printer->outputFileName().isEmpty()) | - | ||||||
582 | preview->print(); | - | ||||||
583 | q->accept(); | - | ||||||
584 | return; | - | ||||||
585 | } | - | ||||||
586 | #endif | - | ||||||
587 | - | |||||||
588 | if (!printDialog) | - | ||||||
589 | printDialog = new QPrintDialog(printer, q); | - | ||||||
590 | if (printDialog->exec() == QDialog::Accepted) { | - | ||||||
591 | preview->print(); | - | ||||||
592 | q->accept(); | - | ||||||
593 | } | - | ||||||
594 | } | - | ||||||
595 | - | |||||||
596 | void QPrintPreviewDialogPrivate::_q_pageSetup() | - | ||||||
597 | { | - | ||||||
598 | Q_Q(QPrintPreviewDialog); | - | ||||||
599 | - | |||||||
600 | QPageSetupDialog pageSetup(printer, q); | - | ||||||
601 | if (pageSetup.exec() == QDialog::Accepted) { | - | ||||||
602 | // update possible orientation changes | - | ||||||
603 | if (preview->orientation() == QPrinter::Portrait) { | - | ||||||
604 | portraitAction->setChecked(true); | - | ||||||
605 | preview->setPortraitOrientation(); | - | ||||||
606 | }else { | - | ||||||
607 | landscapeAction->setChecked(true); | - | ||||||
608 | preview->setLandscapeOrientation(); | - | ||||||
609 | } | - | ||||||
610 | } | - | ||||||
611 | } | - | ||||||
612 | - | |||||||
613 | void QPrintPreviewDialogPrivate::_q_previewChanged() | - | ||||||
614 | { | - | ||||||
615 | updateNavActions(); | - | ||||||
616 | updatePageNumLabel(); | - | ||||||
617 | updateZoomFactor(); | - | ||||||
618 | } | - | ||||||
619 | - | |||||||
620 | void QPrintPreviewDialogPrivate::_q_zoomFactorChanged() | - | ||||||
621 | { | - | ||||||
622 | QString text = zoomFactor->lineEdit()->text(); | - | ||||||
623 | bool ok; | - | ||||||
624 | qreal factor = text.remove(QLatin1Char('%')).toFloat(&ok); | - | ||||||
625 | factor = qMax(qreal(1.0), qMin(qreal(1000.0), factor)); | - | ||||||
626 | if (ok) { | - | ||||||
627 | preview->setZoomFactor(factor/100.0); | - | ||||||
628 | zoomFactor->setEditText(QString::fromLatin1("%1%").arg(factor)); | - | ||||||
629 | setFitting(false); | - | ||||||
630 | } | - | ||||||
631 | } | - | ||||||
632 | - | |||||||
633 | /////////////////////////////////////////////////////////////////////////// | - | ||||||
634 | - | |||||||
635 | /*! | - | ||||||
636 | \class QPrintPreviewDialog | - | ||||||
637 | \since 4.4 | - | ||||||
638 | - | |||||||
639 | \brief The QPrintPreviewDialog class provides a dialog for | - | ||||||
640 | previewing and configuring page layouts for printer output. | - | ||||||
641 | - | |||||||
642 | \ingroup standard-dialogs | - | ||||||
643 | \ingroup printing | - | ||||||
644 | \inmodule QtPrintSupport | - | ||||||
645 | - | |||||||
646 | Using QPrintPreviewDialog in your existing application is | - | ||||||
647 | straightforward: | - | ||||||
648 | - | |||||||
649 | \list 1 | - | ||||||
650 | \li Create the QPrintPreviewDialog. | - | ||||||
651 | - | |||||||
652 | You can construct a QPrintPreviewDialog with an existing QPrinter | - | ||||||
653 | object, or you can have QPrintPreviewDialog create one for you, | - | ||||||
654 | which will be the system default printer. | - | ||||||
655 | - | |||||||
656 | \li Connect the paintRequested() signal to a slot. | - | ||||||
657 | - | |||||||
658 | When the dialog needs to generate a set of preview pages, the | - | ||||||
659 | paintRequested() signal will be emitted. You can use the exact | - | ||||||
660 | same code for the actual printing as for having the preview | - | ||||||
661 | generated, including calling QPrinter::newPage() to start a new | - | ||||||
662 | page in the preview. Connect a slot to the paintRequested() | - | ||||||
663 | signal, where you draw onto the QPrinter object that is passed | - | ||||||
664 | into the slot. | - | ||||||
665 | - | |||||||
666 | \li Call exec(). | - | ||||||
667 | - | |||||||
668 | Call QPrintPreviewDialog::exec() to show the preview dialog. | - | ||||||
669 | \endlist | - | ||||||
670 | - | |||||||
671 | \sa QPrinter, QPrintDialog, QPageSetupDialog, QPrintPreviewWidget | - | ||||||
672 | */ | - | ||||||
673 | - | |||||||
674 | /*! | - | ||||||
675 | Constructs a QPrintPreviewDialog based on \a printer and with \a | - | ||||||
676 | parent as the parent widget. The widget flags \a flags are passed on | - | ||||||
677 | to the QWidget constructor. | - | ||||||
678 | - | |||||||
679 | \sa QWidget::setWindowFlags() | - | ||||||
680 | */ | - | ||||||
681 | QPrintPreviewDialog::QPrintPreviewDialog(QPrinter* printer, QWidget *parent, Qt::WindowFlags flags) | - | ||||||
682 | : QDialog(*new QPrintPreviewDialogPrivate, parent, flags) | - | ||||||
683 | { | - | ||||||
684 | Q_D(QPrintPreviewDialog); | - | ||||||
685 | d->init(printer); | - | ||||||
686 | } | - | ||||||
687 | - | |||||||
688 | /*! | - | ||||||
689 | \overload | - | ||||||
690 | \fn QPrintPreviewDialog::QPrintPreviewDialog(QWidget *parent, Qt::WindowFlags flags) | - | ||||||
691 | - | |||||||
692 | This will create an internal QPrinter object, which will use the | - | ||||||
693 | system default printer. | - | ||||||
694 | */ | - | ||||||
695 | QPrintPreviewDialog::QPrintPreviewDialog(QWidget *parent, Qt::WindowFlags f) | - | ||||||
696 | : QDialog(*new QPrintPreviewDialogPrivate, parent, f) | - | ||||||
697 | { | - | ||||||
698 | Q_D(QPrintPreviewDialog); | - | ||||||
699 | d->init(); | - | ||||||
700 | } | - | ||||||
701 | - | |||||||
702 | /*! | - | ||||||
703 | Destroys the QPrintPreviewDialog. | - | ||||||
704 | */ | - | ||||||
705 | QPrintPreviewDialog::~QPrintPreviewDialog() | - | ||||||
706 | { | - | ||||||
707 | Q_D(QPrintPreviewDialog); | - | ||||||
708 | if (d->ownPrinter) | - | ||||||
709 | delete d->printer; | - | ||||||
710 | delete d->printDialog; | - | ||||||
711 | } | - | ||||||
712 | - | |||||||
713 | /*! | - | ||||||
714 | \reimp | - | ||||||
715 | */ | - | ||||||
716 | void QPrintPreviewDialog::setVisible(bool visible) | - | ||||||
717 | { | - | ||||||
718 | Q_D(QPrintPreviewDialog); | - | ||||||
719 | // this will make the dialog get a decent default size | - | ||||||
720 | if (visible && !d->initialized) { | - | ||||||
721 | d->preview->updatePreview(); | - | ||||||
722 | d->initialized = true; | - | ||||||
723 | } | - | ||||||
724 | QDialog::setVisible(visible); | - | ||||||
725 | } | - | ||||||
726 | - | |||||||
727 | /*! | - | ||||||
728 | \reimp | - | ||||||
729 | */ | - | ||||||
730 | void QPrintPreviewDialog::done(int result) | - | ||||||
731 | { | - | ||||||
732 | Q_D(QPrintPreviewDialog); | - | ||||||
733 | QDialog::done(result); | - | ||||||
734 | if (d->receiverToDisconnectOnClose) { | - | ||||||
735 | disconnect(this, SIGNAL(finished(int)), | - | ||||||
736 | d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose); | - | ||||||
737 | d->receiverToDisconnectOnClose = 0; | - | ||||||
738 | } | - | ||||||
739 | d->memberToDisconnectOnClose.clear(); | - | ||||||
740 | } | - | ||||||
741 | - | |||||||
742 | /*! | - | ||||||
743 | \overload | - | ||||||
744 | \since 4.5 | - | ||||||
745 | - | |||||||
746 | Opens the dialog and connects its finished(int) signal to the slot specified | - | ||||||
747 | by \a receiver and \a member. | - | ||||||
748 | - | |||||||
749 | The signal will be disconnected from the slot when the dialog is closed. | - | ||||||
750 | */ | - | ||||||
751 | void QPrintPreviewDialog::open(QObject *receiver, const char *member) | - | ||||||
752 | { | - | ||||||
753 | Q_D(QPrintPreviewDialog); | - | ||||||
754 | // the int parameter isn't very useful here; we could just as well connect | - | ||||||
755 | // to reject(), but this feels less robust somehow | - | ||||||
756 | connect(this, SIGNAL(finished(int)), receiver, member); | - | ||||||
757 | d->receiverToDisconnectOnClose = receiver; | - | ||||||
758 | d->memberToDisconnectOnClose = member; | - | ||||||
759 | QDialog::open(); | - | ||||||
760 | } | - | ||||||
761 | - | |||||||
762 | /*! | - | ||||||
763 | Returns a pointer to the QPrinter object this dialog is currently | - | ||||||
764 | operating on. | - | ||||||
765 | */ | - | ||||||
766 | QPrinter *QPrintPreviewDialog::printer() | - | ||||||
767 | { | - | ||||||
768 | Q_D(QPrintPreviewDialog); | - | ||||||
769 | return d->printer; | - | ||||||
770 | } | - | ||||||
771 | - | |||||||
772 | /*! | - | ||||||
773 | \fn void QPrintPreviewDialog::paintRequested(QPrinter *printer) | - | ||||||
774 | - | |||||||
775 | This signal is emitted when the QPrintPreviewDialog needs to generate | - | ||||||
776 | a set of preview pages. | - | ||||||
777 | - | |||||||
778 | The \a printer instance supplied is the paint device onto which you should | - | ||||||
779 | paint the contents of each page, using the QPrinter instance in the same way | - | ||||||
780 | as you would when printing directly. | - | ||||||
781 | */ | - | ||||||
782 | - | |||||||
783 | - | |||||||
784 | QT_END_NAMESPACE | - | ||||||
785 | - | |||||||
786 | #include "moc_qprintpreviewdialog.cpp" | - | ||||||
787 | #include "qprintpreviewdialog.moc" | - | ||||||
788 | - | |||||||
789 | #endif // QT_NO_PRINTPREVIEWDIALOG | - | ||||||
790 | - | |||||||
791 | - | |||||||
Source code | Switch to Preprocessed file |