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 | |
4 | |
5 | #include "qcupsjobwidget_p.h" |
6 | |
7 | #include <QCheckBox> |
8 | #include <QDateTime> |
9 | #include <QFontDatabase> |
10 | #include <QLabel> |
11 | #include <QLayout> |
12 | #include <QTime> |
13 | #include <QTableWidget> |
14 | #include <QTableWidgetItem> |
15 | #include <QHeaderView> |
16 | #include <QPrinter> |
17 | #include <QPrintEngine> |
18 | |
19 | #include <kernel/qprintdevice_p.h> |
20 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | /*! |
24 | \internal |
25 | \class QCupsJobWidget |
26 | |
27 | A widget to add to QPrintDialog to enable extra CUPS options |
28 | such as Job Scheduling, Job Priority or Job Billing |
29 | \ingroup printing |
30 | \inmodule QtPrintSupport |
31 | */ |
32 | |
33 | QCupsJobWidget::QCupsJobWidget(QPrinter *printer, QPrintDevice *printDevice, QWidget *parent) |
34 | : QWidget(parent), |
35 | m_printer(printer), |
36 | m_printDevice(printDevice) |
37 | { |
38 | m_ui.setupUi(this); |
39 | //set all the default values |
40 | initJobHold(); |
41 | initJobBilling(); |
42 | initJobPriority(); |
43 | initBannerPages(); |
44 | |
45 | updateSavedValues(); |
46 | } |
47 | |
48 | QCupsJobWidget::~QCupsJobWidget() |
49 | { |
50 | } |
51 | |
52 | void QCupsJobWidget::setupPrinter() |
53 | { |
54 | QCUPSSupport::setJobHold(printer: m_printer, jobHold: jobHold(), holdUntilTime: jobHoldTime()); |
55 | QCUPSSupport::setJobBilling(printer: m_printer, jobBilling: jobBilling()); |
56 | QCUPSSupport::setJobPriority(printer: m_printer, priority: jobPriority()); |
57 | QCUPSSupport::setBannerPages(printer: m_printer, startBannerPage: startBannerPage(), endBannerPage: endBannerPage()); |
58 | } |
59 | |
60 | void QCupsJobWidget::updateSavedValues() |
61 | { |
62 | m_savedJobHoldWithTime = { jobHold(), jobHoldTime() }; |
63 | m_savedJobBilling = jobBilling(); |
64 | m_savedPriority = jobPriority(); |
65 | m_savedJobSheets = { startBannerPage(), endBannerPage() }; |
66 | } |
67 | |
68 | void QCupsJobWidget::revertToSavedValues() |
69 | { |
70 | setJobHold(jobHold: m_savedJobHoldWithTime.jobHold, holdUntilTime: m_savedJobHoldWithTime.time); |
71 | toggleJobHoldTime(); |
72 | |
73 | setJobBilling(m_savedJobBilling); |
74 | |
75 | setJobPriority(m_savedPriority); |
76 | |
77 | setStartBannerPage(m_savedJobSheets.startBannerPage); |
78 | setEndBannerPage(m_savedJobSheets.endBannerPage); |
79 | } |
80 | |
81 | void QCupsJobWidget::initJobHold() |
82 | { |
83 | m_ui.jobHoldComboBox->addItem(tr("Print Immediately" ), QVariant::fromValue(QCUPSSupport::NoHold)); |
84 | m_ui.jobHoldComboBox->addItem(tr("Hold Indefinitely" ), QVariant::fromValue(QCUPSSupport::Indefinite)); |
85 | m_ui.jobHoldComboBox->addItem(tr("Day (06:00 to 17:59)" ), QVariant::fromValue(QCUPSSupport::DayTime)); |
86 | m_ui.jobHoldComboBox->addItem(tr("Night (18:00 to 05:59)" ), QVariant::fromValue(QCUPSSupport::Night)); |
87 | m_ui.jobHoldComboBox->addItem(tr("Second Shift (16:00 to 23:59)" ), QVariant::fromValue(QCUPSSupport::SecondShift)); |
88 | m_ui.jobHoldComboBox->addItem(tr("Third Shift (00:00 to 07:59)" ), QVariant::fromValue(QCUPSSupport::ThirdShift)); |
89 | m_ui.jobHoldComboBox->addItem(tr("Weekend (Saturday to Sunday)" ), QVariant::fromValue(QCUPSSupport::Weekend)); |
90 | m_ui.jobHoldComboBox->addItem(tr("Specific Time" ), QVariant::fromValue(QCUPSSupport::SpecificTime)); |
91 | |
92 | connect(m_ui.jobHoldComboBox, &QComboBox::currentIndexChanged, this, &QCupsJobWidget::toggleJobHoldTime); |
93 | |
94 | QCUPSSupport::JobHoldUntilWithTime jobHoldWithTime; |
95 | |
96 | if (m_printDevice) { |
97 | const QString jobHoldUntilString = m_printDevice->property(PDPK_CupsJobHoldUntil).toString(); |
98 | jobHoldWithTime = QCUPSSupport::parseJobHoldUntil(jobHoldUntil: jobHoldUntilString); |
99 | } |
100 | |
101 | setJobHold(jobHold: jobHoldWithTime.jobHold, holdUntilTime: jobHoldWithTime.time); |
102 | toggleJobHoldTime(); |
103 | } |
104 | |
105 | void QCupsJobWidget::setJobHold(QCUPSSupport::JobHoldUntil jobHold, QTime holdUntilTime) |
106 | { |
107 | if (jobHold == QCUPSSupport::SpecificTime && holdUntilTime.isNull()) { |
108 | jobHold = QCUPSSupport::NoHold; |
109 | toggleJobHoldTime(); |
110 | } |
111 | m_ui.jobHoldComboBox->setCurrentIndex(m_ui.jobHoldComboBox->findData(QVariant::fromValue(jobHold))); |
112 | m_ui.jobHoldTimeEdit->setTime(holdUntilTime); |
113 | } |
114 | |
115 | QCUPSSupport::JobHoldUntil QCupsJobWidget::jobHold() const |
116 | { |
117 | return qvariant_cast<QCUPSSupport::JobHoldUntil>(m_ui.jobHoldComboBox->itemData(m_ui.jobHoldComboBox->currentIndex())); |
118 | } |
119 | |
120 | void QCupsJobWidget::toggleJobHoldTime() |
121 | { |
122 | if (jobHold() == QCUPSSupport::SpecificTime) |
123 | m_ui.jobHoldTimeEdit->setEnabled(true); |
124 | else |
125 | m_ui.jobHoldTimeEdit->setEnabled(false); |
126 | } |
127 | |
128 | QTime QCupsJobWidget::jobHoldTime() const |
129 | { |
130 | return m_ui.jobHoldTimeEdit->time(); |
131 | } |
132 | |
133 | void QCupsJobWidget::initJobBilling() |
134 | { |
135 | QString jobBilling; |
136 | if (m_printDevice) |
137 | jobBilling = m_printDevice->property(PDPK_CupsJobBilling).toString(); |
138 | |
139 | setJobBilling(jobBilling); |
140 | } |
141 | |
142 | void QCupsJobWidget::setJobBilling(const QString &jobBilling) |
143 | { |
144 | m_ui.jobBillingLineEdit->setText(jobBilling); |
145 | } |
146 | |
147 | QString QCupsJobWidget::jobBilling() const |
148 | { |
149 | return m_ui.jobBillingLineEdit->text(); |
150 | } |
151 | |
152 | void QCupsJobWidget::initJobPriority() |
153 | { |
154 | int priority = -1; |
155 | if (m_printDevice) { |
156 | bool ok; |
157 | priority = m_printDevice->property(PDPK_CupsJobPriority).toInt(ok: &ok); |
158 | if (!ok) |
159 | priority = -1; |
160 | } |
161 | |
162 | if (priority < 0 || priority > 100) |
163 | priority = 50; |
164 | |
165 | setJobPriority(priority); |
166 | } |
167 | |
168 | void QCupsJobWidget::setJobPriority(int jobPriority) |
169 | { |
170 | m_ui.jobPrioritySpinBox->setValue(jobPriority); |
171 | } |
172 | |
173 | int QCupsJobWidget::jobPriority() const |
174 | { |
175 | return m_ui.jobPrioritySpinBox->value(); |
176 | } |
177 | |
178 | void QCupsJobWidget::initBannerPages() |
179 | { |
180 | m_ui.startBannerPageCombo->addItem(tr("None" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::NoBanner)); |
181 | m_ui.startBannerPageCombo->addItem(tr("Standard" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Standard)); |
182 | m_ui.startBannerPageCombo->addItem(tr("Unclassified" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Unclassified)); |
183 | m_ui.startBannerPageCombo->addItem(tr("Confidential" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Confidential)); |
184 | m_ui.startBannerPageCombo->addItem(tr("Classified" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Classified)); |
185 | m_ui.startBannerPageCombo->addItem(tr("Secret" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Secret)); |
186 | m_ui.startBannerPageCombo->addItem(tr("Top Secret" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::TopSecret)); |
187 | |
188 | m_ui.endBannerPageCombo->addItem(tr("None" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::NoBanner)); |
189 | m_ui.endBannerPageCombo->addItem(tr("Standard" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Standard)); |
190 | m_ui.endBannerPageCombo->addItem(tr("Unclassified" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Unclassified)); |
191 | m_ui.endBannerPageCombo->addItem(tr("Confidential" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Confidential)); |
192 | m_ui.endBannerPageCombo->addItem(tr("Classified" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Classified)); |
193 | m_ui.endBannerPageCombo->addItem(tr("Secret" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::Secret)); |
194 | m_ui.endBannerPageCombo->addItem(tr("Top Secret" , "CUPS Banner page" ), QVariant::fromValue(QCUPSSupport::TopSecret)); |
195 | |
196 | QCUPSSupport::JobSheets jobSheets; |
197 | |
198 | if (m_printDevice) { |
199 | const QString jobSheetsString = m_printDevice->property(PDPK_CupsJobSheets).toString(); |
200 | jobSheets = QCUPSSupport::parseJobSheets(jobSheets: jobSheetsString); |
201 | } |
202 | |
203 | setStartBannerPage(jobSheets.startBannerPage); |
204 | setEndBannerPage(jobSheets.endBannerPage); |
205 | } |
206 | |
207 | void QCupsJobWidget::setStartBannerPage(const QCUPSSupport::BannerPage bannerPage) |
208 | { |
209 | m_ui.startBannerPageCombo->setCurrentIndex(m_ui.startBannerPageCombo->findData(QVariant::fromValue(bannerPage))); |
210 | } |
211 | |
212 | QCUPSSupport::BannerPage QCupsJobWidget::startBannerPage() const |
213 | { |
214 | return qvariant_cast<QCUPSSupport::BannerPage>(m_ui.startBannerPageCombo->itemData(m_ui.startBannerPageCombo->currentIndex())); |
215 | } |
216 | |
217 | void QCupsJobWidget::setEndBannerPage(const QCUPSSupport::BannerPage bannerPage) |
218 | { |
219 | m_ui.endBannerPageCombo->setCurrentIndex(m_ui.endBannerPageCombo->findData(QVariant::fromValue(bannerPage))); |
220 | } |
221 | |
222 | QCUPSSupport::BannerPage QCupsJobWidget::endBannerPage() const |
223 | { |
224 | return qvariant_cast<QCUPSSupport::BannerPage>(m_ui.endBannerPageCombo->itemData(m_ui.endBannerPageCombo->currentIndex())); |
225 | } |
226 | |
227 | QT_END_NAMESPACE |
228 | |
229 | #include "moc_qcupsjobwidget_p.cpp" |
230 | |