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

0001 /*
0002  * SPDX-FileCopyrightText: 2008 Kare Sars <kare dot sars at iki dot fi>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #ifndef KSANE_VIEWER_H
0008 #define KSANE_VIEWER_H
0009 
0010 #include <QGraphicsView>
0011 #include <QWheelEvent>
0012 
0013 namespace KSaneIface
0014 {
0015 
0016 /*
0017  * Preview image viewer that can handle a selection.
0018  */
0019 class KSaneViewer : public QGraphicsView
0020 {
0021     Q_OBJECT
0022 public:
0023     explicit KSaneViewer(QImage *img, QWidget *parent = nullptr);
0024     ~KSaneViewer() override;
0025 
0026     void setQImage(QImage *img);
0027     void updateImage();
0028     /** Find selections in the picture
0029     * \param area this parameter determine the area of the reduced sized image. */
0030     void findSelections(float area = 10000.0);
0031 
0032     QSize sizeHint() const override;
0033 
0034     int currentImageHeight() const;
0035     int currentImageWidth() const;
0036 
0037 public Q_SLOTS:
0038 
0039     void setTLX(float ratio);
0040     void setTLY(float ratio);
0041     void setBRX(float ratio);
0042     void setBRY(float ratio);
0043 
0044     /** This function is used to set a selection without the user setting it.
0045     * \note all parameters must be in the range 0.0 -> 1.0.
0046     * \param tl_x is the x coordinate of the top left corner 0=0 1=image with.
0047     * \param tl_y is the y coordinate of the top left corner 0=0 1=image height.
0048     * \param br_x is the x coordinate of the bottom right corner 0=0 1=image with.
0049     * \param br_y is the y coordinate of the bottom right corner 0=0 1=image height. */
0050     void setSelection(float tl_x, float tl_y, float br_x, float br_y);
0051     void clearActiveSelection();
0052     void clearSavedSelections();
0053     void clearSelections();
0054 
0055     /** This function is used to darken everything except what is inside the given area.
0056     * \note all parameters must be in the range 0.0 -> 1.0.
0057     * \param tl_x is the x coordinate of the top left corner 0=0 1=image with.
0058     * \param tl_y is the y coordinate of the top left corner 0=0 1=image height.
0059     * \param br_x is the x coordinate of the bottom right corner 0=0 1=image with.
0060     * \param br_y is the y coordinate of the bottom right corner 0=0 1=image height. */
0061     void setHighlightArea(float tl_x, float tl_y, float br_x, float br_y);
0062 
0063     /** This function sets the percentage of the highlighted area that is visible. The rest is hidden.
0064     * \param percentage is the percentage of the highlighted area that is shown.
0065     * \param hideColor is the color to use to hide the highlighted area of the image.*/
0066     void setHighlightShown(int percentage, QColor hideColor = Qt::white);
0067 
0068     /** This function removes the highlight area. */
0069     void clearHighlight();
0070 
0071     void zoomIn();
0072     void zoomOut();
0073     void zoomSel();
0074     void zoom2Fit();
0075 
0076     int selListSize();
0077     /* This function returns the active visible selection in index 0 and after that the "saved" ones */
0078     bool selectionAt(int index, float &tl_x, float &tl_y, float &br_x, float &br_y);
0079 
0080     void setMultiselectionEnabled(bool enabled);
0081 
0082 Q_SIGNALS:
0083     void newSelection(float tl_x, float tl_y, float br_x, float br_y);
0084 
0085 protected:
0086     void wheelEvent(QWheelEvent *e) override;
0087     void mousePressEvent(QMouseEvent *e) override;
0088     void mouseReleaseEvent(QMouseEvent *e) override;
0089     void mouseMoveEvent(QMouseEvent *e) override;
0090     void drawBackground(QPainter *painter, const QRectF &rect) override;
0091 
0092 private:
0093     void updateSelVisibility();
0094     void updateHighlight();
0095     bool activeSelection(float &tl_x, float &tl_y, float &br_x, float &br_y);
0096     void refineSelections(int pixelMargin);
0097 
0098     // fromRow is the row to start the iterations from. fromRow can be grater than toRow.
0099     // rowStart is the x1 coordinate of the row
0100     // all parameters are corrected to be valid pixel indexes,
0101     // but start must be < end
0102     int  refineRow(int fromRow, int toRow, int rowStart, int rowEnd);
0103     int  refineColumn(int fromCol, int toCol, int colStart, int colEnd);
0104 
0105     QPointF scenePos(QMouseEvent *e) const;
0106 
0107     struct Private;
0108     Private *const d;
0109 
0110 };
0111 
0112 }  // NameSpace KSaneIface
0113 
0114 #endif
0115