File indexing completed on 2024-04-28 03:59:11

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2004 Antonio Larrosa <larrosa@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KPIXMAPREGIONSELECTORWIDGET_H
0009 #define KPIXMAPREGIONSELECTORWIDGET_H
0010 
0011 #include <kwidgetsaddons_export.h>
0012 
0013 #include <QPixmap>
0014 #include <QWidget>
0015 #include <memory>
0016 
0017 class QMenu;
0018 
0019 /**
0020  * @class KPixmapRegionSelectorWidget kpixmapregionselectorwidget.h KPixmapRegionSelectorWidget
0021  *
0022  * KPixmapRegionSelectorWidget is a widget that shows a picture and provides the
0023  * user with a friendly way to select a rectangular subregion of the pixmap.
0024  *
0025  * \image html kpixmapregionselectorwidget.png "KPixmapRegionSelectorWidget"
0026  *
0027  * @author Antonio Larrosa <larrosa@kde.org>
0028  */
0029 class KWIDGETSADDONS_EXPORT KPixmapRegionSelectorWidget : public QWidget
0030 {
0031     Q_OBJECT
0032     Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
0033 
0034 public:
0035     /**
0036      * This enum provides a rotation direction.
0037      * @see KPixmapRegionSelectorWidget::rotate()
0038      */
0039     enum RotateDirection {
0040         Rotate90, //!< Rotate 90 degrees to the right.
0041         Rotate180, //!< Rotate 180 degrees.
0042         Rotate270 //!< Rotate 90 degrees to the left.
0043     };
0044 
0045     /**
0046      * Constructor for a KPixmapRegionSelectorWidget.
0047      */
0048     explicit KPixmapRegionSelectorWidget(QWidget *parent = nullptr);
0049 
0050     /**
0051      * Destructor for a KPixmapRegionSelectorWidget
0052      */
0053     ~KPixmapRegionSelectorWidget() override;
0054 
0055     /**
0056      * Sets the pixmap which will be shown for the user to select a region from.
0057      * @param pixmap The pixmap.  Must be non-null.
0058      *
0059      */
0060     void setPixmap(const QPixmap &pixmap);
0061 
0062     /**
0063      * @return the original whole pixmap that we're using in this widget as the
0064      * pixmap the user is selecting a region from.
0065      */
0066     QPixmap pixmap() const;
0067 
0068     /**
0069      * Sets the selected region to be @p rect (in zoomed pixmap coordinates)
0070      */
0071     void setSelectedRegion(const QRect &rect);
0072 
0073     /**
0074      * Returns the selected region ( in zoomed pixmap coordinates )
0075      */
0076     QRect selectedRegion() const;
0077 
0078     /**
0079      * Returns the selected region ( in unzoomed, original pixmap coordinates )
0080      */
0081     QRect unzoomedSelectedRegion() const;
0082 
0083     /**
0084      * Resets the selection to use the whole image
0085      */
0086     void resetSelection();
0087 
0088     /**
0089      * @returns a QImage object with just the region the user selected from the
0090      * image
0091      */
0092     QImage selectedImage() const;
0093 
0094     /**
0095      * Sets the aspect ration that the selected subimage should have. The way to
0096      * select it, is specifying an example valid @p width and @p height.
0097      * @see setFreeSelectionAspectRatio()
0098      */
0099     void setSelectionAspectRatio(int width, int height);
0100 
0101     /**
0102      * Allows the user to do a selection which has any aspect ratio. This is
0103      * the default.
0104      * @see setSelectionAspectRatio()
0105      */
0106     void setFreeSelectionAspectRatio();
0107 
0108     /**
0109      * Sets the maximum size for the widget. If the image is larger than this
0110      * (either horizontally or vertically), it's scaled to adjust to the maximum
0111      * size (preserving the aspect ratio)
0112      */
0113     void setMaximumWidgetSize(int width, int height);
0114 
0115     /**
0116      * Rotates the image as specified by the @p direction parameter, also tries
0117      * to rotate the selected region so that it doesn't change, as long as the
0118      * forced aspect ratio setting is respected, in other case, the selected region
0119      * is reset.
0120      */
0121     void rotate(RotateDirection direction);
0122 
0123 public Q_SLOTS:
0124     /**
0125      * Rotates the current image 90º clockwise
0126      */
0127     void rotateClockwise();
0128     /**
0129      * Rotates the current image 90º counterclockwise
0130      */
0131     void rotateCounterclockwise();
0132 
0133 Q_SIGNALS:
0134     void pixmapRotated();
0135 
0136 protected:
0137     /**
0138      * Creates a QMenu with the menu that appears when clicking with the right button on the label
0139      */
0140     virtual QMenu *createPopupMenu();
0141 
0142     bool eventFilter(QObject *obj, QEvent *ev) override;
0143 
0144 private:
0145     friend class KPixmapRegionSelectorWidgetPrivate;
0146     std::unique_ptr<class KPixmapRegionSelectorWidgetPrivate> const d;
0147 
0148     Q_DISABLE_COPY(KPixmapRegionSelectorWidget)
0149 };
0150 
0151 #endif