3 #include <QPropertyAnimation>
7 QWidget(parent), selectedPos(-1), _current(-1)
9 setMouseTracking(
true);
15 foreach(
const QGradientStop& stop, stops){
16 double pos = stop.first;
17 if(pos < 0. || pos > 1.)
19 _colors[round(pos * 100.)] = stop.second;
22 #if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
34 if(colors.count() == 1){
35 #if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
36 QColor color = colors.first();
38 QColor color = colors.value(colors.keys().first());
40 _stops.append(QGradientStop(0., color));
41 _stops.append(QGradientStop(1., color));
46 #if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
47 if(!colors.contains(0))
48 colors[0] = colors.first();
49 if(!colors.contains(100))
50 colors[100] = colors.last();
52 if(!colors.contains(0))
53 colors[0] = colors.value(colors.keys().first());
54 if(!colors.contains(100))
55 colors[100] = colors.value(colors.keys().last());
57 QMapIterator<int, QColor> i(colors);
60 _stops.append(QGradientStop(i.key() / 100., i.value()));
67 qreal pos = position / 100.;
68 QVectorIterator<QGradientStop> i(
_stops);
69 QGradientStop previous = i.next();
71 QGradientStop next = i.next();
72 if(next.first >= pos){
75 QColor c1 = next.second, c2 = previous.second;
76 qreal distance = next.first - previous.first;
77 qreal dx = (pos - previous.first) / distance;
78 qreal a1 = c1.alphaF(), a2 = c2.alphaF();
79 qreal a3 = a1 * dx + a2 * (1.f - dx);
84 qreal r = (c1.redF() * a1 * dx + c2.redF() * a2 * (1.f - dx)) / a3;
85 qreal g = (c1.greenF() * a1 * dx + c2.greenF() * a2 * (1.f - dx)) / a3;
86 qreal b = (c1.blueF() * a1 * dx + c2.blueF() * a2 * (1.f - dx)) / a3;
87 return QColor::fromRgbF(r, g, b, a3);
123 QMutableMapIterator<int, QColor> i(
_colors);
128 QColor& value = i.value();
129 if(value.rgb() != rgb)
131 int alpha = value.alpha();
133 value.setAlpha(alpha);
140 int w = (
width() - x * 2) / 16 * 16, h = (height() - 24) / 16 * 16;
141 return QRect(x,
y, w, h);
146 int sx = round(pos / 100. * fill.width()) + fill.x() - 6;
147 int sw = 12, sh = 18;
148 int sy = height() - sh - 2;
149 return QRect(sx, sy, sw, sh);
154 int res = round((
selectedPos - rect.x()) * 100. / rect.width());
155 if(res < 0 || res > 100)
166 QMap<int, QColor> res =
_colors;
167 if(selPos >= 0 && selPos <= 100)
173 QPainter painter(
this);
175 int x = fill.x(),
y = fill.y(), w = fill.width(), h = fill.height();
178 painter.setPen(palette().color(QPalette::Dark));
179 painter.drawLine(x - 1,
y - 1, x + w,
y - 1);
180 painter.drawLine(x - 1,
y - 1, x - 1,
y + h);
181 painter.setPen(palette().color(QPalette::Light));
182 painter.drawLine(x - 1,
y + h, x + w,
y + h);
183 painter.drawLine(x + w,
y - 1, x + w,
y + h);
185 for(
int i = 0; i < w; i += 16){
186 for(
int iy = 0; iy < h; iy += 16){
187 if(i % 32 != iy % 32)
188 painter.fillRect(x + i,
y + iy, 16, 16, QColor(192, 192, 192));
190 painter.fillRect(x + i,
y + iy, 16, 16, QColor(255, 255, 255));
194 painter.setRenderHint(QPainter::Antialiasing);
195 QLinearGradient gradient(x, 0, x + w, 0);
196 gradient.setStops(
_stops);
197 painter.fillRect(fill, QBrush(gradient));
200 QMapIterator<int, QColor> i(
_colors);
201 painter.setPen(palette().color(QPalette::Shadow));
204 painter.setBrush(QBrush(i.value()));
208 painter.setPen(QPen(palette().color(QPalette::Highlight), 2));
209 painter.drawRoundedRect(
stopRect(index), 4, 4);
210 painter.setPen(palette().color(QPalette::Shadow));
212 painter.drawRoundedRect(
stopRect(index), 4, 4);
216 painter.setPen(QPen(palette().color(QPalette::Highlight), 2.5));
218 painter.drawRoundedRect(selectedPos - 6, height() - 18 - 2, 12, 18, 4, 4);
223 int x =
event->x(),
y =
event->y();
224 if(
y < height() - 24)
226 QMapIterator<int, QColor> i(
_colors);
231 if(x >= stop.left() - 1 && x <= stop.right() + 1){
237 if(event->button() == Qt::RightButton &&
_colors.count() > 1){
247 if(x > fill.left() && x < fill.right() &&
event->button() != Qt::RightButton){
257 if(event->y() >= height() - 24)
258 setCursor(QCursor(Qt::PointingHandCursor));
260 setCursor(QCursor(Qt::ArrowCursor));
264 setCursor(QCursor(Qt::ClosedHandCursor));
267 bool last = (
_colors.count() == 0);
270 int left = rect.left(),
right = rect.right();
276 int top = -30, bottom = height() + 30;
277 if(!last && (event->y() < top ||
event->y() > bottom))
292 if(event->y() >= height() - 24)
293 setCursor(QCursor(Qt::PointingHandCursor));
295 setCursor(QCursor(Qt::ArrowCursor));