File indexing completed on 2025-01-19 03:55:43

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-12-13
0007  * Description : a widget to preview image effect.
0008  *
0009  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2008      by Kare Sars <kare dot sars at iki dot fi>
0011  * SPDX-FileCopyrightText: 2012      by Benjamin Girault <benjamin dot girault at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef DIGIKAM_DPREVIEW_IMAGE_H
0018 #define DIGIKAM_DPREVIEW_IMAGE_H
0019 
0020 // Qt includes
0021 
0022 #include <QGraphicsView>
0023 #include <QGraphicsPixmapItem>
0024 #include <QString>
0025 #include <QColor>
0026 
0027 // Local includes
0028 
0029 #include "digikam_export.h"
0030 
0031 class QResizeEvent;
0032 class QWheelEvent;
0033 class QMouseEvent;
0034 class QEvent;
0035 
0036 namespace Digikam
0037 {
0038 
0039 class DIGIKAM_EXPORT DSelectionItem : public QGraphicsItem
0040 {
0041 public:
0042 
0043     typedef enum
0044     {
0045         None,
0046         Top,
0047         TopRight,
0048         Right,
0049         BottomRight,
0050         Bottom,
0051         BottomLeft,
0052         Left,
0053         TopLeft,
0054         Move
0055     } Intersects;
0056 
0057 public:
0058 
0059     explicit DSelectionItem(const QRectF& rect);
0060     ~DSelectionItem() override;
0061 
0062 public:
0063 
0064     void setMaxRight(qreal maxRight);
0065     void setMaxBottom(qreal maxBottom);
0066 
0067     Intersects intersects(QPointF& point);
0068 
0069     void saveZoom(qreal zoom);
0070 
0071     void    setRect(const QRectF& rect);
0072     QRectF  rect()                                      const;
0073     QPointF fixTranslation(QPointF dp)                  const;
0074 
0075 public:
0076 
0077     // Graphics Item methods
0078     QRectF boundingRect() const                               override;
0079     void paint(QPainter* painter,
0080                const QStyleOptionGraphicsItem* option,
0081                QWidget* widget)                               override;
0082 
0083 private:
0084 
0085     void updateAnchors();
0086 
0087 private:
0088 
0089     class Private;
0090     Private* const d;
0091 };
0092 
0093 // -----------------------------------------------------------------------------------------
0094 
0095 class DIGIKAM_EXPORT DPreviewImage : public QGraphicsView
0096 {
0097     Q_OBJECT
0098 
0099 public:
0100 
0101     explicit DPreviewImage(QWidget* const parent);
0102     ~DPreviewImage() override;
0103 
0104 public:
0105 
0106     bool load(const QUrl& file)         const;
0107     bool setImage(const QImage& img)    const;
0108     void enableSelectionArea(bool b);
0109 
0110     /**
0111      * Sets a selection area and show it
0112      *
0113      * @param rectangle This rectangle should have height and width of 1.0
0114      */
0115     void   setSelectionArea(const QRectF& rectangle);
0116     QRectF getSelectionArea()           const;
0117 
0118 public Q_SLOTS:
0119 
0120     void slotZoomIn();
0121     void slotZoomOut();
0122 //  void slotZoomSel();      // TODO: add a button for that purpose
0123     void slotZoom2Fit();
0124 
0125     ///@{
0126     /// Selection area specific slots (TL = TopLeft, BR = BottomRight)
0127     void slotSetTLX(float ratio);
0128     void slotSetTLY(float ratio);
0129     void slotSetBRX(float ratio);
0130     void slotSetBRY(float ratio);
0131     ///@}
0132 
0133     /**
0134      * This function is used to set a selection without the user setting it.
0135      * \note all parameters must be in the range 0.0 -> 1.0.
0136      * \param tl_x is the x coordinate of the top left corner 0=0 1=image with.
0137      * \param tl_y is the y coordinate of the top left corner 0=0 1=image height.
0138      * \param br_x is the x coordinate of the bottom right corner 0=0 1=image with.
0139      * \param br_y is the y coordinate of the bottom right corner 0=0 1=image height.
0140      */
0141     void slotSetSelection(float tl_x, float tl_y, float br_x, float br_y);
0142     void slotClearActiveSelection();
0143 
0144     /**
0145      * This function is used to darken everything except what is inside the given area.
0146      * \note all parameters must be in the range 0.0 -> 1.0.
0147      * \param tl_x is the x coordinate of the top left corner 0=0 1=image with.
0148      * \param tl_y is the y coordinate of the top left corner 0=0 1=image height.
0149      * \param br_x is the x coordinate of the bottom right corner 0=0 1=image with.
0150      * \param br_y is the y coordinate of the bottom right corner 0=0 1=image height.
0151      */
0152     void slotSetHighlightArea(float tl_x, float tl_y, float br_x, float br_y);
0153 
0154     /**
0155      * This function sets the percentage of the highlighted area that is visible.
0156      * The rest is hidden. This stacks with the previous highlight area.
0157      * \param percentage is the percentage of the highlighted area that is shown.
0158      * \param hideColor is the color to use to hide the highlighted area of the image.
0159      */
0160     void slotSetHighlightShown(int percentage, const QColor& highLightColor = Qt::white);
0161 
0162     /**
0163      * This function removes the highlight area.
0164      */
0165     void slotClearHighlight();
0166 
0167 protected:
0168 
0169     void wheelEvent(QWheelEvent*)           override;
0170     void mousePressEvent(QMouseEvent*)      override;
0171     void mouseReleaseEvent(QMouseEvent*)    override;
0172     void mouseMoveEvent(QMouseEvent*)       override;
0173     void leaveEvent(QEvent*)                override;
0174     bool eventFilter(QObject*, QEvent*)     override;
0175     void resizeEvent(QResizeEvent*)         override;
0176 
0177 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0178 
0179     void enterEvent(QEnterEvent*)          override;
0180 
0181 #else
0182 
0183     void enterEvent(QEvent*)               override;
0184 
0185 #endif
0186 
0187     void updateSelVisibility();
0188     void updateHighlight();
0189 
0190 private:
0191 
0192     class Private;
0193     Private* const d;
0194 };
0195 
0196 } // namespace Digikam
0197 
0198 #endif // DIGIKAM_DPREVIEW_IMAGE_H