Krita Source Code Documentation
Loading...
Searching...
No Matches
KisGradientColorEditor.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021 Deif Lou <ginoba@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include <QToolButton>
8#include <QCheckBox>
9#include <QHBoxLayout>
10
11#include <kis_slider_spin_box.h>
12#include <kis_color_button.h>
13#include <kis_icon_utils.h>
15
17
18class Q_DECL_HIDDEN KisGradientColorEditor::Private
19{
20public:
21 KisDoubleSliderSpinBox *positionSlider{nullptr};
22 QToolButton *colorTypeForegroundButton{nullptr};
23 QToolButton *colorTypeBackgroundButton{nullptr};
24 QToolButton *colorTypeCustomButton{nullptr};
25 QCheckBox *transparentCheckBox{nullptr};
26 KisColorButton *colorButton{nullptr};
27 KisDoubleSliderSpinBox *opacitySlider{nullptr};
28};
29
31 : QWidget(parent)
32 , m_d(new Private)
33{
34 m_d->colorTypeForegroundButton = new QToolButton;
35 m_d->colorTypeForegroundButton->setCheckable(true);
36 m_d->colorTypeForegroundButton->setChecked(true);
37 m_d->colorTypeForegroundButton->setAutoExclusive(true);
38 m_d->colorTypeForegroundButton->setAutoRaise(true);
39 m_d->colorTypeForegroundButton->setIcon(KisIconUtils::loadIcon("object-order-lower-calligra"));
40 m_d->colorTypeForegroundButton->setToolTip(i18nc("Button to change the gradient stop type to foreground", "Foreground color"));
41 connect(m_d->colorTypeForegroundButton, &QToolButton::toggled,
42 [this](bool toggled)
43 {
44 if (toggled) {
45 setColorType(KisGradientWidgetsUtils::Foreground);
46 }
48 });
49
50 m_d->colorTypeBackgroundButton = new QToolButton;
51 m_d->colorTypeBackgroundButton->setCheckable(true);
52 m_d->colorTypeBackgroundButton->setAutoExclusive(true);
53 m_d->colorTypeBackgroundButton->setAutoRaise(true);
54 m_d->colorTypeBackgroundButton->setIcon(KisIconUtils::loadIcon("object-order-raise-calligra"));
55 m_d->colorTypeBackgroundButton->setToolTip(i18nc("Button to change the gradient stop type to background", "Background color"));
56 connect(m_d->colorTypeBackgroundButton, &QToolButton::toggled,
57 [this](bool toggled)
58 {
59 if (toggled) {
60 setColorType(KisGradientWidgetsUtils::Background);
61 }
62 Q_EMIT colorTypeChanged(KisGradientWidgetsUtils::Background);
63 });
64
65 m_d->colorTypeCustomButton = new QToolButton;
66 m_d->colorTypeCustomButton->setCheckable(true);
67 m_d->colorTypeCustomButton->setAutoExclusive(true);
68 m_d->colorTypeCustomButton->setAutoRaise(true);
69 m_d->colorTypeCustomButton->setIcon(KisIconUtils::loadIcon("wheel-sectors"));
70 m_d->colorTypeCustomButton->setToolTip(i18nc("Button to change the gradient stop type to custom color", "Custom color"));
71 connect(m_d->colorTypeCustomButton, &QToolButton::toggled,
72 [this](bool toggled)
73 {
74 if (toggled) {
75 setColorType(KisGradientWidgetsUtils::Custom);
76 }
77 Q_EMIT colorTypeChanged(KisGradientWidgetsUtils::Custom);
78 });
79
80 QWidget *colorTypeButtonsContainer = new QWidget;
81
82 m_d->transparentCheckBox = new QCheckBox;
83 m_d->transparentCheckBox->setText(i18n("Transparent"));
84 m_d->transparentCheckBox->setProperty("isBeingUsed", true);
85 connect(m_d->transparentCheckBox, SIGNAL(toggled(bool)), this, SIGNAL(transparentToggled(bool)));
86
87 m_d->colorButton = new KisColorButton;
88 m_d->colorButton->setVisible(false);
89 connect(m_d->colorButton, SIGNAL(changed(KoColor)), this, SIGNAL(colorChanged(KoColor)));
90
91 m_d->opacitySlider = new KisDoubleSliderSpinBox;
92 m_d->opacitySlider->setRange(0, 100, 2);
93 KisSpinBoxI18nHelper::setText(m_d->opacitySlider,
94 i18nc("{n} is the number value, % is the percent sign", "Opacity: {n}%"));
95 m_d->opacitySlider->setVisible(false);
96 connect(m_d->opacitySlider, SIGNAL(valueChanged(double)), this, SIGNAL(opacityChanged(qreal)));
97
98 m_d->positionSlider = new KisDoubleSliderSpinBox;
99 m_d->positionSlider->setRange(0, 100, 2);
100 KisSpinBoxI18nHelper::setText(m_d->positionSlider,
101 i18nc("{n} is the number value, % is the percent sign", "Position: {n}%"));
102 connect(m_d->positionSlider, SIGNAL(valueChanged(double)), this, SIGNAL(positionChanged(qreal)));
103
104 QHBoxLayout *colorTypeButtonsLayout = new QHBoxLayout;
105 colorTypeButtonsLayout->setContentsMargins(0, 0, 0, 0);
106 colorTypeButtonsLayout->setSpacing(0);
107 colorTypeButtonsLayout->addWidget(m_d->colorTypeForegroundButton);
108 colorTypeButtonsLayout->addWidget(m_d->colorTypeBackgroundButton);
109 colorTypeButtonsLayout->addWidget(m_d->colorTypeCustomButton);
110 colorTypeButtonsContainer->setLayout(colorTypeButtonsLayout);
111
112 QHBoxLayout *mainLayout = new QHBoxLayout;
113 mainLayout->setContentsMargins(0, 0, 0, 0);
114 mainLayout->setSpacing(5);
115 mainLayout->addWidget(colorTypeButtonsContainer);
116 mainLayout->addWidget(m_d->transparentCheckBox);
117 mainLayout->addWidget(m_d->colorButton);
118 mainLayout->addWidget(m_d->opacitySlider);
119 mainLayout->addStretch();
120 mainLayout->addWidget(m_d->positionSlider);
121 setLayout(mainLayout);
122
123 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
124}
125
127 : QWidget()
128{
129 setPosition(other.position());
130 setColorType(other.colorType());
132 setColor(other.color());
133 setOpacity(other.opacity());
134}
135
138
140{
141 return m_d->positionSlider->value();
142}
143
145{
146 if (m_d->colorTypeForegroundButton->isChecked()) {
148 } else if (m_d->colorTypeBackgroundButton->isChecked()) {
150 } else {
152 }
153}
154
156{
157 return m_d->colorButton->color();
158}
159
161{
162 return m_d->transparentCheckBox->isChecked();
163}
164
166{
167 return m_d->opacitySlider->value();
168}
169
171{
172 m_d->positionSlider->setValue(position);
173}
174
176{
178 m_d->colorTypeForegroundButton->setChecked(true);
179 } else if (type == KisGradientWidgetsUtils::Background) {
180 m_d->colorTypeBackgroundButton->setChecked(true);
181 } else {
182 m_d->colorTypeCustomButton->setChecked(true);
183 }
184
185 // "if" to avoid flickering. setUpdatesEnabled doesn't seems to work here?
187 m_d->transparentCheckBox->setVisible(false);
188 m_d->colorButton->setVisible(true);
189 m_d->opacitySlider->setVisible(true);
190 } else {
191 m_d->colorButton->setVisible(false);
192 m_d->opacitySlider->setVisible(false);
193 m_d->transparentCheckBox->setVisible(m_d->transparentCheckBox->property("isBeingUsed").toBool());
194 }
195
196 if (type != colorType()) {
197 Q_EMIT colorTypeChanged(type);
198 }
199}
200
202{
203 m_d->transparentCheckBox->setChecked(checked);
204}
205
207{
208 color.setOpacity(1.0);
209 if (color == m_d->colorButton->color()) {
210 return;
211 }
212 m_d->colorButton->setColor(color);
213}
214
216{
217 m_d->opacitySlider->setValue(opacity);
218}
219
221{
222 m_d->transparentCheckBox->setProperty("isBeingUsed", use);
224 m_d->transparentCheckBox->setVisible(use);
225 }
226}
227
229{
230 m_d->positionSlider->setVisible(use);
231}
232
234{
235 m_d->positionSlider->setEnabled(enabled);
236}
237
239{
240 return QSize(
241 m_d->colorTypeForegroundButton->sizeHint().width() +
242 m_d->colorTypeBackgroundButton->sizeHint().width() +
243 m_d->colorTypeCustomButton->sizeHint().width() +
244 qMax(
245 m_d->transparentCheckBox->sizeHint().width(),
246 m_d->colorButton->sizeHint().width() +
247 m_d->opacitySlider->sizeHint().width()
248 ) +
249 (m_d->positionSlider->isVisible() ? m_d->positionSlider->sizeHint().width() : 0) +
250 15, // spacing
251
252 qMax(
253 m_d->colorTypeForegroundButton->sizeHint().height(),
254 qMax(
255 m_d->transparentCheckBox->sizeHint().height(),
256 qMax(
257 m_d->colorButton->sizeHint().height(),
258 m_d->opacitySlider->sizeHint().height()
259 )
260 )
261 )
262 );
263}
264
266{
267 return QSize(
268 m_d->colorTypeForegroundButton->minimumSizeHint().width() +
269 m_d->colorTypeBackgroundButton->minimumSizeHint().width() +
270 m_d->colorTypeCustomButton->minimumSizeHint().width() +
271 qMax(
272 m_d->transparentCheckBox->minimumSizeHint().width(),
273 m_d->colorButton->minimumSizeHint().width() +
274 m_d->opacitySlider->minimumSizeHint().width()
275 ) +
276 (m_d->positionSlider->isVisible() ? m_d->positionSlider->minimumSizeHint().width() : 0) +
277 15, // spacing
278
279 qMax(
280 m_d->colorTypeForegroundButton->minimumSizeHint().height(),
281 qMax(
282 m_d->transparentCheckBox->minimumSizeHint().height(),
283 qMax(
284 m_d->colorButton->minimumSizeHint().height(),
285 m_d->opacitySlider->minimumSizeHint().height()
286 )
287 )
288 )
289 );
290}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
A pushbutton to display or allow user selection of a color.
This class is a spinbox in which you can click and drag to set the value. A slider like bar is displa...
void setRange(qreal newMinimum, qreal newMaximum, int newNumberOfDecimals=0, bool computeNewFastSliderStep=true)
Set the minimum and the maximum values of the range.
QSize sizeHint() const override
QScopedPointer< Private > m_d
void setPositionSliderEnabled(bool enabled)
QSize minimumSizeHint() const override
void colorTypeChanged(KisGradientWidgetsUtils::ColorType type)
KisGradientColorEditor(QWidget *parent=nullptr)
KisGradientWidgetsUtils::ColorType colorType() const
void setColorType(KisGradientWidgetsUtils::ColorType type)
void setOpacity(quint8 alpha)
Definition KoColor.cpp:333
QIcon loadIcon(const QString &name)
void setText(QSpinBox *spinBox, const QStringView textTemplate)