From 4a3738759f9cf33690447b981f4d9c8a26623d8e Mon Sep 17 00:00:00 2001 From: Rick Vogel Date: Wed, 26 Oct 2022 13:00:24 +0200 Subject: [PATCH] initial commit --- examples/gallery/gallery.pro | 6 +++ .../gallery/slider/LinearGradientSlider.cpp | 51 +++++++++++++++++++ .../gallery/slider/LinearGradientSlider.h | 25 +++++++++ examples/gallery/slider/SliderPage.cpp | 4 ++ 4 files changed, 86 insertions(+) create mode 100644 examples/gallery/slider/LinearGradientSlider.cpp create mode 100644 examples/gallery/slider/LinearGradientSlider.h diff --git a/examples/gallery/gallery.pro b/examples/gallery/gallery.pro index cae45a37..4176af3b 100644 --- a/examples/gallery/gallery.pro +++ b/examples/gallery/gallery.pro @@ -45,6 +45,12 @@ SOURCES += \ HEADERS += \ Page.h +HEADERS += \ + slider/LinearGradientSlider.h \ + +SOURCES += \ + slider/LinearGradientSlider.cpp \ + SOURCES += \ Page.cpp \ main.cpp diff --git a/examples/gallery/slider/LinearGradientSlider.cpp b/examples/gallery/slider/LinearGradientSlider.cpp new file mode 100644 index 00000000..b0b180fa --- /dev/null +++ b/examples/gallery/slider/LinearGradientSlider.cpp @@ -0,0 +1,51 @@ +#include "LinearGradientSlider.h" + +#include +#include + +LinearGradientSlider::LinearGradientSlider( QQuickItem* parent ) + : LinearGradientSlider( Qt::Horizontal, parent ) +{ +} + +LinearGradientSlider::LinearGradientSlider( Qt::Orientation orientation, QQuickItem* parent ) + : Inherited( orientation, parent ) +{ + const QVector< QskGradientStop > gradientStop = { + { 0.0000, QColor::fromRgb( 255, 0, 0 ) }, + { 0.1667, QColor::fromRgb( 255, 255, 0 ) }, + { 0.3333, QColor::fromRgb( 0, 255, 0 ) }, + { 0.5000, QColor::fromRgb( 0, 255, 255 ) }, + { 0.6667, QColor::fromRgb( 0, 0, 255 ) }, + { 0.8333, QColor::fromRgb( 255, 0, 255 ) }, + { 1.0000, QColor::fromRgb( 255, 0, 0 ) }, + }; + + const QskGradient gradient( orientation, gradientStop ); + setColor( Inherited::Fill, Qt::transparent ); + setGradientHint( Inherited::Groove, gradient ); + setBoxBorderColorsHint( Inherited::Handle, Qt::white ); + setBoxBorderMetricsHint( Inherited::Handle, 2 ); + + connect( this, &QskSlider::valueChanged, this, [ this, gradient ]( qreal value ) { + value = this->orientation() == Qt::Horizontal ? value : 1.0 - value; + const auto selectedColor = gradient.extracted( value, value ).startColor(); + setColor( Inherited::Handle, selectedColor ); + setSelectedColor( selectedColor ); + } ); +} + +const QColor& LinearGradientSlider::selectedColor() const +{ + return m_selectedColor; +} + +void LinearGradientSlider::setSelectedColor( const QColor& newSelectedColor ) +{ + if ( m_selectedColor == newSelectedColor ) + { + return; + } + m_selectedColor = newSelectedColor; + Q_EMIT selectedColorChanged(); +} diff --git a/examples/gallery/slider/LinearGradientSlider.h b/examples/gallery/slider/LinearGradientSlider.h new file mode 100644 index 00000000..719b1eff --- /dev/null +++ b/examples/gallery/slider/LinearGradientSlider.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +class LinearGradientSlider : public QskSlider +{ + Q_OBJECT + Q_PROPERTY( + QColor selectedColor READ selectedColor WRITE setSelectedColor NOTIFY selectedColorChanged ) + using Inherited = QskSlider; + + public: + explicit LinearGradientSlider( QQuickItem* parent = nullptr ); + explicit LinearGradientSlider( Qt::Orientation orientation, QQuickItem* parent = nullptr ); + const QColor& selectedColor() const; + + public Q_SLOTS: + void setSelectedColor( const QColor& newSelectedColor ); + + Q_SIGNALS: + void selectedColorChanged(); + + private: + QColor m_selectedColor; +}; diff --git a/examples/gallery/slider/SliderPage.cpp b/examples/gallery/slider/SliderPage.cpp index 733692d6..df608694 100644 --- a/examples/gallery/slider/SliderPage.cpp +++ b/examples/gallery/slider/SliderPage.cpp @@ -4,6 +4,7 @@ *****************************************************************************/ #include "SliderPage.h" +#include "LinearGradientSlider.h" #include namespace @@ -50,4 +51,7 @@ void SliderPage::populate() { ( void ) new Slider( Qt::Horizontal, this ); ( void ) new Slider( Qt::Vertical, this ); + + ( void ) new LinearGradientSlider( Qt::Horizontal, this ); + ( void ) new LinearGradientSlider( Qt::Vertical, this ); }