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_realCustomPageSizeIndex != -1
438 && (m_pageLayout.pageSize().id() == QPageSize::Custom
439 || m_ui.pageSizeCombo->currentIndex() == m_realCustomPageSizeIndex);
440 if (isCustom)
441 m_ui.pageSizeCombo->setCurrentIndex(m_realCustomPageSizeIndex);
442 else
443 m_ui.pageSizeCombo->setCurrentIndex(m_ui.pageSizeCombo->findData(QVariant::fromValue(m_pageLayout.pageSize())));
444
445 QMarginsF min;
446 QMarginsF max;
447
448 if (m_pageLayout.mode() == QPageLayout::FullPageMode) {
449 min = QMarginsF(0.0, 0.0, 0.0, 0.0);
450 max = QMarginsF(9999.9999, 9999.9999, 9999.9999, 9999.9999);
451 } else {
452 min = m_pageLayout.minimumMargins();
453 max = m_pageLayout.maximumMargins();
454 }
455
456 m_ui.leftMargin->setSuffix(suffix);
457 m_ui.leftMargin->setMinimum(min.left());
458 m_ui.leftMargin->setMaximum(max.left());
459 m_ui.leftMargin->setValue(m_pageLayout.margins().left());
460
461 m_ui.rightMargin->setSuffix(suffix);
462 m_ui.rightMargin->setMinimum(min.right());
463 m_ui.rightMargin->setMaximum(max.right());
464 m_ui.rightMargin->setValue(m_pageLayout.margins().right());
465
466 m_ui.topMargin->setSuffix(suffix);
467 m_ui.topMargin->setMinimum(min.top());
468 m_ui.topMargin->setMaximum(max.top());
469 m_ui.topMargin->setValue(m_pageLayout.margins().top());
470
471 m_ui.bottomMargin->setSuffix(suffix);
472 m_ui.bottomMargin->setMinimum(min.bottom());
473 m_ui.bottomMargin->setMaximum(max.bottom());
474 m_ui.bottomMargin->setValue(m_pageLayout.margins().bottom());
475
476 m_ui.pageWidth->setSuffix(suffix);
477 m_ui.pageWidth->setValue(m_pageLayout.fullRect(m_units).width());
478 m_ui.pageWidth->setEnabled(isCustom);
479 m_ui.widthLabel->setEnabled(isCustom);
480
481 m_ui.pageHeight->setSuffix(suffix);
482 m_ui.pageHeight->setValue(m_pageLayout.fullRect(m_units).height());
483 m_ui.pageHeight->setEnabled(isCustom);
484 m_ui.heightLabel->setEnabled(isCustom);
485
486 m_ui.portrait->setChecked(m_pageLayout.orientation() == QPageLayout::Portrait);
487 m_ui.landscape->setChecked(m_pageLayout.orientation() == QPageLayout::Landscape);
488
489 m_ui.pagesPerSheetButtonGroup->setEnabled(m_outputFormat == QPrinter::NativeFormat);
490
491#ifdef PSD_ENABLE_PAPERSOURCE
492 m_ui.paperSource->setCurrentItem(printer->paperSource());
493#endif
494
495 m_blockSignals = false;
496}
497
498// Set the dialog chosen options on the QPrinter
499// Normally only called when the QPrintDialog or QPageSetupDialog OK button is pressed
501{
502 m_printer->setPageLayout(m_pageLayout);
503 m_printer->setPageOrientation(m_pageLayout.orientation());
504#if QT_CONFIG(cups)
505 QCUPSSupport::PagesPerSheet pagesPerSheet = qvariant_cast<QCUPSSupport::PagesPerSheet>(m_ui.pagesPerSheetCombo->currentData()
506);
507 QCUPSSupport::PagesPerSheetLayout pagesPerSheetLayout = qvariant_cast<QCUPSSupport::PagesPerSheetLayout>(m_ui.pagesPerSheetLayoutCombo->currentData()
508);
509 QCUPSSupport::setPagesPerSheetLayout(m_printer, pagesPerSheet, pagesPerSheetLayout);
510#endif
511#ifdef PSD_ENABLE_PAPERSOURCE
512 m_printer->setPaperSource((QPrinter::PaperSource)m_ui.paperSource->currentIndex());
513#endif
514}
515
517{
518 m_savedUnits = m_units;
519 m_savedPageLayout = m_pageLayout;
520 m_savedPagesPerSheet = m_ui.pagesPerSheetCombo->currentIndex();
521 m_savedPagesPerSheetLayout = m_ui.pagesPerSheetLayoutCombo->currentIndex();
522}
523
525{
526 m_units = m_savedUnits;
527 m_pageLayout = m_savedPageLayout;
528 m_pagePreview->setPageLayout(m_pageLayout);
529
530 updateWidget();
531
532 m_ui.pagesPerSheetCombo->setCurrentIndex(m_savedPagesPerSheet);
533 m_ui.pagesPerSheetLayoutCombo->setCurrentIndex(m_savedPagesPerSheetLayout);
534}
535
536#if QT_CONFIG(cups)
537bool QPageSetupWidget::hasPpdConflict() const
538{
539 if (m_pageSizePpdOption) {
540 if (m_pageSizePpdOption->conflicted) {
541 const QIcon warning = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, nullptr);
542 const int pixmap_size = m_ui.pageSizeCombo->sizeHint().height() * .75;
543 m_ui.pageSizeWarningLabel->setPixmap(warning.pixmap(pixmap_size, pixmap_size));
544 } else {
545 m_ui.pageSizeWarningLabel->setPixmap(QPixmap());
546 }
547 return m_pageSizePpdOption->conflicted;
548 }
549
550 return false;
551}
552#endif
553
554// Updates size/preview after the combobox has been changed.
555void QPageSetupWidget::pageSizeChanged()
556{
557 QPageSize pageSize;
558 if (m_ui.pageSizeCombo->currentIndex() != m_realCustomPageSizeIndex) {
559 pageSize = qvariant_cast<QPageSize>(m_ui.pageSizeCombo->currentData());
560
561#if QT_CONFIG(cups)
562 if (m_pageSizePpdOption) {
563 ppd_file_t *ppd = qvariant_cast<ppd_file_t*>(m_printDevice->property(PDPK_PpdFile));
564 QStringDecoder toUtf16(ppd->lang_encoding, QStringDecoder::Flag::Stateless);
565 if (!toUtf16.isValid()) {
566 qWarning() << "QPrinSupport: Cups uses unsupported encoding" << ppd->lang_encoding;
567 toUtf16 = QStringDecoder(QStringDecoder::Utf8);
568 }
569 for (int i = 0; i < m_pageSizePpdOption->num_choices; ++i) {
570 const ppd_choice_t *choice = &m_pageSizePpdOption->choices[i];
571 if (toUtf16(choice->text) == m_ui.pageSizeCombo->currentText()) {
572 const auto values = QStringList{} << QString::fromLatin1(m_pageSizePpdOption->keyword)
573 << QString::fromLatin1(choice->choice);
574 m_printDevice->setProperty(PDPK_PpdOption, values);
575 emit ppdOptionChanged();
576 break;
577 }
578 }
579 }
580#endif
581
582 } else {
583 QSizeF customSize;
584 if (m_pageLayout.orientation() == QPageLayout::Landscape)
585 customSize = QSizeF(m_ui.pageHeight->value(), m_ui.pageWidth->value());
586 else
587 customSize = QSizeF(m_ui.pageWidth->value(), m_ui.pageHeight->value());
588 pageSize = QPageSize(customSize, QPageSize::Unit(m_units));
589
590#if QT_CONFIG(cups)
591 if (m_pageSizePpdOption) {
592 const auto values = QStringList{} << QString::fromLatin1(m_pageSizePpdOption->keyword)
593 << QStringLiteral("Custom");
594 m_printDevice->setProperty(PDPK_PpdOption, values);
595 emit ppdOptionChanged();
596 }
597#endif
598 }
599
600 // We always need to update the m_pageSizePpdOption when the page size changes
601 // even if it's from inside updateWidget, so do not move up
602 if (m_blockSignals)
603 return;
604
605 const QMarginsF printable = m_printDevice ? m_printDevice->printableMargins(pageSize, m_pageLayout.orientation(), m_printer->resolution())
606 : QMarginsF();
607 m_pageLayout.setPageSize(pageSize, qt_convertMargins(printable, QPageLayout::Point, m_pageLayout.units()));
608 m_pagePreview->setPageLayout(m_pageLayout);
609
610 updateWidget();
611}
612
613void QPageSetupWidget::pageOrientationChanged()
614{
615 if (m_blockSignals)
616 return;
617 m_pageLayout.setOrientation(m_ui.portrait->isChecked() ? QPageLayout::Portrait : QPageLayout::Landscape);
618 m_pagePreview->setPageLayout(m_pageLayout);
619 updateWidget();
620}
621
622void QPageSetupWidget::pagesPerSheetChanged()
623{
624#if QT_CONFIG(cups)
625 switch (m_ui.pagesPerSheetCombo->currentData().toInt()) {
626 case QCUPSSupport::OnePagePerSheet:
627 m_pagePreview->setPagePreviewLayout(1, 1);
628 break;
629 case QCUPSSupport::TwoPagesPerSheet:
630 m_pagePreview->setPagePreviewLayout(1, 2);
631 break;
632 case QCUPSSupport::FourPagesPerSheet:
633 m_pagePreview->setPagePreviewLayout(2, 2);
634 break;
635 case QCUPSSupport::SixPagesPerSheet:
636 m_pagePreview->setPagePreviewLayout(3, 2);
637 break;
638 case QCUPSSupport::NinePagesPerSheet:
639 m_pagePreview->setPagePreviewLayout(3, 3);
640 break;
641 case QCUPSSupport::SixteenPagesPerSheet:
642 m_pagePreview->setPagePreviewLayout(4, 4);
643 break;
644 }
645#endif
646}
647
648void QPageSetupWidget::unitChanged()
649{
650 if (m_blockSignals)
651 return;
652 m_units = qvariant_cast<QPageLayout::Unit>(m_ui.unitCombo->currentData());
653 m_pageLayout.setUnits(m_units);
654 updateWidget();
655}
656
657void QPageSetupWidget::topMarginChanged(double newValue)
658{
659 if (m_blockSignals)
660 return;
661 m_pageLayout.setTopMargin(newValue);
662 m_pagePreview->setPageLayout(m_pageLayout);
663}
664
665void QPageSetupWidget::bottomMarginChanged(double newValue)
666{
667 if (m_blockSignals)
668 return;
669 m_pageLayout.setBottomMargin(newValue);
670 m_pagePreview->setPageLayout(m_pageLayout);
671}
672
673void QPageSetupWidget::leftMarginChanged(double newValue)
674{
675 if (m_blockSignals)
676 return;
677 m_pageLayout.setLeftMargin(newValue);
678 m_pagePreview->setPageLayout(m_pageLayout);
679}
680
681void QPageSetupWidget::rightMarginChanged(double newValue)
682{
683 if (m_blockSignals)
684 return;
685 m_pageLayout.setRightMargin(newValue);
686 m_pagePreview->setPageLayout(m_pageLayout);
687}
688
689// QPageSetupDialog
690// - Public Linux / CUPS class implementation
691
692QPageSetupDialog::QPageSetupDialog(QPrinter *printer, QWidget *parent)
693 : QDialog(*(new QUnixPageSetupDialogPrivate(printer)), parent)
694{
695 Q_D(QPageSetupDialog);
696 setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
697 static_cast<QUnixPageSetupDialogPrivate *>(d)->init();
698}
699
700QPageSetupDialog::QPageSetupDialog(QWidget *parent)
701 : QDialog(*(new QUnixPageSetupDialogPrivate(nullptr)), parent)
702{
703 Q_D(QPageSetupDialog);
704 setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
705 static_cast<QUnixPageSetupDialogPrivate *>(d)->init();
706}
707
708int QPageSetupDialog::exec()
709{
710 Q_D(QPageSetupDialog);
711
712 int ret = QDialog::exec();
713 if (ret == Accepted) {
714 static_cast <QUnixPageSetupDialogPrivate*>(d)->widget->setupPrinter();
715 static_cast <QUnixPageSetupDialogPrivate*>(d)->widget->updateSavedValues();
716 } else {
717 static_cast <QUnixPageSetupDialogPrivate*>(d)->widget->revertToSavedValues();
718 }
719 return ret;
720}
721
722QT_END_NAMESPACE
723
724#include "moc_qpagesetupdialog_unix_p.cpp"
725
726#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)