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:
parent
5d0827cbe7
commit
bafa5a14dd
Binary file not shown.
Before Width: | Height: | Size: 91 KiB After Width: | Height: | Size: 83 KiB |
@ -51,7 +51,12 @@
|
|||||||
gradient. You can move points, and add new ones, by clicking with the left
|
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.
|
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
|
the page that are provided as suggestions on how a color table could be
|
||||||
configured.
|
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.
|
||||||
*/
|
*/
|
||||||
|
@ -301,8 +301,15 @@ GradientWidget::GradientWidget(QWidget *parent)
|
|||||||
m_reflectSpreadButton = new QRadioButton(tr("Reflect Spread"), spreadGroup);
|
m_reflectSpreadButton = new QRadioButton(tr("Reflect Spread"), spreadGroup);
|
||||||
m_repeatSpreadButton = new QRadioButton(tr("Repeat 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);
|
QGroupBox *defaultsGroup = new QGroupBox(mainGroup);
|
||||||
defaultsGroup->setTitle(tr("Defaults"));
|
defaultsGroup->setTitle(tr("Examples"));
|
||||||
QPushButton *default1Button = new QPushButton(tr("1"), defaultsGroup);
|
QPushButton *default1Button = new QPushButton(tr("1"), defaultsGroup);
|
||||||
QPushButton *default2Button = new QPushButton(tr("2"), defaultsGroup);
|
QPushButton *default2Button = new QPushButton(tr("2"), defaultsGroup);
|
||||||
QPushButton *default3Button = new QPushButton(tr("3"), defaultsGroup);
|
QPushButton *default3Button = new QPushButton(tr("3"), defaultsGroup);
|
||||||
@ -327,11 +334,12 @@ GradientWidget::GradientWidget(QWidget *parent)
|
|||||||
mainLayout->addWidget(m_renderer);
|
mainLayout->addWidget(m_renderer);
|
||||||
mainLayout->addWidget(mainGroup);
|
mainLayout->addWidget(mainGroup);
|
||||||
|
|
||||||
mainGroup->setFixedWidth(180);
|
mainGroup->setFixedWidth(200);
|
||||||
QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
|
QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
|
||||||
mainGroupLayout->addWidget(editorGroup);
|
mainGroupLayout->addWidget(editorGroup);
|
||||||
mainGroupLayout->addWidget(typeGroup);
|
mainGroupLayout->addWidget(typeGroup);
|
||||||
mainGroupLayout->addWidget(spreadGroup);
|
mainGroupLayout->addWidget(spreadGroup);
|
||||||
|
mainGroupLayout->addWidget(presetsGroup);
|
||||||
mainGroupLayout->addWidget(defaultsGroup);
|
mainGroupLayout->addWidget(defaultsGroup);
|
||||||
mainGroupLayout->addStretch(1);
|
mainGroupLayout->addStretch(1);
|
||||||
mainGroupLayout->addWidget(showSourceButton);
|
mainGroupLayout->addWidget(showSourceButton);
|
||||||
@ -353,6 +361,11 @@ GradientWidget::GradientWidget(QWidget *parent)
|
|||||||
spreadGroupLayout->addWidget(m_repeatSpreadButton);
|
spreadGroupLayout->addWidget(m_repeatSpreadButton);
|
||||||
spreadGroupLayout->addWidget(m_reflectSpreadButton);
|
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);
|
QHBoxLayout *defaultsGroupLayout = new QHBoxLayout(defaultsGroup);
|
||||||
defaultsGroupLayout->addWidget(default1Button);
|
defaultsGroupLayout->addWidget(default1Button);
|
||||||
defaultsGroupLayout->addWidget(default2Button);
|
defaultsGroupLayout->addWidget(default2Button);
|
||||||
@ -375,6 +388,13 @@ GradientWidget::GradientWidget(QWidget *parent)
|
|||||||
connect(m_repeatSpreadButton, &QRadioButton::clicked,
|
connect(m_repeatSpreadButton, &QRadioButton::clicked,
|
||||||
m_renderer, &GradientRenderer::setRepeatSpread);
|
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,
|
connect(default1Button, &QPushButton::clicked,
|
||||||
this, &GradientWidget::setDefault1);
|
this, &GradientWidget::setDefault1);
|
||||||
connect(default2Button, &QPushButton::clicked,
|
connect(default2Button, &QPushButton::clicked,
|
||||||
@ -471,6 +491,40 @@ void GradientWidget::setDefault(int config)
|
|||||||
m_renderer->setGradientStops(stops);
|
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)
|
GradientRenderer::GradientRenderer(QWidget *parent)
|
||||||
: ArthurFrame(parent)
|
: ArthurFrame(parent)
|
||||||
{
|
{
|
||||||
|
@ -164,9 +164,14 @@ public slots:
|
|||||||
void setDefault2() { setDefault(2); }
|
void setDefault2() { setDefault(2); }
|
||||||
void setDefault3() { setDefault(3); }
|
void setDefault3() { setDefault(3); }
|
||||||
void setDefault4() { setDefault(4); }
|
void setDefault4() { setDefault(4); }
|
||||||
|
void setPreset() { changePresetBy(0); }
|
||||||
|
void setPrevPreset() { changePresetBy(-1); }
|
||||||
|
void setNextPreset() { changePresetBy(1); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setDefault(int i);
|
void setDefault(int i);
|
||||||
|
void updatePresetName();
|
||||||
|
void changePresetBy(int indexOffset);
|
||||||
|
|
||||||
GradientRenderer *m_renderer;
|
GradientRenderer *m_renderer;
|
||||||
GradientEditor *m_editor;
|
GradientEditor *m_editor;
|
||||||
@ -177,7 +182,9 @@ private:
|
|||||||
QRadioButton *m_padSpreadButton;
|
QRadioButton *m_padSpreadButton;
|
||||||
QRadioButton *m_reflectSpreadButton;
|
QRadioButton *m_reflectSpreadButton;
|
||||||
QRadioButton *m_repeatSpreadButton;
|
QRadioButton *m_repeatSpreadButton;
|
||||||
|
QPushButton *m_presetButton;
|
||||||
|
|
||||||
|
int m_presetIndex = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GRADIENTS_H
|
#endif // GRADIENTS_H
|
||||||
|
@ -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
|
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>
|
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
|
the page that are provided as suggestions on how a color table could be
|
||||||
configured.</p>
|
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>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user