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

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

trunk: Merged in qt 4.6.2 sources.

File size: 26.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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#if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
211 QAction *closeAction;
212#endif
213
214 QPointer<QObject> receiverToDisconnectOnClose;
215 QByteArray memberToDisconnectOnClose;
216};
217
218void QPrintPreviewDialogPrivate::init(QPrinter *_printer)
219{
220 Q_Q(QPrintPreviewDialog);
221
222 if (_printer) {
223 preview = new QPrintPreviewWidget(_printer, q);
224 printer = _printer;
225 } else {
226 ownPrinter = true;
227 printer = new QPrinter;
228 preview = new QPrintPreviewWidget(printer, q);
229 }
230 QObject::connect(preview, SIGNAL(paintRequested(QPrinter*)), q, SIGNAL(paintRequested(QPrinter*)));
231 QObject::connect(preview, SIGNAL(previewChanged()), q, SLOT(_q_previewChanged()));
232 setupActions();
233
234 pageNumEdit = new LineEdit;
235 pageNumEdit->setAlignment(Qt::AlignRight);
236 pageNumEdit->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
237 pageNumLabel = new QLabel;
238 QObject::connect(pageNumEdit, SIGNAL(editingFinished()), q, SLOT(_q_pageNumEdited()));
239
240 zoomFactor = new QComboBox;
241 zoomFactor->setEditable(true);
242 zoomFactor->setMinimumContentsLength(7);
243 zoomFactor->setInsertPolicy(QComboBox::NoInsert);
244 LineEdit *zoomEditor = new LineEdit;
245 zoomEditor->setValidator(new ZoomFactorValidator(1, 1000, 1, zoomEditor));
246 zoomFactor->setLineEdit(zoomEditor);
247 static const short factorsX2[] = { 25, 50, 100, 200, 250, 300, 400, 800, 1600 };
248 for (int i = 0; i < int(sizeof(factorsX2) / sizeof(factorsX2[0])); ++i)
249 zoomFactor->addItem(QPrintPreviewDialog::tr("%1%").arg(factorsX2[i] / 2.0));
250 QObject::connect(zoomFactor->lineEdit(), SIGNAL(editingFinished()),
251 q, SLOT(_q_zoomFactorChanged()));
252 QObject::connect(zoomFactor, SIGNAL(currentIndexChanged(int)),
253 q, SLOT(_q_zoomFactorChanged()));
254
255 QPrintPreviewMainWindow *mw = new QPrintPreviewMainWindow(q);
256 QToolBar *toolbar = new QToolBar(mw);
257 toolbar->addAction(fitWidthAction);
258 toolbar->addAction(fitPageAction);
259 toolbar->addSeparator();
260 toolbar->addWidget(zoomFactor);
261 toolbar->addAction(zoomOutAction);
262 toolbar->addAction(zoomInAction);
263 toolbar->addSeparator();
264 toolbar->addAction(portraitAction);
265 toolbar->addAction(landscapeAction);
266 toolbar->addSeparator();
267 toolbar->addAction(firstPageAction);
268 toolbar->addAction(prevPageAction);
269
270 // this is to ensure the label text and the editor text are
271 // aligned in all styles - the extra QVBoxLayout is a workaround
272 // for bug in QFormLayout
273 QWidget *pageEdit = new QWidget(toolbar);
274 QVBoxLayout *vboxLayout = new QVBoxLayout;
275 vboxLayout->setContentsMargins(0, 0, 0, 0);
276 QFormLayout *formLayout = new QFormLayout;
277 formLayout->setWidget(0, QFormLayout::LabelRole, pageNumEdit);
278 formLayout->setWidget(0, QFormLayout::FieldRole, pageNumLabel);
279 vboxLayout->addLayout(formLayout);
280 vboxLayout->setAlignment(Qt::AlignVCenter);
281 pageEdit->setLayout(vboxLayout);
282 toolbar->addWidget(pageEdit);
283
284 toolbar->addAction(nextPageAction);
285 toolbar->addAction(lastPageAction);
286 toolbar->addSeparator();
287 toolbar->addAction(singleModeAction);
288 toolbar->addAction(facingModeAction);
289 toolbar->addAction(overviewModeAction);
290 toolbar->addSeparator();
291 toolbar->addAction(pageSetupAction);
292 toolbar->addAction(printAction);
293#if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
294 toolbar->addAction(closeAction);
295#endif
296
297 // Cannot use the actions' triggered signal here, since it doesn't autorepeat
298 QToolButton *zoomInButton = static_cast<QToolButton *>(toolbar->widgetForAction(zoomInAction));
299 QToolButton *zoomOutButton = static_cast<QToolButton *>(toolbar->widgetForAction(zoomOutAction));
300 zoomInButton->setAutoRepeat(true);
301 zoomInButton->setAutoRepeatInterval(200);
302 zoomInButton->setAutoRepeatDelay(200);
303 zoomOutButton->setAutoRepeat(true);
304 zoomOutButton->setAutoRepeatInterval(200);
305 zoomOutButton->setAutoRepeatDelay(200);
306 QObject::connect(zoomInButton, SIGNAL(clicked()), q, SLOT(_q_zoomIn()));
307 QObject::connect(zoomOutButton, SIGNAL(clicked()), q, SLOT(_q_zoomOut()));
308
309 mw->addToolBar(toolbar);
310 mw->setCentralWidget(preview);
311 // QMainWindows are always created as top levels, force it to be a
312 // plain widget
313 mw->setParent(q, Qt::Widget);
314
315 QVBoxLayout *topLayout = new QVBoxLayout;
316 topLayout->addWidget(mw);
317 topLayout->setMargin(0);
318 q->setLayout(topLayout);
319
320 QString caption = QCoreApplication::translate("QPrintPreviewDialog", "Print Preview");
321 if (!printer->docName().isEmpty())
322 caption += QString::fromLatin1(": ") + printer->docName();
323 q->setWindowTitle(caption);
324
325 if (!printer->isValid()
326#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
327 || printer->outputFormat() != QPrinter::NativeFormat
328#endif
329 )
330 pageSetupAction->setEnabled(false);
331 preview->setFocus();
332}
333
334static inline void qt_setupActionIcon(QAction *action, const QLatin1String &name)
335{
336 QLatin1String imagePrefix(":/trolltech/dialogs/qprintpreviewdialog/images/");
337 QIcon icon;
338 icon.addFile(imagePrefix + name + QLatin1String("-24.png"), QSize(24, 24));
339 icon.addFile(imagePrefix + name + QLatin1String("-32.png"), QSize(32, 32));
340 action->setIcon(icon);
341}
342
343void QPrintPreviewDialogPrivate::setupActions()
344{
345 Q_Q(QPrintPreviewDialog);
346
347 // Navigation
348 navGroup = new QActionGroup(q);
349 navGroup->setExclusive(false);
350 nextPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Next page"));
351 prevPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Previous page"));
352 firstPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "First page"));
353 lastPageAction = navGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Last page"));
354 qt_setupActionIcon(nextPageAction, QLatin1String("go-next"));
355 qt_setupActionIcon(prevPageAction, QLatin1String("go-previous"));
356 qt_setupActionIcon(firstPageAction, QLatin1String("go-first"));
357 qt_setupActionIcon(lastPageAction, QLatin1String("go-last"));
358 QObject::connect(navGroup, SIGNAL(triggered(QAction*)), q, SLOT(_q_navigate(QAction*)));
359
360
361 fitGroup = new QActionGroup(q);
362 fitWidthAction = fitGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Fit width"));
363 fitPageAction = fitGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Fit page"));
364 fitWidthAction->setObjectName(QLatin1String("fitWidthAction"));
365 fitPageAction->setObjectName(QLatin1String("fitPageAction"));
366 fitWidthAction->setCheckable(true);
367 fitPageAction->setCheckable(true);
368 qt_setupActionIcon(fitWidthAction, QLatin1String("fit-width"));
369 qt_setupActionIcon(fitPageAction, QLatin1String("fit-page"));
370 QObject::connect(fitGroup, SIGNAL(triggered(QAction*)), q, SLOT(_q_fit(QAction*)));
371
372 // Zoom
373 zoomGroup = new QActionGroup(q);
374 zoomInAction = zoomGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Zoom in"));
375 zoomOutAction = zoomGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Zoom out"));
376 qt_setupActionIcon(zoomInAction, QLatin1String("zoom-in"));
377 qt_setupActionIcon(zoomOutAction, QLatin1String("zoom-out"));
378
379 // Portrait/Landscape
380 orientationGroup = new QActionGroup(q);
381 portraitAction = orientationGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Portrait"));
382 landscapeAction = orientationGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Landscape"));
383 portraitAction->setCheckable(true);
384 landscapeAction->setCheckable(true);
385 qt_setupActionIcon(portraitAction, QLatin1String("layout-portrait"));
386 qt_setupActionIcon(landscapeAction, QLatin1String("layout-landscape"));
387 QObject::connect(portraitAction, SIGNAL(triggered(bool)), preview, SLOT(setPortraitOrientation()));
388 QObject::connect(landscapeAction, SIGNAL(triggered(bool)), preview, SLOT(setLandscapeOrientation()));
389
390 // Display mode
391 modeGroup = new QActionGroup(q);
392 singleModeAction = modeGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Show single page"));
393 facingModeAction = modeGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Show facing pages"));
394 overviewModeAction = modeGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Show overview of all pages"));
395 qt_setupActionIcon(singleModeAction, QLatin1String("view-page-one"));
396 qt_setupActionIcon(facingModeAction, QLatin1String("view-page-sided"));
397 qt_setupActionIcon(overviewModeAction, QLatin1String("view-page-multi"));
398 singleModeAction->setObjectName(QLatin1String("singleModeAction"));
399 facingModeAction->setObjectName(QLatin1String("facingModeAction"));
400 overviewModeAction->setObjectName(QLatin1String("overviewModeAction"));
401
402 singleModeAction->setCheckable(true);
403 facingModeAction->setCheckable(true);
404 overviewModeAction->setCheckable(true);
405 QObject::connect(modeGroup, SIGNAL(triggered(QAction*)), q, SLOT(_q_setMode(QAction*)));
406
407 // Print
408 printerGroup = new QActionGroup(q);
409 printAction = printerGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Print"));
410 pageSetupAction = printerGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Page setup"));
411 qt_setupActionIcon(printAction, QLatin1String("print"));
412 qt_setupActionIcon(pageSetupAction, QLatin1String("page-setup"));
413 QObject::connect(printAction, SIGNAL(triggered(bool)), q, SLOT(_q_print()));
414 QObject::connect(pageSetupAction, SIGNAL(triggered(bool)), q, SLOT(_q_pageSetup()));
415#if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
416 closeAction = printerGroup->addAction(QCoreApplication::translate("QPrintPreviewDialog", "Close"));
417 QObject::connect(closeAction, SIGNAL(triggered(bool)), q, SLOT(reject()));
418#endif
419
420 // Initial state:
421 fitPageAction->setChecked(true);
422 singleModeAction->setChecked(true);
423 if (preview->orientation() == QPrinter::Portrait)
424 portraitAction->setChecked(true);
425 else
426 landscapeAction->setChecked(true);
427}
428
429
430bool QPrintPreviewDialogPrivate::isFitting()
431{
432 return (fitGroup->isExclusive()
433 && (fitWidthAction->isChecked() || fitPageAction->isChecked()));
434}
435
436
437void QPrintPreviewDialogPrivate::setFitting(bool on)
438{
439 if (isFitting() == on)
440 return;
441 fitGroup->setExclusive(on);
442 if (on) {
443 QAction* action = fitWidthAction->isChecked() ? fitWidthAction : fitPageAction;
444 action->setChecked(true);
445 if (fitGroup->checkedAction() != action) {
446 // work around exclusitivity problem
447 fitGroup->removeAction(action);
448 fitGroup->addAction(action);
449 }
450 } else {
451 fitWidthAction->setChecked(false);
452 fitPageAction->setChecked(false);
453 }
454}
455
456void QPrintPreviewDialogPrivate::updateNavActions()
457{
458 int curPage = preview->currentPage();
459 int numPages = preview->pageCount();
460 nextPageAction->setEnabled(curPage < numPages);
461 prevPageAction->setEnabled(curPage > 1);
462 firstPageAction->setEnabled(curPage > 1);
463 lastPageAction->setEnabled(curPage < numPages);
464 pageNumEdit->setText(QString::number(curPage));
465}
466
467void QPrintPreviewDialogPrivate::updatePageNumLabel()
468{
469 Q_Q(QPrintPreviewDialog);
470
471 int numPages = preview->pageCount();
472 int maxChars = QString::number(numPages).length();
473 pageNumLabel->setText(QString::fromLatin1("/ %1").arg(numPages));
474 int cyphersWidth = q->fontMetrics().width(QString().fill(QLatin1Char('8'), maxChars));
475 int maxWidth = pageNumEdit->minimumSizeHint().width() + cyphersWidth;
476 pageNumEdit->setMinimumWidth(maxWidth);
477 pageNumEdit->setMaximumWidth(maxWidth);
478 pageNumEdit->setValidator(new QIntValidator(1, numPages, pageNumEdit));
479 // any old one will be deleted later along with its parent pageNumEdit
480}
481
482void QPrintPreviewDialogPrivate::updateZoomFactor()
483{
484 zoomFactor->lineEdit()->setText(QString().sprintf("%.1f%%", preview->zoomFactor()*100));
485}
486
487void QPrintPreviewDialogPrivate::_q_fit(QAction* action)
488{
489 setFitting(true);
490 if (action == fitPageAction)
491 preview->fitInView();
492 else
493 preview->fitToWidth();
494}
495
496void QPrintPreviewDialogPrivate::_q_zoomIn()
497{
498 setFitting(false);
499 preview->zoomIn();
500 updateZoomFactor();
501}
502
503void QPrintPreviewDialogPrivate::_q_zoomOut()
504{
505 setFitting(false);
506 preview->zoomOut();
507 updateZoomFactor();
508}
509
510void QPrintPreviewDialogPrivate::_q_pageNumEdited()
511{
512 bool ok = false;
513 int res = pageNumEdit->text().toInt(&ok);
514 if (ok)
515 preview->setCurrentPage(res);
516}
517
518void QPrintPreviewDialogPrivate::_q_navigate(QAction* action)
519{
520 int curPage = preview->currentPage();
521 if (action == prevPageAction)
522 preview->setCurrentPage(curPage - 1);
523 else if (action == nextPageAction)
524 preview->setCurrentPage(curPage + 1);
525 else if (action == firstPageAction)
526 preview->setCurrentPage(1);
527 else if (action == lastPageAction)
528 preview->setCurrentPage(preview->pageCount());
529 updateNavActions();
530}
531
532void QPrintPreviewDialogPrivate::_q_setMode(QAction* action)
533{
534 if (action == overviewModeAction) {
535 preview->setViewMode(QPrintPreviewWidget::AllPagesView);
536 setFitting(false);
537 fitGroup->setEnabled(false);
538 navGroup->setEnabled(false);
539 pageNumEdit->setEnabled(false);
540 pageNumLabel->setEnabled(false);
541 } else if (action == facingModeAction) {
542 preview->setViewMode(QPrintPreviewWidget::FacingPagesView);
543 } else {
544 preview->setViewMode(QPrintPreviewWidget::SinglePageView);
545 }
546 if (action == facingModeAction || action == singleModeAction) {
547 fitGroup->setEnabled(true);
548 navGroup->setEnabled(true);
549 pageNumEdit->setEnabled(true);
550 pageNumLabel->setEnabled(true);
551 setFitting(true);
552 }
553}
554
555void QPrintPreviewDialogPrivate::_q_print()
556{
557 Q_Q(QPrintPreviewDialog);
558
559#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
560 if (printer->outputFormat() != QPrinter::NativeFormat) {
561 QString title;
562 QString suffix;
563 if (printer->outputFormat() == QPrinter::PdfFormat) {
564 title = QCoreApplication::translate("QPrintPreviewDialog", "Export to PDF");
565 suffix = QLatin1String(".pdf");
566 } else {
567 title = QCoreApplication::translate("QPrintPreviewDialog", "Export to PostScript");
568 suffix = QLatin1String(".ps");
569 }
570 QString fileName = QFileDialog::getSaveFileName(q, title, printer->outputFileName(),
571 QLatin1Char('*') + suffix);
572 if (!fileName.isEmpty()) {
573 if (QFileInfo(fileName).suffix().isEmpty())
574 fileName.append(suffix);
575 printer->setOutputFileName(fileName);
576 }
577 if (!printer->outputFileName().isEmpty())
578 preview->print();
579 q->accept();
580 return;
581 }
582#endif
583
584 if (!printDialog)
585 printDialog = new QPrintDialog(printer, q);
586 if (printDialog->exec() == QDialog::Accepted) {
587 preview->print();
588 q->accept();
589 }
590}
591
592void QPrintPreviewDialogPrivate::_q_pageSetup()
593{
594 Q_Q(QPrintPreviewDialog);
595
596 QPageSetupDialog pageSetup(printer, q);
597 if (pageSetup.exec() == QDialog::Accepted) {
598 // update possible orientation changes
599 if (preview->orientation() == QPrinter::Portrait) {
600 portraitAction->setChecked(true);
601 preview->setPortraitOrientation();
602 }else {
603 landscapeAction->setChecked(true);
604 preview->setLandscapeOrientation();
605 }
606 }
607}
608
609void QPrintPreviewDialogPrivate::_q_previewChanged()
610{
611 updateNavActions();
612 updatePageNumLabel();
613 updateZoomFactor();
614}
615
616void QPrintPreviewDialogPrivate::_q_zoomFactorChanged()
617{
618 QString text = zoomFactor->lineEdit()->text();
619 bool ok;
620 qreal factor = text.remove(QLatin1Char('%')).toFloat(&ok);
621 factor = qMax(qreal(1.0), qMin(qreal(1000.0), factor));
622 if (ok) {
623 preview->setZoomFactor(factor/100.0);
624 zoomFactor->setEditText(QString::fromLatin1("%1%").arg(factor));
625 setFitting(false);
626 }
627}
628
629///////////////////////////////////////////////////////////////////////////
630
631/*!
632 \class QPrintPreviewDialog
633 \since 4.4
634
635 \brief The QPrintPreviewDialog class provides a dialog for
636 previewing and configuring page layouts for printer output.
637
638 \ingroup standard-dialogs
639 \ingroup printing
640
641 Using QPrintPreviewDialog in your existing application is
642 straightforward:
643
644 \list 1
645 \o Create the QPrintPreviewDialog.
646
647 You can construct a QPrintPreviewDialog with an existing QPrinter
648 object, or you can have QPrintPreviewDialog create one for you,
649 which will be the system default printer.
650
651 \o Connect the paintRequested() signal to a slot.
652
653 When the dialog needs to generate a set of preview pages, the
654 paintRequested() signal will be emitted. You can use the exact
655 same code for the actual printing as for having the preview
656 generated, including calling QPrinter::newPage() to start a new
657 page in the preview. Connect a slot to the paintRequested()
658 signal, where you draw onto the QPrinter object that is passed
659 into the slot.
660
661 \o Call exec().
662
663 Call QPrintPreviewDialog::exec() to show the preview dialog.
664 \endlist
665
666
667 \sa QPrinter, QPrintDialog, QPageSetupDialog, QPrintPreviewWidget
668*/
669
670/*!
671 Constructs a QPrintPreviewDialog based on \a printer and with \a
672 parent as the parent widget. The widget flags \a flags are passed on
673 to the QWidget constructor.
674
675 \sa QWidget::setWindowFlags()
676*/
677QPrintPreviewDialog::QPrintPreviewDialog(QPrinter* printer, QWidget *parent, Qt::WindowFlags flags)
678 : QDialog(*new QPrintPreviewDialogPrivate, parent, flags)
679{
680 Q_D(QPrintPreviewDialog);
681 d->init(printer);
682}
683
684/*!
685 \overload
686 \fn QPrintPreviewDialog::QPrintPreviewDialog(QWidget *parent, Qt::WindowFlags flags)
687
688 This will create an internal QPrinter object, which will use the
689 system default printer.
690*/
691QPrintPreviewDialog::QPrintPreviewDialog(QWidget *parent, Qt::WindowFlags f)
692 : QDialog(*new QPrintPreviewDialogPrivate, parent, f)
693{
694 Q_D(QPrintPreviewDialog);
695 d->init();
696}
697
698/*!
699 Destroys the QPrintPreviewDialog.
700*/
701QPrintPreviewDialog::~QPrintPreviewDialog()
702{
703 Q_D(QPrintPreviewDialog);
704 if (d->ownPrinter)
705 delete d->printer;
706 delete d->printDialog;
707}
708
709/*!
710 \reimp
711*/
712void QPrintPreviewDialog::setVisible(bool visible)
713{
714 Q_D(QPrintPreviewDialog);
715 // this will make the dialog get a decent default size
716 if (visible && !d->initialized) {
717 d->preview->updatePreview();
718 d->initialized = true;
719 }
720 QDialog::setVisible(visible);
721}
722
723/*!
724 \reimp
725*/
726void QPrintPreviewDialog::done(int result)
727{
728 Q_D(QPrintPreviewDialog);
729 QDialog::done(result);
730 if (d->receiverToDisconnectOnClose) {
731 disconnect(this, SIGNAL(finished(int)),
732 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
733 d->receiverToDisconnectOnClose = 0;
734 }
735 d->memberToDisconnectOnClose.clear();
736}
737
738/*!
739 \overload
740 \since 4.5
741
742 Opens the dialog and connects its finished(int) signal to the slot specified
743 by \a receiver and \a member.
744
745 The signal will be disconnected from the slot when the dialog is closed.
746*/
747void QPrintPreviewDialog::open(QObject *receiver, const char *member)
748{
749 Q_D(QPrintPreviewDialog);
750 // the int parameter isn't very useful here; we could just as well connect
751 // to reject(), but this feels less robust somehow
752 connect(this, SIGNAL(finished(int)), receiver, member);
753 d->receiverToDisconnectOnClose = receiver;
754 d->memberToDisconnectOnClose = member;
755 QDialog::open();
756}
757
758/*!
759 Returns a pointer to the QPrinter object this dialog is currently
760 operating on.
761*/
762QPrinter *QPrintPreviewDialog::printer()
763{
764 Q_D(QPrintPreviewDialog);
765 return d->printer;
766}
767
768/*!
769 \fn void QPrintPreviewDialog::paintRequested(QPrinter *printer)
770
771 This signal is emitted when the QPrintPreviewDialog needs to generate
772 a set of preview pages.
773
774 The \a printer instance supplied is the paint device onto which you should
775 paint the contents of each page, using the QPrinter instance in the same way
776 as you would when printing directly.
777*/
778
779
780QT_END_NAMESPACE
781
782#include "moc_qprintpreviewdialog.cpp"
783#include "qprintpreviewdialog.moc"
784
785#endif // QT_NO_PRINTPREVIEWDIALOG
786
787
Note: See TracBrowser for help on using the repository browser.