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 <simon.hausmann@qt.io>
This commit is contained in:
Eirik Aavitsland 2018-06-26 17:26:29 +02:00
parent 5d0827cbe7
commit bafa5a14dd
5 changed files with 74 additions and 4 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 91 KiB

After

Width:  |  Height:  |  Size: 83 KiB

View File

@ -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.
*/

View File

@ -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<QGradient::Preset>();
m_presetButton->setText(QLatin1String(presetEnum.key(m_presetIndex)));
}
void GradientWidget::changePresetBy(int indexOffset)
{
QMetaEnum presetEnum = QMetaEnum::fromType<QGradient::Preset>();
m_presetIndex = qBound(0, m_presetIndex + indexOffset, presetEnum.keyCount() - 1);
QGradient::Preset preset = static_cast<QGradient::Preset>(presetEnum.value(m_presetIndex));
QGradient gradient(preset);
if (gradient.type() != QGradient::LinearGradient)
return;
QLinearGradient *linearGradientPointer = static_cast<QLinearGradient *>(&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)
{

View File

@ -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

View File

@ -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.</p>
<p>There are three default configurations available at the bottom of
<p>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.</p>
<p>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.</p>
</html>