8#include <QtWidgets/qstyleoption.h>
10#include <QtGui/qevent.h>
11#include <QtGui/qpainter.h>
12#include <QtGui/qregion.h>
18 QtColorLine *q_ptr =
nullptr;
19 Q_DECLARE_PUBLIC(QtColorLine)
52 bool isMainPixmapValid()
const;
54 void recreateMainPixmap();
55 QSize pixmapSizeFromGeometrySize(QSize geometrySize)
const;
56 QPixmap gradientPixmap(
int size, Qt::Orientation orientation,
57 QColor begin, QColor end,
bool flipped =
false)
const;
58 QPixmap gradientPixmap(Qt::Orientation orientation,
59 QColor begin, QColor end,
bool flipped =
false)
const;
60 QPixmap hueGradientPixmap(
int size, Qt::Orientation orientation,
bool flipped =
false,
61 int saturation = 0xFF,
int value = 0xFF,
int alpha = 0xFF)
const;
62 QPixmap hueGradientPixmap(Qt::Orientation orientation,
bool flipped =
false,
63 int saturation = 0xFF,
int value = 0xFF,
int alpha = 0xFF)
const;
65 QList<QRect> rects(QPointF point)
const;
67 QColor colorFromPoint(QPointF point)
const;
68 QPointF pointFromColor(QColor color)
const;
72 bool m_flipped =
false;
73 bool m_backgroundCheckered =
true;
75 bool m_dragging =
false;
76 bool m_combiningAlpha =
false;
77 int m_indicatorSize = 22;
78 int m_indicatorSpace = 0;
83 QPixmap m_alphalessPixmap;
84 QPixmap m_semiAlphaPixmap;
85 QSize m_pixmapSize{0, 0};
90 QtColorLine::ColorComponent component;
92 Qt::Orientation orientation;
95 PixData m_lastValidMainPixmapData;
99 : m_point(pointFromColor(m_color))
105 if (m_color == color)
107 if (!color.isValid())
113 m_point = pointFromColor(m_color);
124 if (m_component == component)
128 m_component = component;
130 m_point = pointFromColor(m_color);
145 if (m_indicatorSize == size)
147 m_indicatorSize = size;
148 m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size());
150 q_ptr->updateGeometry();
155 return m_indicatorSize;
164 if (m_indicatorSpace == space)
166 m_indicatorSpace = space;
167 m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size());
173 return m_indicatorSpace;
180 if (m_flipped == flip)
183 m_point = pointFromColor(m_color);
194 if (m_backgroundCheckered == checkered)
196 m_backgroundCheckered = checkered;
202 return m_backgroundCheckered;
209 if (m_orientation == orientation)
212 m_orientation = orientation;
213 if (!q_ptr->testAttribute(Qt::WA_WState_OwnSizePolicy)) {
214 QSizePolicy sp = q_ptr->sizePolicy();
216 q_ptr->setSizePolicy(sp);
217 q_ptr->setAttribute(Qt::WA_WState_OwnSizePolicy,
false);
219 m_point = pointFromColor(m_color);
221 q_ptr->updateGeometry();
226 return m_orientation;
231 switch (m_component) {
232 case QtColorLine::Red:
233 case QtColorLine::Green:
234 case QtColorLine::Blue:
235 if (m_color.spec() != QColor::Rgb)
236 m_color = m_color.toRgb();
238 case QtColorLine::Hue:
239 case QtColorLine::Saturation:
240 case QtColorLine::Value:
241 if (m_color.spec() != QColor::Hsv)
242 m_color = m_color.toHsv();
247 if (m_color.spec() == QColor::Hsv) {
248 if (m_color.hue() == 360 || m_color.hue() == -1) {
249 m_color.setHsvF(0.0, m_color.saturationF(), m_color.valueF(), m_color.alphaF());
256 if (m_mainPixmap.isNull()) {
257 if (m_pixmapSize.isEmpty())
262 if (m_lastValidMainPixmapData.component != m_component)
264 if (m_lastValidMainPixmapData.size != m_pixmapSize)
266 if (m_lastValidMainPixmapData.flipped != m_flipped)
268 if (m_lastValidMainPixmapData.orientation != m_orientation)
270 if (m_lastValidMainPixmapData.color == m_color)
272 switch (m_component) {
273 case QtColorLine::Red:
274 if (m_color.green() == m_lastValidMainPixmapData.color.green() &&
275 m_color.blue() == m_lastValidMainPixmapData.color.blue() &&
276 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
279 case QtColorLine::Green:
280 if (m_color.red() == m_lastValidMainPixmapData.color.red() &&
281 m_color.blue() == m_lastValidMainPixmapData.color.blue() &&
282 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
285 case QtColorLine::Blue:
286 if (m_color.red() == m_lastValidMainPixmapData.color.red() &&
287 m_color.green() == m_lastValidMainPixmapData.color.green() &&
288 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
291 case QtColorLine::Hue:
292 if (m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
293 m_color.value() == m_lastValidMainPixmapData.color.value() &&
294 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
297 case QtColorLine::Saturation:
298 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
299 m_color.value() == m_lastValidMainPixmapData.color.value() &&
300 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
303 case QtColorLine::Value:
304 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
305 m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
306 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
309 case QtColorLine::Alpha:
310 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
311 m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
312 m_color.value() == m_lastValidMainPixmapData.color.value())
320 if (isMainPixmapValid())
323 recreateMainPixmap();
327 QColor begin, QColor end,
bool flipped)
const
329 int size = m_pixmapSize.width();
330 if (orientation == Qt::Vertical)
331 size = m_pixmapSize.height();
332 return gradientPixmap(size, orientation, begin, end, flipped);
336 QColor begin, QColor end,
bool flipped)
const
342 if (orientation == Qt::Horizontal) {
355 QLinearGradient lg(0, 0, gradW, gradH);
356 lg.setColorAt(0, c1);
357 lg.setColorAt(1, c2);
358 QImage img(w, h, QImage::Format_ARGB32);
360 p.setCompositionMode(QPainter::CompositionMode_Source);
361 p.fillRect(QRect(0, 0, w, h), lg);
362 return QPixmap::fromImage(img);
365QPixmap
QtColorLinePrivate::hueGradientPixmap(Qt::Orientation orientation,
bool flipped,
366 int saturation,
int value,
int alpha)
const
368 int size = m_pixmapSize.width();
369 if (orientation == Qt::Vertical)
370 size = m_pixmapSize.height();
371 return hueGradientPixmap(size, orientation, flipped, saturation, value, alpha);
374QPixmap
QtColorLinePrivate::hueGradientPixmap(
int size, Qt::Orientation orientation,
bool flipped,
375 int saturation,
int value,
int alpha)
const
377 int gradW = size + 1;
378 int gradH = size + 1;
381 if (orientation == Qt::Horizontal) {
388 QList<QColor> colorList;
389 colorList << QColor::fromHsv(0, saturation, value, alpha);
390 colorList << QColor::fromHsv(60, saturation, value, alpha);
391 colorList << QColor::fromHsv(120, saturation, value, alpha);
392 colorList << QColor::fromHsv(180, saturation, value, alpha);
393 colorList << QColor::fromHsv(240, saturation, value, alpha);
394 colorList << QColor::fromHsv(300, saturation, value, alpha);
395 colorList << QColor::fromHsv(0, saturation, value, alpha);
396 QLinearGradient lg(0, 0, gradW, gradH);
397 for (
int i = 0; i <= 6; i++)
398 lg.setColorAt(
double(i) / 6.0, flipped ? colorList.at(6 - i) : colorList.at(i));
399 QImage img(w, h, QImage::Format_ARGB32);
401 p.setCompositionMode(QPainter::CompositionMode_Source);
402 p.fillRect(QRect(0, 0, w, h), lg);
403 return QPixmap::fromImage(img);
408 m_lastValidMainPixmapData.size = m_pixmapSize;
409 m_lastValidMainPixmapData.component = m_component;
410 m_lastValidMainPixmapData.color = m_color;
411 m_lastValidMainPixmapData.flipped = m_flipped;
412 m_lastValidMainPixmapData.orientation = m_orientation;
414 if (m_pixmapSize.isEmpty()) {
415 m_mainPixmap = QPixmap();
416 m_alphalessPixmap = QPixmap();
417 m_semiAlphaPixmap = QPixmap();
421 if (m_mainPixmap.size() != m_pixmapSize) {
422 m_mainPixmap = QPixmap(m_pixmapSize);
423 m_alphalessPixmap = QPixmap(m_pixmapSize);
424 m_semiAlphaPixmap = QPixmap(m_pixmapSize);
427 Qt::Orientation orient = m_orientation;
428 const bool flip = m_flipped;
430 const int r = m_color.red();
431 const int g = m_color.green();
432 const int b = m_color.blue();
433 const int h = m_color.hue();
434 const int s = m_color.saturation();
435 const int v = m_color.value();
436 const int a = m_color.alpha();
437 const double coef = 0.5;
438 const int semi = qRound(a * coef + 0xFF * (1.0 - coef));
440 if (m_component == QtColorLine::Hue) {
441 m_alphalessPixmap = hueGradientPixmap(orient, flip, s, v, 0xFF);
442 if (m_combiningAlpha) {
443 m_mainPixmap = hueGradientPixmap(orient, flip, s, v, a);
444 m_semiAlphaPixmap = hueGradientPixmap(orient, flip, s, v, semi);
446 }
else if (m_component == QtColorLine::Saturation) {
447 m_alphalessPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, 0xFF), QColor::fromHsv(h, 0xFF, v, 0xFF), flip);
448 if (m_combiningAlpha) {
449 m_mainPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, a), QColor::fromHsv(h, 0xFF, v, a), flip);
450 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, semi), QColor::fromHsv(h, 0xFF, v, semi), flip);
452 }
else if (m_component == QtColorLine::Value) {
453 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, 0xFF), QColor::fromHsv(h, s, 0xFF, 0xFF), flip);
454 if (m_combiningAlpha) {
455 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, a), QColor::fromHsv(h, s, 0xFF, a), flip);
456 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, semi), QColor::fromHsv(h, s, 0xFF, semi), flip);
458 }
else if (m_component == QtColorLine::Red) {
459 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, 0xFF), QColor::fromRgb(0xFF, g, b, 0xFF), flip);
460 if (m_combiningAlpha) {
461 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, a), QColor::fromRgb(0xFF, g, b, a), flip);
462 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, semi), QColor::fromRgb(0xFF, g, b, semi), flip);
464 }
else if (m_component == QtColorLine::Green) {
465 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, 0xFF), QColor::fromRgb(r, 0xFF, b, 0xFF), flip);
466 if (m_combiningAlpha) {
467 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, a), QColor::fromRgb(r, 0xFF, b, a), flip);
468 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, semi), QColor::fromRgb(r, 0xFF, b, semi), flip);
470 }
else if (m_component == QtColorLine::Blue) {
471 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, 0xFF), QColor::fromRgb(r, g, 0xFF, 0xFF), flip);
472 if (m_combiningAlpha) {
473 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, a), QColor::fromRgb(r, g, 0xFF, a), flip);
474 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, semi), QColor::fromRgb(r, g, 0xFF, semi), flip);
476 }
else if (m_component == QtColorLine::Alpha) {
477 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, b, 0), QColor::fromRgb(r, g, b, 0xFF), flip);
482 if (!m_combiningAlpha && m_component != QtColorLine::Alpha)
483 m_mainPixmap = m_alphalessPixmap;
488 QSize size(m_indicatorSize + 2 * m_indicatorSpace - 1,
489 m_indicatorSize + 2 * m_indicatorSpace - 1);
490 if (m_orientation == Qt::Horizontal)
494 return geometrySize - size;
510 if (m_orientation == Qt::Vertical)
516 switch (m_component) {
517 case QtColorLine::Red:
518 c.setRgbF(pos, m_color.greenF(), m_color.blueF(), m_color.alphaF());
520 case QtColorLine::Green:
521 c.setRgbF(m_color.redF(), pos, m_color.blueF(), m_color.alphaF());
523 case QtColorLine::Blue:
524 c.setRgbF(m_color.redF(), m_color.greenF(), pos, m_color.alphaF());
526 case QtColorLine::Hue:
528 hue *= 35999.0 / 36000.0;
529 c.setHsvF(hue, m_color.saturationF(), m_color.valueF(), m_color.alphaF());
531 case QtColorLine::Saturation:
532 c.setHsvF(m_color.hueF(), pos, m_color.valueF(), m_color.alphaF());
534 case QtColorLine::Value:
535 c.setHsvF(m_color.hueF(), m_color.saturationF(), pos, m_color.alphaF());
537 case QtColorLine::Alpha:
538 c.setHsvF(m_color.hueF(), m_color.saturationF(), m_color.valueF(), pos);
546 qreal hue = color.hueF();
547 if (color.hue() == 360)
550 hue *= 36000.0 / 35999.0;
553 switch (m_component) {
554 case QtColorLine::Red:
557 case QtColorLine::Green:
558 pos = color.greenF();
560 case QtColorLine::Blue:
563 case QtColorLine::Hue:
566 case QtColorLine::Saturation:
567 pos = color.saturationF();
569 case QtColorLine::Value:
570 pos = color.valueF();
572 case QtColorLine::Alpha:
573 pos = color.alphaF();
579 if (m_orientation == Qt::Horizontal)
588 QRect r = q_ptr->geometry();
591 int x1 = (
int)((r.width() - m_indicatorSize - 2 * m_indicatorSpace) * point.x() + 0.5);
592 int x2 = x1 + m_indicatorSize + 2 * m_indicatorSpace;
593 int y1 = (
int)((r.height() - m_indicatorSize - 2 * m_indicatorSpace) * point.y() + 0.5);
594 int y2 = y1 + m_indicatorSize + 2 * m_indicatorSpace;
597 if (m_orientation == Qt::Horizontal) {
599 QRect r0(0, 0, x1, r.height());
600 QRect r1(x1 + m_indicatorSpace, 0, m_indicatorSize, r.height());
601 QRect r2(x2, 0, r.width() - x2, r.height());
603 rects << r0 << r1 << r2;
608 QRect r0(0, 0, r.width(), y1);
609 QRect r1(0, y1 + m_indicatorSpace, r.width(), m_indicatorSize);
610 QRect r2(0, y2, r.width(), r.height() - y2);
612 rects << r0 << r1 << r2;
619 m_pixmapSize = pixmapSizeFromGeometrySize(event->size());
624 QRect rect = q_ptr->rect();
626 QList<QRect> r = rects(m_point);
628 QColor c = colorFromPoint(m_point);
629 if (!m_combiningAlpha && m_component != QtColorLine::Alpha)
633 if (q_ptr->isEnabled()) {
634 if (m_backgroundCheckered) {
636 QPixmap pm(2 * pixSize, 2 * pixSize);
638 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
639 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
640 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
641 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
644 p.setBrushOrigin((rect.width() % pixSize + pixSize) / 2, (rect.height() % pixSize + pixSize) / 2);
646 QRegion region(r[1].adjusted(4, 4, -4, -4));
647 region += QRect(rect.topLeft(),
QPoint(r[1].left() + 0, rect.bottom()));
648 region += QRect(
QPoint(r[1].right() - 0, rect.top()), rect.bottomRight());
649 region += QRect(rect.topLeft(),
QPoint(rect.right(), r[1].top() + 0));
650 region += QRect(
QPoint(rect.left(), r[1].bottom() - 0), rect.bottomRight());
651 p.setClipRegion(region);
652 p.fillRect(rect, pm);
653 p.setBrushOrigin(0, 0);
654 p.setClipping(
false);
659 QSize fieldSize = pixmapSizeFromGeometrySize(q_ptr->geometry().size());
661 QPoint posOnField = r[1].topLeft() - QPoint(m_indicatorSpace, m_indicatorSpace);
662 int x = posOnField.x();
663 int y = posOnField.y();
664 int w = fieldSize.width();
665 int h = fieldSize.height();
668 if (m_orientation == Qt::Horizontal) {
669 r0 = QRect(0, 0, x, m_pixmapSize.height());
670 r2 = QRect(x + 1, 0, w - x - 1, m_pixmapSize.height());
672 r0 = QRect(0, 0, m_pixmapSize.width(), y);
673 r2 = QRect(0, y + 1, m_pixmapSize.width(), h - y - 1);
676 p.setBrush(m_mainPixmap);
678 if (r[0].isValid()) {
681 if (r[2].isValid()) {
682 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
685 if (m_indicatorSpace) {
687 if (m_orientation == Qt::Horizontal) {
688 p.drawRect(r[1].adjusted(-m_indicatorSpace, 0, -r[1].width(), 0));
689 p.drawRect(r[1].adjusted(r[1].width(), 0, m_indicatorSpace, 0));
691 p.drawRect(r[1].adjusted(0, -m_indicatorSpace, 0, -r[1].height()));
692 p.drawRect(r[1].adjusted(0, r[1].height(), 0, m_indicatorSpace));
698 p.setBrush(Qt::NoBrush);
699 if (r[1].isValid()) {
700 p.drawRect(r[1].adjusted(0, 0, -1, -1));
703 double coef = 9.0 / 10;
705 if (m_component != QtColorLine::Alpha && m_combiningAlpha) {
706 p.setBrush(m_alphalessPixmap);
707 if (r[0].isValid()) {
708 p.setBrushOrigin(QPoint(0, 0));
709 QRect thinRect1 = r[0];
710 QRect thinRect2 = r[0];
711 QRect thinRect = r[0];
712 if (m_orientation == Qt::Horizontal) {
713 thinRect1.adjust(0, qRound(thinRect1.height() * coef), 0, 0);
714 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef));
715 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
717 thinRect1.adjust(qRound(thinRect1.width() * coef), 0, 0, 0);
718 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef), 0);
719 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
721 p.drawRect(thinRect1);
722 p.drawRect(thinRect2);
725 if (r[2].isValid()) {
726 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
727 QRect thinRect1 = r[2];
728 QRect thinRect2 = r[2];
729 QRect thinRect = r[2];
730 if (m_orientation == Qt::Horizontal) {
731 thinRect1.adjust(0, qRound(thinRect1.height() * coef), 0, 0);
732 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef));
733 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
735 thinRect1.adjust(qRound(thinRect1.width() * coef), 0, 0, 0);
736 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef), 0);
737 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
739 p.drawRect(thinRect1);
740 p.drawRect(thinRect2);
744
745
753 p.setBrush(m_semiAlphaPixmap);
754 if (r[0].isValid()) {
755 p.setBrushOrigin(QPoint(0, 0));
756 QRect thinRect1 = r[0];
757 QRect thinRect2 = r[0];
758 QRect thinRect = r[0];
759 if (m_orientation == Qt::Horizontal) {
760 thinRect1.adjust(0, qRound(thinRect1.height() * coef) - 1, 0, 0);
761 thinRect1.setBottom(thinRect1.top());
762 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef) + 1);
763 thinRect2.setTop(thinRect2.bottom());
764 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
766 thinRect1.adjust(qRound(thinRect1.width() * coef) - 1, 0, 0, 0);
767 thinRect1.setRight(thinRect1.left());
768 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef) + 1, 0);
769 thinRect2.setLeft(thinRect2.right());
770 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
772 p.drawRect(thinRect1);
773 p.drawRect(thinRect2);
776 if (r[2].isValid()) {
777 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
778 QRect thinRect1 = r[2];
779 QRect thinRect2 = r[2];
780 QRect thinRect = r[2];
781 if (m_orientation == Qt::Horizontal) {
782 thinRect1.adjust(0, qRound(thinRect1.height() * coef) - 1, 0, 0);
783 thinRect1.setBottom(thinRect1.top());
784 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef) + 1);
785 thinRect2.setTop(thinRect2.bottom());
786 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
788 thinRect1.adjust(qRound(thinRect1.width() * coef) - 1, 0, 0, 0);
789 thinRect1.setRight(thinRect1.left());
790 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef) + 1, 0);
791 thinRect2.setLeft(thinRect2.right());
792 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
794 p.drawRect(thinRect1);
795 p.drawRect(thinRect2);
798 p.setBrush(m_alphalessPixmap);
800 if (m_orientation == Qt::Horizontal) {
801 region += r[1].adjusted(0, qRound(r[1].height() * coef), 0, 0);
802 region += r[1].adjusted(0, 0, 0, -qRound(r[1].height() * coef));
803 p.setClipRegion(region);
805 region += r[1].adjusted(qRound(r[1].width() * coef), 0, 0, 0);
806 region += r[1].adjusted(0, 0, -qRound(r[1].width() * coef), 0);
807 p.setClipRegion(region);
809 p.setClipRegion(region);
810 p.setBrush(Qt::NoBrush);
811 p.setPen(QPen(QColor(c.rgb())));
813 p.drawRect(r[1].adjusted(0, 0, -1, -1));
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833 p.setClipping(
false);
837 p.setBrush(Qt::NoBrush);
841 r[1].adjust(br, br, -br, -br);
842 if (r[1].adjusted(lw, lw, -lw, -lw).isValid()) {
843 QStyleOptionFrame opt;
847 opt.midLineWidth = 1;
849 opt.state |= QStyle::State_Sunken;
851 opt.state |= QStyle::State_Raised;
852 q_ptr->style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, q_ptr);
853 QRect colorRect = r[1].adjusted(lw, lw, -lw, -lw);
854 if (q_ptr->isEnabled()) {
855 p.fillRect(colorRect, c);
856 const QColor frameColor(0, 0, 0, 38);
857 p.setPen(frameColor);
858 p.drawRect(colorRect.adjusted(0, 0, -1, -1));
860
861
862
863
864
865
867
868
869
870
871
878 if (event->button() != Qt::LeftButton)
881 QList<QRect> r = rects(m_point);
882 QPoint clickPos = event->position().toPoint();
884 QPoint posOnField = r[1].topLeft() - QPoint(m_indicatorSpace, m_indicatorSpace);
885 m_clickOffset = posOnField - clickPos;
887 if (!r[1].contains(clickPos))
897 QPoint newPos = event->position().toPoint();
899 QSize fieldSize = q_ptr->geometry().size() -
900 QSize(m_indicatorSize + 2 * m_indicatorSpace - 1, m_indicatorSize + 2 * m_indicatorSpace - 1);
901 QPoint newPosOnField = newPos + m_clickOffset;
902 if (newPosOnField.x() < 0)
903 newPosOnField.setX(0);
904 else if (newPosOnField.x() > fieldSize.width())
905 newPosOnField.setX(fieldSize.width());
906 if (newPosOnField.y() < 0)
907 newPosOnField.setY(0);
908 else if (newPosOnField.y() > fieldSize.height())
909 newPosOnField.setY(fieldSize.height());
911 const double x =
double(newPosOnField.x()) / fieldSize.width();
912 const double y =
double(newPosOnField.y()) / fieldSize.height();
913 m_point = QPointF(x, y);
914 QColor color = colorFromPoint(m_point);
915 if (m_color == color)
918 emit q_ptr->colorChanged(color);
924 if (event->button() != Qt::LeftButton)
932 if (event->button() != Qt::LeftButton)
935 QList<QRect> r = rects(m_point);
936 QPoint clickPos = event->position().toPoint();
937 if (!r[0].contains(clickPos) && !r[2].contains(clickPos))
939 QPoint newPosOnField = clickPos;
940 if (r[2].contains(clickPos))
941 newPosOnField -= QPoint(m_indicatorSize + 2 * m_indicatorSpace - 2, m_indicatorSize + 2 * m_indicatorSpace - 2);
942 QSize fieldSize = q_ptr->geometry().size() -
943 QSize(m_indicatorSize + 2 * m_indicatorSpace - 1, m_indicatorSize + 2 * m_indicatorSpace - 1);
945 const double x =
double(newPosOnField.x()) / fieldSize.width();
946 const double y =
double(newPosOnField.y()) / fieldSize.height();
947 m_point = QPointF(x, y);
948 QColor color = colorFromPoint(m_point);
949 if (m_color == color)
952 emit q_ptr->colorChanged(color);
958QtColorLine::QtColorLine(QWidget *parent)
959 : QWidget(parent), d_ptr(
new QtColorLinePrivate)
963 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
966QtColorLine::~QtColorLine()
970QSize QtColorLine::minimumSizeHint()
const
972 return QSize(d_ptr->m_indicatorSize, d_ptr->m_indicatorSize);
975QSize QtColorLine::sizeHint()
const
977 return QSize(d_ptr->m_indicatorSize, d_ptr->m_indicatorSize);
980void QtColorLine::setColor(QColor color)
982 d_ptr->setColor(color);
985QColor QtColorLine::color()
const
987 return d_ptr->color();
990void QtColorLine::setColorComponent(QtColorLine::ColorComponent component)
992 d_ptr->setColorComponent(component);
995QtColorLine::ColorComponent QtColorLine::colorComponent()
const
997 return d_ptr->colorComponent();
1000void QtColorLine::setIndicatorSize(
int size)
1002 d_ptr->setIndicatorSize(size);
1005int QtColorLine::indicatorSize()
const
1007 return d_ptr->indicatorSize();
1010void QtColorLine::setIndicatorSpace(
int space)
1012 d_ptr->setIndicatorSpace(space);
1015int QtColorLine::indicatorSpace()
const
1017 return d_ptr->indicatorSpace();
1020void QtColorLine::setFlip(
bool flip)
1022 d_ptr->setFlip(flip);
1025bool QtColorLine::flip()
const
1027 return d_ptr->flip();
1030void QtColorLine::setBackgroundCheckered(
bool checkered)
1032 d_ptr->setBackgroundCheckered(checkered);
1035bool QtColorLine::isBackgroundCheckered()
const
1037 return d_ptr->isBackgroundCheckered();
1040void QtColorLine::setOrientation(Qt::Orientation orientation)
1042 d_ptr->setOrientation(orientation);
1045Qt::Orientation QtColorLine::orientation()
const
1047 return d_ptr->orientation();
1049void QtColorLine::resizeEvent(QResizeEvent *event)
1051 d_ptr->resizeEvent(event);
1054void QtColorLine::paintEvent(QPaintEvent *event)
1056 d_ptr->paintEvent(event);
1059void QtColorLine::mousePressEvent(QMouseEvent *event)
1061 d_ptr->mousePressEvent(event);
1064void QtColorLine::mouseMoveEvent(QMouseEvent *event)
1066 d_ptr->mouseMoveEvent(event);
1069void QtColorLine::mouseReleaseEvent(QMouseEvent *event)
1071 d_ptr->mouseReleaseEvent(event);
1074void QtColorLine::mouseDoubleClickEvent(QMouseEvent *event)
1076 d_ptr->mouseDoubleClickEvent(event);
\inmodule QtCore\reentrant
void setIndicatorSize(int size)
void mouseDoubleClickEvent(QMouseEvent *event)
Qt::Orientation orientation() const
void mouseReleaseEvent(QMouseEvent *event)
void setOrientation(Qt::Orientation orientation)
void setColor(QColor color)
int indicatorSize() const
void mouseMoveEvent(QMouseEvent *event)
void paintEvent(QPaintEvent *event)
int indicatorSpace() const
void setIndicatorSpace(int space)
void resizeEvent(QResizeEvent *event)
QtColorLine::ColorComponent colorComponent() const
void setColorComponent(QtColorLine::ColorComponent component)
bool isBackgroundCheckered() const
void setBackgroundCheckered(bool checkered)
void mousePressEvent(QMouseEvent *event)