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

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 // Selector widgets for KDE Color Selector, but probably useful for other
0009 // stuff also.
0010 
0011 #ifndef KSELECTOR_H
0012 #define KSELECTOR_H
0013 
0014 #include <kwidgetsaddons_export.h>
0015 
0016 #include <QAbstractSlider>
0017 #include <QGradient>
0018 #include <QWidget>
0019 #include <memory>
0020 
0021 /**
0022  * @class KSelector kselector.h KSelector
0023  *
0024  * KSelector is the base class for other widgets which
0025  * provides the ability to choose from a one-dimensional
0026  * range of values. An example is the KGradientSelector
0027  * which allows to choose from a range of colors.
0028  *
0029  * A custom drawing routine for the widget surface has
0030  * to be provided by the subclass.
0031  */
0032 class KWIDGETSADDONS_EXPORT KSelector : public QAbstractSlider
0033 {
0034     Q_OBJECT
0035     Q_PROPERTY(int value READ value WRITE setValue)
0036     Q_PROPERTY(int minValue READ minimum WRITE setMinimum)
0037     Q_PROPERTY(int maxValue READ maximum WRITE setMaximum)
0038     Q_PROPERTY(bool indent READ indent WRITE setIndent)
0039     Q_PROPERTY(Qt::ArrowType arrowDirection READ arrowDirection WRITE setArrowDirection)
0040 public:
0041     /**
0042      * Constructs a horizontal one-dimensional selection widget.
0043      */
0044     explicit KSelector(QWidget *parent = nullptr);
0045     /**
0046      * Constructs a one-dimensional selection widget with
0047      * a given orientation.
0048      */
0049     explicit KSelector(Qt::Orientation o, QWidget *parent = nullptr);
0050     /*
0051      * Destructs the widget.
0052      */
0053     ~KSelector() override;
0054 
0055     /**
0056      * @return the rectangle on which subclasses should draw.
0057      */
0058     QRect contentsRect() const;
0059 
0060     /**
0061      * Sets the indent option of the widget to i.
0062      * This determines whether a shaded frame is drawn.
0063      */
0064     void setIndent(bool i);
0065 
0066     /**
0067      * @return whether the indent option is set.
0068      */
0069     bool indent() const;
0070 
0071     /**
0072      * Sets the arrow direction.
0073      */
0074     void setArrowDirection(Qt::ArrowType direction);
0075 
0076     /**
0077      * @return the current arrow direction
0078      */
0079     Qt::ArrowType arrowDirection() const;
0080 
0081 protected:
0082     /**
0083      * Override this function to draw the contents of the control.
0084      * The default implementation does nothing.
0085      *
0086      * Draw only within contentsRect().
0087      */
0088     virtual void drawContents(QPainter *);
0089     /**
0090      * Override this function to draw the cursor which
0091      * indicates the current value.
0092      */
0093     virtual void drawArrow(QPainter *painter, const QPoint &pos);
0094 
0095     void paintEvent(QPaintEvent *) override;
0096     void mousePressEvent(QMouseEvent *e) override;
0097     void mouseMoveEvent(QMouseEvent *e) override;
0098     void mouseReleaseEvent(QMouseEvent *e) override;
0099     void wheelEvent(QWheelEvent *) override;
0100 
0101 private:
0102     KWIDGETSADDONS_NO_EXPORT QPoint calcArrowPos(int val);
0103     KWIDGETSADDONS_NO_EXPORT void moveArrow(const QPoint &pos);
0104 
0105 private:
0106     friend class KSelectorPrivate;
0107     std::unique_ptr<class KSelectorPrivate> const d;
0108 
0109     Q_DISABLE_COPY(KSelector)
0110 };
0111 
0112 /**
0113  * @class KGradientSelector kselector.h KGradientSelector
0114  *
0115  * The KGradientSelector widget allows the user to choose
0116  * from a one-dimensional range of colors which is given as a
0117  * gradient between two colors provided by the programmer.
0118  *
0119  * \image html kgradientselector.png "KGradientSelector Widget"
0120  */
0121 class KWIDGETSADDONS_EXPORT KGradientSelector : public KSelector
0122 {
0123     Q_OBJECT
0124 
0125     Q_PROPERTY(QColor firstColor READ firstColor WRITE setFirstColor)
0126     Q_PROPERTY(QColor secondColor READ secondColor WRITE setSecondColor)
0127     Q_PROPERTY(QString firstText READ firstText WRITE setFirstText)
0128     Q_PROPERTY(QString secondText READ secondText WRITE setSecondText)
0129 
0130 public:
0131     /**
0132      * Constructs a horizontal color selector which
0133      * contains a gradient between white and black.
0134      */
0135     explicit KGradientSelector(QWidget *parent = nullptr);
0136     /**
0137      * Constructs a colors selector with orientation o which
0138      * contains a gradient between white and black.
0139      */
0140     explicit KGradientSelector(Qt::Orientation o, QWidget *parent = nullptr);
0141     /**
0142      * Destructs the widget.
0143      */
0144     ~KGradientSelector() override;
0145 
0146     /**
0147      * Sets the colors that make up the gradient. Any previously set colors
0148      * are removed.
0149      * @since 4.5
0150      */
0151     void setStops(const QGradientStops &stops);
0152 
0153     /**
0154      * Get the colors that make up the gradient.
0155      * @since 4.5
0156      */
0157     QGradientStops stops() const;
0158 
0159     /**
0160      * Sets the two colors which span the gradient.
0161      */
0162     void setColors(const QColor &col1, const QColor &col2);
0163     void setText(const QString &t1, const QString &t2);
0164 
0165     /**
0166      * Set each color on its own.
0167      */
0168     void setFirstColor(const QColor &col);
0169     void setSecondColor(const QColor &col);
0170 
0171     /**
0172      * Set each description on its own
0173      */
0174     void setFirstText(const QString &t);
0175     void setSecondText(const QString &t);
0176 
0177     QColor firstColor() const;
0178     QColor secondColor() const;
0179 
0180     QString firstText() const;
0181     QString secondText() const;
0182 
0183 protected:
0184     void drawContents(QPainter *) override;
0185     virtual QSize minimumSize() const;
0186 
0187 private:
0188     friend class KGradientSelectorPrivate;
0189     std::unique_ptr<class KGradientSelectorPrivate> const d;
0190 
0191     Q_DISABLE_COPY(KGradientSelector)
0192 };
0193 
0194 #endif // KSELECTOR_H