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
qohosprintdevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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
6#include <QPageSize>
7#include <QtCore/private/qohoslogger_p.h>
8#if __has_include(<BasicServicesKit/ohprint.h>)
9#include <BasicServicesKit/ohprint.h>
10#else
11#include <ohprint/print_base.h>
12#endif
13
15
16namespace
17{
18
19QPrint::DuplexMode convertOhosToQtDuplexMode(Print_DuplexMode nativeDuplexMode)
20{
21 switch (nativeDuplexMode) {
22 case DUPLEX_MODE_ONE_SIDED:
23 return QPrint::DuplexNone;
24 case DUPLEX_MODE_TWO_SIDED_LONG_EDGE:
25 return QPrint::DuplexLongSide;
26 case DUPLEX_MODE_TWO_SIDED_SHORT_EDGE:
27 return QPrint::DuplexShortSide;
28 }
29}
30
31QPrint::ColorMode convertOhosToQtColorMode(Print_ColorMode nativeColorMode)
32{
33 switch (nativeColorMode) {
34 case COLOR_MODE_MONOCHROME:
35 return QPrint::GrayScale;
36 case COLOR_MODE_COLOR:
37 return QPrint::Color;
38 case COLOR_MODE_AUTO:
39 // NOTE: Qt doesn't have a QPrint::ColorMode::Auto option. Use GrayScale for safety.
40 return QPrint::GrayScale;
41 }
42}
43
44}
45
50
53{
54 QOhosNativePrint::PrinterInfo printerInfo;
55 if (QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
56 m_name = printerInfo.name;
57 m_location = printerInfo.location;
58 m_makeAndModel = printerInfo.makeAndModel;
59 m_supportsMultipleCopies = printerInfo.capabilities.supportedCopies > 1;
60 }
61}
62
64
66{
67 QStringList printerIdList;
68 if (!QOhosNativePrint::queryPrinterIdList(printerIdList))
69 return false;
70 return printerIdList.contains(m_id);
71}
72
74{
75 QOhosNativePrint::PrinterInfo printerInfo;
76 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
77 qOhosCritical(QtForOhos) << "QOhosPrintDevice::isDefault: Failed to get printerInfo";
78 return false;
79 }
80
81 return printerInfo.isDefault;
82}
83
85{
86 if (!isValid())
87 return QPrint::Error;
88
90 if (!QOhosNativePrint::queryPrinterInfo(m_id, pi)) {
91 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
92 return QPrint::Error;
93 }
94
95 switch (pi.state) {
96 case PRINTER_IDLE:
97 return QPrint::Idle;
98
99 case PRINTER_BUSY:
100 return QPrint::Active;
101
102 case PRINTER_UNAVAILABLE:
103 return QPrint::Error;
104 }
105}
106
108{
109 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::defaultPageSize");
110
111 QOhosNativePrint::PrinterInfo printerInfo;
112 if (QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
113 for (const auto &pageSize : supportedPageSizes()) {
114 if (pageSize.key() == printerInfo.defaultValues.pageSizeId)
115 return pageSize;
116 }
117 }
118
119 // NOTE: OHOS provides just a page size ID of the default page size. It does not provide the
120 // size, which is required by Qt. Use the A4 format as a backup for default page. However, we
121 // cannot simply return QPageSize(QPageSize::A4) as a backup. The problem is that page size IDs
122 // provided by OHOS are different than what Qt uses. For example, the A4 size has ID "ISO_A4" in
123 // OHOS, while Qt uses "A4".
124 QString isoA4OhosId = QString::fromLocal8Bit("ISO_A4");
125 QString isoA4OhosName = QString::fromLocal8Bit("iso_a4_210x297mm");
126 return createPageSize(isoA4OhosId, QPageSize(QPageSize::A4).sizePoints(), isoA4OhosName);
127}
128
130{
131 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::defaultDuplexMode");
132
133 QOhosNativePrint::PrinterInfo printerInfo;
134 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
135 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
136 return QPrint::DuplexNone;
137 }
138
139 return convertOhosToQtDuplexMode(printerInfo.defaultValues.duplexMode);
140}
141
143{
144 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::defaultColorMode");
145
146 QOhosNativePrint::PrinterInfo printerInfo;
147 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
148 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
149 return QPrint::GrayScale;
150 }
151
152 return convertOhosToQtColorMode(printerInfo.defaultValues.colorMode);
153}
154
156{
157 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::defaultResolution");
158
159 QOhosNativePrint::PrinterInfo printerInfo;
160 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
161 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
162 return 0;
163 }
164
165 return printerInfo.defaultValues.resolution.verticalDpi;
166}
167
168QStringList QOhosPrintDevice::availablePrintDeviceIds()
169{
170 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::availablePrintDeviceIds");
171
172 QStringList printerIdList;
173 if (!QOhosNativePrint::queryPrinterIdList(printerIdList))
174 return {};
175
176 return printerIdList;
177}
178
179QString QOhosPrintDevice::defaultPrintDeviceId()
180{
181 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::defaultPrintDeviceId");
182
183 for (const auto &printerId : availablePrintDeviceIds()) {
184 QOhosNativePrint::PrinterInfo printerInfo;
185 if (QOhosNativePrint::queryPrinterInfo(printerId, printerInfo) && printerInfo.isDefault)
186 return printerId;
187 }
188
189 return QString();
190}
191
193{
194 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::loadPageSizes");
195
196 QOhosNativePrint::PrinterInfo printerInfo;
197 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
198 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
199 return;
200 }
201
202 qOhosDebug(QtForOhos) << "Available page sizes:";
203
204 m_pageSizes.clear();
205 for (const auto &nativePageSize : printerInfo.capabilities.supportedPageSizes) {
206 QSize size(
207 QOhosNativePrint::convertPixelsPerThousandDpiToPoints(nativePageSize.width),
208 QOhosNativePrint::convertPixelsPerThousandDpiToPoints(nativePageSize.height));
209 QPageSize pageSize = createPageSize(nativePageSize.id, size, nativePageSize.name);
210
211 qOhosDebug(QtForOhos) << "\t*" << pageSize;
212
213 m_pageSizes.append(pageSize);
214 }
215
216 m_havePageSizes = true;
217}
218
220{
221 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::loadDuplexModes");
222
223 QOhosNativePrint::PrinterInfo printerInfo;
224 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
225 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
226 return;
227 }
228
229 qOhosDebug(QtForOhos) << "Available duplex modes:";
230
231 m_duplexModes.clear();
232 for (auto nativeDuplexMode : printerInfo.capabilities.supportedDuplexModes) {
233 auto duplexMode = convertOhosToQtDuplexMode(nativeDuplexMode);
234
235 qOhosDebug(QtForOhos) << "\t*" << duplexMode;
236
237 m_duplexModes.append(duplexMode);
238 }
239
240 m_haveDuplexModes = true;
241}
242
244{
245 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::loadColorModes");
246
247 QOhosNativePrint::PrinterInfo printerInfo;
248 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
249 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
250 return;
251 }
252
253 qOhosDebug(QtForOhos) << "Printer colorModes:";
254
255 m_colorModes.clear();
256 for (auto nativeColorMode : printerInfo.capabilities.supportedColorModes) {
257 auto colorMode = convertOhosToQtColorMode(nativeColorMode);
258
259 qOhosDebug(QtForOhos) << "\t*" << colorMode;
260
261 m_colorModes.append(colorMode);
262 }
263
264 m_haveColorModes = true;
265}
266
268{
269 auto __dbg = make_QCScopedDebug("QOhosPrintDevice::loadResolutions");
270
271 QOhosNativePrint::PrinterInfo printerInfo;
272 if (!QOhosNativePrint::queryPrinterInfo(m_id, printerInfo)) {
273 qOhosCritical(QtForOhos) << "Failed to get printerInfo";
274 return;
275 }
276
277 qOhosDebug(QtForOhos) << "Printer resolutions:";
278
279 m_resolutions.clear();
280 for (const auto &nativeResolution : printerInfo.capabilities.supportedResolutions) {
281 auto resolution = nativeResolution.verticalDpi;
282
283 qOhosDebug(QtForOhos) << "\t*" << resolution;
284
285 m_resolutions.append(resolution);
286 }
287
288 m_haveResolutions = true;
289}
290
291QT_END_NAMESPACE
void loadDuplexModes() const override
void loadResolutions() const override
QPrint::ColorMode defaultColorMode() const override
QPageSize defaultPageSize() const override
QOhosPrintDevice(const QString &id)
int defaultResolution() const override
bool isValid() const override
QPrint::DeviceState state() const override
QPrint::DuplexMode defaultDuplexMode() const override
void loadColorModes() const override
void loadPageSizes() const override
virtual ~QOhosPrintDevice()
bool isDefault() const override
bool queryPrinterInfo(const QString &printerId, PrinterInfo &printerInfo)
Combined button and popup list for selecting options.
QPrint::ColorMode convertOhosToQtColorMode(Print_ColorMode nativeColorMode)
QPrint::DuplexMode convertOhosToQtDuplexMode(Print_DuplexMode nativeDuplexMode)
#define __has_include(x)