From bafa5a14dde4415b13de3f3d44af6333538deff5 Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Tue, 26 Jun 2018 17:26:29 +0200 Subject: Add demo of the new QGradient presets to the gradients example In the gradients example, allow the user to select and show QGradient's named presets. Change-Id: I40bc6cbe3a0316ce49d67d63511881b6f6112574 Reviewed-by: Simon Hausmann --- examples/widgets/doc/src/gradients.qdoc | 7 ++- examples/widgets/painting/gradients/gradients.cpp | 58 +++++++++++++++++++++- examples/widgets/painting/gradients/gradients.h | 7 +++ examples/widgets/painting/gradients/gradients.html | 6 ++- 4 files changed, 74 insertions(+), 4 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/doc/src/gradients.qdoc b/examples/widgets/doc/src/gradients.qdoc index 936bd2b014..457e6f837a 100644 --- a/examples/widgets/doc/src/gradients.qdoc +++ b/examples/widgets/doc/src/gradients.qdoc @@ -51,7 +51,12 @@ gradient. You can move points, and add new ones, by clicking with the left mouse button, and remove points by clicking with the right button. - There are three default configurations available at the bottom of + There are three example configurations available at the bottom of the page that are provided as suggestions on how a color table could be configured. + + Qt also provides a suite of named gradient presets. They are based on the + free WebGradients collection. Click on the name in the Presets box to show + the gradient. Use the arrow buttons to browse through the available + presets. */ diff --git a/examples/widgets/painting/gradients/gradients.cpp b/examples/widgets/painting/gradients/gradients.cpp index 78c174a8bf..6d9f514a8d 100644 --- a/examples/widgets/painting/gradients/gradients.cpp +++ b/examples/widgets/painting/gradients/gradients.cpp @@ -301,8 +301,15 @@ GradientWidget::GradientWidget(QWidget *parent) m_reflectSpreadButton = new QRadioButton(tr("Reflect Spread"), spreadGroup); m_repeatSpreadButton = new QRadioButton(tr("Repeat Spread"), spreadGroup); + QGroupBox *presetsGroup = new QGroupBox(mainGroup); + presetsGroup->setTitle(tr("Presets")); + QPushButton *prevPresetButton = new QPushButton(tr("<"), presetsGroup); + m_presetButton = new QPushButton(tr("(unset)"), presetsGroup); + QPushButton *nextPresetButton = new QPushButton(tr(">"), presetsGroup); + updatePresetName(); + QGroupBox *defaultsGroup = new QGroupBox(mainGroup); - defaultsGroup->setTitle(tr("Defaults")); + defaultsGroup->setTitle(tr("Examples")); QPushButton *default1Button = new QPushButton(tr("1"), defaultsGroup); QPushButton *default2Button = new QPushButton(tr("2"), defaultsGroup); QPushButton *default3Button = new QPushButton(tr("3"), defaultsGroup); @@ -327,11 +334,12 @@ GradientWidget::GradientWidget(QWidget *parent) mainLayout->addWidget(m_renderer); mainLayout->addWidget(mainGroup); - mainGroup->setFixedWidth(180); + mainGroup->setFixedWidth(200); QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup); mainGroupLayout->addWidget(editorGroup); mainGroupLayout->addWidget(typeGroup); mainGroupLayout->addWidget(spreadGroup); + mainGroupLayout->addWidget(presetsGroup); mainGroupLayout->addWidget(defaultsGroup); mainGroupLayout->addStretch(1); mainGroupLayout->addWidget(showSourceButton); @@ -353,6 +361,11 @@ GradientWidget::GradientWidget(QWidget *parent) spreadGroupLayout->addWidget(m_repeatSpreadButton); spreadGroupLayout->addWidget(m_reflectSpreadButton); + QHBoxLayout *presetsGroupLayout = new QHBoxLayout(presetsGroup); + presetsGroupLayout->addWidget(prevPresetButton); + presetsGroupLayout->addWidget(m_presetButton, 1); + presetsGroupLayout->addWidget(nextPresetButton); + QHBoxLayout *defaultsGroupLayout = new QHBoxLayout(defaultsGroup); defaultsGroupLayout->addWidget(default1Button); defaultsGroupLayout->addWidget(default2Button); @@ -375,6 +388,13 @@ GradientWidget::GradientWidget(QWidget *parent) connect(m_repeatSpreadButton, &QRadioButton::clicked, m_renderer, &GradientRenderer::setRepeatSpread); + connect(prevPresetButton, &QPushButton::clicked, + this, &GradientWidget::setPrevPreset); + connect(m_presetButton, &QPushButton::clicked, + this, &GradientWidget::setPreset); + connect(nextPresetButton, &QPushButton::clicked, + this, &GradientWidget::setNextPreset); + connect(default1Button, &QPushButton::clicked, this, &GradientWidget::setDefault1); connect(default2Button, &QPushButton::clicked, @@ -471,6 +491,40 @@ void GradientWidget::setDefault(int config) m_renderer->setGradientStops(stops); } +void GradientWidget::updatePresetName() +{ + QMetaEnum presetEnum = QMetaEnum::fromType(); + m_presetButton->setText(QLatin1String(presetEnum.key(m_presetIndex))); +} + +void GradientWidget::changePresetBy(int indexOffset) +{ + QMetaEnum presetEnum = QMetaEnum::fromType(); + m_presetIndex = qBound(0, m_presetIndex + indexOffset, presetEnum.keyCount() - 1); + + QGradient::Preset preset = static_cast(presetEnum.value(m_presetIndex)); + QGradient gradient(preset); + if (gradient.type() != QGradient::LinearGradient) + return; + + QLinearGradient *linearGradientPointer = static_cast(&gradient); + QLineF objectStopsLine(linearGradientPointer->start(), linearGradientPointer->finalStop()); + qreal scaleX = qFuzzyIsNull(objectStopsLine.dx()) ? 1.0 : (0.8 * m_renderer->width() / qAbs(objectStopsLine.dx())); + qreal scaleY = qFuzzyIsNull(objectStopsLine.dy()) ? 1.0 : (0.8 * m_renderer->height() / qAbs(objectStopsLine.dy())); + QLineF logicalStopsLine = QTransform::fromScale(scaleX, scaleY).map(objectStopsLine); + logicalStopsLine.translate(m_renderer->rect().center() - logicalStopsLine.center()); + QPolygonF logicalStops; + logicalStops << logicalStopsLine.p1() << logicalStopsLine.p2(); + + m_linearButton->animateClick(); + m_padSpreadButton->animateClick(); + m_editor->setGradientStops(gradient.stops()); + m_renderer->hoverPoints()->setPoints(logicalStops); + m_renderer->setGradientStops(gradient.stops()); + + updatePresetName(); +} + GradientRenderer::GradientRenderer(QWidget *parent) : ArthurFrame(parent) { diff --git a/examples/widgets/painting/gradients/gradients.h b/examples/widgets/painting/gradients/gradients.h index b4db298bb4..c6525d18f8 100644 --- a/examples/widgets/painting/gradients/gradients.h +++ b/examples/widgets/painting/gradients/gradients.h @@ -164,9 +164,14 @@ public slots: void setDefault2() { setDefault(2); } void setDefault3() { setDefault(3); } void setDefault4() { setDefault(4); } + void setPreset() { changePresetBy(0); } + void setPrevPreset() { changePresetBy(-1); } + void setNextPreset() { changePresetBy(1); } private: void setDefault(int i); + void updatePresetName(); + void changePresetBy(int indexOffset); GradientRenderer *m_renderer; GradientEditor *m_editor; @@ -177,7 +182,9 @@ private: QRadioButton *m_padSpreadButton; QRadioButton *m_reflectSpreadButton; QRadioButton *m_repeatSpreadButton; + QPushButton *m_presetButton; + int m_presetIndex = 0; }; #endif // GRADIENTS_H diff --git a/examples/widgets/painting/gradients/gradients.html b/examples/widgets/painting/gradients/gradients.html index 1ea2c0ed6c..82c449035f 100644 --- a/examples/widgets/painting/gradients/gradients.html +++ b/examples/widgets/painting/gradients/gradients.html @@ -24,8 +24,12 @@ green and blue components while the last defines the alpha of the gradient. You can move points, and add new ones, by clicking with the left mouse button, and remove points by clicking with the right button.

-

There are three default configurations available at the bottom of +

There are three example configurations available at the bottom of the page that are provided as suggestions on how a color table could be configured.

+

Qt also provides a suite of named gradient presets. They are based on the +free WebGradients collection. Click on the name in the Presets box to show the +gradient. Use the arrow buttons to browse through the available presets.

+ -- cgit v1.2.3