From 4984b36c73fb59006a28051ea82bdca0b7aeb457 Mon Sep 17 00:00:00 2001 From: John Layt Date: Tue, 16 Feb 2010 01:13:13 +0000 Subject: QPrinter: Add Current Page print range support Support the selection of Current Page option in the print dialog by activating a new QPrintDialog::PrintDialogOption. If selected the PrintRange is set to CurrentPage. It is the responsibility of the application to read the PrintRange and apply the Current Page selection when rendering the pages. Merge-request: 2311 Reviewed-by: Trond --- src/gui/dialogs/qabstractprintdialog.cpp | 7 +- src/gui/dialogs/qabstractprintdialog.h | 6 +- src/gui/dialogs/qprintdialog_qws.cpp | 11 ++ src/gui/dialogs/qprintdialog_unix.cpp | 11 +- src/gui/dialogs/qprintdialog_win.cpp | 16 +- src/gui/dialogs/qprintsettingsoutput.ui | 246 +++++++++++++++---------------- src/gui/painting/qprinter.cpp | 3 + src/gui/painting/qprinter.h | 2 +- tests/auto/qprinter/tst_qprinter.cpp | 23 +++ 9 files changed, 186 insertions(+), 139 deletions(-) diff --git a/src/gui/dialogs/qabstractprintdialog.cpp b/src/gui/dialogs/qabstractprintdialog.cpp index 4523433..25d9ebb 100644 --- a/src/gui/dialogs/qabstractprintdialog.cpp +++ b/src/gui/dialogs/qabstractprintdialog.cpp @@ -76,6 +76,7 @@ class QPrintDialogPrivate : public QAbstractPrintDialogPrivate \value AllPages All pages should be printed. \value Selection Only the selection should be printed. \value PageRange The specified page range should be printed. + \value CurrentPage Only the currently visible page should be printed. \sa QPrinter::PrintRange */ @@ -89,7 +90,9 @@ class QPrintDialogPrivate : public QAbstractPrintDialogPrivate \value PrintToFile The print to file option is enabled. \value PrintSelection The print selection option is enabled. \value PrintPageRange The page range selection option is enabled. - \value PrintCollateCopies + \value PrintShowPageSize Show the page size + margins page only if this is enabled. + \value PrintCollateCopies The collate copies option is enabled + \value PrintCurrentPage The print current page option is enabled This value is obsolete and does nothing since Qt 4.5: @@ -97,8 +100,6 @@ class QPrintDialogPrivate : public QAbstractPrintDialogPrivate would create a sheet by default the dialog was given a parent. This is no longer supported in Qt 4.5. If you want to use sheets, use QPrintDialog::open() instead. - - \value PrintShowPageSize Show the page size + margins page only if this is enabled. */ /*! diff --git a/src/gui/dialogs/qabstractprintdialog.h b/src/gui/dialogs/qabstractprintdialog.h index 4d867f6..82e3df8 100644 --- a/src/gui/dialogs/qabstractprintdialog.h +++ b/src/gui/dialogs/qabstractprintdialog.h @@ -65,7 +65,8 @@ public: enum PrintRange { AllPages, Selection, - PageRange + PageRange, + CurrentPage }; enum PrintDialogOption { @@ -75,7 +76,8 @@ public: PrintPageRange = 0x0004, PrintShowPageSize = 0x0008, PrintCollateCopies = 0x0010, - DontUseSheet = 0x0020 + DontUseSheet = 0x0020, + PrintCurrentPage = 0x0040 }; Q_DECLARE_FLAGS(PrintDialogOptions, PrintDialogOption) diff --git a/src/gui/dialogs/qprintdialog_qws.cpp b/src/gui/dialogs/qprintdialog_qws.cpp index 1336c04..b071427 100644 --- a/src/gui/dialogs/qprintdialog_qws.cpp +++ b/src/gui/dialogs/qprintdialog_qws.cpp @@ -178,6 +178,10 @@ void QPrintDialogPrivate::_q_okClicked() q->setPrintRange(QPrintDialog::PageRange); q->setFromTo(firstPage->value(), lastPage->value()); break; + case (int)QPrintDialog::CurrentPage: + q->setPrintRange(QPrintDialog::CurrentPage); + q->setFromTo(0, 0); + break; } q->accept(); } @@ -375,6 +379,7 @@ void QPrintDialogPrivate::setupOptions() rangeCombo->addItem(QPrintDialog::tr("Print all"), QPrintDialog::AllPages); rangeCombo->addItem(QPrintDialog::tr("Print selection"), QPrintDialog::Selection); rangeCombo->addItem(QPrintDialog::tr("Print range"), QPrintDialog::PageRange); + rangeCombo->addItem(QPrintDialog::tr("Print current page"), QPrintDialog::CurrentPage); QObject::connect(rangeCombo, SIGNAL(activated(int)), q, SLOT(_q_printRangeSelected(int))); @@ -490,6 +495,9 @@ void QPrintDialogPrivate::setPrinter(QPrinter *p, bool pickUpSettings) if (!q->isOptionEnabled(QPrintDialog::PrintPageRange) && rangeCombo->findData(QPrintDialog::PageRange) > 0) rangeCombo->removeItem(rangeCombo->findData(QPrintDialog::PageRange)); + if (!q->isOptionEnabled(QPrintDialog::PrintCurrentPage) + && rangeCombo->findData(QPrintDialog::CurrentPage) > 0) + rangeCombo->removeItem(rangeCombo->findData(QPrintDialog::CurrentPage)); switch (q->printRange()) { case QPrintDialog::AllPages: @@ -501,6 +509,9 @@ void QPrintDialogPrivate::setPrinter(QPrinter *p, bool pickUpSettings) case QPrintDialog::PageRange: rangeCombo->setCurrentIndex((int)(QPrintDialog::PageRange)); break; + case QPrintDialog::CurrentPage: + rangeCombo->setCurrentIndex((int)(QPrintDialog::CurrentPage)); + break; } } diff --git a/src/gui/dialogs/qprintdialog_unix.cpp b/src/gui/dialogs/qprintdialog_unix.cpp index 2d169cf..0487f23 100644 --- a/src/gui/dialogs/qprintdialog_unix.cpp +++ b/src/gui/dialogs/qprintdialog_unix.cpp @@ -502,6 +502,9 @@ void QPrintDialogPrivate::setupPrinter() } else if (options.printSelection->isChecked()) { p->setPrintRange(QPrinter::Selection); p->setFromTo(0,0); + } else if (options.printCurrentPage->isChecked()) { + p->setPrintRange(QPrinter::CurrentPage); + p->setFromTo(0,0); } else if (options.printRange->isChecked()) { p->setPrintRange(QPrinter::PageRange); p->setFromTo(options.from->value(), qMax(options.from->value(), options.to->value())); @@ -518,10 +521,12 @@ void QPrintDialogPrivate::updateWidgets() { Q_Q(QPrintDialog); options.gbPrintRange->setVisible(q->isOptionEnabled(QPrintDialog::PrintPageRange) || - q->isOptionEnabled(QPrintDialog::PrintSelection)); + q->isOptionEnabled(QPrintDialog::PrintSelection) || + q->isOptionEnabled(QPrintDialog::PrintCurrentPage)); options.printRange->setEnabled(q->isOptionEnabled(QPrintDialog::PrintPageRange)); options.printSelection->setVisible(q->isOptionEnabled(QPrintDialog::PrintSelection)); + options.printCurrentPage->setVisible(q->isOptionEnabled(QPrintDialog::PrintCurrentPage)); options.collate->setVisible(q->isOptionEnabled(QPrintDialog::PrintCollateCopies)); switch (q->printRange()) { @@ -534,6 +539,10 @@ void QPrintDialogPrivate::updateWidgets() case QPrintDialog::PageRange: options.printRange->setChecked(true); break; + case QPrintDialog::CurrentPage: + if (q->isOptionEnabled(QPrintDialog::PrintCurrentPage)) + options.printCurrentPage->setChecked(true); + break; default: break; } diff --git a/src/gui/dialogs/qprintdialog_win.cpp b/src/gui/dialogs/qprintdialog_win.cpp index fa0c99f..1061660 100644 --- a/src/gui/dialogs/qprintdialog_win.cpp +++ b/src/gui/dialogs/qprintdialog_win.cpp @@ -128,11 +128,14 @@ static void qt_win_setup_PRINTDLGEX(PRINTDLGEX *pd, QWidget *parent, if (pd->nMinPage==0 && pd->nMaxPage==0) pd->Flags |= PD_NOPAGENUMS; - // we don't have a 'current page' notion in the QPrinter API yet. - // Neither do we support more than one page range, so limit those - // options - pd->Flags |= PD_NOCURRENTPAGE; + // Disable Current Page option if not required as default is Enabled + if (!pdlg->isOptionEnabled(QPrintDialog::PrintCurrentPage)) + pd->Flags |= PD_NOCURRENTPAGE; + + // Default to showing the General tab first pd->nStartPage = START_PAGE_GENERAL; + + // We don't support more than one page range in the QPrinter API yet. pd->nPageRanges = 1; pd->nMaxPageRanges = 1; @@ -153,7 +156,10 @@ static void qt_win_read_back_PRINTDLGEX(PRINTDLGEX *pd, QPrintDialog *pdlg, QPri } else if (pd->Flags & PD_PAGENUMS) { pdlg->setPrintRange(QPrintDialog::PageRange); pdlg->setFromTo(pd->lpPageRanges[0].nFromPage, pd->lpPageRanges[0].nToPage); - } else { + } else if (pd->Flags & PD_CURRENTPAGE) { + pdlg->setPrintRange(QPrintDialog::CurrentPage); + pdlg->setFromTo(0, 0); + } else { // PD_ALLPAGES pdlg->setPrintRange(QPrintDialog::AllPages); pdlg->setFromTo(0, 0); } diff --git a/src/gui/dialogs/qprintsettingsoutput.ui b/src/gui/dialogs/qprintsettingsoutput.ui index fc57e86..be91679 100644 --- a/src/gui/dialogs/qprintsettingsoutput.ui +++ b/src/gui/dialogs/qprintsettingsoutput.ui @@ -1,121 +1,114 @@ - + + QPrintSettingsOutput - - + + 0 0 - 416 - 166 + 426 + 171 - + Form - - + + 0 - - + + 0 - - - - 0 - 0 - 412 - 139 - - - + + Copies - + - - - + + + 0 0 - + Print range - - + + 4 - + 6 - - + + Print all - + true - - + + 6 - + 0 - - + + Pages from - - + + false - + 1 - + 999 - - + + to - - + + false - + 1 - + 999 - + Qt::Horizontal - + 0 20 @@ -126,18 +119,25 @@ - - + + + Current Page + + + + + + Selection - - + + Qt::Vertical - + 1 1 @@ -149,37 +149,37 @@ - - + + Output Settings - - - - + + + + Copies: - + copies - - - + + + 1 - + 999 - - - + + + Qt::Horizontal - + 91 20 @@ -187,36 +187,36 @@ - - - + + + Collate - - - - + + + + 0 0 - - - + + + Reverse - - - + + + Qt::Vertical - + 0 1 @@ -229,31 +229,23 @@ - - - - 0 - 0 - 412 - 139 - - - + + Options - - - - + + + + Color Mode - - - - + + + + Qt::Vertical - + 1 0 @@ -261,19 +253,19 @@ - - - + + + Color - - + + - - - + + + Grayscale @@ -281,42 +273,42 @@ - - - + + + Duplex Printing - + - - + + None - + true - - + + Long side - - + + Short side - - + + Qt::Vertical - + 1 0 @@ -341,11 +333,11 @@ from setEnabled(bool) - + 76 59 - + 122 57 @@ -357,11 +349,11 @@ to setEnabled(bool) - + 69 67 - + 215 67 diff --git a/src/gui/painting/qprinter.cpp b/src/gui/painting/qprinter.cpp index edf224d..ae21416 100644 --- a/src/gui/painting/qprinter.cpp +++ b/src/gui/painting/qprinter.cpp @@ -382,6 +382,7 @@ void QPrinterPrivate::addToManualSetList(QPrintEngine::PrintEnginePropertyKey ke \value AllPages All pages should be printed. \value Selection Only the selection should be printed. \value PageRange The specified page range should be printed. + \value CurrentPage Only the current page should be printed. \sa QAbstractPrintDialog::PrintRange */ @@ -571,6 +572,7 @@ void QPrinterPrivate::addToManualSetList(QPrintEngine::PrintEnginePropertyKey ke \value AllPages All the pages should be printed. \value Selection Only the selection should be printed. \value PageRange Print according to the from page and to page options. + \value CurrentPage Only the current page should be printed. \sa setPrintRange(), printRange() */ @@ -586,6 +588,7 @@ void QPrinterPrivate::addToManualSetList(QPrintEngine::PrintEnginePropertyKey ke \value PrintSelection Describes if printing selections should be enabled. \value PrintPageRange Describes if printing page ranges (from, to) should be enabled + \value PrintCurrentPage if Print Current Page option should be enabled \sa setOptionEnabled(), isOptionEnabled() */ diff --git a/src/gui/painting/qprinter.h b/src/gui/painting/qprinter.h index 6636179..996a954 100644 --- a/src/gui/painting/qprinter.h +++ b/src/gui/painting/qprinter.h @@ -124,7 +124,7 @@ public: enum OutputFormat { NativeFormat, PdfFormat, PostScriptFormat }; // ### Qt 5: Merge with QAbstractPrintDialog::PrintRange - enum PrintRange { AllPages, Selection, PageRange }; + enum PrintRange { AllPages, Selection, PageRange, CurrentPage }; enum Unit { Millimeter, diff --git a/tests/auto/qprinter/tst_qprinter.cpp b/tests/auto/qprinter/tst_qprinter.cpp index 7e8ce84..49bddb2 100644 --- a/tests/auto/qprinter/tst_qprinter.cpp +++ b/tests/auto/qprinter/tst_qprinter.cpp @@ -107,6 +107,7 @@ private slots: void printDialogCompleter(); void testCopyCount(); + void testCurrentPage(); void taskQTBUG4497_reusePrinterOnDifferentFiles(); @@ -1005,5 +1006,27 @@ void tst_QPrinter::taskQTBUG4497_reusePrinterOnDifferentFiles() QCOMPARE(file1.readAll(), file2.readAll()); } +void tst_QPrinter::testCurrentPage() +{ + QPrinter printer; + printer.setFromTo(1, 10); + + // Test set print range + printer.setPrintRange(QPrinter::CurrentPage); + QCOMPARE(printer.printRange(), QPrinter::CurrentPage); + QCOMPARE(printer.fromPage(), 1); + QCOMPARE(printer.toPage(), 10); + + QPrintDialog dialog(&printer); + + // Test default Current Page option to off + QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), false); + + // Test enable Current Page option + dialog.setOption(QPrintDialog::PrintCurrentPage); + QCOMPARE(dialog.isOptionEnabled(QPrintDialog::PrintCurrentPage), true); + +} + QTEST_MAIN(tst_QPrinter) #include "tst_qprinter.moc" -- cgit v0.12