File: gradient_slider.hpp

package info (click to toggle)
qt-color-widgets 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,088 kB
  • sloc: cpp: 7,426; sh: 236; makefile: 14
file content (102 lines) | stat: -rw-r--r-- 2,833 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * SPDX-FileCopyrightText: 2013-2020 Mattia Basaglia
 * SPDX-FileCopyrightText: 2014 Calle Laakkonen
 * SPDX-FileCopyrightText: 2017 caryoscelus
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#ifndef GRADIENT_SLIDER_HPP
#define GRADIENT_SLIDER_HPP

#include "colorwidgets_global.hpp"

#include <QSlider>
#include <QGradient>

namespace color_widgets {

/**
 * \brief A slider that moves on top of a gradient
 */
class QCP_EXPORT GradientSlider : public QSlider
{
    Q_OBJECT
    Q_PROPERTY(QBrush background READ background WRITE setBackground NOTIFY backgroundChanged)
    Q_PROPERTY(QGradientStops colors READ colors WRITE setColors DESIGNABLE false)
    Q_PROPERTY(QColor firstColor READ firstColor WRITE setFirstColor STORED false)
    Q_PROPERTY(QColor lastColor READ lastColor WRITE setLastColor STORED false)
    Q_PROPERTY(QLinearGradient gradient READ gradient WRITE setGradient)

public:
    explicit GradientSlider(QWidget *parent = nullptr);
    explicit GradientSlider(Qt::Orientation orientation, QWidget *parent = nullptr);
    ~GradientSlider();

    /// Get the background, it's visible for transparent gradient stops
    QBrush background() const;
    /// Set the background, it's visible for transparent gradient stops
    void setBackground(const QBrush &bg);

    /// Get the colors that make up the gradient
    QGradientStops colors() const;
    /// Set the colors that make up the gradient
    void setColors(const QGradientStops &colors);

    /// Get the gradient
    QLinearGradient gradient() const;
    /// Set the gradient
    void setGradient(const QLinearGradient &gradient);

    /**
     * Overload: create an evenly distributed gradient of the given colors
     */
    void setColors(const QVector<QColor> &colors);

    /**
     * \brief Set the first color of the gradient
     *
     * If the gradient is currently empty it will create a stop with the given color
     */
    void setFirstColor(const QColor &c);

    /**
     * \brief Set the last color of the gradient
     *
     * If the gradient is has less than two colors,
     * it will create a stop with the given color
     */
    void setLastColor(const QColor &c);

    /**
     * \brief Get the first color
     *
     * \returns QColor() con empty gradient
     */
    QColor firstColor() const;

    /**
     * \brief Get the last color
     *
     * \returns QColor() con empty gradient
     */
    QColor lastColor() const;

Q_SIGNALS:
    void backgroundChanged(const QBrush&);
    
protected:
    void paintEvent(QPaintEvent *ev) override;

    void mousePressEvent(QMouseEvent *ev) override;
    void mouseMoveEvent(QMouseEvent *ev) override;
    void mouseReleaseEvent(QMouseEvent *ev) override;

private:
    class Private;
    Private * const p;
};

} // namespace color_widgets

#endif // GRADIENT_SLIDER_HPP