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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1997 Martin Jones <mjones@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KXYSELECTOR_H
0009 #define KXYSELECTOR_H
0010 
0011 #include <kwidgetsaddons_export.h>
0012 
0013 #include <QWidget>
0014 #include <memory>
0015 
0016 /**
0017  * @class KXYSelector kxyselector.h KXYSelector
0018  *
0019  * KXYSelector is the base class for other widgets which
0020  * provides the ability to choose from a two-dimensional
0021  * range of values. The currently chosen value is indicated
0022  * by a cross. An example is the KHSSelector which
0023  * allows to choose from a range of colors, and which is
0024  * used in KColorDialog.
0025  *
0026  * A custom drawing routine for the widget surface has
0027  * to be provided by the subclass.
0028  */
0029 class KWIDGETSADDONS_EXPORT KXYSelector : public QWidget
0030 {
0031     Q_OBJECT
0032     Q_PROPERTY(int xValue READ xValue WRITE setXValue)
0033     Q_PROPERTY(int yValue READ yValue WRITE setYValue)
0034 
0035 public:
0036     /**
0037      * Constructs a two-dimensional selector widget which
0038      * has a value range of [0..100] in both directions.
0039      */
0040     explicit KXYSelector(QWidget *parent = nullptr);
0041     /**
0042      * Destructs the widget.
0043      */
0044     ~KXYSelector() override;
0045 
0046     /**
0047      * Sets the current values in horizontal and
0048      * vertical direction.
0049      * @param xPos the horizontal value
0050      * @param yPos the vertical value
0051      */
0052     void setValues(int xPos, int yPos);
0053 
0054     /**
0055      * Sets the current horizontal value
0056      * @param xPos the horizontal value
0057      */
0058     void setXValue(int xPos);
0059 
0060     /**
0061      * Sets the current vertical value
0062      * @param yPos the vertical value
0063      */
0064     void setYValue(int yPos);
0065 
0066     /**
0067      * Sets the range of possible values.
0068      */
0069     void setRange(int minX, int minY, int maxX, int maxY);
0070 
0071     /**
0072      * Sets the color used to draw the marker
0073      * @param col the color
0074      */
0075     void setMarkerColor(const QColor &col);
0076 
0077     /**
0078      * @return the current value in horizontal direction.
0079      */
0080     int xValue() const;
0081     /**
0082      * @return the current value in vertical direction.
0083      */
0084     int yValue() const;
0085 
0086     /**
0087      * @return the rectangle on which subclasses should draw.
0088      */
0089     QRect contentsRect() const;
0090 
0091     /**
0092      * Reimplemented to give the widget a minimum size
0093      */
0094     QSize minimumSizeHint() const override;
0095 
0096 Q_SIGNALS:
0097     /**
0098      * This signal is emitted whenever the user chooses a value,
0099      * e.g. by clicking with the mouse on the widget.
0100      */
0101     void valueChanged(int x, int y);
0102 
0103 protected:
0104     /**
0105      * Override this function to draw the contents of the widget.
0106      * The default implementation does nothing.
0107      *
0108      * Draw within contentsRect() only.
0109      */
0110     virtual void drawContents(QPainter *);
0111 
0112     /**
0113      * Override this function to draw the marker which
0114      * indicates the currently selected value pair.
0115      */
0116     virtual void drawMarker(QPainter *p, int xp, int yp);
0117 
0118     void paintEvent(QPaintEvent *e) override;
0119     void mousePressEvent(QMouseEvent *e) override;
0120     void mouseMoveEvent(QMouseEvent *e) override;
0121     void wheelEvent(QWheelEvent *) override;
0122 
0123     /**
0124      * Converts a pixel position to its corresponding values.
0125      */
0126     void valuesFromPosition(int x, int y, int &xVal, int &yVal) const;
0127 
0128 private:
0129     KWIDGETSADDONS_NO_EXPORT void setPosition(int xp, int yp);
0130 
0131 private:
0132     friend class KXYSelectorPrivate;
0133     std::unique_ptr<class KXYSelectorPrivate> const d;
0134 
0135     Q_DISABLE_COPY(KXYSelector)
0136 };
0137 
0138 #endif /* KXYSELECTOR_H */