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
318
319
320
321
322
323
324
325
326
327 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2008-07-03
* Description : a color gradient slider
*
* SPDX-FileCopyrightText: 2008-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2008 by Cyrille Berger <cberger at cberger dot net>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dgradientslider.h"
// Qt includes
#include <QPainter>
#include <QPoint>
#include <QPen>
#include <QMouseEvent>
namespace Digikam
{
class Q_DECL_HIDDEN DGradientSlider::Private
{
public:
enum Cursor
{
NoCursor = 0,
LeftCursor,
RightCursor,
MiddleCursor
};
public:
Private()
{
middleColor.setRgb((leftColor.red() + rightColor.red()) / 2,
(leftColor.green() + rightColor.green()) / 2,
(leftColor.blue() + rightColor.blue()) / 2);
}
int gradientHeight() const
{
return (parent->height() / 3);
}
int cursorWidth() const
{
return gradientHeight();
}
int gradientWidth() const<--- Shadowed function<--- Shadowed function
{
return (parent->width() - cursorWidth());
}
int gradientOffset() const
{
return (cursorWidth() / 2);
}
public:
bool showMiddleCursor = false;
double leftCursor = 0.0;
double middleCursor = 0.5;
double rightCursor = 1.0;
QColor leftColor = Qt::black;
QColor rightColor = Qt::white;
QColor middleColor;
DGradientSlider* parent = nullptr;
Cursor activeCursor = NoCursor;
};
DGradientSlider::DGradientSlider(QWidget* const parent)
: QWidget(parent),
d (new Private)
{
d->parent = this;
setMinimumWidth(256);
setFixedHeight(20);
}
DGradientSlider::~DGradientSlider()
{
delete d;
}
int DGradientSlider::gradientOffset() const
{
return d->gradientOffset();
}
void DGradientSlider::drawCursorAt(QPainter& painter,
double pos,
const QColor& brushColor,
int width,
int height,
int gradientWidth)<--- Shadow argument
{
painter.setBrush(brushColor);
int pos2 = (int)(gradientWidth * pos);
QPoint points[3];
points[0] = QPoint(pos2, 3 * height - 1);
points[1] = QPoint(pos2 + width / 2, 2 * height);
points[2] = QPoint(pos2 + width, 3 * height - 1);
painter.drawPolygon(points, 3);
}
void DGradientSlider::paintEvent(QPaintEvent*)
{
int grdHeight = d->gradientHeight();
int curWidth = d->cursorWidth();
int grdWidth = d->gradientWidth();
int grdOffset = d->gradientOffset();
QPainter painter(this);
// Draw first gradient
QLinearGradient lrGradient(QPointF(0, 0), QPointF(grdWidth, 0));
lrGradient.setColorAt(0.0, d->leftColor);
lrGradient.setColorAt(1.0, d->rightColor);
painter.setPen(Qt::NoPen);
painter.setBrush(lrGradient);
painter.drawRect(grdOffset, 0, grdWidth, grdHeight);
// Draw second gradient
QLinearGradient lrcGradient(QPointF(0, 0), QPointF(grdWidth, 0));
lrcGradient.setColorAt(d->leftCursor, d->leftColor);
if (d->showMiddleCursor)
{
lrcGradient.setColorAt(d->middleCursor, d->middleColor);
}
lrcGradient.setColorAt(d->rightCursor, d->rightColor);
painter.setBrush(lrcGradient);
painter.drawRect(grdOffset, grdHeight, grdWidth, grdHeight);
// Draw cursors
painter.setPen(palette().color(QPalette::Text));
drawCursorAt(painter, d->leftCursor, d->leftColor, curWidth, grdHeight, grdWidth);
if (d->showMiddleCursor)
{
drawCursorAt(painter, d->middleCursor, d->middleColor, curWidth, grdHeight, grdWidth);
}
drawCursorAt(painter, d->rightCursor, d->rightColor, curWidth, grdHeight, grdWidth);
}
inline bool isCursorClicked(const QPoint& pos,
double cursorPos,
int width,
int height,
int gradientWidth)<--- Shadow argument
{
int pos2 = (int)(gradientWidth * cursorPos);
return ((pos.y() >= 2 * height) &&
(pos.y() < 3 * height) &&
(pos.x() >= pos2) &&
(pos.x() <= (pos2 + width)));
}
void DGradientSlider::mousePressEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
{
int grdHeight = d->gradientHeight();
int curWidth = d->cursorWidth();
int grdWidth = d->gradientWidth();
// Select cursor
if (isCursorClicked(e->pos(), d->leftCursor , curWidth, grdHeight, grdWidth))
{
d->activeCursor = Private::LeftCursor;
}
else if (d->showMiddleCursor && isCursorClicked(e->pos(), d->middleCursor , curWidth, grdHeight, grdWidth))
{
d->activeCursor = Private::MiddleCursor;
}
else if (isCursorClicked(e->pos(), d->rightCursor, curWidth, grdHeight, grdWidth))
{
d->activeCursor = Private::RightCursor;
}
}
}
void DGradientSlider::mouseReleaseEvent(QMouseEvent*)
{
d->activeCursor = Private::NoCursor;
}
void DGradientSlider::mouseMoveEvent(QMouseEvent* e)
{
double v = (e->pos().x() - d->gradientOffset()) / (double) d->gradientWidth();
switch (d->activeCursor)
{
case Private::LeftCursor:
{
setLeftValue(v);
break;
}
case Private::MiddleCursor:
{
setMiddleValue(v);
break;
}
case Private::RightCursor:
{
setRightValue(v);
break;
}
default:
{
break;
}
}
}
void DGradientSlider::leaveEvent(QEvent*)
{
d->activeCursor = Private::NoCursor;
}
void DGradientSlider::showMiddleCursor(bool b)
{
d->showMiddleCursor = b;
}
double DGradientSlider::leftValue() const
{
return d->leftCursor;
}
double DGradientSlider::rightValue() const
{
return d->rightCursor;
}
double DGradientSlider::middleValue() const
{
return d->middleCursor;
}
void DGradientSlider::adjustMiddleValue(double newLeftValue,
double newRightValue)
{
double newDist = newRightValue - newLeftValue;
double oldDist = d->rightCursor - d->leftCursor;
double oldPos = d->middleCursor - d->leftCursor;
d->middleCursor = oldPos * newDist / oldDist + newLeftValue;
}
void DGradientSlider::setRightValue(double v)
{
if ((v <= 1.0) &&
(v > d->leftCursor) &&
(v != d->rightCursor))
{
adjustMiddleValue(d->leftCursor, v);
d->rightCursor = v;
update();
Q_EMIT rightValueChanged(v);
Q_EMIT middleValueChanged(d->middleCursor);
}
}
void DGradientSlider::setLeftValue(double v)
{
if ((v >= 0.0) &&
(v != d->leftCursor) &&
(v < d->rightCursor))
{
adjustMiddleValue(v, d->rightCursor);
d->leftCursor = v;
update();
Q_EMIT leftValueChanged(v);
Q_EMIT middleValueChanged(d->middleCursor);
}
}
void DGradientSlider::setMiddleValue(double v)
{
if ((v > d->leftCursor) && (v != d->middleCursor) && (v < d->rightCursor))
{
d->middleCursor = v;
update();
Q_EMIT middleValueChanged(v);
}
}
void DGradientSlider::setColors(const QColor& lcolor, const QColor& rcolor)
{
d->leftColor = lcolor;
d->rightColor = rcolor;
update();
}
} // namespace Digikam
#include "moc_dgradientslider.cpp"
|