From a48943e68fd423b566637c9350544cb12cc88e0c Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Fri, 9 Dec 2022 12:07:46 +0100 Subject: [PATCH] initial implementation for the playground/gradients added. This demo is intended to compare how gradients are rendered with the different QSkinny backends to the raster paint engine --- playground/gradients/gradients.pro | 16 +++ playground/gradients/main.cpp | 150 +++++++++++++++++++++++++++++ playground/playground.pro | 3 +- 3 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 playground/gradients/gradients.pro create mode 100644 playground/gradients/main.cpp diff --git a/playground/gradients/gradients.pro b/playground/gradients/gradients.pro new file mode 100644 index 00000000..3cd3c87b --- /dev/null +++ b/playground/gradients/gradients.pro @@ -0,0 +1,16 @@ +CONFIG += qskexample + +QT += widgets quickwidgets + +HEADERS += + +SOURCES += \ + main.cpp + +linux { + + pedantic { + QMAKE_CXXFLAGS += -isystem $$[QT_INSTALL_HEADERS]/QtQuickWidgets + QMAKE_CXXFLAGS += -isystem $$[QT_INSTALL_HEADERS]/QtWidgets + } +} diff --git a/playground/gradients/main.cpp b/playground/gradients/main.cpp new file mode 100644 index 00000000..e3aa704c --- /dev/null +++ b/playground/gradients/main.cpp @@ -0,0 +1,150 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include +#include + +class BoxQsk: public QQuickWidget +{ + public: + BoxQsk( QWidget* parent = nullptr ) + : QQuickWidget( parent ) + { + setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored ); + + setContentsMargins( QMargins() ); + setResizeMode( QQuickWidget::SizeRootObjectToView ); + + m_control = new QskControl(); + setContent( QUrl(), nullptr, m_control ); + } + + void showGradient( const QGradient& gradient ) + { + QskGradient qskGradient( gradient ); + + /* + Eliminate the useless offsets that have been added to work around + QGradients limitation to have stops at the same position + */ + + auto stops = qskGradient.stops(); + for ( int i = 1; i < stops.count(); i++ ) + { + if ( stops[i].position() - stops[i-1].position() < 1e-5 ) + stops[i].setPosition( stops[i-1].position() ); + } + + qskGradient.setStops( stops ); + + m_control->setBackground( qskGradient ); + } + + private: + QskControl* m_control; +}; + +class BoxQt: public QWidget +{ + public: + BoxQt( QWidget* parent = nullptr ) + : QWidget( parent ) + { + setContentsMargins( QMargins() ); + setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored ); + } + + void showGradient( const QGradient& gradient ) + { + m_gradient = gradient; + m_gradient.setCoordinateMode( QGradient::ObjectMode ); + + update(); + } + + protected: + void paintEvent( QPaintEvent* ) override + { + QPainter painter( this ); + painter.fillRect( contentsRect(), m_gradient ); + } + + private: + QGradient m_gradient; +}; + +class GradientBox: public QWidget +{ + Q_OBJECT + + public: + GradientBox( QWidget* parent = nullptr ) + : QWidget( parent ) + { + m_qtBox = new BoxQt(); + m_qskBox = new BoxQsk(); + + auto layout = new QHBoxLayout( this ); + layout->addWidget( m_qskBox ); + layout->addWidget( m_qtBox ); + + showGradient( { Qt::green, Qt::red, Qt::yellow, Qt::cyan, Qt::darkCyan } ); + } + + void showGradient( const QVector< QColor >& colors ) + { + const auto step = 1.0 / colors.size(); + + QGradientStops stops; + + for ( int i = 0; i < colors.size(); i++ ) + { + auto pos = i * step; + if ( i > 0 ) + pos += 1e-6; // QGradient does not support stops at the same position + + stops += { pos, colors[i] }; + stops += { ( i + 1 ) * step, colors[i] }; + } + + QLinearGradient gradient( 0.0, 0.0, 0.0, 1.0 ); + gradient.setStops( stops ); + + showGradient( gradient ); + } + + public Q_SLOTS: + void showGradient( const QGradient& gradient ) + { + m_qtBox->showGradient( gradient ); + m_qskBox->showGradient( gradient ); + } + + private: + BoxQt* m_qtBox; + BoxQsk* m_qskBox; +}; + +int main( int argc, char** argv ) +{ + QApplication app( argc, argv ); + Skinny::init(); // we need a skin + + GradientBox box; + + box.resize( 600, 600 ); + box.show(); + + return app.exec(); +} + +#include "main.moc" diff --git a/playground/playground.pro b/playground/playground.pro index 1cdf0540..509d40a3 100644 --- a/playground/playground.pro +++ b/playground/playground.pro @@ -19,5 +19,6 @@ qtHaveModule(webengine) { qtHaveModule(quickwidgets) { SUBDIRS += \ - grids + grids \ + gradients }