From 00e50fb97c3d9b91193fef50d63b430e4648ac52 Mon Sep 17 00:00:00 2001 From: Martin Klapetek Date: Wed, 18 Sep 2013 16:22:48 +0200 Subject: Expose more CUPS options via the 'Properties' dialog On systems with CUPS support users can now choose how many pages from a document are to be printed on paper. This can vary from 1 page per document to 16 pages per document. The page preview changes upon the users selection. Also included in the patch is an option to choose the flow for text. Users can now print documents in the "Right to Left" order or "Bottom to Top", including many other options. [ChangeLog][QtPrintSupport][QPrintDialog] Added support for setting CUPS Pages Per Sheet and Pages Per Sheet Layout options Change-Id: I4e60a4523c6e06d4c15fe9ee9590248fa7ae2038 Reviewed-by: John Layt --- src/printsupport/dialogs/qpagesetupdialog_unix.cpp | 95 +++++++++++- src/printsupport/dialogs/qpagesetupdialog_unix_p.h | 5 +- src/printsupport/dialogs/qpagesetupwidget.ui | 169 +++++++++++++-------- src/printsupport/dialogs/qprintdialog_unix.cpp | 15 ++ src/printsupport/kernel/qcups.cpp | 11 ++ src/printsupport/kernel/qcups_p.h | 26 ++++ 6 files changed, 256 insertions(+), 65 deletions(-) diff --git a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp index 1add522cb8..cdb80f96a8 100644 --- a/src/printsupport/dialogs/qpagesetupdialog_unix.cpp +++ b/src/printsupport/dialogs/qpagesetupdialog_unix.cpp @@ -158,6 +158,13 @@ public: update(); } + void setPagePreviewLayout(int columns, int rows) + { + m_pagePreviewColumns = columns; + m_pagePreviewRows = rows; + update(); + } + protected: void paintEvent(QPaintEvent *) { @@ -202,13 +209,27 @@ protected: QString text(QLatin1String("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.")); for (int i=0; i<3; ++i) text += text; - p.drawText(marginRect, Qt::TextWordWrap|Qt::AlignVCenter, text); + + const int spacing = pageRect.width() * 0.1; + const int textWidth = (marginRect.width() - (spacing * (m_pagePreviewColumns-1))) / m_pagePreviewColumns; + const int textHeight = (marginRect.height() - (spacing * (m_pagePreviewRows-1))) / m_pagePreviewRows; + + for (int x = 0 ; x < m_pagePreviewColumns; ++x) { + for (int y = 0 ; y < m_pagePreviewRows; ++y) { + QRect textRect(marginRect.left() + x * (textWidth + spacing), + marginRect.top() + y * (textHeight + spacing), + textWidth, textHeight); + p.drawText(textRect, Qt::TextWordWrap|Qt::AlignVCenter, text); + } + } } } private: // all these are in points qreal m_left, m_top, m_right, m_bottom; + // specify width / height of one page in preview + int m_pagePreviewColumns, m_pagePreviewRows; QSizeF m_size; }; @@ -272,6 +293,8 @@ QPageSetupWidget::QPageSetupWidget(QWidget *parent) QVBoxLayout *lay = new QVBoxLayout(widget.preview); widget.preview->setLayout(lay); m_pagePreview = new QPagePreview(widget.preview); + m_pagePreview->setPagePreviewLayout(1, 1); + lay->addWidget(m_pagePreview); setAttribute(Qt::WA_WState_Polished, false); @@ -288,7 +311,7 @@ QPageSetupWidget::QPageSetupWidget(QWidget *parent) widget.reversePortrait->setVisible(false); populatePaperSizes(widget.paperSize); - + initPagesPerSheet(); QStringList units; units << tr("Centimeters (cm)") << tr("Millimeters (mm)") << tr("Inches (in)") << tr("Points (pt)"); widget.unit->addItems(units); @@ -306,6 +329,8 @@ QPageSetupWidget::QPageSetupWidget(QWidget *parent) connect(widget.portrait, SIGNAL(clicked()), this, SLOT(_q_pageOrientationChanged())); connect(widget.landscape, SIGNAL(clicked()), this, SLOT(_q_pageOrientationChanged())); + connect(widget.pagesPerSheetCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(_q_pagesPerSheetChanged())); + } void QPageSetupWidget::setPrinter(QPrinter *printer) @@ -366,6 +391,14 @@ void QPageSetupWidget::setupPrinter() const #endif m_printer->setPageMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin, QPrinter::Point); +#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) + QCUPSSupport::PagesPerSheet pagesPerSheet = widget.pagesPerSheetCombo->currentData() + .value(); + QCUPSSupport::PagesPerSheetLayout pagesPerSheetLayout = widget.pagesPerSheetLayoutCombo->currentData() + .value(); + + QCUPSSupport::setPagesPerSheetLayout(m_printer, pagesPerSheet, pagesPerSheetLayout); +#endif } void QPageSetupWidget::selectPrinter() @@ -500,6 +533,36 @@ void QPageSetupWidget::_q_pageOrientationChanged() _q_paperSizeChanged(); } +void QPageSetupWidget::_q_pagesPerSheetChanged() +{ +#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) + QCUPSSupport::PagesPerSheet pagesPerSheet = widget.pagesPerSheetCombo->currentData() + .value(); + + switch (pagesPerSheet) { + case QCUPSSupport::TwoPagesPerSheet: + m_pagePreview->setPagePreviewLayout(1, 2); + break; + case QCUPSSupport::FourPagesPerSheet: + m_pagePreview->setPagePreviewLayout(2, 2); + break; + case QCUPSSupport::SixPagesPerSheet: + m_pagePreview->setPagePreviewLayout(3, 2); + break; + case QCUPSSupport::NinePagesPerSheet: + m_pagePreview->setPagePreviewLayout(3, 3); + break; + case QCUPSSupport::SixteenPagesPerSheet: + m_pagePreview->setPagePreviewLayout(4, 4); + break; + case QCUPSSupport::OnePagePerSheet: + default: + m_pagePreview->setPagePreviewLayout(1, 1); + break; + } +#endif +} + extern double qt_multiplierForUnit(QPrinter::Unit unit, int resolution); void QPageSetupWidget::unitChanged(int item) @@ -598,6 +661,34 @@ int QPageSetupDialog::exec() return ret; } +void QPageSetupWidget::initPagesPerSheet() +{ +#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Left to Right, Top to Bottom"), QVariant::fromValue(QCUPSSupport::LeftToRightTopToBottom)); + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Left to Right, Bottom to Top"), QVariant::fromValue(QCUPSSupport::LeftToRightBottomToTop)); + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Right to Left, Bottom to Top"), QVariant::fromValue(QCUPSSupport::RightToLeftBottomToTop)); + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Right to Left, Top to Bottom"), QVariant::fromValue(QCUPSSupport::RightToLeftTopToBottom)); + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Bottom to Top, Left to Right"), QVariant::fromValue(QCUPSSupport::BottomToTopLeftToRight)); + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Bottom to Top, Right to Left"), QVariant::fromValue(QCUPSSupport::BottomToTopRightToLeft)); + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Top to Bottom, Left to Right"), QVariant::fromValue(QCUPSSupport::TopToBottomLeftToRight)); + widget.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Top to Bottom, Right to Left"), QVariant::fromValue(QCUPSSupport::TopToBottomRightToLeft)); + + widget.pagesPerSheetCombo->addItem(QPrintDialog::tr("1 (1x1)"), QVariant::fromValue(QCUPSSupport::OnePagePerSheet)); + widget.pagesPerSheetCombo->addItem(QPrintDialog::tr("2 (2x1)"), QVariant::fromValue(QCUPSSupport::TwoPagesPerSheet)); + widget.pagesPerSheetCombo->addItem(QPrintDialog::tr("4 (2x2)"), QVariant::fromValue(QCUPSSupport::FourPagesPerSheet)); + widget.pagesPerSheetCombo->addItem(QPrintDialog::tr("6 (2x3)"), QVariant::fromValue(QCUPSSupport::SixPagesPerSheet)); + widget.pagesPerSheetCombo->addItem(QPrintDialog::tr("9 (3x3)"), QVariant::fromValue(QCUPSSupport::NinePagesPerSheet)); + widget.pagesPerSheetCombo->addItem(QPrintDialog::tr("16 (4x4)"), QVariant::fromValue(QCUPSSupport::SixteenPagesPerSheet)); + + // Set the combo to "1 (1x1)" -- QCUPSSupport::OnePagePerSheet + widget.pagesPerSheetCombo->setCurrentIndex(0); + // Set the layout combo to QCUPSSupport::LeftToRightTopToBottom + widget.pagesPerSheetLayoutCombo->setCurrentIndex(0); +#else + // Disable if CUPS wasn't found + widget.pagesPerSheetButtonGroup->hide(); +#endif +} QT_END_NAMESPACE diff --git a/src/printsupport/dialogs/qpagesetupdialog_unix_p.h b/src/printsupport/dialogs/qpagesetupdialog_unix_p.h index a83d6e4fbe..b96d300ab9 100644 --- a/src/printsupport/dialogs/qpagesetupdialog_unix_p.h +++ b/src/printsupport/dialogs/qpagesetupdialog_unix_p.h @@ -63,7 +63,6 @@ QT_BEGIN_NAMESPACE class QPrinter; class QPagePreview; -class QCUPSSupport; class QPageSetupWidget : public QWidget { Q_OBJECT @@ -79,6 +78,7 @@ public: private slots: void _q_pageOrientationChanged(); void _q_paperSizeChanged(); + void _q_pagesPerSheetChanged(); void unitChanged(int item); void setTopMargin(double newValue); void setBottomMargin(double newValue); @@ -86,6 +86,7 @@ private slots: void setRightMargin(double newValue); private: + friend class QUnixPrintWidgetPrivate; Ui::QPageSetupWidget widget; QPagePreview *m_pagePreview; QPrinter *m_printer; @@ -97,6 +98,8 @@ private: qreal m_currentMultiplier; bool m_blockSignals; bool m_cups; + + void initPagesPerSheet(); }; QT_END_NAMESPACE diff --git a/src/printsupport/dialogs/qpagesetupwidget.ui b/src/printsupport/dialogs/qpagesetupwidget.ui index ace2ab8f44..ffd2650f4c 100644 --- a/src/printsupport/dialogs/qpagesetupwidget.ui +++ b/src/printsupport/dialogs/qpagesetupwidget.ui @@ -6,7 +6,7 @@ 0 0 416 - 488 + 515 @@ -16,38 +16,18 @@ 0 - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - + + + Paper - - - - + + + + Page size: - + paperSize @@ -122,9 +102,32 @@ - - - + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Orientation @@ -175,12 +178,9 @@ - - - - - - + + + Margins @@ -280,9 +280,25 @@ - - - + + + + Qt::Horizontal + + + QSizePolicy::MinimumExpanding + + + + 0 + 20 + + + + + + + bottom margin @@ -296,28 +312,15 @@ - - - + + + Qt::Horizontal QSizePolicy::MinimumExpanding - - - 0 - 20 - - - - - - - - Qt::Horizontal - - + QSizePolicy::MinimumExpanding @@ -333,15 +336,57 @@ - - - + + + + Page Layout + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Page order: + + + + + + + + + + Pages per sheet: + + + + + + + + + Qt::Vertical 20 - 0 + 40 diff --git a/src/printsupport/dialogs/qprintdialog_unix.cpp b/src/printsupport/dialogs/qprintdialog_unix.cpp index eb4ce6840f..882d76a041 100644 --- a/src/printsupport/dialogs/qprintdialog_unix.cpp +++ b/src/printsupport/dialogs/qprintdialog_unix.cpp @@ -126,6 +126,7 @@ protected: void showEvent(QShowEvent* event); private: + friend class QUnixPrintWidgetPrivate; Ui::QPrintPropertiesWidget widget; QDialogButtonBox *m_buttons; #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) @@ -862,6 +863,20 @@ bool QUnixPrintWidgetPrivate::checkFields() } } +#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) + QCUPSSupport::PagesPerSheet pagesPerSheet = propertiesDialog->widget.pageSetup->widget.pagesPerSheetCombo->currentData().value(); + + QCUPSSupport::PageSet pageSet = optionsPane->options.pageSetCombo->currentData().value(); + + if (propertiesDialogShown + && pagesPerSheet != QCUPSSupport::OnePagePerSheet + && pageSet != QCUPSSupport::AllPages) { + QMessageBox::warning(q, q->windowTitle(), + QPrintDialog::tr("Options 'Pages Per Sheet' and 'Page Set' cannot be used together.\nPlease turn one of those options off.")); + return false; + } +#endif + // Every test passed. Accept the dialog. return true; } diff --git a/src/printsupport/kernel/qcups.cpp b/src/printsupport/kernel/qcups.cpp index a10baaa7fb..7c426d4d36 100644 --- a/src/printsupport/kernel/qcups.cpp +++ b/src/printsupport/kernel/qcups.cpp @@ -511,6 +511,17 @@ void QCUPSSupport::setPageSet(QPrinter *printer, const PageSet pageSet) setCupsOptions(printer, cupsOptions); } +void QCUPSSupport::setPagesPerSheetLayout(QPrinter *printer, const PagesPerSheet pagesPerSheet, + const PagesPerSheetLayout pagesPerSheetLayout) +{ + QStringList cupsOptions = cupsOptionsList(printer); + static const char *pagesPerSheetData[] = { "1", "2", "4", "6", "9", "16", 0 }; + static const char *pageLayoutData[] = {"lrtb", "lrbt", "rlbt", "rltb", "btlr", "btrl", "tblr", "tbrl", 0}; + setCupsOption(cupsOptions, QStringLiteral("number-up"), pagesPerSheetData[pagesPerSheet]); + setCupsOption(cupsOptions, QStringLiteral("number-up-layout"), pageLayoutData[pagesPerSheetLayout]); + setCupsOptions(printer, cupsOptions); +} + bool QCUPSSupport::printerHasPPD(const char *printerName) { if (!isAvailable()) diff --git a/src/printsupport/kernel/qcups_p.h b/src/printsupport/kernel/qcups_p.h index 412c50cfac..f3e71df3e3 100644 --- a/src/printsupport/kernel/qcups_p.h +++ b/src/printsupport/kernel/qcups_p.h @@ -121,6 +121,28 @@ public: EvenPages }; + // Enum for valid number of pages per sheet + enum PagesPerSheet { + OnePagePerSheet = 0, + TwoPagesPerSheet, + FourPagesPerSheet, + SixPagesPerSheet, + NinePagesPerSheet, + SixteenPagesPerSheet + }; + + // Enum for valid layouts of pages per sheet + enum PagesPerSheetLayout { + LeftToRightTopToBottom = 0, + LeftToRightBottomToTop, + RightToLeftTopToBottom, + RightToLeftBottomToTop, + BottomToTopLeftToRight, + BottomToTopRightToLeft, + TopToBottomLeftToRight, + TopToBottomRightToLeft + }; + static bool isAvailable(); static int cupsVersion() { return isAvailable() ? CUPS_VERSION_MAJOR*10000+CUPS_VERSION_MINOR*100+CUPS_VERSION_PATCH : 0; } int availablePrintersCount() const; @@ -151,6 +173,8 @@ public: static void setJobPriority(QPrinter *printer, int priority = 50); static void setBannerPages(QPrinter *printer, const BannerPage startBannerPage, const BannerPage endBannerPage); static void setPageSet(QPrinter *printer, const PageSet pageSet); + static void setPagesPerSheetLayout(QPrinter *printer, const PagesPerSheet pagesPerSheet, + const PagesPerSheetLayout pagesPerSheetLayout); static bool printerHasPPD(const char *printerName); @@ -183,6 +207,8 @@ QT_END_NAMESPACE Q_DECLARE_METATYPE(QCUPSSupport::JobHoldUntil) Q_DECLARE_METATYPE(QCUPSSupport::BannerPage) Q_DECLARE_METATYPE(QCUPSSupport::PageSet) +Q_DECLARE_METATYPE(QCUPSSupport::PagesPerSheetLayout) +Q_DECLARE_METATYPE(QCUPSSupport::PagesPerSheet) #endif // QT_NO_CUPS -- cgit v1.2.3