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
qpagesetupdialog_unix.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
6
8
9#include <private/qpagesetupdialog_p.h>
10#include <private/qprintdevice_p.h>
11#if QT_CONFIG(cups)
12#include <private/qcups_p.h>
13#endif
14
15#include "qpainter.h"
16#include "qprintdialog.h"
19#include <ui_qpagesetupwidget.h>
20
21#include <QtPrintSupport/qprinter.h>
22
23#include <qpa/qplatformprintplugin.h>
24#include <qpa/qplatformprintersupport.h>
25
26QT_BEGIN_NAMESPACE
27
28using namespace Qt::StringLiterals;
29
30extern QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits);
31
32// Disabled until we have support for papersources on unix
33// #define PSD_ENABLE_PAPERSOURCE
34
35#ifdef PSD_ENABLE_PAPERSOURCE
36static const char *paperSourceNames[] = {
37 "Only One",
38 "Lower",
39 "Middle",
40 "Manual",
41 "Envelope",
42 "Envelope manual",
43 "Auto",
44 "Tractor",
45 "Small format",
46 "Large format",
47 "Large capacity",
48 "Cassette",
49 "Form source",
50 0
51};
52
53struct PaperSourceNames
54{
55 PaperSourceNames(const char *nam, QPrinter::PaperSource ps)
56 : paperSource(ps), name(nam) {}
57 QPrinter::PaperSource paperSource;
58 const char *name;
59};
60#endif
61
62
63// QPagePreview
64// - Private widget to display preview of page layout
65// - Embedded in QPageSetupWidget
66
67class QPagePreview : public QWidget
68{
69public:
71 {
72 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
73 setMinimumSize(50, 50);
74 }
75
76 void setPageLayout(const QPageLayout &layout)
77 {
78 m_pageLayout = layout;
79 update();
80 }
81
82 void setPagePreviewLayout(int columns, int rows)
83 {
84 m_pagePreviewColumns = columns;
85 m_pagePreviewRows = rows;
86 update();
87 }
88
89protected:
90 void paintEvent(QPaintEvent *) override
91 {
92 QSize pageSize = m_pageLayout.fullRectPoints().size();
93 QSizeF scaledSize = pageSize.scaled(width() - 10, height() - 10, Qt::KeepAspectRatio);
94 QRect pageRect = QRect(QPoint(0,0), scaledSize.toSize());
95 pageRect.moveCenter(rect().center());
96 qreal width_factor = scaledSize.width() / pageSize.width();
97 qreal height_factor = scaledSize.height() / pageSize.height();
98 QMarginsF margins = m_pageLayout.margins(QPageLayout::Point);
99 int left = qRound(margins.left() * width_factor);
100 int top = qRound(margins.top() * height_factor);
101 int right = qRound(margins.right() * width_factor);
102 int bottom = qRound(margins.bottom() * height_factor);
103 QRect marginRect(pageRect.x() + left, pageRect.y() + top,
104 pageRect.width() - (left + right + 1), pageRect.height() - (top + bottom + 1));
105
106 QPainter p(this);
107 QColor shadow(palette().mid().color());
108 for (int i=1; i<6; ++i) {
109 shadow.setAlpha(180-i*30);
110 QRect offset(pageRect.adjusted(i, i, i, i));
111 p.setPen(shadow);
112 p.drawLine(offset.left(), offset.bottom(), offset.right(), offset.bottom());
113 p.drawLine(offset.right(), offset.top(), offset.right(), offset.bottom()-1);
114 }
115 p.fillRect(pageRect, palette().light());
116
117 if (marginRect.isValid()) {
118 p.setPen(QPen(palette().color(QPalette::Dark), 0, Qt::DotLine));
119 p.drawRect(marginRect);
120
121 marginRect.adjust(2, 2, -1, -1);
122 p.setClipRect(marginRect);
123 QFont font;
124 font.setPointSizeF(font.pointSizeF()*0.25);
125 p.setFont(font);
126 p.setPen(palette().color(QPalette::Dark));
127 QString text("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."_L1);
128 for (int i=0; i<3; ++i)
129 text += text;
130
131 const int spacing = pageRect.width() * 0.1;
132 const int textWidth = (marginRect.width() - (spacing * (m_pagePreviewColumns-1))) / m_pagePreviewColumns;
133 const int textHeight = (marginRect.height() - (spacing * (m_pagePreviewRows-1))) / m_pagePreviewRows;
134
135 for (int x = 0 ; x < m_pagePreviewColumns; ++x) {
136 for (int y = 0 ; y < m_pagePreviewRows; ++y) {
137 QRect textRect(marginRect.left() + x * (textWidth + spacing),
138 marginRect.top() + y * (textHeight + spacing),
139 textWidth, textHeight);
140 p.drawText(textRect, Qt::TextWordWrap|Qt::AlignVCenter, text);
141 }
142 }
143 }
144 }
145
146private:
147 // Page Layout
148 QPageLayout m_pageLayout;
149 // Pages Per Sheet / n-up layout
150 int m_pagePreviewColumns, m_pagePreviewRows;
151};
152
153
154// QUnixPageSetupDialogPrivate
155// - Linux / Cups implementation of QPageSetupDialogPrivate
156// - Embeds QPageSetupWidget
157
159{
160 Q_DECLARE_PUBLIC(QPageSetupDialog)
161
162public:
165 void init();
166
168};
169
170QUnixPageSetupDialogPrivate::QUnixPageSetupDialogPrivate(QPrinter *printer) : QPageSetupDialogPrivate(printer)
171{
172}
173
177
179{
180 Q_Q(QPageSetupDialog);
181
182 widget = new QPageSetupWidget(q);
183 widget->setPrinter(printer, nullptr, printer->outputFormat(), printer->printerName());
184
185 QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok
186 | QDialogButtonBox::Cancel,
187 Qt::Horizontal, q);
188 QObject::connect(buttons, SIGNAL(accepted()), q, SLOT(accept()));
189 QObject::connect(buttons, SIGNAL(rejected()), q, SLOT(reject()));
190
191 QVBoxLayout *lay = new QVBoxLayout(q);
192 lay->addWidget(widget);
193 lay->addWidget(buttons);
194}
195
196// QPageSetupWidget
197// - Private widget implementation for Linux / CUPS
198// - Embeds QPagePreview
199// - TODO Could be made public as a stand-alone widget?
200
201QPageSetupWidget::QPageSetupWidget(QWidget *parent)
202 : QWidget(parent),
203 m_pagePreview(nullptr),
204 m_printer(nullptr),
205 m_printDevice(nullptr),
206#if QT_CONFIG(cups)
207 m_pageSizePpdOption(nullptr),
208#endif
209 m_outputFormat(QPrinter::PdfFormat),
210 m_units(QPageLayout::Point),
211 m_savedUnits(QPageLayout::Point),
212 m_savedPagesPerSheet(-1),
213 m_savedPagesPerSheetLayout(-1),
214 m_blockSignals(false),
215 m_realCustomPageSizeIndex(-1)
216{
217 m_ui.setupUi(this);
218
219 QVBoxLayout *lay = new QVBoxLayout(m_ui.preview);
220 m_pagePreview = new QPagePreview(m_ui.preview);
221 m_pagePreview->setPagePreviewLayout(1, 1);
222
223 lay->addWidget(m_pagePreview);
224
225 setAttribute(Qt::WA_WState_Polished, false);
226
227#ifdef PSD_ENABLE_PAPERSOURCE
228 for (int i=0; paperSourceNames[i]; ++i)
229 m_ui.paperSource->insertItem(paperSourceNames[i]);
230#else
231 m_ui.paperSourceLabel->setVisible(false);
232 m_ui.paperSource->setVisible(false);
233#endif
234
235 m_ui.reverseLandscape->setVisible(false);
236 m_ui.reversePortrait->setVisible(false);
237
238 initUnits();
239 initPagesPerSheet();
240
241 connect(m_ui.unitCombo, &QComboBox::activated, this, &QPageSetupWidget::unitChanged);
242
243 connect(m_ui.pageSizeCombo, &QComboBox::currentIndexChanged, this, &QPageSetupWidget::pageSizeChanged);
244 connect(m_ui.pageWidth, &QDoubleSpinBox::valueChanged, this, &QPageSetupWidget::pageSizeChanged);
245 connect(m_ui.pageHeight, &QDoubleSpinBox::valueChanged, this, &QPageSetupWidget::pageSizeChanged);
246
247 connect(m_ui.leftMargin, &QDoubleSpinBox::valueChanged, this, &QPageSetupWidget::leftMarginChanged);
248 connect(m_ui.topMargin, &QDoubleSpinBox::valueChanged, this, &QPageSetupWidget::topMarginChanged);
249 connect(m_ui.rightMargin, &QDoubleSpinBox::valueChanged, this, &QPageSetupWidget::rightMarginChanged);
250 connect(m_ui.bottomMargin, &QDoubleSpinBox::valueChanged, this, &QPageSetupWidget::bottomMarginChanged);
251
252 connect(m_ui.portrait, &QRadioButton::clicked, this, &QPageSetupWidget::pageOrientationChanged);
253 connect(m_ui.landscape, &QRadioButton::clicked, this, &QPageSetupWidget::pageOrientationChanged);
254
255 connect(m_ui.pagesPerSheetCombo, &QComboBox::currentIndexChanged, this, &QPageSetupWidget::pagesPerSheetChanged);
256}
257
258// Init the Units combo box
259void QPageSetupWidget::initUnits()
260{
261 m_ui.unitCombo->addItem(tr("Millimeters (mm)"), QVariant::fromValue(QPageLayout::Millimeter));
262 m_ui.unitCombo->addItem(tr("Inches (in)"), QVariant::fromValue(QPageLayout::Inch));
263 m_ui.unitCombo->addItem(tr("Points (pt)"), QVariant::fromValue(QPageLayout::Point));
264 m_ui.unitCombo->addItem(tr("Pica (P̸)"), QVariant::fromValue(QPageLayout::Pica));
265 m_ui.unitCombo->addItem(tr("Didot (DD)"), QVariant::fromValue(QPageLayout::Didot));
266 m_ui.unitCombo->addItem(tr("Cicero (CC)"), QVariant::fromValue(QPageLayout::Cicero));
267
268 // Initially default to locale measurement system, mm if metric, in otherwise
269 m_ui.unitCombo->setCurrentIndex(QLocale().measurementSystem() != QLocale::MetricSystem);
270}
271
272// Init the Pages Per Sheet (n-up) combo boxes if using CUPS
273void QPageSetupWidget::initPagesPerSheet()
274{
275#if QT_CONFIG(cups)
276 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Left to Right, Top to Bottom"),
277 QVariant::fromValue(QCUPSSupport::LeftToRightTopToBottom));
278 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Left to Right, Bottom to Top"),
279 QVariant::fromValue(QCUPSSupport::LeftToRightBottomToTop));
280 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Right to Left, Bottom to Top"),
281 QVariant::fromValue(QCUPSSupport::RightToLeftBottomToTop));
282 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Right to Left, Top to Bottom"),
283 QVariant::fromValue(QCUPSSupport::RightToLeftTopToBottom));
284 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Bottom to Top, Left to Right"),
285 QVariant::fromValue(QCUPSSupport::BottomToTopLeftToRight));
286 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Bottom to Top, Right to Left"),
287 QVariant::fromValue(QCUPSSupport::BottomToTopRightToLeft));
288 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Top to Bottom, Left to Right"),
289 QVariant::fromValue(QCUPSSupport::TopToBottomLeftToRight));
290 m_ui.pagesPerSheetLayoutCombo->addItem(QPrintDialog::tr("Top to Bottom, Right to Left"),
291 QVariant::fromValue(QCUPSSupport::TopToBottomRightToLeft));
292
293 m_ui.pagesPerSheetCombo->addItem(QPrintDialog::tr("1 (1x1)"),
294 QVariant::fromValue(QCUPSSupport::OnePagePerSheet));
295 m_ui.pagesPerSheetCombo->addItem(QPrintDialog::tr("2 (2x1)"),
296 QVariant::fromValue(QCUPSSupport::TwoPagesPerSheet));
297 m_ui.pagesPerSheetCombo->addItem(QPrintDialog::tr("4 (2x2)"),
298 QVariant::fromValue(QCUPSSupport::FourPagesPerSheet));
299 m_ui.pagesPerSheetCombo->addItem(QPrintDialog::tr("6 (2x3)"),
300 QVariant::fromValue(QCUPSSupport::SixPagesPerSheet));
301 m_ui.pagesPerSheetCombo->addItem(QPrintDialog::tr("9 (3x3)"),
302 QVariant::fromValue(QCUPSSupport::NinePagesPerSheet));
303 m_ui.pagesPerSheetCombo->addItem(QPrintDialog::tr("16 (4x4)"),
304 QVariant::fromValue(QCUPSSupport::SixteenPagesPerSheet));
305
306 // Set to QCUPSSupport::OnePagePerSheet
307 m_ui.pagesPerSheetCombo->setCurrentIndex(0);
308 // Set to QCUPSSupport::LeftToRightTopToBottom
309 m_ui.pagesPerSheetLayoutCombo->setCurrentIndex(0);
310#else
311 // Disable if CUPS wasn't found
312 m_ui.pagesPerSheetButtonGroup->hide();
313#endif
314}
315
316void QPageSetupWidget::initPageSizes()
317{
318 m_blockSignals = true;
319
320 m_ui.pageSizeCombo->clear();
321
322 m_realCustomPageSizeIndex = -1;
323
324 if (m_outputFormat == QPrinter::NativeFormat && !m_printerName.isEmpty()) {
325 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
326 if (ps) {
327 QPrintDevice printDevice = ps->createPrintDevice(m_printerName);
328 const QPageSize defaultSize = printDevice.defaultPageSize();
329 const auto pageSizes = printDevice.supportedPageSizes();
330 for (const QPageSize &pageSize : pageSizes)
331 m_ui.pageSizeCombo->addItem(pageSize.name(), QVariant::fromValue(pageSize));
332 if (m_ui.pageSizeCombo->count() > 0) {
333 if (printDevice.supportsCustomPageSizes()) {
334 m_ui.pageSizeCombo->addItem(tr("Custom"));
335 m_realCustomPageSizeIndex = m_ui.pageSizeCombo->count() - 1;
336 }
337 m_blockSignals = false;
338
339 // If the defaultSize is index 0, setCurrentIndex won't emit the currentIndexChanged
340 // signal; workaround the issue by initially setting the currentIndex to -1
341 m_ui.pageSizeCombo->setCurrentIndex(-1);
342 m_ui.pageSizeCombo->setCurrentIndex(m_ui.pageSizeCombo->findData(QVariant::fromValue(defaultSize)));
343 return;
344 }
345 }
346 }
347
348 // If PdfFormat or no available printer page sizes, populate with all page sizes
349 for (int id = 0; id < QPageSize::LastPageSize; ++id) {
350 if (QPageSize::PageSizeId(id) == QPageSize::Custom) {
351 m_ui.pageSizeCombo->addItem(tr("Custom"));
352 m_realCustomPageSizeIndex = m_ui.pageSizeCombo->count() - 1;
353 } else {
354 QPageSize pageSize = QPageSize(QPageSize::PageSizeId(id));
355 m_ui.pageSizeCombo->addItem(pageSize.name(), QVariant::fromValue(pageSize));
356 }
357 }
358
359 m_blockSignals = false;
360}
361
362// Set the dialog to use the given QPrinter
363// Usually only called on first creation
364void QPageSetupWidget::setPrinter(QPrinter *printer, QPrintDevice *printDevice,
365 QPrinter::OutputFormat outputFormat, const QString &printerName)
366{
367 m_printer = printer;
368 m_printDevice = printDevice;
369
370#if QT_CONFIG(cups)
371 // find the PageSize cups option
372 m_pageSizePpdOption = m_printDevice ? QCUPSSupport::findPpdOption("PageSize", m_printDevice) : nullptr;
373#endif
374
375 // Initialize the layout to the current QPrinter layout
376 m_pageLayout = m_printer->pageLayout();
377
378 // Assume if margins are Points then is by default, so set to locale default units
379 if (m_pageLayout.units() == QPageLayout::Point) {
380 if (QLocale().measurementSystem() == QLocale::MetricSystem)
381 m_pageLayout.setUnits(QPageLayout::Millimeter);
382 else
383 m_pageLayout.setUnits(QPageLayout::Inch);
384 }
385 m_units = m_pageLayout.units();
386 m_pagePreview->setPageLayout(m_pageLayout);
387
388 m_outputFormat = outputFormat;
389 m_printerName = printerName;
390 initPageSizes();
391 updateWidget();
393
394 if (m_ui.pageSizeCombo->currentIndex() == -1) {
395 // This can happen in raw printers that since they don't have a default
396 // page size none will get selected so just default to the first size (A4)
397 m_ui.pageSizeCombo->setCurrentIndex(0);
398 }
399}
400
401// Update the widget with the current settings
402// TODO Break up into more intelligent chunks?
403void QPageSetupWidget::updateWidget()
404{
405 m_blockSignals = true;
406
407 QString suffix;
408 switch (m_units) {
409 case QPageLayout::Millimeter:
410 //: Unit 'Millimeter'
411 suffix = tr("mm");
412 break;
413 case QPageLayout::Point:
414 //: Unit 'Points'
415 suffix = tr("pt");
416 break;
417 case QPageLayout::Inch:
418 //: Unit 'Inch'
419 suffix = tr("in");
420 break;
421 case QPageLayout::Pica:
422 //: Unit 'Pica'
423 suffix = tr("P̸");
424 break;
425 case QPageLayout::Didot:
426 //: Unit 'Didot'
427 suffix = tr("DD");
428 break;
429 case QPageLayout::Cicero:
430 //: Unit 'Cicero'
431 suffix = tr("CC");
432 break;
433 }
434
435 m_ui.unitCombo->setCurrentIndex(m_ui.unitCombo->findData(QVariant::fromValue(m_units)));
436
437 const bool isCustom = m_ui.pageSizeCombo->currentIndex() == m_realCustomPageSizeIndex && m_realCustomPageSizeIndex != -1;
438 if (!isCustom)
439 m_ui.pageSizeCombo->setCurrentIndex(m_ui.pageSizeCombo->findData(QVariant::fromValue(m_pageLayout.pageSize())));
440
441 QMarginsF min;
442 QMarginsF max;
443
444 if (m_pageLayout.mode() == QPageLayout::FullPageMode) {
445 min = QMarginsF(0.0, 0.0, 0.0, 0.0);
446 max = QMarginsF(9999.9999, 9999.9999, 9999.9999, 9999.9999);
447 } else {
448 min = m_pageLayout.minimumMargins();
449 max = m_pageLayout.maximumMargins();
450 }
451
452 m_ui.leftMargin->setSuffix(suffix);
453 m_ui.leftMargin->setMinimum(min.left());
454 m_ui.leftMargin->setMaximum(max.left());
455 m_ui.leftMargin->setValue(m_pageLayout.margins().left());
456
457 m_ui.rightMargin->setSuffix(suffix);
458 m_ui.rightMargin->setMinimum(min.right());
459 m_ui.rightMargin->setMaximum(max.right());
460 m_ui.rightMargin->setValue(m_pageLayout.margins().right());
461
462 m_ui.topMargin->setSuffix(suffix);
463 m_ui.topMargin->setMinimum(min.top());
464 m_ui.topMargin->setMaximum(max.top());
465 m_ui.topMargin->setValue(m_pageLayout.margins().top());
466
467 m_ui.bottomMargin->setSuffix(suffix);
468 m_ui.bottomMargin->setMinimum(min.bottom());
469 m_ui.bottomMargin->setMaximum(max.bottom());
470 m_ui.bottomMargin->setValue(m_pageLayout.margins().bottom());
471
472 m_ui.pageWidth->setSuffix(suffix);
473 m_ui.pageWidth->setValue(m_pageLayout.fullRect(m_units).width());
474 m_ui.pageWidth->setEnabled(isCustom);
475 m_ui.widthLabel->setEnabled(isCustom);
476
477 m_ui.pageHeight->setSuffix(suffix);
478 m_ui.pageHeight->setValue(m_pageLayout.fullRect(m_units).height());
479 m_ui.pageHeight->setEnabled(isCustom);
480 m_ui.heightLabel->setEnabled(isCustom);
481
482 m_ui.portrait->setChecked(m_pageLayout.orientation() == QPageLayout::Portrait);
483 m_ui.landscape->setChecked(m_pageLayout.orientation() == QPageLayout::Landscape);
484
485 m_ui.pagesPerSheetButtonGroup->setEnabled(m_outputFormat == QPrinter::NativeFormat);
486
487#ifdef PSD_ENABLE_PAPERSOURCE
488 m_ui.paperSource->setCurrentItem(printer->paperSource());
489#endif
490
491 m_blockSignals = false;
492}
493
494// Set the dialog chosen options on the QPrinter
495// Normally only called when the QPrintDialog or QPageSetupDialog OK button is pressed
497{
498 m_printer->setPageLayout(m_pageLayout);
499 m_printer->setPageOrientation(m_pageLayout.orientation());
500#if QT_CONFIG(cups)
501 QCUPSSupport::PagesPerSheet pagesPerSheet = qvariant_cast<QCUPSSupport::PagesPerSheet>(m_ui.pagesPerSheetCombo->currentData()
502);
503 QCUPSSupport::PagesPerSheetLayout pagesPerSheetLayout = qvariant_cast<QCUPSSupport::PagesPerSheetLayout>(m_ui.pagesPerSheetLayoutCombo->currentData()
504);
505 QCUPSSupport::setPagesPerSheetLayout(m_printer, pagesPerSheet, pagesPerSheetLayout);
506#endif
507#ifdef PSD_ENABLE_PAPERSOURCE
508 m_printer->setPaperSource((QPrinter::PaperSource)m_ui.paperSource->currentIndex());
509#endif
510}
511
513{
514 m_savedUnits = m_units;
515 m_savedPageLayout = m_pageLayout;
516 m_savedPagesPerSheet = m_ui.pagesPerSheetCombo->currentIndex();
517 m_savedPagesPerSheetLayout = m_ui.pagesPerSheetLayoutCombo->currentIndex();
518}
519
521{
522 m_units = m_savedUnits;
523 m_pageLayout = m_savedPageLayout;
524 m_pagePreview->setPageLayout(m_pageLayout);
525
526 updateWidget();
527
528 m_ui.pagesPerSheetCombo->setCurrentIndex(m_savedPagesPerSheet);
529 m_ui.pagesPerSheetLayoutCombo->setCurrentIndex(m_savedPagesPerSheetLayout);
530}
531
532#if QT_CONFIG(cups)
533bool QPageSetupWidget::hasPpdConflict() const
534{
535 if (m_pageSizePpdOption) {
536 if (m_pageSizePpdOption->conflicted) {
537 const QIcon warning = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, nullptr);
538 const int pixmap_size = m_ui.pageSizeCombo->sizeHint().height() * .75;
539 m_ui.pageSizeWarningLabel->setPixmap(warning.pixmap(pixmap_size, pixmap_size));
540 } else {
541 m_ui.pageSizeWarningLabel->setPixmap(QPixmap());
542 }
543 return m_pageSizePpdOption->conflicted;
544 }
545
546 return false;
547}
548#endif
549
550// Updates size/preview after the combobox has been changed.
551void QPageSetupWidget::pageSizeChanged()
552{
553 QPageSize pageSize;
554 if (m_ui.pageSizeCombo->currentIndex() != m_realCustomPageSizeIndex) {
555 pageSize = qvariant_cast<QPageSize>(m_ui.pageSizeCombo->currentData());
556
557#if QT_CONFIG(cups)
558 if (m_pageSizePpdOption) {
559 ppd_file_t *ppd = qvariant_cast<ppd_file_t*>(m_printDevice->property(PDPK_PpdFile));
560 QStringDecoder toUtf16(ppd->lang_encoding, QStringDecoder::Flag::Stateless);
561 if (!toUtf16.isValid()) {
562 qWarning() << "QPrinSupport: Cups uses unsupported encoding" << ppd->lang_encoding;
563 toUtf16 = QStringDecoder(QStringDecoder::Utf8);
564 }
565 for (int i = 0; i < m_pageSizePpdOption->num_choices; ++i) {
566 const ppd_choice_t *choice = &m_pageSizePpdOption->choices[i];
567 if (toUtf16(choice->text) == m_ui.pageSizeCombo->currentText()) {
568 const auto values = QStringList{} << QString::fromLatin1(m_pageSizePpdOption->keyword)
569 << QString::fromLatin1(choice->choice);
570 m_printDevice->setProperty(PDPK_PpdOption, values);
571 emit ppdOptionChanged();
572 break;
573 }
574 }
575 }
576#endif
577
578 } else {
579 QSizeF customSize;
580 if (m_pageLayout.orientation() == QPageLayout::Landscape)
581 customSize = QSizeF(m_ui.pageHeight->value(), m_ui.pageWidth->value());
582 else
583 customSize = QSizeF(m_ui.pageWidth->value(), m_ui.pageHeight->value());
584 pageSize = QPageSize(customSize, QPageSize::Unit(m_units));
585
586#if QT_CONFIG(cups)
587 if (m_pageSizePpdOption) {
588 const auto values = QStringList{} << QString::fromLatin1(m_pageSizePpdOption->keyword)
589 << QStringLiteral("Custom");
590 m_printDevice->setProperty(PDPK_PpdOption, values);
591 emit ppdOptionChanged();
592 }
593#endif
594 }
595
596 // We always need to update the m_pageSizePpdOption when the page size changes
597 // even if it's from inside updateWidget, so do not move up
598 if (m_blockSignals)
599 return;
600
601 const QMarginsF printable = m_printDevice ? m_printDevice->printableMargins(pageSize, m_pageLayout.orientation(), m_printer->resolution())
602 : QMarginsF();
603 m_pageLayout.setPageSize(pageSize, qt_convertMargins(printable, QPageLayout::Point, m_pageLayout.units()));
604 m_pagePreview->setPageLayout(m_pageLayout);
605
606 updateWidget();
607}
608
609void QPageSetupWidget::pageOrientationChanged()
610{
611 if (m_blockSignals)
612 return;
613 m_pageLayout.setOrientation(m_ui.portrait->isChecked() ? QPageLayout::Portrait : QPageLayout::Landscape);
614 m_pagePreview->setPageLayout(m_pageLayout);
615 updateWidget();
616}
617
618void QPageSetupWidget::pagesPerSheetChanged()
619{
620#if QT_CONFIG(cups)
621 switch (m_ui.pagesPerSheetCombo->currentData().toInt()) {
622 case QCUPSSupport::OnePagePerSheet:
623 m_pagePreview->setPagePreviewLayout(1, 1);
624 break;
625 case QCUPSSupport::TwoPagesPerSheet:
626 m_pagePreview->setPagePreviewLayout(1, 2);
627 break;
628 case QCUPSSupport::FourPagesPerSheet:
629 m_pagePreview->setPagePreviewLayout(2, 2);
630 break;
631 case QCUPSSupport::SixPagesPerSheet:
632 m_pagePreview->setPagePreviewLayout(3, 2);
633 break;
634 case QCUPSSupport::NinePagesPerSheet:
635 m_pagePreview->setPagePreviewLayout(3, 3);
636 break;
637 case QCUPSSupport::SixteenPagesPerSheet:
638 m_pagePreview->setPagePreviewLayout(4, 4);
639 break;
640 }
641#endif
642}
643
644void QPageSetupWidget::unitChanged()
645{
646 if (m_blockSignals)
647 return;
648 m_units = qvariant_cast<QPageLayout::Unit>(m_ui.unitCombo->currentData());
649 m_pageLayout.setUnits(m_units);
650 updateWidget();
651}
652
653void QPageSetupWidget::topMarginChanged(double newValue)
654{
655 if (m_blockSignals)
656 return;
657 m_pageLayout.setTopMargin(newValue);
658 m_pagePreview->setPageLayout(m_pageLayout);
659}
660
661void QPageSetupWidget::bottomMarginChanged(double newValue)
662{
663 if (m_blockSignals)
664 return;
665 m_pageLayout.setBottomMargin(newValue);
666 m_pagePreview->setPageLayout(m_pageLayout);
667}
668
669void QPageSetupWidget::leftMarginChanged(double newValue)
670{
671 if (m_blockSignals)
672 return;
673 m_pageLayout.setLeftMargin(newValue);
674 m_pagePreview->setPageLayout(m_pageLayout);
675}
676
677void QPageSetupWidget::rightMarginChanged(double newValue)
678{
679 if (m_blockSignals)
680 return;
681 m_pageLayout.setRightMargin(newValue);
682 m_pagePreview->setPageLayout(m_pageLayout);
683}
684
685// QPageSetupDialog
686// - Public Linux / CUPS class implementation
687
688QPageSetupDialog::QPageSetupDialog(QPrinter *printer, QWidget *parent)
689 : QDialog(*(new QUnixPageSetupDialogPrivate(printer)), parent)
690{
691 Q_D(QPageSetupDialog);
692 setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
693 static_cast<QUnixPageSetupDialogPrivate *>(d)->init();
694}
695
696QPageSetupDialog::QPageSetupDialog(QWidget *parent)
697 : QDialog(*(new QUnixPageSetupDialogPrivate(nullptr)), parent)
698{
699 Q_D(QPageSetupDialog);
700 setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
701 static_cast<QUnixPageSetupDialogPrivate *>(d)->init();
702}
703
704int QPageSetupDialog::exec()
705{
706 Q_D(QPageSetupDialog);
707
708 int ret = QDialog::exec();
709 if (ret == Accepted) {
710 static_cast <QUnixPageSetupDialogPrivate*>(d)->widget->setupPrinter();
711 static_cast <QUnixPageSetupDialogPrivate*>(d)->widget->updateSavedValues();
712 } else {
713 static_cast <QUnixPageSetupDialogPrivate*>(d)->widget->revertToSavedValues();
714 }
715 return ret;
716}
717
718QT_END_NAMESPACE
719
720#include "moc_qpagesetupdialog_unix_p.cpp"
721
722#include "moc_qpagesetupdialog.cpp"
void setPageLayout(const QPageLayout &layout)
void setPagePreviewLayout(int columns, int rows)
QPagePreview(QWidget *parent)
void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
void setPrinter(QPrinter *printer, QPrintDevice *printDevice, QPrinter::OutputFormat outputFormat, const QString &printerName)
QPainter(QPaintDevice *)
Constructs a painter that begins painting the paint device immediately.
friend class QWidget
Definition qpainter.h:432
QMarginsF qt_convertMargins(const QMarginsF &margins, QPageLayout::Unit fromUnits, QPageLayout::Unit toUnits)