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
qprintdialog_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
5#include "qplatformdefs.h"
6#include <QtPrintSupport/private/qtprintsupportglobal_p.h>
7
8#include "private/qabstractprintdialog_p.h"
9#if QT_CONFIG(messagebox)
10#include <QtWidgets/qmessagebox.h>
11#endif
12#include "qprintdialog.h"
13#if QT_CONFIG(filedialog)
14#include "qfiledialog.h"
15#endif
16#include <QtCore/qdebug.h>
17#include <QtCore/qdir.h>
18#include <QtCore/qglobal.h>
19#include <QtCore/qstringconverter.h>
20#include <QtGui/qevent.h>
21#if QT_CONFIG(filesystemmodel)
22#include <QtGui/qfilesystemmodel.h>
23#endif
24#include <QtWidgets/qstyleditemdelegate.h>
25#include <QtWidgets/qformlayout.h>
26#include <QtPrintSupport/qprinter.h>
27
28#include <qpa/qplatformprintplugin.h>
29#include <qpa/qplatformprintersupport.h>
30
31#include <private/qprintdevice_p.h>
32
33#include <QtWidgets/qdialogbuttonbox.h>
34
35#if QT_CONFIG(regularexpression)
36#include <qregularexpression.h>
37#endif
38
39#if QT_CONFIG(completer)
40#include <private/qcompleter_p.h>
41#endif
42#include "ui_qprintpropertieswidget.h"
43#include "ui_qprintsettingsoutput.h"
44#include "ui_qprintwidget.h"
45
46#if QT_CONFIG(cups)
48#include <private/qcups_p.h>
49#if QT_CONFIG(cupsjobwidget)
50#include "qcupsjobwidget_p.h"
51#endif
52#endif
53
54/*
55
56Print dialog class declarations
57
58 QPrintDialog: The main Print Dialog, nothing really held here.
59
60 QUnixPrintWidget:
61 QUnixPrintWidgetPrivate: The real Unix Print Dialog implementation.
62
63 Directly includes the upper half of the Print Dialog
64 containing the Printer Selection widgets and
65 Properties button.
66
67 Embeds the Properties pop-up dialog from
68 QPrintPropertiesDialog
69
70 Embeds the lower half from separate widget class
71 QPrintDialogPrivate
72
73 Layout in qprintwidget.ui
74
75 QPrintDialogPrivate: The lower half of the Print Dialog containing the
76 Copies and Options tabs that expands when the
77 Options button is selected.
78
79 Layout in qprintsettingsoutput.ui
80
81 QPrintPropertiesDialog: Dialog displayed when clicking on Properties button to
82 allow editing of Page and Advanced tabs.
83
84 Layout in qprintpropertieswidget.ui
85*/
86
88{
89 Q_INIT_RESOURCE(qprintdialog);
90}
91
92QT_BEGIN_NAMESPACE
93
94using namespace Qt::StringLiterals;
95
97{
99public:
104
105 void setupPrinter() const;
106
107private slots:
110
111private:
112 void showEvent(QShowEvent *event) override;
113
115#if QT_CONFIG(cups)
117#endif
118 Ui::QPrintPropertiesWidget widget;
119 QDialogButtonBox *m_buttons;
120#if QT_CONFIG(cupsjobwidget)
122#endif
123
124#if QT_CONFIG(cups)
129 bool anyPpdOptionConflict() const;
130 bool anyAdvancedOptionConflict() const;
131
133
136#endif
137};
138
140
142{
144
145public:
149
150private:
151 friend class QPrintDialog;
155 Q_PRIVATE_SLOT(d, void _q_printerChanged(int))
158};
159
161{
162public:
165
170// slots
171 void _q_printerChanged(int index);
174
179 QPrinter *printer;
181
183
184#if QT_CONFIG(cups)
187#endif
188
189private:
190 QPrintDialogPrivate *optionsPane;
191 bool filePrintersAdded;
192 int initialPrinterIndex;
193};
194
196{
197 Q_DECLARE_PUBLIC(QPrintDialog)
199public:
202
203 void init();
204
205 void selectPrinter(const QPrinter::OutputFormat outputFormat);
206
208#if QT_CONFIG(messagebox)
209 void _q_checkFields();
210#endif
212
213#if QT_CONFIG(cups)
215#endif
218
219 virtual void setTabs(const QList<QWidget*> &tabs) override;
220
225 QPushButton *collapseButton;
227private:
228 void setExplicitDuplexMode(QPrint::DuplexMode duplexMode);
229 // duplex mode explicitly set by user, QPrint::DuplexAuto otherwise
230 QPrint::DuplexMode explicitDuplexMode;
231};
232
233////////////////////////////////////////////////////////////////////////////////
234////////////////////////////////////////////////////////////////////////////////
235
236/*
237
238 QPrintPropertiesDialog
239
240 Dialog displayed when clicking on Properties button to allow editing of Page
241 and Advanced tabs.
242
243*/
244
245QPrintPropertiesDialog::QPrintPropertiesDialog(QPrinter *printer, QPrintDevice *currentPrintDevice,
246 QPrinter::OutputFormat outputFormat, const QString &printerName,
247 QAbstractPrintDialog *parent)
248 : QDialog(parent)
249#if QT_CONFIG(cups)
250 , m_printer(printer)
251#endif
252{
253 setWindowTitle(tr("Printer Properties"));
254 QVBoxLayout *lay = new QVBoxLayout(this);
255 QWidget *content = new QWidget(this);
256 widget.setupUi(content);
257 m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
258 lay->addWidget(content);
259 lay->addWidget(m_buttons);
260
261 connect(m_buttons->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &QPrintPropertiesDialog::accept);
262 connect(m_buttons->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &QPrintPropertiesDialog::reject);
263
264 widget.pageSetup->setPrinter(printer, currentPrintDevice, outputFormat, printerName);
265
266#if QT_CONFIG(cupsjobwidget)
267 m_jobOptions = new QCupsJobWidget(printer, currentPrintDevice);
268 widget.tabs->insertTab(1, m_jobOptions, tr("Job Options"));
269#endif
270
271 const int advancedTabIndex = widget.tabs->indexOf(widget.cupsPropertiesPage);
272#if QT_CONFIG(cups)
273 m_currentPrintDevice = currentPrintDevice;
274 const bool anyWidgetCreated = createAdvancedOptionsWidget();
275
276 widget.tabs->setTabEnabled(advancedTabIndex, anyWidgetCreated);
277
278 connect(widget.pageSetup, &QPageSetupWidget::ppdOptionChanged, this, [this] {
279 widget.conflictsLabel->setVisible(anyPpdOptionConflict());
280 });
281
282#else
283 Q_UNUSED(currentPrintDevice);
284 widget.tabs->setTabEnabled(advancedTabIndex, false);
285#endif
286}
287
291
293{
294#if QT_CONFIG(cups)
295 QCUPSSupport::clearCupsOptions(m_printer);
296#endif
297
298 widget.pageSetup->setupPrinter();
299#if QT_CONFIG(cupsjobwidget)
300 m_jobOptions->setupPrinter();
301#endif
302
303#if QT_CONFIG(cups)
304 // Set Color by default, that will change if the "ColorModel" property is available
305 m_printer->setColorMode(QPrinter::Color);
306
307 setPrinterAdvancedCupsOptions();
308#endif
309}
310
312{
313 widget.pageSetup->revertToSavedValues();
314
315#if QT_CONFIG(cupsjobwidget)
316 m_jobOptions->revertToSavedValues();
317#endif
318
319#if QT_CONFIG(cups)
320 revertAdvancedOptionsToSavedValues();
321#endif
322 QDialog::reject();
323}
324
326{
327#if QT_CONFIG(cups) && QT_CONFIG(messagebox)
328 if (widget.pageSetup->hasPpdConflict()) {
329 widget.tabs->setCurrentWidget(widget.tabPage);
330 const QMessageBox::StandardButton answer = QMessageBox::warning(this, tr("Page Setup Conflicts"),
331 tr("There are conflicts in page setup options. Do you want to fix them?"),
332 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
333 if (answer != QMessageBox::No)
334 return;
335 } else if (anyAdvancedOptionConflict()) {
336 widget.tabs->setCurrentWidget(widget.cupsPropertiesPage);
337 const QMessageBox::StandardButton answer = QMessageBox::warning(this, tr("Advanced Option Conflicts"),
338 tr("There are conflicts in some advanced options. Do you want to fix them?"),
339 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
340 if (answer != QMessageBox::No)
341 return;
342 }
343 advancedOptionsUpdateSavedValues();
344#endif
345
346#if QT_CONFIG(cupsjobwidget)
347 m_jobOptions->updateSavedValues();
348#endif
349
350 widget.pageSetup->updateSavedValues();
351
352 QDialog::accept();
353}
354
355void QPrintPropertiesDialog::showEvent(QShowEvent *event)
356{
357#if QT_CONFIG(cups)
358 widget.conflictsLabel->setVisible(anyPpdOptionConflict());
359#endif
360 QDialog::showEvent(event);
361}
362
363#if QT_CONFIG(cups)
364
365// Used to store the ppd_option_t for each QComboBox that represents an advanced option
366static const char *ppdOptionProperty = "_q_ppd_option";
367
368// Used to store the originally selected choice index for each QComboBox that represents an advanced option
369static const char *ppdOriginallySelectedChoiceProperty = "_q_ppd_originally_selected_choice";
370
371// Used to store the QLineEdit pointer for each advanced option that supports custom values
372static const char *customValueLineEditProperty = "_q_custom_value_lineedit";
373
374// Used to store the warning label pointer for each QComboBox that represents an advanced option
375static const char *warningLabelProperty = "_q_warning_label";
376
377static bool isBlacklistedGroup(const ppd_group_t *group) noexcept
378{
379 return qstrcmp(group->name, "InstallableOptions") == 0;
380};
381
382static bool isBlacklistedOption(const char *keyword) noexcept
383{
384 // We already let the user set these options elsewhere
385 const char *cupsOptionBlacklist[] = {
386 "Collate",
387 "Copies",
388 "OutputOrder",
389 "PageRegion",
390 "PageSize",
391 "Duplex" // handled by the main dialog
392 };
393 auto equals = [](const char *keyword) {
394 return [keyword](const char *candidate) {
395 return qstrcmp(keyword, candidate) == 0;
396 };
397 };
398 return std::any_of(std::begin(cupsOptionBlacklist), std::end(cupsOptionBlacklist), equals(keyword));
399};
400
401bool QPrintPropertiesDialog::createAdvancedOptionsWidget()
402{
403 bool anyWidgetCreated = false;
404
405 ppd_file_t *ppd = qvariant_cast<ppd_file_t*>(m_currentPrintDevice->property(PDPK_PpdFile));
406
407 if (ppd) {
408 toUnicode = QStringDecoder(ppd->lang_encoding, QStringDecoder::Flag::Stateless);
409 if (!toUnicode.isValid()) {
410 qWarning() << "QPrinSupport: Cups uses unsupported encoding" << ppd->lang_encoding;
411 toUnicode = QStringDecoder(QStringDecoder::Utf8, QStringDecoder::Flag::Stateless);
412 }
413
414 QWidget *holdingWidget = new QWidget();
415 QVBoxLayout *layout = new QVBoxLayout(holdingWidget);
416
417 for (int i = 0; i < ppd->num_groups; ++i) {
418 const ppd_group_t *group = &ppd->groups[i];
419
420 if (!isBlacklistedGroup(group)) {
421 QFormLayout *groupLayout = new QFormLayout();
422
423 for (int i = 0; i < group->num_options; ++i) {
424 const ppd_option_t *option = &group->options[i];
425
426 if (!isBlacklistedOption(option->keyword)) {
427 QComboBox *choicesCb = new QComboBox();
428
429 const auto setPpdOptionFromCombo = [this, choicesCb, option] {
430 // We can't use choicesCb->currentIndex() to know the index of the option in the choices[] array
431 // because some of them may not be present in the list because they conflict with the
432 // installable options so use the index passed on addItem
433 const int selectedChoiceIndex = choicesCb->currentData().toInt();
434 const QString &choice = QString::fromLatin1(
435 option->choices[selectedChoiceIndex].choice);
436 const QStringList &values { QString::fromLatin1(option->keyword), choice };
437 m_currentPrintDevice->setProperty(PDPK_PpdOption, values);
438 widget.conflictsLabel->setVisible(anyPpdOptionConflict());
439
440 if (auto *customValueEdit = qvariant_cast<QLineEdit *>(
441 choicesCb->property(customValueLineEditProperty))) {
442 const bool customSelected = choice == u"Custom";
443 customValueEdit->setVisible(customSelected);
444 if (customSelected) {
445 const QStringList params{ QString::fromLatin1(
446 option->keyword) };
447 QString value =
448 m_currentPrintDevice->property(PDPK_OptionValue, params)
449 .toString();
450 if (value.startsWith(u"Custom."))
451 customValueEdit->setText(value.mid(7));
452 }
453 }
454 };
455
456 bool foundMarkedChoice = false;
457 bool markedChoiceNotAvailable = false;
458 for (int i = 0; i < option->num_choices; ++i) {
459 const ppd_choice_t *choice = &option->choices[i];
460 const auto values = QStringList{} << QString::fromLatin1(option->keyword) << QString::fromLatin1(choice->choice);
461 const bool choiceIsInstallableConflict = m_currentPrintDevice->isFeatureAvailable(PDPK_PpdChoiceIsInstallableConflict, values);
462 if (choiceIsInstallableConflict && static_cast<int>(choice->marked) == 1) {
463 markedChoiceNotAvailable = true;
464 } else if (!choiceIsInstallableConflict) {
465 choicesCb->addItem(toUnicode(choice->text), i);
466 if (static_cast<int>(choice->marked) == 1) {
467 choicesCb->setCurrentIndex(choicesCb->count() - 1);
468 choicesCb->setProperty(ppdOriginallySelectedChoiceProperty, QVariant(i));
469 foundMarkedChoice = true;
470 } else if (!foundMarkedChoice && qstrcmp(choice->choice, option->defchoice) == 0) {
471 choicesCb->setCurrentIndex(choicesCb->count() - 1);
472 choicesCb->setProperty(ppdOriginallySelectedChoiceProperty, QVariant(i));
473 }
474 }
475 }
476
477 if (markedChoiceNotAvailable) {
478 // If the user default option is not available because of it conflicting with
479 // the installed options, we need to set the internal ppd value to the value
480 // being shown in the combo
481 setPpdOptionFromCombo();
482 }
483
484 if (choicesCb->count() > 1) {
485
486 connect(choicesCb, &QComboBox::currentIndexChanged, this, setPpdOptionFromCombo);
487
488 // We need an extra label at the end to show the conflict warning
489 QWidget *choicesCbWithLabel = new QWidget();
490 QHBoxLayout *choicesCbWithLabelLayout = new QHBoxLayout(choicesCbWithLabel);
491 choicesCbWithLabelLayout->setContentsMargins(0, 0, 0, 0);
492 QLabel *warningLabel = new QLabel();
493 choicesCbWithLabelLayout->addWidget(choicesCb);
494
495 const QString &optionText = toUnicode(option->text);
496 const QStringList &params{ QString::fromLatin1(option->keyword) };
497 const bool hasCustomOption = m_currentPrintDevice->isFeatureAvailable(
498 PDPK_PpdCustomOption, params);
499 if (hasCustomOption) {
500 auto *customValueEdit = new QLineEdit;
501#if QT_CONFIG(accessibility)
502 customValueEdit->setAccessibleName(
503 tr("Custom value for %1").arg(optionText));
504#endif
505 choicesCbWithLabelLayout->addWidget(customValueEdit);
506 choicesCb->setProperty(customValueLineEditProperty,
507 QVariant::fromValue(customValueEdit));
508 // ensure line edit is shown/hidden as needed
509 setPpdOptionFromCombo();
510 }
511 choicesCbWithLabelLayout->addWidget(warningLabel);
512
513 QLabel *optionLabel = new QLabel(optionText);
514 optionLabel->setBuddy(choicesCb);
515 groupLayout->addRow(optionLabel, choicesCbWithLabel);
516 anyWidgetCreated = true;
517 choicesCb->setProperty(ppdOptionProperty, QVariant::fromValue(option));
518 choicesCb->setProperty(warningLabelProperty, QVariant::fromValue(warningLabel));
519 m_advancedOptionsCombos << choicesCb;
520 } else {
521 delete choicesCb;
522 }
523 }
524 }
525
526 if (groupLayout->rowCount() > 0) {
527 QGroupBox *groupBox = new QGroupBox(toUnicode(group->text));
528 groupBox->setLayout(groupLayout);
529 layout->addWidget(groupBox);
530 } else {
531 delete groupLayout;
532 }
533 }
534 }
535
536 layout->addStretch();
537 widget.scrollArea->setWidget(holdingWidget);
538 }
539
540 return anyWidgetCreated;
541}
542
543void QPrintPropertiesDialog::setPrinterAdvancedCupsOptions() const
544{
545 for (const QComboBox *choicesCb : m_advancedOptionsCombos) {
546 const ppd_option_t *option = qvariant_cast<const ppd_option_t *>(choicesCb->property(ppdOptionProperty));
547
548 // We can't use choicesCb->currentIndex() to know the index of the option in the choices[] array
549 // because some of them may not be present in the list because they conflict with the
550 // installable options so use the index passed on addItem
551 const int selectedChoiceIndex = choicesCb->currentData().toInt();
552 QString selectedChoice = QString::fromLatin1(option->choices[selectedChoiceIndex].choice);
553
554 if (selectedChoice == u"Custom") {
555 if (auto *customValueEdit = qvariant_cast<QLineEdit *>(
556 choicesCb->property(customValueLineEditProperty))) {
557 const QString &customValue = customValueEdit->text();
558 if (!customValue.isEmpty())
559 selectedChoice = u"Custom." + customValue;
560 }
561 }
562
563 if (qstrcmp(option->keyword, "ColorModel") == 0)
564 m_printer->setColorMode(selectedChoice == u"Gray" ? QPrinter::GrayScale
565 : QPrinter::Color);
566
567 if (qstrcmp(option->defchoice, selectedChoice.toLatin1().constData()) != 0)
568 QCUPSSupport::setCupsOption(m_printer, QString::fromLatin1(option->keyword),
569 selectedChoice);
570 }
571}
572
573void QPrintPropertiesDialog::revertAdvancedOptionsToSavedValues() const
574{
575 for (QComboBox *choicesCb : m_advancedOptionsCombos) {
576 const int originallySelectedChoice = qvariant_cast<int>(choicesCb->property(ppdOriginallySelectedChoiceProperty));
577 const int newComboIndexToSelect = choicesCb->findData(originallySelectedChoice);
578 choicesCb->setCurrentIndex(newComboIndexToSelect);
579 // The currentIndexChanged lambda takes care of resetting the ppd option
580 }
581 widget.conflictsLabel->setVisible(anyPpdOptionConflict());
582}
583
584void QPrintPropertiesDialog::advancedOptionsUpdateSavedValues() const
585{
586 for (QComboBox *choicesCb : m_advancedOptionsCombos)
587 choicesCb->setProperty(ppdOriginallySelectedChoiceProperty, choicesCb->currentData());
588}
589
590bool QPrintPropertiesDialog::anyPpdOptionConflict() const
591{
592 // we need to execute both since besides returning true/false they update the warning icons
593 const bool pageSetupConflicts = widget.pageSetup->hasPpdConflict();
594 const bool advancedOptionConflicts = anyAdvancedOptionConflict();
595 return pageSetupConflicts || advancedOptionConflicts;
596}
597
598bool QPrintPropertiesDialog::anyAdvancedOptionConflict() const
599{
600 const QIcon warning = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, nullptr);
601
602 bool anyConflicted = false;
603
604 for (const QComboBox *choicesCb : m_advancedOptionsCombos) {
605 const ppd_option_t *option = qvariant_cast<const ppd_option_t *>(choicesCb->property(ppdOptionProperty));
606 QLabel *warningLabel = qvariant_cast<QLabel *>(choicesCb->property(warningLabelProperty));
607 if (option->conflicted) {
608 anyConflicted = true;
609 const int pixmap_size = choicesCb->sizeHint().height() * .75;
610 warningLabel->setPixmap(warning.pixmap(pixmap_size, pixmap_size));
611 } else {
612 warningLabel->setPixmap(QPixmap());
613 }
614 }
615
616 return anyConflicted;
617}
618
619#endif
620
621
622////////////////////////////////////////////////////////////////////////////////
623////////////////////////////////////////////////////////////////////////////////
624
625/*
626
627 QPrintDialogPrivate
628
629 The lower half of the Print Dialog containing the Copies and Options
630 tabs that expands when the Options button is selected.
631
632*/
633QPrintDialogPrivate::QPrintDialogPrivate()
634 : top(nullptr), bottom(nullptr), buttons(nullptr), collapseButton(nullptr),
635 explicitDuplexMode(QPrint::DuplexAuto)
636{
638}
639
643
645{
646 Q_Q(QPrintDialog);
647
648 top = new QUnixPrintWidget(q->printer(), q);
649 bottom = new QWidget(q);
650 options.setupUi(bottom);
651 options.color->setIconSize(QSize(32, 32));
652 options.color->setIcon(QIcon(":/qt-project.org/dialogs/qprintdialog/images/status-color.png"_L1));
653 options.grayscale->setIconSize(QSize(32, 32));
654 options.grayscale->setIcon(QIcon(":/qt-project.org/dialogs/qprintdialog/images/status-gray-scale.png"_L1));
655
656#if QT_CONFIG(cups)
657 // Add Page Set widget if CUPS is available
658 options.pageSetCombo->addItem(tr("All Pages"), QVariant::fromValue(QCUPSSupport::AllPages));
659 options.pageSetCombo->addItem(tr("Odd Pages"), QVariant::fromValue(QCUPSSupport::OddPages));
660 options.pageSetCombo->addItem(tr("Even Pages"), QVariant::fromValue(QCUPSSupport::EvenPages));
661#else
662 delete options.pagesRadioButton;
663 delete options.pagesLineEdit;
664 options.pagesRadioButton = nullptr;
665 options.pagesLineEdit = nullptr;
666#endif
667
669
670 buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, q);
671 collapseButton = new QPushButton(QPrintDialog::tr("&Options >>"), buttons);
672 buttons->addButton(collapseButton, QDialogButtonBox::ResetRole);
673 bottom->setVisible(false);
674
675 QPushButton *printButton = buttons->button(QDialogButtonBox::Ok);
676 printButton->setText(QPrintDialog::tr("&Print"));
677 printButton->setDefault(true);
678
679 QVBoxLayout *lay = new QVBoxLayout(q);
680 lay->addWidget(top);
681 lay->addWidget(bottom);
682 lay->addWidget(buttons);
683
684#if !QT_CONFIG(messagebox)
685 QObject::connect(buttons, SIGNAL(accepted()), q, SLOT(accept()));
686#else
687 QObject::connect(buttons, SIGNAL(accepted()), q, SLOT(_q_checkFields()));
688#endif
689 QObject::connect(buttons, SIGNAL(rejected()), q, SLOT(reject()));
690
691 QObject::connect(options.printSelection, SIGNAL(toggled(bool)),
692 q, SLOT(_q_togglePageSetCombo(bool)));
693
694 QObject::connect(options.printCurrentPage, SIGNAL(toggled(bool)),
695 q, SLOT(_q_togglePageSetCombo(bool)));
696
697 QObject::connect(collapseButton, SIGNAL(released()), q, SLOT(_q_collapseOrExpandDialog()));
698
699 QObject::connect(options.noDuplex, &QAbstractButton::clicked, q, [this] { setExplicitDuplexMode(QPrint::DuplexNone); });
700 QObject::connect(options.duplexLong, &QAbstractButton::clicked, q, [this] { setExplicitDuplexMode(QPrint::DuplexLongSide); });
701 QObject::connect(options.duplexShort, &QAbstractButton::clicked, q, [this] { setExplicitDuplexMode(QPrint::DuplexShortSide); });
702
703#if QT_CONFIG(cups)
704 QObject::connect(options.noDuplex, &QAbstractButton::toggled, q, [this] { updatePpdDuplexOption(options.noDuplex); });
705 QObject::connect(options.duplexLong, &QAbstractButton::toggled, q, [this] { updatePpdDuplexOption(options.duplexLong); });
706 QObject::connect(options.duplexShort, &QAbstractButton::toggled, q, [this] { updatePpdDuplexOption(options.duplexShort); });
707#endif
708}
709
710// initialize printer options
711void QPrintDialogPrivate::selectPrinter(const QPrinter::OutputFormat outputFormat)
712{
713 Q_Q(QPrintDialog);
714 QPrinter *p = q->printer();
715 printerOutputFormat = outputFormat;
716
717 // printer supports duplex mode?
718 const auto supportedDuplexMode = top->d->m_currentPrintDevice.supportedDuplexModes();
719 options.duplexLong->setEnabled(supportedDuplexMode.contains(QPrint::DuplexLongSide));
720 options.duplexShort->setEnabled(supportedDuplexMode.contains(QPrint::DuplexShortSide));
721
722 if (p->colorMode() == QPrinter::Color)
723 options.color->setChecked(true);
724 else
725 options.grayscale->setChecked(true);
726
727 // duplex priorities to be as follows:
728 // 1) a user-selected duplex value in the dialog has highest priority
729 // 2) duplex value set in the QPrinter
730 QPrint::DuplexMode duplex;
731 if (explicitDuplexMode != QPrint::DuplexAuto && supportedDuplexMode.contains(explicitDuplexMode))
732 duplex = explicitDuplexMode;
733 else
734 duplex = static_cast<QPrint::DuplexMode>(p->duplex());
735 switch (duplex) {
736 case QPrint::DuplexNone:
737 options.noDuplex->setChecked(true); break;
738 case QPrint::DuplexLongSide:
739 case QPrint::DuplexAuto:
740 options.duplexLong->setChecked(true); break;
741 case QPrint::DuplexShortSide:
742 options.duplexShort->setChecked(true); break;
743 }
744 options.copies->setValue(p->copyCount());
745 options.collate->setChecked(p->collateCopies());
746 options.reverse->setChecked(p->pageOrder() == QPrinter::LastPageFirst);
747
748 if (outputFormat == QPrinter::PdfFormat || options.printSelection->isChecked()
749 || options.printCurrentPage->isChecked())
750
751 options.pageSetCombo->setEnabled(false);
752 else
753 options.pageSetCombo->setEnabled(true);
754
755#if QT_CONFIG(cups)
756 // Disable complex page ranges widget when printing to pdf
757 // It doesn't work since it relies on cups to do the heavy lifting and cups
758 // is not used when printing to PDF
759 options.pagesRadioButton->setEnabled(outputFormat != QPrinter::PdfFormat);
760
761 // Disable color options on main dialog if not printing to file, it will be handled by CUPS advanced dialog
762 options.colorMode->setVisible(outputFormat == QPrinter::PdfFormat);
763#endif
764}
765
766#if QT_CONFIG(cups)
767
768void QPrintDialogPrivate::updatePpdDuplexOption(QRadioButton *radio)
769{
770 const bool checked = radio->isChecked();
771 if (checked) {
772 if (radio == options.noDuplex) top->d->setPpdDuplex(QPrinter::DuplexNone);
773 else if (radio == options.duplexLong) top->d->setPpdDuplex(QPrinter::DuplexLongSide);
774 else if (radio == options.duplexShort) top->d->setPpdDuplex(QPrinter::DuplexShortSide);
775 }
776 const bool conflict = checked && top->d->m_duplexPpdOption && top->d->m_duplexPpdOption->conflicted;
777 radio->setIcon(conflict ? QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, nullptr) : QIcon());
778}
779
780#endif
781
782void QPrintDialogPrivate::setExplicitDuplexMode(const QPrint::DuplexMode duplexMode)
783{
784 explicitDuplexMode = duplexMode;
785}
786
788{
789 // First setup the requested OutputFormat, Printer and Page Size first
791
792 // Then setup Print Job options
793 Q_Q(QPrintDialog);
794 QPrinter* p = q->printer();
795
796 if (options.duplex->isEnabled()) {
797 if (options.noDuplex->isChecked())
798 p->setDuplex(QPrinter::DuplexNone);
799 else if (options.duplexLong->isChecked())
800 p->setDuplex(QPrinter::DuplexLongSide);
801 else
802 p->setDuplex(QPrinter::DuplexShortSide);
803 }
804
805#if QT_CONFIG(cups)
806 // When printing to a device the colorMode will be set by the advanced panel
807 if (p->outputFormat() == QPrinter::PdfFormat)
808#endif
809 p->setColorMode(options.color->isChecked() ? QPrinter::Color : QPrinter::GrayScale);
810
811 p->setPageOrder(options.reverse->isChecked() ? QPrinter::LastPageFirst : QPrinter::FirstPageFirst);
812
813 // print range
814 if (options.printAll->isChecked()) {
815 p->setPrintRange(QPrinter::AllPages);
816 p->setPageRanges(QPageRanges());
817 } else if (options.printSelection->isChecked()) {
818 p->setPrintRange(QPrinter::Selection);
819 p->setPageRanges(QPageRanges());
820 } else if (options.printCurrentPage->isChecked()) {
821 p->setPrintRange(QPrinter::CurrentPage);
822 p->setPageRanges(QPageRanges());
823 } else if (options.printRange->isChecked()) {
824 if (q->testOption(QPrintDialog::PrintPageRange)) {
825 p->setPrintRange(QPrinter::PageRange);
826 p->setFromTo(options.from->value(), qMax(options.from->value(), options.to->value()));
827 } else {
828 // This case happens when CUPS server-side page range is enabled
829 // Setting the range to the printer occurs below
830 p->setPrintRange(QPrinter::AllPages);
831 p->setPageRanges(QPageRanges());
832 }
833 }
834
835#if QT_CONFIG(cups)
836 if (options.pagesRadioButton->isChecked()) {
837 const QPageRanges ranges = QPageRanges::fromString(options.pagesLineEdit->text());
838 p->setPrintRange(QPrinter::AllPages);
839 p->setPageRanges(QPageRanges());
840
841 // server-side page filtering
842 QCUPSSupport::setPageRange(p, ranges.toString());
843 }
844
845 // page set
846 if (p->printRange() == QPrinter::AllPages || p->printRange() == QPrinter::PageRange) {
847 //If the application is selecting pages and the first page number is even then need to adjust the odd-even accordingly
848 QCUPSSupport::PageSet pageSet = qvariant_cast<QCUPSSupport::PageSet>(options.pageSetCombo->itemData(options.pageSetCombo->currentIndex()));
849 if (q->testOption(QPrintDialog::PrintPageRange)
850 && p->printRange() == QPrinter::PageRange
851 && (q->fromPage() % 2 == 0)) {
852
853 switch (pageSet) {
854 case QCUPSSupport::AllPages:
855 break;
856 case QCUPSSupport::OddPages:
857 QCUPSSupport::setPageSet(p, QCUPSSupport::EvenPages);
858 break;
859 case QCUPSSupport::EvenPages:
860 QCUPSSupport::setPageSet(p, QCUPSSupport::OddPages);
861 break;
862 }
863 } else if (pageSet != QCUPSSupport::AllPages) {
864 QCUPSSupport::setPageSet(p, pageSet);
865 }
866
867 // server-side page range, since we set the page range on the printer to 0-0/AllPages above,
868 // we need to take the values directly from the widget as q->fromPage() will return 0
869 if (!q->testOption(QPrintDialog::PrintPageRange) && options.printRange->isChecked())
870 QCUPSSupport::setPageRange(p, options.from->value(), qMax(options.from->value(), options.to->value()));
871 }
872#endif
873
874 // copies
875 p->setCopyCount(options.copies->value());
876 p->setCollateCopies(options.collate->isChecked());
877}
878
880{
881 if (printerOutputFormat == QPrinter::PdfFormat)
882 return;
883
884 options.pageSetCombo->setDisabled(checked);
885}
886
888{
889 int collapseHeight = 0;
890 Q_Q(QPrintDialog);
891 QWidget *widgetToHide = bottom;
892 if (widgetToHide->isVisible()) {
893 collapseButton->setText(QPrintDialog::tr("&Options >>"));
894 collapseHeight = widgetToHide->y() + widgetToHide->height() - (top->y() + top->height());
895 }
896 else
897 collapseButton->setText(QPrintDialog::tr("&Options <<"));
898 widgetToHide->setVisible(! widgetToHide->isVisible());
899 if (! widgetToHide->isVisible()) { // make it shrink
900 q->layout()->activate();
901 q->resize( QSize(q->width(), q->height() - collapseHeight) );
902 }
903}
904
905#if QT_CONFIG(messagebox)
906void QPrintDialogPrivate::_q_checkFields()
907{
908 Q_Q(QPrintDialog);
909 if (top->d->checkFields())
910 q->accept();
911}
912#endif // QT_CONFIG(messagebox)
913
914
916{
917 Q_Q(QPrintDialog);
918 options.gbPrintRange->setVisible(q->testOption(QPrintDialog::PrintPageRange) ||
919 q->testOption(QPrintDialog::PrintSelection) ||
920 q->testOption(QPrintDialog::PrintCurrentPage));
921
922 options.printRange->setEnabled(q->testOption(QPrintDialog::PrintPageRange));
923 options.printSelection->setVisible(q->testOption(QPrintDialog::PrintSelection));
924 options.printCurrentPage->setVisible(q->testOption(QPrintDialog::PrintCurrentPage));
925 options.collate->setVisible(q->testOption(QPrintDialog::PrintCollateCopies));
926
927#if QT_CONFIG(cups)
928 // Don't display Page Set if only Selection or Current Page are enabled
929 if (!q->testOption(QPrintDialog::PrintPageRange)
930 && (q->testOption(QPrintDialog::PrintSelection) || q->testOption(QPrintDialog::PrintCurrentPage))) {
931 options.pageSetCombo->setVisible(false);
932 options.pageSetLabel->setVisible(false);
933 } else {
934 options.pageSetCombo->setVisible(true);
935 options.pageSetLabel->setVisible(true);
936 }
937
938 if (!q->testOption(QPrintDialog::PrintPageRange)) {
939 // If we can do CUPS server side pages selection,
940 // display the page range widgets
941 options.gbPrintRange->setVisible(true);
942 options.printRange->setEnabled(true);
943 }
944#endif
945
946 switch (q->printRange()) {
947 case QPrintDialog::AllPages:
948 options.printAll->setChecked(true);
949 options.pageSetCombo->setEnabled(true);
950 break;
951 case QPrintDialog::Selection:
952 options.printSelection->setChecked(true);
953 options.pageSetCombo->setEnabled(false);
954 break;
955 case QPrintDialog::PageRange:
956 options.printRange->setChecked(true);
957 options.pageSetCombo->setEnabled(true);
958 break;
959 case QPrintDialog::CurrentPage:
960 if (q->testOption(QPrintDialog::PrintCurrentPage)) {
961 options.printCurrentPage->setChecked(true);
962 options.pageSetCombo->setEnabled(false);
963 }
964 break;
965 default:
966 break;
967 }
968 const int minPage = qMax(1, qMin(q->minPage() , q->maxPage()));
969 const int maxPage = qMax(1, q->maxPage() == INT_MAX ? 9999 : q->maxPage());
970
971 options.from->setMinimum(minPage);
972 options.to->setMinimum(minPage);
973 options.from->setMaximum(maxPage);
974 options.to->setMaximum(maxPage);
975
976 options.from->setValue(q->fromPage());
977 options.to->setValue(q->toPage());
979}
980
981void QPrintDialogPrivate::setTabs(const QList<QWidget*> &tabWidgets)
982{
983 QList<QWidget*>::ConstIterator iter = tabWidgets.begin();
984 while(iter != tabWidgets.constEnd()) {
985 QWidget *tab = *iter;
986 options.tabs->addTab(tab, tab->windowTitle());
987 ++iter;
988 }
989}
990
991////////////////////////////////////////////////////////////////////////////////
992////////////////////////////////////////////////////////////////////////////////
993
994/*
995
996 QPrintDialog
997
998 The main Print Dialog.
999
1000*/
1001
1002QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
1003 : QAbstractPrintDialog(*(new QPrintDialogPrivate), printer, parent)
1004{
1005 Q_D(QPrintDialog);
1006 d->init();
1007}
1008
1009/*!
1010 Constructs a print dialog with the given \a parent.
1011*/
1012QPrintDialog::QPrintDialog(QWidget *parent)
1013 : QAbstractPrintDialog(*(new QPrintDialogPrivate), nullptr, parent)
1014{
1015 Q_D(QPrintDialog);
1016 d->init();
1017}
1018
1019QPrintDialog::~QPrintDialog()
1020{
1021}
1022
1023void QPrintDialog::setVisible(bool visible)
1024{
1025 Q_D(QPrintDialog);
1026
1027 if (visible)
1028 d->updateWidgets();
1029
1030 QAbstractPrintDialog::setVisible(visible);
1031}
1032
1033int QPrintDialog::exec()
1034{
1035 return QAbstractPrintDialog::exec();
1036}
1037
1038void QPrintDialog::accept()
1039{
1040 Q_D(QPrintDialog);
1041#if QT_CONFIG(cups) && QT_CONFIG(messagebox)
1042 if (d->options.pagesRadioButton->isChecked()) {
1043 const QString rangesText = d->options.pagesLineEdit->text();
1044 if (rangesText.isEmpty() || QPageRanges::fromString(rangesText).isEmpty()) {
1045 QMessageBox::critical(this, tr("Invalid Pages Definition"),
1046 tr("%1 does not follow the correct syntax. Please use ',' to separate "
1047 "ranges and pages, '-' to define ranges and make sure ranges do "
1048 "not intersect with each other.").arg(rangesText),
1049 QMessageBox::Ok, QMessageBox::Ok);
1050 return;
1051 }
1052 }
1053 if (d->top->d->m_duplexPpdOption && d->top->d->m_duplexPpdOption->conflicted) {
1054 const QMessageBox::StandardButton answer = QMessageBox::warning(this, tr("Duplex Settings Conflicts"),
1055 tr("There are conflicts in duplex settings. Do you want to fix them?"),
1056 QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
1057 if (answer != QMessageBox::No)
1058 return;
1059 }
1060#endif
1061 d->setupPrinter();
1062 QDialog::accept();
1063}
1064
1065////////////////////////////////////////////////////////////////////////////////
1066////////////////////////////////////////////////////////////////////////////////
1067
1068/*
1069
1070 QUnixPrintWidget && QUnixPrintWidgetPrivate
1071
1072 The upper half of the Print Dialog containing the Printer Selection widgets
1073
1074*/
1075
1076#if defined (Q_OS_UNIX)
1077
1078/*! \internal
1079*/
1080QUnixPrintWidgetPrivate::QUnixPrintWidgetPrivate(QUnixPrintWidget *p, QPrinter *prn)
1081 : parent(p), propertiesDialog(nullptr), printer(prn),
1082#if QT_CONFIG(cups)
1083 m_duplexPpdOption(nullptr),
1084#endif
1085 optionsPane(nullptr), filePrintersAdded(false), initialPrinterIndex(0)
1086{
1087 q = nullptr;
1088 if (parent)
1089 q = qobject_cast<QPrintDialog*> (parent->parent());
1090
1091 widget.setupUi(parent);
1092
1093 int currentPrinterIndex = 0;
1094 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
1095 if (ps) {
1096 const QStringList printers = ps->availablePrintDeviceIds();
1097 const QString defaultPrinter = ps->defaultPrintDeviceId();
1098
1099 widget.printers->addItems(printers);
1100
1101 const QString selectedPrinter = prn && !prn->printerName().isEmpty() ? prn->printerName() : defaultPrinter;
1102 const int idx = printers.indexOf(selectedPrinter);
1103
1104 if (idx >= 0)
1105 currentPrinterIndex = idx;
1106 }
1107 initialPrinterIndex = currentPrinterIndex;
1108 widget.properties->setEnabled(true);
1109
1110#if QT_CONFIG(filesystemmodel) && QT_CONFIG(completer)
1111 QFileSystemModel *fsm = new QFileSystemModel(widget.filename);
1112 fsm->setRootPath(QDir::homePath());
1113 widget.filename->setCompleter(new QCompleter(fsm, widget.filename));
1114#endif
1115 if (!prn || prn->outputFormat() != QPrinter::PdfFormat)
1116 _q_printerChanged(currentPrinterIndex);
1117
1118 QObject::connect(widget.printers, SIGNAL(currentIndexChanged(int)),
1119 parent, SLOT(_q_printerChanged(int)));
1120 QObject::connect(widget.fileBrowser, SIGNAL(clicked()), parent, SLOT(_q_btnBrowseClicked()));
1121 QObject::connect(widget.properties, SIGNAL(clicked()), parent, SLOT(_q_btnPropertiesClicked()));
1122
1123 // disable features that QPrinter does not yet support.
1124 widget.preview->setVisible(false);
1125}
1126
1127void QUnixPrintWidgetPrivate::updateWidget()
1128{
1129 const bool printToFile = q == nullptr || q->testOption(QPrintDialog::PrintToFile);
1130 if (printToFile && !filePrintersAdded) {
1131 if (widget.printers->count())
1132 widget.printers->insertSeparator(widget.printers->count());
1133 widget.printers->addItem(QPrintDialog::tr("Print to File (PDF)"));
1134 filePrintersAdded = true;
1135 if (widget.printers->count() == 1)
1136 _q_printerChanged(0);
1137 }
1138 if (!printToFile && filePrintersAdded) {
1139 widget.printers->removeItem(widget.printers->count()-1);
1140 widget.printers->removeItem(widget.printers->count()-1);
1141 if (widget.printers->count())
1142 widget.printers->removeItem(widget.printers->count()-1); // remove separator
1143 filePrintersAdded = false;
1144 }
1145 if (printer && filePrintersAdded && (printer->outputFormat() != QPrinter::NativeFormat
1146 || printer->printerName().isEmpty()))
1147 {
1148 if (printer->outputFormat() == QPrinter::PdfFormat)
1149 widget.printers->setCurrentIndex(widget.printers->count() - 1);
1150 widget.filename->setEnabled(true);
1151 widget.lOutput->setEnabled(true);
1152 }
1153 if (!printToFile && printer && printer->outputFormat() == QPrinter::PdfFormat
1154 && widget.printers->count()) {
1155 {
1156 const QSignalBlocker blocker(widget.printers);
1157 widget.printers->setCurrentIndex(initialPrinterIndex);
1158 }
1159 _q_printerChanged(initialPrinterIndex);
1160 }
1161
1162 widget.filename->setVisible(printToFile);
1163 widget.lOutput->setVisible(printToFile);
1164 widget.fileBrowser->setVisible(printToFile);
1165
1166 if (q)
1167 widget.properties->setVisible(q->testOption(QAbstractPrintDialog::PrintShowPageSize));
1168}
1169
1170QUnixPrintWidgetPrivate::~QUnixPrintWidgetPrivate()
1171{
1172}
1173
1174void QUnixPrintWidgetPrivate::_q_printerChanged(int index)
1175{
1176 if (index < 0)
1177 return;
1178 const int printerCount = widget.printers->count();
1179 widget.filename->setEnabled(false);
1180 widget.lOutput->setEnabled(false);
1181
1182 // Reset properties dialog when printer is changed
1183 if (propertiesDialog){
1184 delete propertiesDialog;
1185 propertiesDialog = nullptr;
1186 }
1187
1188#if QT_CONFIG(cups)
1189 m_duplexPpdOption = nullptr;
1190#endif
1191
1192 if (filePrintersAdded) {
1193 Q_ASSERT(index != printerCount - 2); // separator
1194 if (index == printerCount - 1) { // PDF
1195 widget.location->setText(QPrintDialog::tr("Local file"));
1196 widget.type->setText(QPrintDialog::tr("Write PDF file"));
1197 widget.properties->setEnabled(true);
1198 widget.filename->setEnabled(true);
1199 QString filename = widget.filename->text();
1200 widget.filename->setText(filename);
1201 widget.lOutput->setEnabled(true);
1202 printer->setOutputFormat(QPrinter::PdfFormat);
1203 m_currentPrintDevice = QPrintDevice();
1204 if (optionsPane)
1205 optionsPane->selectPrinter(QPrinter::PdfFormat);
1206 return;
1207 }
1208 }
1209
1210 if (printer) {
1211 printer->setOutputFormat(QPrinter::NativeFormat);
1212
1213 QPlatformPrinterSupport *ps = QPlatformPrinterSupportPlugin::get();
1214 if (ps)
1215 m_currentPrintDevice = ps->createPrintDevice(widget.printers->itemText(index));
1216 else
1217 m_currentPrintDevice = QPrintDevice();
1218
1219 printer->setPrinterName(m_currentPrintDevice.id());
1220
1221 widget.location->setText(m_currentPrintDevice.location());
1222 widget.type->setText(m_currentPrintDevice.makeAndModel());
1223 if (optionsPane)
1224 optionsPane->selectPrinter(QPrinter::NativeFormat);
1225 }
1226
1227#if QT_CONFIG(cups)
1228 m_duplexPpdOption = QCUPSSupport::findPpdOption("Duplex", &m_currentPrintDevice);
1229#endif
1230}
1231
1232void QUnixPrintWidgetPrivate::setOptionsPane(QPrintDialogPrivate *pane)
1233{
1234 optionsPane = pane;
1235 if (optionsPane)
1236 optionsPane->selectPrinter(QPrinter::NativeFormat);
1237}
1238
1239void QUnixPrintWidgetPrivate::_q_btnBrowseClicked()
1240{
1241 QString filename = widget.filename->text();
1242#if QT_CONFIG(filedialog)
1243 filename = QFileDialog::getSaveFileName(parent, QPrintDialog::tr("Print To File ..."), filename,
1244 QString(), nullptr, QFileDialog::DontConfirmOverwrite);
1245#else
1246 filename.clear();
1247#endif
1248 if (!filename.isEmpty()) {
1249 widget.filename->setText(filename);
1250 widget.printers->setCurrentIndex(widget.printers->count() - 1); // the pdf one
1251 }
1252}
1253
1254#if QT_CONFIG(messagebox)
1255bool QUnixPrintWidgetPrivate::checkFields()
1256{
1257 if (widget.filename->isEnabled()) {
1258 QString file = widget.filename->text();
1259 QFile f(file);
1260 QFileInfo fi(f);
1261 bool exists = fi.exists();
1262 bool opened = false;
1263 if (exists && fi.isDir()) {
1264 QMessageBox::warning(q, q->windowTitle(),
1265 QPrintDialog::tr("%1 is a directory.\nPlease choose a different file name.").arg(file));
1266 return false;
1267 } else if ((exists && !fi.isWritable()) || !(opened = f.open(QFile::Append))) {
1268 QMessageBox::warning(q, q->windowTitle(),
1269 QPrintDialog::tr("File %1 is not writable.\nPlease choose a different file name.").arg(file));
1270 return false;
1271 } else if (exists) {
1272 int ret = QMessageBox::question(q, q->windowTitle(),
1273 QPrintDialog::tr("%1 already exists.\nDo you want to overwrite it?").arg(file),
1274 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
1275 if (ret == QMessageBox::No)
1276 return false;
1277 }
1278 if (opened) {
1279 f.close();
1280 if (!exists)
1281 f.remove();
1282 }
1283 }
1284
1285#if QT_CONFIG(cups)
1286 if (propertiesDialog) {
1287 QCUPSSupport::PagesPerSheet pagesPerSheet = qvariant_cast<QCUPSSupport::PagesPerSheet>(propertiesDialog->widget.pageSetup->m_ui.pagesPerSheetCombo
1288 ->currentData());
1289
1290 QCUPSSupport::PageSet pageSet = qvariant_cast<QCUPSSupport::PageSet>(optionsPane->options.pageSetCombo->currentData());
1291
1292
1293 if (pagesPerSheet != QCUPSSupport::OnePagePerSheet
1294 && pageSet != QCUPSSupport::AllPages) {
1295 QMessageBox::warning(q, q->windowTitle(),
1296 QPrintDialog::tr("Options 'Pages Per Sheet' and 'Page Set' cannot be used together.\nPlease turn one of those options off."));
1297 return false;
1298 }
1299 }
1300#endif
1301
1302 // Every test passed. Accept the dialog.
1303 return true;
1304}
1305#endif // QT_CONFIG(messagebox)
1306
1307void QUnixPrintWidgetPrivate::setupPrinterProperties()
1308{
1309 delete propertiesDialog;
1310
1311 QPrinter::OutputFormat outputFormat;
1312 QString printerName;
1313
1314 if (q->testOption(QPrintDialog::PrintToFile)
1315 && (widget.printers->currentIndex() == widget.printers->count() - 1)) {// PDF
1316 outputFormat = QPrinter::PdfFormat;
1317 } else {
1318 outputFormat = QPrinter::NativeFormat;
1319 printerName = widget.printers->currentText();
1320 }
1321
1322 propertiesDialog = new QPrintPropertiesDialog(q->printer(), &m_currentPrintDevice, outputFormat, printerName, q);
1323}
1324
1325#if QT_CONFIG(cups)
1326void QUnixPrintWidgetPrivate::setPpdDuplex(QPrinter::DuplexMode mode)
1327{
1328 auto values = QStringList{} << QStringLiteral("Duplex");
1329 if (mode == QPrinter::DuplexNone) values << QStringLiteral("None");
1330 else if (mode == QPrinter::DuplexLongSide) values << QStringLiteral("DuplexNoTumble");
1331 else if (mode == QPrinter::DuplexShortSide) values << QStringLiteral("DuplexTumble");
1332
1333 m_currentPrintDevice.setProperty(PDPK_PpdOption, values);
1334}
1335#endif
1336
1337void QUnixPrintWidgetPrivate::_q_btnPropertiesClicked()
1338{
1339 if (!propertiesDialog)
1340 setupPrinterProperties();
1341 propertiesDialog->exec();
1342
1343#if QT_CONFIG(cups)
1344 // update the warning icon on the duplex options if needed
1345 optionsPane->updatePpdDuplexOption(optionsPane->options.noDuplex);
1346 optionsPane->updatePpdDuplexOption(optionsPane->options.duplexLong);
1347 optionsPane->updatePpdDuplexOption(optionsPane->options.duplexShort);
1348#endif
1349}
1350
1351void QUnixPrintWidgetPrivate::setupPrinter()
1352{
1353 const int printerCount = widget.printers->count();
1354 const int index = widget.printers->currentIndex();
1355
1356 if (filePrintersAdded && index == printerCount - 1) { // PDF
1357 printer->setPrinterName(QString());
1358 Q_ASSERT(index != printerCount - 2); // separator
1359 printer->setOutputFormat(QPrinter::PdfFormat);
1360 QString path = widget.filename->text();
1361 if (QDir::isRelativePath(path))
1362 path = QDir::homePath() + QDir::separator() + path;
1363 printer->setOutputFileName(path);
1364 }
1365 else {
1366 printer->setPrinterName(widget.printers->currentText());
1367 printer->setOutputFileName(QString());
1368 }
1369
1370 if (!propertiesDialog)
1371 setupPrinterProperties();
1372
1373 propertiesDialog->setupPrinter();
1374}
1375
1376/*! \internal
1377*/
1378QUnixPrintWidget::QUnixPrintWidget(QPrinter *printer, QWidget *parent)
1379 : QWidget(parent), d(new QUnixPrintWidgetPrivate(this, printer))
1380{
1381 if (printer == nullptr)
1382 return;
1383 if (printer->outputFileName().isEmpty()) {
1384 QString home = QDir::homePath();
1385 QString cur = QDir::currentPath();
1386 if (!home.endsWith(u'/'))
1387 home += u'/';
1388 if (!cur.startsWith(home))
1389 cur = home;
1390 else if (!cur.endsWith(u'/'))
1391 cur += u'/';
1392 if (QGuiApplication::platformName() == "xcb"_L1) {
1393 if (printer->docName().isEmpty()) {
1394 cur += "print.pdf"_L1;
1395 } else {
1396#if QT_CONFIG(regularexpression)
1397 const QRegularExpression re(QStringLiteral("(.*)\\.\\S+"));
1398 auto match = re.match(printer->docName());
1399 if (match.hasMatch())
1400 cur += match.captured(1);
1401 else
1402#endif
1403 cur += printer->docName();
1404 cur += ".pdf"_L1;
1405 }
1406 } // xcb
1407
1408 d->widget.filename->setText(cur);
1409 }
1410 else
1411 d->widget.filename->setText(printer->outputFileName());
1412 const QString printerName = printer->printerName();
1413 if (!printerName.isEmpty()) {
1414 const int i = d->widget.printers->findText(printerName);
1415 if (i >= 0)
1416 d->widget.printers->setCurrentIndex(i);
1417 }
1418 // PDF printer not added to the dialog yet, we'll handle those cases in QUnixPrintWidgetPrivate::updateWidget
1419}
1420
1421/*! \internal
1422*/
1423QUnixPrintWidget::~QUnixPrintWidget()
1424{
1425 delete d;
1426}
1427
1428/*! \internal
1429
1430 Updates the printer with the states held in the QUnixPrintWidget.
1431*/
1432void QUnixPrintWidget::updatePrinter()
1433{
1434 d->setupPrinter();
1435}
1436
1437#if QT_CONFIG(cups)
1438
1439////////////////////////////////////////////////////////////////////////////////
1440////////////////////////////////////////////////////////////////////////////////
1441
1442#endif // QT_CONFIG(cups)
1443#endif // defined (Q_OS_UNIX)
1444
1445QT_END_NAMESPACE
1446
1447#include "moc_qprintdialog.cpp"
1448#include "qprintdialog_unix.moc"
QUnixPrintWidget * top
void selectPrinter(const QPrinter::OutputFormat outputFormat)
virtual void setTabs(const QList< QWidget * > &tabs) override
QDialogButtonBox * buttons
Ui::QPrintSettingsOutput options
QPrinter::OutputFormat printerOutputFormat
void showEvent(QShowEvent *event) override
\reimp
QUnixPrintWidgetPrivate(QUnixPrintWidget *q, QPrinter *prn)
void _q_printerChanged(int index)
QPrintPropertiesDialog * propertiesDialog
QUnixPrintWidget *const parent
void setOptionsPane(QPrintDialogPrivate *pane)
static void _q_pdu_initResources()