KSaneCore

interface.h
1/*
2 * SPDX-FileCopyrightText: 2007-2010 Kare Sars <kare dot sars at iki dot fi>
3 * SPDX-FileCopyrightText: 2007 Gilles Caulier <caulier dot gilles at gmail dot com>
4 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
5 * SPDX-FileCopyrightText: 2021 Alexander Stippich <a.stippich@gmx.net>
6 *
7 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 */
9
10#ifndef KSANE_COREINTERFACE_H
11#define KSANE_COREINTERFACE_H
12
13#include "ksanecore_export.h"
14
15#include <memory>
16
17#include <QImage>
18#include <QJsonObject>
19#include <QList>
20#include <QObject>
21
22#include "deviceinformation.h"
23
24namespace KSaneCore
25{
26
27class InterfacePrivate;
28class Option;
29
30/**
31 * This class provides the core interface for accessing the scan controls and options.
32 */
33class KSANECORE_EXPORT Interface : public QObject
34{
35 Q_OBJECT
36 friend class InterfacePrivate;
37
38public:
39 /**
40 * Enum defining the message level of the returned scan status string.
41 * @note There might come more enumerations in the future.
42 */
44 NoError, // The scanning has finished successfully
45 ErrorGeneral, // The error string should contain an error message.
46 Information // There is some information to the user.
47 };
48
49 /**
50 * Enum determining whether the scanner opened correctly.
51 */
53 OpeningSucceeded, // scanner opened successfully
54 OpeningDenied, // access was denied,
55 OpeningFailed, // opening the scanner failed for unknown reasons
56 };
57
58 /**
59 * This enumeration is used to obtain a specific option with getOption(KSaneOptionName).
60 * Depending on the backend, not all options are available, nor this list is complete.
61 * For the remaining options, getOptionsList() must be used.
62 */
64 SourceOption,
65 ScanModeOption,
66 BitDepthOption,
67 ResolutionOption,
68 TopLeftXOption,
69 TopLeftYOption,
70 BottomRightXOption,
71 BottomRightYOption,
72 FilmTypeOption,
73 NegativeOption,
74 InvertColorOption,
75 PageSizeOption,
76 ThresholdOption,
77 XResolutionOption,
78 YResolutionOption,
79 PreviewOption,
80 WaitForButtonOption,
81 BrightnessOption,
82 ContrastOption,
83 GammaOption,
84 GammaRedOption,
85 GammaGreenOption,
86 GammaBlueOption,
87 BlackLevelOption,
88 WhiteLevelOption,
89 BatchModeOption,
90 BatchDelayOption,
91 };
92
93 /**
94 * This enumeration is used to filter the devices found by SANE.
95 * Sometimes, webcam may show up as scanner device and some
96 * more special scanner are also classified as cameras.
97 */
98 enum DeviceType { AllDevices, NoCameraAndVirtualDevices };
99
100 /**
101 * This constructor initializes the private class variables.
102 */
103 explicit Interface(QObject *parent = nullptr);
104
105 /**
106 * Standard destructor.
107 */
108 ~Interface() override;
109
110 /**
111 * Get the list of available scanning devices. Connect to availableDevices()
112 * which is fired once these devices are known.
113 * @note While the querying is done in a separate thread and thus not blocking
114 * the application, the application must ensure that no other action accessing
115 * the scanner device (settings options etc.) is performed during this period.
116 * @return whether the devices list are being reloaded or not
117 * @param type specify whether only specific device types shall be queried
118 */
119 bool reloadDevicesList(DeviceType type = AllDevices);
120
121 /**
122 * This method opens the specified scanner device and adds the scan options to the
123 * options list.
124 * @param deviceName is the SANE device name for the scanner to open.
125 * @return the status of the opening action.
126 */
127 OpenStatus openDevice(const QString &deviceName);
128
129 /**
130 * This method opens the specified scanner device with a specified username and password.
131 * Adds the scan options to the options list.
132 * @param deviceName is the SANE device name for the scanner to open.
133 * @param userName the username required to open for the scanner.
134 * @param password the password required to open for the scanner.
135 * @return the status of the opening action.
136 */
137 OpenStatus openRestrictedDevice(const QString &deviceName, const QString &userName, const QString &password);
138
139 /**
140 * This method closes the currently open scanner device.
141 * @return 'true' if all goes well and 'false' if no device is open.
142 */
143 bool closeDevice();
144
145 /**
146 * This method returns the internal device name of the currently opened scanner.
147 * @note Due to limitations of the SANE API, this will function will return an empty string
148 * if reloadDevicesList() has not been called before.
149 */
150 QString deviceName() const;
151
152 /**
153 * This method returns the vendor name of the currently opened scanner.
154 * @note Due to limitations of the SANE API, this will function will return an empty string
155 * if reloadDevicesList() has not been called before.
156 */
157 QString deviceVendor() const;
158
159 /**
160 * This method returns the model of the currently opened scanner.
161 * @note Due to limitations of the SANE API, this will function will return an empty string
162 * if reloadDevicesList() has not been called before.
163 */
164 QString deviceModel() const;
165
166 /**
167 * This function is used to set the preferred resolution for scanning the preview.
168 * @param dpi is the wanted scan resolution for the preview
169 * @note if the set value is not supported, the closest one is used
170 * @note setting the value 0 means that the default calculated value should be used.
171 * @since 25.04
172 */
173 void setPreviewResolution(float dpi);
174
175 /**
176 * This function returns all available options when a device is opened.
177 * @return list containing pointers to all KSaneOptions provided by the backend.
178 * Becomes invalid when closing a device.
179 * The pointers must not be deleted by the client.
180 */
181 QList<Option *> getOptionsList();
182
183 /**
184 * This function returns a specific option.
185 * @param optionEnum the enum specifying the option.
186 * @return pointer to the KSaneOption. Returns a nullptr in case the options
187 * is not available for the currently opened device.
188 */
189 Option *getOption(OptionName optionEnum);
190
191 /**
192 * This function returns a specific option.
193 * @param optionName the internal name of the option defined by SANE.
194 * @return pointer to the KSaneOption. Returns a nullptr in case the options
195 * is not available for the currently opened device.
196 */
197 Option *getOption(const QString &optionName);
198
199 /**
200 * This method reads the available parameters and their values and
201 * returns them in a QMap (Name, value)
202 * @return map with the parameter names and their values.
203 */
204 QMap<QString, QString> getOptionsMap();
205
206 /**
207 * This method can be used to write many parameter values at once.
208 * @param options a QMap with the parameter names and values.
209 * @return This function returns the number of successful writes
210 * or -1 if scanning is in progress.
211 */
212 int setOptionsMap(const QMap<QString, QString> &options);
213
214 /**
215 * Gives direct access to the QImage that is used to store the image
216 * data retrieved from the scanner.
217 * Useful to display an in-progress image while scanning.
218 * When accessing the direct image pointer during a scan, the image
219 * must be locked before accessing the image and unlocked afterwards
220 * using the lockScanImage() and unlockScanImage() functions.
221 * @return pointer for direct access of the QImage data.
222 */
223 QImage *scanImage() const;
224
225 /**
226 * Locks the mutex protecting the QImage pointer of scanImage() from
227 * concurrent access during scanning.
228 */
229 void lockScanImage();
230
231 /**
232 * Unlocks the mutex protecting the QImage pointer of scanImage() from
233 * concurrent access during scanning. The scanning progress will blocked
234 * when lockScanImage() is called until unlockScanImage() is called.
235 */
236 void unlockScanImage();
237
238 /**
239 * Returns a JSON object containing the device name, model and vendor.
240 * A scanner device must have been opened before, returns an empty
241 * object otherwise. Mainly intended for debugging purposes and
242 * identifying issues with different scanner hardware.
243 * @return JSON object holding the data
244 */
245 QJsonObject scannerDeviceToJson();
246
247 /**
248 * Returns a JSON Object with all available data for all scanner options.
249 * A scanner device must have been opened before, returns an empty
250 * object otherwise. Mainly intended for debugging purposes and
251 * identifying issues with different scanner hardware.
252 * @return JSON object holding the data
253 */
254 QJsonObject scannerOptionsToJson();
255
256public Q_SLOTS:
257 /**
258 * This method is used to cancel a scan or prevent an automatic new scan.
259 */
260 void stopScan();
261
262 /**
263 * This method is used to start a scan.
264 * @note KSaneCore::Interface may return one or more images as a result of one invocation of this slot.
265 * If no more images are wanted stopScan() should be called in the slot handling the
266 * imageReady signal.
267 * @note While scanning is done in a separate thread and thus not blocking
268 * the application, the application must ensure that no other action accessing
269 * the scanner device (settings options etc.) is performed during this period besides accessing
270 * *scanImage() that must be guarded lockScanImage() and unlockScanImage() before and after the access.
271 */
272 void startScan();
273
274 /**
275 * This method is used to start a preview scan.
276 * @note KSaneCore::Interface may return one image as a result of one invocation of this slot.
277 * The image always covers the full scan area of the device with a reduced resolution.
278 * A preferred preview DPI can be set via setPreviewResolution(), but will be adjusted to capabilities
279 * of the device.
280 * @note While scanning is done in a separate thread and thus not blocking
281 * the application, the application must ensure that no other action accessing
282 * the scanner device (settings options etc.) is performed during this period besides accessing
283 * *scanImage() that must be guarded lockScanImage() and unlockScanImage() before and after the access.
284 * @since 25.04
285 */
286 void startPreviewScan();
287
288Q_SIGNALS:
289 /**
290 * This signal is emitted when a final scan is ready.
291 * @param scannedImage is the QImage containing the scanned image data.
292 * @since 25.04
293 */
294 void scannedImageReady(const QImage &scannedImage);
295
296 /**
297 * This signal is emitted when a preview scan is ready.
298 * @param scannedImage is the QImage containing the scanned image data of the preview.
299 * @since 25.04
300 */
301 void previewImageReady(const QImage &previewImage);
302
303 /**
304 * This signal is emitted when the scanning has ended.
305 * @param status contains a ScanStatus status code.
306 * @param strStatus If an error has occurred this string will contain an error message.
307 * otherwise the string is empty.
308 */
310
311 /**
312 * This signal is emitted when the scanning for a preview has ended.
313 * @param status contains a ScanStatus status code.
314 * @param strStatus If an error has occurred this string will contain an error message.
315 * otherwise the string is empty.
316 * @since 25.04
317 */
319
320 /**
321 * This signal is emitted when the user is to be notified about something.
322 * @param type contains a ScanStatus code to identify the type of message (error/info/...).
323 * @param strStatus If an error has occurred this string will contain an error message.
324 * otherwise the string is empty.
325 */
327
328 /**
329 * This signal is emitted for progress information during a scan.
330 * @param percent is the percentage of the scan progress (0-100).
331 * A negative value indicates that a scan is being prepared.
332 */
333 void scanProgress(int percent);
334
335 /**
336 * This signal is emitted for progress information during a preview scan.
337 * @param percent is the percentage of the preview scan progress (0-100).
338 * A negative value indicates that a preview scan is being prepared.
339 * @since 25.04
340 */
341 void previewProgress(int percent);
342
343 /**
344 * This signal is emitted every time the device list is updated or
345 * after reloadDevicesList() is called.
346 * @param deviceList is a QList of KSane::DeviceInformation that contain the
347 * device name, model, vendor and type of the attached scanners.
348 * @note The list is only a snapshot of the current available devices. Devices
349 * might be added or removed/opened after the signal is emitted.
350 */
352
353 /**
354 * This signal is emitted when a hardware button is pressed.
355 * @param optionName is the untranslated technical name of the sane-option.
356 * @param optionLabel is the translated user visible label of the sane-option.
357 * @param pressed indicates if the value is true or false.
358 * @note The SANE standard does not specify hardware buttons and their behaviors,
359 * so this signal is emitted for sane-options that behave like hardware buttons.
360 * That is the sane-options are read-only and type boolean. The naming of hardware
361 * buttons also differ from backend to backend.
362 */
363 void buttonPressed(const QString &optionName, const QString &optionLabel, bool pressed);
364
365 /**
366 * This signal is emitted for the count down when in batch mode.
367 * @param remainingSeconds are the remaining seconds until the next scan starts.
368 */
369 void batchModeCountDown(int remainingSeconds);
370
371private:
372 std::unique_ptr<InterfacePrivate> d;
373};
374
375} // namespace KSaneCore
376
377#endif // KSANE_COREINTERFACE_H
This class provides the core interface for accessing the scan controls and options.
Definition interface.h:34
void previewImageReady(const QImage &previewImage)
This signal is emitted when a preview scan is ready.
OptionName
This enumeration is used to obtain a specific option with getOption(KSaneOptionName).
Definition interface.h:63
void scannedImageReady(const QImage &scannedImage)
This signal is emitted when a final scan is ready.
void previewScanFinished(KSaneCore::Interface::ScanStatus status, const QString &strStatus)
This signal is emitted when the scanning for a preview has ended.
ScanStatus
Enum defining the message level of the returned scan status string.
Definition interface.h:43
void availableDevices(const QList< DeviceInformation * > &deviceList)
This signal is emitted every time the device list is updated or after reloadDevicesList() is called.
void scanFinished(KSaneCore::Interface::ScanStatus status, const QString &strStatus)
This signal is emitted when the scanning has ended.
OpenStatus
Enum determining whether the scanner opened correctly.
Definition interface.h:52
void batchModeCountDown(int remainingSeconds)
This signal is emitted for the count down when in batch mode.
void previewProgress(int percent)
This signal is emitted for progress information during a preview scan.
void userMessage(KSaneCore::Interface::ScanStatus status, const QString &strStatus)
This signal is emitted when the user is to be notified about something.
DeviceType
This enumeration is used to filter the devices found by SANE.
Definition interface.h:98
void scanProgress(int percent)
This signal is emitted for progress information during a scan.
void buttonPressed(const QString &optionName, const QString &optionLabel, bool pressed)
This signal is emitted when a hardware button is pressed.
A wrapper class providing access to the internal KSaneBaseOption to access all options provided by KS...
Definition option.h:29
Q_SCRIPTABLE CaptureState status()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:46:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.