source: trunk/src/gui/dialogs/qprintpreviewdialog.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

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