-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetgradienteditor.cpp
317 lines (277 loc) · 10 KB
/
widgetgradienteditor.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "widgetgradienteditor.h"
#include <QPainter>
#include <QDebug>
#include <QColor>
#include <QMouseEvent>
#include <QColorDialog>
WidgetGradientEditor::WidgetGradientEditor(QWidget *parent)
: QWidget(parent),
mPadding(0.1),
mSpreadMode(QGradient::PadSpread)
{
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
setToolTip("");
setFocusPolicy(Qt::StrongFocus);
mRectGradient = QRect(QPoint(0,0), QPoint(0,0));
setMinimumSize(parent->width() - 10, 25);
setMouseTracking(true);
slotReset();
}
const QMap<float, QColor> WidgetGradientEditor::getGradient() const
{
QMap<float, QColor> gradientStops;
for(int i=0;i<mMarkers.size();i++)
gradientStops.insert(mMarkers[i].position, mMarkers[i].color);
return gradientStops;
}
void WidgetGradientEditor::setGradient(const QMap<float, QColor> stops)
{
if(stops.size() < 2)
{
slotReset();
return;
}
mMarkers.clear();
QMapIterator<float, QColor> i(stops);
while (i.hasNext())
{
i.next();
mMarkers.append(GradientMarker(i.key(), i.value()));
}
}
const QString WidgetGradientEditor::gradientToString(const QMap<float, QColor> stops)
{
QString config;
QMapIterator<float, QColor> i(stops);
while (i.hasNext())
{
i.next();
config.append(
QString("%1%2,%3,%4,%5")
.arg(i.hasPrevious() ? ":":"")
.arg(i.key())
.arg(i.value().red())
.arg(i.value().green())
.arg(i.value().blue()));
}
return config;
}
QMap<float, QColor> WidgetGradientEditor::stringToGradient(const QString config)
{
QMap<float, QColor> gradientStops;
const QStringList stops = config.split(":", QString::SkipEmptyParts);
for(int i=0;i<stops.size();i++)
{
const QStringList props = stops.at(i).split(",");
if(props.size() == 4) gradientStops.insert(props.at(0).toFloat(), QColor(props.at(1).toInt(), props.at(2).toInt(), props.at(3).toInt()));
}
return gradientStops;
}
void WidgetGradientEditor::slotReset(const Preset &preset)
{
mMarkerIsReadyToMove = false;
mMarkerHasBeenMoved = false;
mMarkers.clear();
// do not emit a changed signal for every single marker!
blockSignals(true);
if(preset == PresetJet)
{
slotAddMarker(QColor(255,000,000), 1.00);
slotAddMarker(QColor(255,255,000), 0.75);
slotAddMarker(QColor(000,255,000), 0.50);
slotAddMarker(QColor(000,255,255), 0.25);
slotAddMarker(QColor(000,000,255), 0.00);
}
else if(preset == PresetJetDark)
{
slotAddMarker(QColor(255,128,000), 0.75);
slotAddMarker(QColor(255,000,000), 1.00);
slotAddMarker(QColor(000,128,255), 0.25);
slotAddMarker(QColor(000,128,000), 0.50);
slotAddMarker(QColor(000,000,255), 0.00);
}
else if(preset == PresetEarth)
{
slotAddMarker(QColor(000,128,255), 0.25);
slotAddMarker(QColor(000,128,000), 0.50);
}
blockSignals(false);
emit gradientChanged(getGradient());
update();
}
void WidgetGradientEditor::slotAddMarker(const QColor &color, float position, const bool isMovedByMouse)
{
GradientMarker marker;
marker.color = color;
marker.position = position;
if(isMovedByMouse)
{
marker.hasFocus = true;
mMarkerIsReadyToMove = true;
// clear all other marker's focus!
for(int i=0;i<mMarkers.size();i++)
{
Q_ASSERT(!mMarkers[i].hasFocus);
mMarkers[i].hasFocus = false;
qDebug() << "addMarker: marker" << i << "has focus:" << mMarkers[i].hasFocus;
}
}
mMarkers.append(marker);
emit gradientChanged(getGradient());
}
void WidgetGradientEditor::removeMarker(int index)
{
if(mMarkers.size() <= 2 || mMarkers.size() <= index)
{
return;
}
mMarkers.removeAt(index);
update();
emit gradientChanged(getGradient());
}
void WidgetGradientEditor::paintEvent(QPaintEvent *)
{
if(mRectView.size().isNull() || mRectView.size().isEmpty() || mRectView.topLeft() == mRectView.bottomRight())
{
mRectView = QRect(QPoint(0,0), QPoint(width(), height()));
mRectGradient = mRectView.adjusted(
width() * mPadding, // left padding
0, // top padding
-width() * mPadding, // right padding
0); // bottom padding
}
QPainter painter(this);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.setRenderHint(QPainter::Antialiasing);
QPoint gradStart = QPoint(mRectGradient.topLeft ().x(), mRectGradient.bottomLeft ().y()/2);
QPoint gradStop = QPoint(mRectGradient.topRight().x(), mRectGradient.bottomRight().y()/2);
QLinearGradient gradient(gradStart, gradStop);
gradient.setSpread(mSpreadMode);
for(int i=0;i<mMarkers.size();i++)
{
const GradientMarker& marker = mMarkers.at(i);
gradient.setColorAt(marker.position, marker.color);
}
painter.fillRect(mRectView.adjusted(0, 1, 0, -12), QBrush(gradient)); // 12 px bottom padding - leave room for sliders!
painter.setPen(QPen(palette().color(QPalette::WindowText)));
painter.drawLine(mRectGradient.topLeft(), mRectGradient.bottomLeft());
painter.drawLine(mRectGradient.topRight(), mRectGradient.bottomRight());
painter.setPen(QPen(palette().color(QPalette::WindowText)));
QPen pen;
pen.setWidth(3);
for(int i=0;i<mMarkers.size();i++)
{
/*painter.drawPie(
QRect(
mRectGradient.left() + mMarkers[i].position*mRectGradient.width() - 8,
height()-17,
16, 16),
-45*16, // start angle * 16
-90*16); // span angle * 16*/
pen.setColor(mMarkers[i].color);
painter.setPen(pen);
painter.drawEllipse(QRect(mRectGradient.left() + mMarkers[i].position*mRectGradient.width() - 2, height()-7, 4, 4));
painter.setPen(QPen(palette().color(QPalette::WindowText)));
painter.drawEllipse(QRect(mRectGradient.left() + mMarkers[i].position*mRectGradient.width() - 4, height()-9, 8, 8));
}
painter.end();
}
void WidgetGradientEditor::resizeEvent(QResizeEvent * event)
{
mRectView = QRect();
update();
}
void WidgetGradientEditor::mousePressEvent(QMouseEvent *event)
{
dragStart = event->pos();
//qDebug() << "viewrect:" << viewRect;
for(int i=0;i<mMarkers.size();i++)
{
GradientMarker& marker = mMarkers[i];//it.value();
const int pixelPosOfMarker = mRectGradient.left() + marker.position * mRectGradient.width();
if(abs(dragStart.x() - pixelPosOfMarker) < 9)
{
if(event->button() == Qt::LeftButton)
{
marker.hasFocus = true;
mMarkerIsReadyToMove = true;
mMarkerHasBeenMoved = false;
}
else
{
removeMarker(i);
update();
emit gradientChanged(getGradient());
return;
}
}
else
{
marker.hasFocus = false;
}
}
if(!mMarkerIsReadyToMove && event->button() == Qt::LeftButton)
{
// We did not find a marker whose poly covers the clicked point, so the user doesn't want to move a marker. Add a marker here!
const float markerPos = (float)(event->pos().x() - mRectGradient.left())/(mRectGradient.width() - 5);
qDebug() << "adding marker at" << markerPos;
//mMarkerIsReadyToMove = true;
QColor colorSelected = QColorDialog::getColor(QColor(((float)qrand())/RAND_MAX*255, (float)qrand()/RAND_MAX*255, (float)qrand()/RAND_MAX*255), this, "Select step color");
if(colorSelected.isValid()) slotAddMarker(colorSelected, markerPos/*, mMarkerIsReadyToMove*/);
}
update();
}
void WidgetGradientEditor::mouseMoveEvent(QMouseEvent *event)
{
if(!mMarkerIsReadyToMove) return;
for(int i=0;i<mMarkers.size();i++)
{
GradientMarker& marker = mMarkers[i];
if(marker.hasFocus)
{
const int pixelPosOfMarker = mRectGradient.left() + marker.position * mRectGradient.width();
const float dPos = (float)(event->pos().x() - dragStart.x())/((qreal)mRectGradient.width());
// We always set this true. If the user wants to drag a slider at 0.0 to the left or at 1.0 to the right, it won't work.
// But we shouldn't show a color-dialog after releasing the mouse!
mMarkerHasBeenMoved = true;
if(marker.position + dPos > 1 || marker.position + dPos < 0) break; // do not move beyond borders
if(dPos > 0 && event->pos().x() < pixelPosOfMarker) break; // sync mouse cursor with slider before moving
if(dPos < 0 && event->pos().x() > pixelPosOfMarker) break; // sync mouse cursor with slider before moving
marker.position += dPos;
emit gradientChanged(getGradient());
break;
}
}
dragStart = event->pos();
update();
}
void WidgetGradientEditor::mouseReleaseEvent(QMouseEvent *)
{
mMarkerIsReadyToMove = false;
if(mMarkerHasBeenMoved)
{
// make sure that marker doesn't completely overlap with neighbors!
}
else
{
GradientMarker& marker = mMarkers[0];
int index = -1;
for(int i=0;i<mMarkers.size();i++)
{
if(mMarkers[i].hasFocus) index = i;
}
if(index != -1)
{
// A marker was clicked/released without being moved. Change color!
QColor newColor = QColorDialog::getColor(marker.color, this, "Select step color");
if(newColor.isValid())
{
qDebug() << "marker 1" << marker.color;
mMarkers[index].color = newColor;
qDebug() << "marker 2" << mMarkers[index].color;
update();
emit gradientChanged(getGradient());
}
}
}
}