fix: preset dropdown selection for predefined presets
This commit is contained in:
+41
-16
@@ -17,39 +17,58 @@ static QVector<QColor> makePreset4(const QColor &c0, const QColor &c1,
|
||||
|
||||
struct Preset {
|
||||
QString name;
|
||||
QVector<QColor> colors;
|
||||
QVector<QColor> colors;
|
||||
};
|
||||
|
||||
// Presets are defined in a Vector for simplicity of adding new ones
|
||||
static const QVector<Preset> PRESETS = {
|
||||
{ "Default",
|
||||
{ QColor("#078D70"), QColor("#26CEAA"), QColor("#98E8C1"), QColor("#FFFFFF") } },
|
||||
{ QColor("#078D70"), QColor("#26CEAA"), QColor("#98E8C1"), QColor("#FFFFFF") } },
|
||||
|
||||
{ "Pastels",
|
||||
makePreset4(QColor("#92ccdd"), QColor("#c7eff0"),
|
||||
QColor("#f5d5fd"), QColor("#fdc4ec"), QColor("#ffc2cb")) },
|
||||
makePreset4(QColor("#92ccdd"), QColor("#c7eff0"),
|
||||
QColor("#f5d5fd"), QColor("#fdc4ec"), QColor("#ffc2cb")) },
|
||||
|
||||
{ "Raddish",
|
||||
makePreset4(QColor("#ff0000"), QColor("#b40000"),
|
||||
QColor("#890000"), QColor("#540000"), QColor("#240000")) },
|
||||
makePreset4(QColor("#ff0000"), QColor("#b40000"),
|
||||
QColor("#890000"), QColor("#540000"), QColor("#240000")) },
|
||||
|
||||
{ "Blau",
|
||||
makePreset4(QColor("#72c8dd"), QColor("#43b5d2"),
|
||||
QColor("#14a3c7"), QColor("#1293b3"), QColor("#10829f")) },
|
||||
makePreset4(QColor("#72c8dd"), QColor("#43b5d2"),
|
||||
QColor("#14a3c7"), QColor("#1293b3"), QColor("#10829f")) },
|
||||
|
||||
{ "Turquoise",
|
||||
makePreset4(QColor("#d8f2f6"), QColor("#8bd9ef"),
|
||||
QColor("#3ec1d5"), QColor("#319aaa"), QColor("#25737f")) },
|
||||
makePreset4(QColor("#d8f2f6"), QColor("#8bd9ef"),
|
||||
QColor("#3ec1d5"), QColor("#319aaa"), QColor("#25737f")) },
|
||||
|
||||
{ "Aqua",
|
||||
makePreset4(QColor("#2278FB"), QColor("#3A9AF0"),
|
||||
QColor("#53BDE6"), QColor("#6BDFDB"), QColor("#1A5BB8")) },
|
||||
makePreset4(QColor("#2278FB"), QColor("#3A9AF0"),
|
||||
QColor("#53BDE6"), QColor("#6BDFDB"), QColor("#1A5BB8")) },
|
||||
|
||||
{ "Peaches",
|
||||
makePreset4(QColor("#FFCAA6"), QColor("#FDA8A0"),
|
||||
QColor("#FA879A"), QColor("#F86594"), QColor("#FFE0CC")) }
|
||||
makePreset4(QColor("#FFCAA6"), QColor("#FDA8A0"),
|
||||
QColor("#FA879A"), QColor("#F86594"), QColor("#FFE0CC")) }
|
||||
};
|
||||
|
||||
static int findPresetIndex(const QVector<QColor> &stops)
|
||||
{
|
||||
for (int i = 0; i < PRESETS.size(); ++i) {
|
||||
const auto &pc = PRESETS[i].colors;
|
||||
if (pc.size() != stops.size())
|
||||
continue;
|
||||
bool match = true;
|
||||
for (int j = 0; j < pc.size(); ++j) {
|
||||
if (pc[j].rgb() != stops[j].rgb()) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (match)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
GradientDialog::GradientDialog(const QVector<QColor> &stops,
|
||||
QWidget *parent)
|
||||
: QDialog(parent), m_stops(stops)
|
||||
@@ -99,6 +118,12 @@ GradientDialog::GradientDialog(const QVector<QColor> &stops,
|
||||
mainLay->addLayout(dialogBtns);
|
||||
|
||||
updatePreview();
|
||||
|
||||
int presetIdx = findPresetIndex(m_stops);
|
||||
if (presetIdx >= 0) {
|
||||
QSignalBlocker blocker(m_presetCombo);
|
||||
m_presetCombo->setCurrentIndex(presetIdx);
|
||||
}
|
||||
}
|
||||
|
||||
void GradientDialog::onStopClicked(int index)
|
||||
@@ -128,11 +153,11 @@ void GradientDialog::updatePreview()
|
||||
QLinearGradient grad(0, 0, pix.width(), 0);
|
||||
for (int i = 0; i < m_stops.size(); ++i) {
|
||||
qreal pos = (m_stops.size() == 1) ? 0.5
|
||||
: qreal(i) / (m_stops.size() - 1);
|
||||
: qreal(i) / (m_stops.size() - 1);
|
||||
grad.setColorAt(pos, m_stops[i]);
|
||||
}
|
||||
p.setBrush(grad);
|
||||
p.drawRect(pix.rect());
|
||||
p.end();
|
||||
m_preview->setPixmap(pix);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user