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