File indexing completed on 2024-04-28 04:32:22

0001 /*
0002  * SPDX-FileCopyrightText: 2007-2010 Kare Sars <kare dot sars at iki dot fi>
0003  * SPDX-FileCopyrightText: 2007 Gilles Caulier <caulier dot gilles at gmail dot com>
0004  * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
0005  * SPDX-FileCopyrightText: 2021 Alexander Stippich <a.stippich@gmx.net>
0006  *
0007  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0008  */
0009 
0010 #ifndef KSANE_H
0011 #define KSANE_H
0012 
0013 #include "ksane_export.h"
0014 
0015 #include <QWidget>
0016 
0017 namespace KSaneIface
0018 {
0019 
0020 class KSaneWidgetPrivate;
0021 
0022 /**
0023  * This class provides the widget containing the scan options and the preview.
0024  * @author Kare Sars <kare.sars@iki.fi>
0025  */
0026 class KSANE_EXPORT KSaneWidget : public QWidget
0027 {
0028     Q_OBJECT
0029     friend class KSaneWidgetPrivate;
0030 
0031 public:
0032 
0033     /** @note There might come more enumerations in the future. */
0034     typedef enum {
0035         NoError,            /**< The scanning was finished successfully.*/
0036         ErrorCannotSegment, /**< If this error status is returned libksane can not segment the
0037                              * returned data. Scanning without segmentation should work.
0038                              * @note segmentation is not implemented yet.*/
0039         ErrorGeneral,        /**< The error string should contain an error message. */
0040         Information          /**< There is some information to the user. */
0041     } ScanStatus;
0042 
0043     struct DeviceInfo {
0044         QString name;     /* unique device name */
0045         QString vendor;   /* device vendor string */
0046         QString model;    /* device model name */
0047         QString type;     /* device type (e.g., "flatbed scanner") */
0048     };
0049 
0050     /** This constructor initializes the private class variables, but the widget is left empty.
0051      * The options and the preview are added with the call to openDevice(). */
0052     KSaneWidget(QWidget *parent = nullptr);
0053 
0054     /** Standard destructor */
0055     ~KSaneWidget() override;
0056 
0057     /** This helper method displays a dialog for selecting a scanner. The libsane
0058      * device name of the selected scanner device is returned. */
0059     QString selectDevice(QWidget *parent = nullptr);
0060 
0061     /** This method opens the specified scanner device and adds the scan options to the
0062      * KSane widget.
0063      * @param device_name is the libsane device name for the scanner to open.
0064      * @return 'true' if all goes well and 'false' if the specified scanner can not be opened. */
0065     bool openDevice(const QString &device_name);
0066 
0067     /** This method closes the currently open scanner device.
0068     * @return 'true' if all goes well and 'false' if no device is open. */
0069     bool closeDevice();
0070 
0071     /** This method returns the internal device name of the currently opened scanner. */
0072     QString deviceName() const;
0073 
0074     /** This method returns the vendor name of the currently opened scanner. */
0075     QString deviceVendor() const;
0076 
0077     /** This method returns the model of the currently opened scanner. */
0078     QString deviceModel() const;
0079 
0080     /** This method returns the scan area's width in mm
0081     * @return Width of the scannable area in mm */
0082     float scanAreaWidth();
0083 
0084     /** This method returns the scan area's height in mm
0085     * @return Height of the scannable area in mm */
0086     float scanAreaHeight();
0087 
0088     /** This method sets the selection according to the given points
0089      * @note The points are defined with respect to the scan areas top-left corner in mm
0090      * @param topLeft Upper left corner of the selection (in mm)
0091      * @param bottomRight Lower right corner of the selection (in mm) */
0092     void setSelection(QPointF topLeft, QPointF bottomRight);
0093 
0094     /** This function is used to set the preferred resolution for scanning the preview.
0095      * @param dpi is the wanted scan resolution for the preview
0096      * @note if the set value is not supported, the cloasest one is used
0097      * @note setting the value 0 means that the default calculated value should be used */
0098     void setPreviewResolution(float dpi);
0099 
0100     /** This method reads the available parameters and their values and
0101      * returns them in a QMap (Name, value)
0102      * @param opts is a QMap with the parameter names and values. */
0103     void getOptionValues(QMap <QString, QString> &options);
0104 
0105     /** This method can be used to write many parameter values at once.
0106      * @param opts is a QMap with the parameter names and values.
0107      * @return This function returns the number of successful writes
0108      * or -1 if scanning is in progress. */
0109     int setOptionValues(const QMap <QString, QString> &options);
0110 
0111     /** This function reads one parameter value into a string.
0112      * @param optname is the name of the parameter to read.
0113      * @param value is the string representation of the value.
0114      * @return this function returns true if the read was successful. */
0115     bool getOptionValue(const QString &option, QString &value);
0116 
0117     /** This function writes one parameter value into a string.
0118      * @param optname is the name of the parameter to write.
0119      * @param value is the string representation of the value.
0120      * @return this function returns true if the write was successful and
0121      * false if it was unsuccessful or scanning is in progress. */
0122     bool setOptionValue(const QString &option, const QString &value);
0123 
0124     /** This function can be used to enable/disable automatic selections on previews.
0125     * The default state is enabled.
0126     * @param enable specifies if the auto selection should be turned on or off. */
0127     void enableAutoSelect(bool enable);
0128 
0129 public Q_SLOTS:
0130     /** This method can be used to cancel a scan or prevent an automatic new scan. */
0131     void cancelScan();
0132 
0133     /** This method can be used to start a scan (if no GUI is needed).
0134     * @note libksane may return one or more images as a result of one invocation of this slot.
0135     * If no more images are wanted cancelScan should be called in the slot handling the
0136     * imageReady signal. */
0137     void startScan();
0138 
0139     /** This method can be used to start a preview scan. */
0140     void startPreviewScan();
0141 
0142 Q_SIGNALS:
0143     /**
0144      * This signal is emitted when a final scan is ready.
0145      * @param scannedImage is the QImage containing the scanned image data. */
0146     void scannedImageReady(const QImage &scannedImage);
0147 
0148     /**
0149      * This signal is emitted when the scanning has ended.
0150      * @param status contains a ScanStatus status code.
0151      * @param strStatus If an error has occurred this string will contain an error message.
0152      * otherwise the string is empty. */
0153     void scanDone(int status, const QString &strStatus);
0154 
0155     /**
0156      * This signal is emitted when the user is to be notified about something.
0157      * @note If no slot is connected to this signal the message will be displayed in a KMessageBox.
0158      * @param type contains a ScanStatus code to identify the type of message (error/info/...).
0159      * @param strStatus If an error has occurred this string will contain an error message.
0160      * otherwise the string is empty. */
0161     void userMessage(int type, const QString &strStatus);
0162 
0163     /**
0164      * This Signal is emitted for progress information during a scan.
0165      * The GUI already has a progress bar, but if the GUI is hidden,
0166      * this can be used to display a progress bar.
0167      * @param percent is the percentage of the scan progress (0-100). */
0168     void scanProgress(int percent);
0169 
0170     /**
0171      * This signal is emitted every time the device list is updated or
0172      * after initGetDeviceList() is called.
0173      * @param deviceList is a QList of KSaneWidget::DeviceInfo that contain the
0174      * device name, model, vendor and type of the attached scanners.
0175      * @note The list is only a snapshot of the current available devices. Devices
0176      * might be added or removed/opened after the signal is emitted.
0177      */
0178     void availableDevices(const QList<KSaneWidget::DeviceInfo> &deviceList);
0179 
0180     /**
0181      * This Signal is emitted when a hardware button is pressed.
0182      * @param optionName is the untranslated technical name of the sane-option.
0183      * @param optionLabel is the translated user visible label of the sane-option.
0184      * @param pressed indicates if the value is true or false.
0185      * @note The SANE standard does not specify hardware buttons and their behaviors,
0186      * so this signal is emitted for sane-options that behave like hardware buttons.
0187      * That is the sane-options are read-only and type boolean. The naming of hardware
0188      * buttons also differ from backend to backend.
0189      */
0190     void buttonPressed(const QString &optionName, const QString &optionLabel, bool pressed);
0191 
0192     /**
0193      * This signal is not emitted anymore.
0194      */
0195     void openedDeviceInfoUpdated(const QString &deviceName, const QString &deivceVendor, const QString &deviceModel);
0196 
0197 private:
0198     KSaneWidgetPrivate *const d;
0199 };
0200 
0201 }  // NameSpace KSaneIface
0202 
0203 #endif // SANE_WIDGET_H