-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient_stops_edit.cc
203 lines (164 loc) · 4.56 KB
/
gradient_stops_edit.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* SPDX-FileCopyrightText: 2020 Nick Korotysh <[email protected]>
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "gradient_stops_edit.h"
#include <QColorDialog>
#include <QLinearGradient>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
inline bool cmp_pos(const QGradientStop& a, const QGradientStop& b) noexcept
{
return a.first < b.first;
}
static QPixmap create_background()
{
QPixmap pxm(8, 8);
pxm.fill(Qt::lightGray);
QPainter p(&pxm);
QRect qp(0, 0, pxm.width() / 2, pxm.height() / 2);
p.fillRect(qp, Qt::gray);
p.fillRect(qp.translated(qp.width(), qp.height()), Qt::gray);
return pxm;
}
GradientStopsEdit::GradientStopsEdit(const QGradientStops& stops, QWidget* parent)
: QWidget(parent)
, m_background(create_background())
, m_stops(stops)
, m_gradient(100, 1, QImage::Format_ARGB32)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
if (m_stops.isEmpty()) {
m_stops.append({0, Qt::black});
m_stops.append({0.25, Qt::yellow});
m_stops.append({0.75, Qt::red});
m_stops.append({1, Qt::white});
}
updateImage();
}
QSize GradientStopsEdit::sizeHint() const
{
return minimumSizeHint();
}
QSize GradientStopsEdit::minimumSizeHint() const
{
return QSize(160, 20);
}
void GradientStopsEdit::setStops(const QGradientStops& stops)
{
m_stops = stops;
updateImage();
update();
emit stopsChanged(m_stops);
}
void GradientStopsEdit::mousePressEvent(QMouseEvent* event)
{
int stop_index = findStopIndex(event->position().x());
// do not allow to select first and last stops
if (stop_index != 0 && stop_index != m_stops.size() - 1)
m_stop_index = stop_index;
// create new stop on left button click
if (stop_index == -1 && event->button() == Qt::LeftButton) {
addStop(event->position().x() / width());
m_stop_index = findStopIndex(event->position().x());
Q_ASSERT(m_stop_index != -1);
update();
}
event->accept();
}
void GradientStopsEdit::mouseMoveEvent(QMouseEvent* event)
{
if ((event->buttons() & Qt::LeftButton) && (m_stop_index != -1) &&
(4 < event->position().x()) && (event->position().x() < width() - 4)) {
m_stops[m_stop_index].first = event->position().x() / width();
updateImage();
update();
emit stopsChanged(m_stops);
}
event->accept();
}
void GradientStopsEdit::mouseReleaseEvent(QMouseEvent* event)
{
if (m_stop_index != -1) {
if (event->button() == Qt::LeftButton)
std::sort(m_stops.begin(), m_stops.end(), cmp_pos);
if (event->button() == Qt::RightButton)
removeStop(m_stop_index);
m_stop_index = -1;
update();
}
event->accept();
}
void GradientStopsEdit::mouseDoubleClickEvent(QMouseEvent* event)
{
int idx = findStopIndex(event->position().x());
if (idx != -1) {
QColor color = QColorDialog::getColor(
m_stops[idx].second,
window(), QString(),
QColorDialog::ShowAlphaChannel);
if (color.isValid()) {
m_stops[idx].second = color;
updateImage();
update();
emit stopsChanged(m_stops);
}
}
event->accept();
}
void GradientStopsEdit::paintEvent(QPaintEvent* event)
{
QPainter p(this);
QLinearGradient g(0, 0, width(), 0);
g.setStops(m_stops);
p.setPen(Qt::NoPen);
p.setBrush(m_background);
p.drawRect(rect().adjusted(0, 2, 0, -2));
p.setPen(palette().color(QPalette::Dark));
p.setBrush(g);
p.drawRect(rect().adjusted(0, 2, 0, -2));
p.setPen(Qt::gray);
p.setBrush(QColor(255, 255, 255, 180));
for (auto& s : std::as_const(m_stops))
p.drawRoundedRect(QRect(-4, 0, 8, height()).translated(s.first * width(), 0), 2, 2);
event->accept();
}
int GradientStopsEdit::findStopIndex(qreal xpos) const
{
for (int i = 0; i < m_stops.size(); ++i) {
qreal x = m_stops[i].first * width();
constexpr const int dx = 4;
if (x - dx < xpos && xpos < x + dx)
return i;
}
return -1;
}
void GradientStopsEdit::addStop(qreal pos)
{
m_stops.append({pos, pickColor(pos)});
std::sort(m_stops.begin(), m_stops.end(), cmp_pos);
updateImage();
emit stopsChanged(m_stops);
}
void GradientStopsEdit::removeStop(int idx)
{
m_stops.removeAt(idx);
updateImage();
emit stopsChanged(m_stops);
}
QColor GradientStopsEdit::pickColor(qreal pos) const
{
return m_gradient.pixelColor(qRound(pos * m_gradient.width()), 0);
}
void GradientStopsEdit::updateImage()
{
m_gradient.fill(Qt::transparent);
QPainter p(&m_gradient);
QLinearGradient g(0, 0, m_gradient.width(), 0);
g.setStops(m_stops);
p.setBrush(g);
p.setPen(Qt::NoPen);
p.drawRect(m_gradient.rect());
}