7#include <QtGui/QPainter>
8#include <QtGui/QPaintEvent>
9#include <QtWidgets/QStyleOption>
10#include <QtGui/QRegion>
16 QtColorLine *q_ptr =
nullptr;
17 Q_DECLARE_PUBLIC(QtColorLine)
50 bool isMainPixmapValid()
const;
52 void recreateMainPixmap();
53 QSize pixmapSizeFromGeometrySize(QSize geometrySize)
const;
54 QPixmap gradientPixmap(
int size, Qt::Orientation orientation,
55 QColor begin, QColor end,
bool flipped =
false)
const;
56 QPixmap gradientPixmap(Qt::Orientation orientation,
57 QColor begin, QColor end,
bool flipped =
false)
const;
58 QPixmap hueGradientPixmap(
int size, Qt::Orientation orientation,
bool flipped =
false,
59 int saturation = 0xFF,
int value = 0xFF,
int alpha = 0xFF)
const;
60 QPixmap hueGradientPixmap(Qt::Orientation orientation,
bool flipped =
false,
61 int saturation = 0xFF,
int value = 0xFF,
int alpha = 0xFF)
const;
63 QList<QRect> rects(QPointF point)
const;
65 QColor colorFromPoint(QPointF point)
const;
66 QPointF pointFromColor(QColor color)
const;
70 bool m_flipped =
false;
71 bool m_backgroundCheckered =
true;
73 bool m_dragging =
false;
74 bool m_combiningAlpha =
false;
75 int m_indicatorSize = 22;
76 int m_indicatorSpace = 0;
81 QPixmap m_alphalessPixmap;
82 QPixmap m_semiAlphaPixmap;
83 QSize m_pixmapSize{0, 0};
88 QtColorLine::ColorComponent component;
90 Qt::Orientation orientation;
93 PixData m_lastValidMainPixmapData;
97 : m_point(pointFromColor(m_color))
103 if (m_color == color)
105 if (!color.isValid())
111 m_point = pointFromColor(m_color);
122 if (m_component == component)
126 m_component = component;
128 m_point = pointFromColor(m_color);
143 if (m_indicatorSize == size)
145 m_indicatorSize = size;
146 m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size());
148 q_ptr->updateGeometry();
153 return m_indicatorSize;
162 if (m_indicatorSpace == space)
164 m_indicatorSpace = space;
165 m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size());
171 return m_indicatorSpace;
178 if (m_flipped == flip)
181 m_point = pointFromColor(m_color);
192 if (m_backgroundCheckered == checkered)
194 m_backgroundCheckered = checkered;
200 return m_backgroundCheckered;
207 if (m_orientation == orientation)
210 m_orientation = orientation;
211 if (!q_ptr->testAttribute(Qt::WA_WState_OwnSizePolicy)) {
212 QSizePolicy sp = q_ptr->sizePolicy();
214 q_ptr->setSizePolicy(sp);
215 q_ptr->setAttribute(Qt::WA_WState_OwnSizePolicy,
false);
217 m_point = pointFromColor(m_color);
219 q_ptr->updateGeometry();
224 return m_orientation;
229 switch (m_component) {
230 case QtColorLine::Red:
231 case QtColorLine::Green:
232 case QtColorLine::Blue:
233 if (m_color.spec() != QColor::Rgb)
234 m_color = m_color.toRgb();
236 case QtColorLine::Hue:
237 case QtColorLine::Saturation:
238 case QtColorLine::Value:
239 if (m_color.spec() != QColor::Hsv)
240 m_color = m_color.toHsv();
245 if (m_color.spec() == QColor::Hsv) {
246 if (m_color.hue() == 360 || m_color.hue() == -1) {
247 m_color.setHsvF(0.0, m_color.saturationF(), m_color.valueF(), m_color.alphaF());
254 if (m_mainPixmap.isNull()) {
255 if (m_pixmapSize.isEmpty())
260 if (m_lastValidMainPixmapData.component != m_component)
262 if (m_lastValidMainPixmapData.size != m_pixmapSize)
264 if (m_lastValidMainPixmapData.flipped != m_flipped)
266 if (m_lastValidMainPixmapData.orientation != m_orientation)
268 if (m_lastValidMainPixmapData.color == m_color)
270 switch (m_component) {
271 case QtColorLine::Red:
272 if (m_color.green() == m_lastValidMainPixmapData.color.green() &&
273 m_color.blue() == m_lastValidMainPixmapData.color.blue() &&
274 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
277 case QtColorLine::Green:
278 if (m_color.red() == m_lastValidMainPixmapData.color.red() &&
279 m_color.blue() == m_lastValidMainPixmapData.color.blue() &&
280 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
283 case QtColorLine::Blue:
284 if (m_color.red() == m_lastValidMainPixmapData.color.red() &&
285 m_color.green() == m_lastValidMainPixmapData.color.green() &&
286 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
289 case QtColorLine::Hue:
290 if (m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
291 m_color.value() == m_lastValidMainPixmapData.color.value() &&
292 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
295 case QtColorLine::Saturation:
296 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
297 m_color.value() == m_lastValidMainPixmapData.color.value() &&
298 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
301 case QtColorLine::Value:
302 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
303 m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
304 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
307 case QtColorLine::Alpha:
308 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
309 m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
310 m_color.value() == m_lastValidMainPixmapData.color.value())
318 if (isMainPixmapValid())
321 recreateMainPixmap();
325 QColor begin, QColor end,
bool flipped)
const
327 int size = m_pixmapSize.width();
328 if (orientation == Qt::Vertical)
329 size = m_pixmapSize.height();
330 return gradientPixmap(size, orientation, begin, end, flipped);
334 QColor begin, QColor end,
bool flipped)
const
340 if (orientation == Qt::Horizontal) {
353 QLinearGradient lg(0, 0, gradW, gradH);
354 lg.setColorAt(0, c1);
355 lg.setColorAt(1, c2);
356 QImage img(w, h, QImage::Format_ARGB32);
358 p.setCompositionMode(QPainter::CompositionMode_Source);
359 p.fillRect(QRect(0, 0, w, h), lg);
360 return QPixmap::fromImage(img);
363QPixmap
QtColorLinePrivate::hueGradientPixmap(Qt::Orientation orientation,
bool flipped,
364 int saturation,
int value,
int alpha)
const
366 int size = m_pixmapSize.width();
367 if (orientation == Qt::Vertical)
368 size = m_pixmapSize.height();
369 return hueGradientPixmap(size, orientation, flipped, saturation, value, alpha);
372QPixmap
QtColorLinePrivate::hueGradientPixmap(
int size, Qt::Orientation orientation,
bool flipped,
373 int saturation,
int value,
int alpha)
const
375 int gradW = size + 1;
376 int gradH = size + 1;
379 if (orientation == Qt::Horizontal) {
386 QList<QColor> colorList;
387 colorList << QColor::fromHsv(0, saturation, value, alpha);
388 colorList << QColor::fromHsv(60, saturation, value, alpha);
389 colorList << QColor::fromHsv(120, saturation, value, alpha);
390 colorList << QColor::fromHsv(180, saturation, value, alpha);
391 colorList << QColor::fromHsv(240, saturation, value, alpha);
392 colorList << QColor::fromHsv(300, saturation, value, alpha);
393 colorList << QColor::fromHsv(0, saturation, value, alpha);
394 QLinearGradient lg(0, 0, gradW, gradH);
395 for (
int i = 0; i <= 6; i++)
396 lg.setColorAt(
double(i) / 6.0, flipped ? colorList.at(6 - i) : colorList.at(i));
397 QImage img(w, h, QImage::Format_ARGB32);
399 p.setCompositionMode(QPainter::CompositionMode_Source);
400 p.fillRect(QRect(0, 0, w, h), lg);
401 return QPixmap::fromImage(img);
406 m_lastValidMainPixmapData.size = m_pixmapSize;
407 m_lastValidMainPixmapData.component = m_component;
408 m_lastValidMainPixmapData.color = m_color;
409 m_lastValidMainPixmapData.flipped = m_flipped;
410 m_lastValidMainPixmapData.orientation = m_orientation;
412 if (m_pixmapSize.isEmpty()) {
413 m_mainPixmap = QPixmap();
414 m_alphalessPixmap = QPixmap();
415 m_semiAlphaPixmap = QPixmap();
419 if (m_mainPixmap.size() != m_pixmapSize) {
420 m_mainPixmap = QPixmap(m_pixmapSize);
421 m_alphalessPixmap = QPixmap(m_pixmapSize);
422 m_semiAlphaPixmap = QPixmap(m_pixmapSize);
425 Qt::Orientation orient = m_orientation;
426 const bool flip = m_flipped;
428 const int r = m_color.red();
429 const int g = m_color.green();
430 const int b = m_color.blue();
431 const int h = m_color.hue();
432 const int s = m_color.saturation();
433 const int v = m_color.value();
434 const int a = m_color.alpha();
435 const double coef = 0.5;
436 const int semi = qRound(a * coef + 0xFF * (1.0 - coef));
438 if (m_component == QtColorLine::Hue) {
439 m_alphalessPixmap = hueGradientPixmap(orient, flip, s, v, 0xFF);
440 if (m_combiningAlpha) {
441 m_mainPixmap = hueGradientPixmap(orient, flip, s, v, a);
442 m_semiAlphaPixmap = hueGradientPixmap(orient, flip, s, v, semi);
444 }
else if (m_component == QtColorLine::Saturation) {
445 m_alphalessPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, 0xFF), QColor::fromHsv(h, 0xFF, v, 0xFF), flip);
446 if (m_combiningAlpha) {
447 m_mainPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, a), QColor::fromHsv(h, 0xFF, v, a), flip);
448 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, semi), QColor::fromHsv(h, 0xFF, v, semi), flip);
450 }
else if (m_component == QtColorLine::Value) {
451 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, 0xFF), QColor::fromHsv(h, s, 0xFF, 0xFF), flip);
452 if (m_combiningAlpha) {
453 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, a), QColor::fromHsv(h, s, 0xFF, a), flip);
454 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, semi), QColor::fromHsv(h, s, 0xFF, semi), flip);
456 }
else if (m_component == QtColorLine::Red) {
457 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, 0xFF), QColor::fromRgb(0xFF, g, b, 0xFF), flip);
458 if (m_combiningAlpha) {
459 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, a), QColor::fromRgb(0xFF, g, b, a), flip);
460 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, semi), QColor::fromRgb(0xFF, g, b, semi), flip);
462 }
else if (m_component == QtColorLine::Green) {
463 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, 0xFF), QColor::fromRgb(r, 0xFF, b, 0xFF), flip);
464 if (m_combiningAlpha) {
465 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, a), QColor::fromRgb(r, 0xFF, b, a), flip);
466 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, semi), QColor::fromRgb(r, 0xFF, b, semi), flip);
468 }
else if (m_component == QtColorLine::Blue) {
469 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, 0xFF), QColor::fromRgb(r, g, 0xFF, 0xFF), flip);
470 if (m_combiningAlpha) {
471 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, a), QColor::fromRgb(r, g, 0xFF, a), flip);
472 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, semi), QColor::fromRgb(r, g, 0xFF, semi), flip);
474 }
else if (m_component == QtColorLine::Alpha) {
475 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, b, 0), QColor::fromRgb(r, g, b, 0xFF), flip);
480 if (!m_combiningAlpha && m_component != QtColorLine::Alpha)
481 m_mainPixmap = m_alphalessPixmap;
486 QSize size(m_indicatorSize + 2 * m_indicatorSpace - 1,
487 m_indicatorSize + 2 * m_indicatorSpace - 1);
488 if (m_orientation == Qt::Horizontal)
492 return geometrySize - size;
508 if (m_orientation == Qt::Vertical)
514 switch (m_component) {
515 case QtColorLine::Red:
516 c.setRgbF(pos, m_color.greenF(), m_color.blueF(), m_color.alphaF());
518 case QtColorLine::Green:
519 c.setRgbF(m_color.redF(), pos, m_color.blueF(), m_color.alphaF());
521 case QtColorLine::Blue:
522 c.setRgbF(m_color.redF(), m_color.greenF(), pos, m_color.alphaF());
524 case QtColorLine::Hue:
526 hue *= 35999.0 / 36000.0;
527 c.setHsvF(hue, m_color.saturationF(), m_color.valueF(), m_color.alphaF());
529 case QtColorLine::Saturation:
530 c.setHsvF(m_color.hueF(), pos, m_color.valueF(), m_color.alphaF());
532 case QtColorLine::Value:
533 c.setHsvF(m_color.hueF(), m_color.saturationF(), pos, m_color.alphaF());
535 case QtColorLine::Alpha:
536 c.setHsvF(m_color.hueF(), m_color.saturationF(), m_color.valueF(), pos);
544 qreal hue = color.hueF();
545 if (color.hue() == 360)
548 hue *= 36000.0 / 35999.0;
551 switch (m_component) {
552 case QtColorLine::Red:
555 case QtColorLine::Green:
556 pos = color.greenF();
558 case QtColorLine::Blue:
561 case QtColorLine::Hue:
564 case QtColorLine::Saturation:
565 pos = color.saturationF();
567 case QtColorLine::Value:
568 pos = color.valueF();
570 case QtColorLine::Alpha:
571 pos = color.alphaF();
577 if (m_orientation == Qt::Horizontal)
586 QRect r = q_ptr->geometry();
589 int x1 = (
int)((r.width() - m_indicatorSize - 2 * m_indicatorSpace) * point.x() + 0.5);
590 int x2 = x1 + m_indicatorSize + 2 * m_indicatorSpace;
591 int y1 = (
int)((r.height() - m_indicatorSize - 2 * m_indicatorSpace) * point.y() + 0.5);
592 int y2 = y1 + m_indicatorSize + 2 * m_indicatorSpace;
595 if (m_orientation == Qt::Horizontal) {
597 QRect r0(0, 0, x1, r.height());
598 QRect r1(x1 + m_indicatorSpace, 0, m_indicatorSize, r.height());
599 QRect r2(x2, 0, r.width() - x2, r.height());
601 rects << r0 << r1 << r2;
606 QRect r0(0, 0, r.width(), y1);
607 QRect r1(0, y1 + m_indicatorSpace, r.width(), m_indicatorSize);
608 QRect r2(0, y2, r.width(), r.height() - y2);
610 rects << r0 << r1 << r2;
617 m_pixmapSize = pixmapSizeFromGeometrySize(event->size());
622 QRect rect = q_ptr->rect();
624 QList<QRect> r = rects(m_point);
626 QColor c = colorFromPoint(m_point);
627 if (!m_combiningAlpha && m_component != QtColorLine::Alpha)
631 if (q_ptr->isEnabled()) {
632 if (m_backgroundCheckered) {
634 QPixmap pm(2 * pixSize, 2 * pixSize);
636 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
637 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
638 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
639 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
642 p.setBrushOrigin((rect.width() % pixSize + pixSize) / 2, (rect.height() % pixSize + pixSize) / 2);
644 QRegion region(r[1].adjusted(4, 4, -4, -4));
645 region += QRect(rect.topLeft(),
QPoint(r[1].left() + 0, rect.bottom()));
646 region += QRect(
QPoint(r[1].right() - 0, rect.top()), rect.bottomRight());
647 region += QRect(rect.topLeft(),
QPoint(rect.right(), r[1].top() + 0));
648 region += QRect(
QPoint(rect.left(), r[1].bottom() - 0), rect.bottomRight());
649 p.setClipRegion(region);
650 p.fillRect(rect, pm);
651 p.setBrushOrigin(0, 0);
652 p.setClipping(
false);
657 QSize fieldSize = pixmapSizeFromGeometrySize(q_ptr->geometry().size());
659 QPoint posOnField = r[1].topLeft() - QPoint(m_indicatorSpace, m_indicatorSpace);
660 int x = posOnField.x();
661 int y = posOnField.y();
662 int w = fieldSize.width();
663 int h = fieldSize.height();
666 if (m_orientation == Qt::Horizontal) {
667 r0 = QRect(0, 0, x, m_pixmapSize.height());
668 r2 = QRect(x + 1, 0, w - x - 1, m_pixmapSize.height());
670 r0 = QRect(0, 0, m_pixmapSize.width(), y);
671 r2 = QRect(0, y + 1, m_pixmapSize.width(), h - y - 1);
674 p.setBrush(m_mainPixmap);
676 if (r[0].isValid()) {
679 if (r[2].isValid()) {
680 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
683 if (m_indicatorSpace) {
685 if (m_orientation == Qt::Horizontal) {
686 p.drawRect(r[1].adjusted(-m_indicatorSpace, 0, -r[1].width(), 0));
687 p.drawRect(r[1].adjusted(r[1].width(), 0, m_indicatorSpace, 0));
689 p.drawRect(r[1].adjusted(0, -m_indicatorSpace, 0, -r[1].height()));
690 p.drawRect(r[1].adjusted(0, r[1].height(), 0, m_indicatorSpace));
696 p.setBrush(Qt::NoBrush);
697 if (r[1].isValid()) {
698 p.drawRect(r[1].adjusted(0, 0, -1, -1));
701 double coef = 9.0 / 10;
703 if (m_component != QtColorLine::Alpha && m_combiningAlpha) {
704 p.setBrush(m_alphalessPixmap);
705 if (r[0].isValid()) {
706 p.setBrushOrigin(QPoint(0, 0));
707 QRect thinRect1 = r[0];
708 QRect thinRect2 = r[0];
709 QRect thinRect = r[0];
710 if (m_orientation == Qt::Horizontal) {
711 thinRect1.adjust(0, qRound(thinRect1.height() * coef), 0, 0);
712 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef));
713 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
715 thinRect1.adjust(qRound(thinRect1.width() * coef), 0, 0, 0);
716 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef), 0);
717 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
719 p.drawRect(thinRect1);
720 p.drawRect(thinRect2);
723 if (r[2].isValid()) {
724 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
725 QRect thinRect1 = r[2];
726 QRect thinRect2 = r[2];
727 QRect thinRect = r[2];
728 if (m_orientation == Qt::Horizontal) {
729 thinRect1.adjust(0, qRound(thinRect1.height() * coef), 0, 0);
730 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef));
731 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
733 thinRect1.adjust(qRound(thinRect1.width() * coef), 0, 0, 0);
734 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef), 0);
735 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
737 p.drawRect(thinRect1);
738 p.drawRect(thinRect2);
742
743
751 p.setBrush(m_semiAlphaPixmap);
752 if (r[0].isValid()) {
753 p.setBrushOrigin(QPoint(0, 0));
754 QRect thinRect1 = r[0];
755 QRect thinRect2 = r[0];
756 QRect thinRect = r[0];
757 if (m_orientation == Qt::Horizontal) {
758 thinRect1.adjust(0, qRound(thinRect1.height() * coef) - 1, 0, 0);
759 thinRect1.setBottom(thinRect1.top());
760 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef) + 1);
761 thinRect2.setTop(thinRect2.bottom());
762 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
764 thinRect1.adjust(qRound(thinRect1.width() * coef) - 1, 0, 0, 0);
765 thinRect1.setRight(thinRect1.left());
766 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef) + 1, 0);
767 thinRect2.setLeft(thinRect2.right());
768 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
770 p.drawRect(thinRect1);
771 p.drawRect(thinRect2);
774 if (r[2].isValid()) {
775 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
776 QRect thinRect1 = r[2];
777 QRect thinRect2 = r[2];
778 QRect thinRect = r[2];
779 if (m_orientation == Qt::Horizontal) {
780 thinRect1.adjust(0, qRound(thinRect1.height() * coef) - 1, 0, 0);
781 thinRect1.setBottom(thinRect1.top());
782 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef) + 1);
783 thinRect2.setTop(thinRect2.bottom());
784 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
786 thinRect1.adjust(qRound(thinRect1.width() * coef) - 1, 0, 0, 0);
787 thinRect1.setRight(thinRect1.left());
788 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef) + 1, 0);
789 thinRect2.setLeft(thinRect2.right());
790 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
792 p.drawRect(thinRect1);
793 p.drawRect(thinRect2);
796 p.setBrush(m_alphalessPixmap);
798 if (m_orientation == Qt::Horizontal) {
799 region += r[1].adjusted(0, qRound(r[1].height() * coef), 0, 0);
800 region += r[1].adjusted(0, 0, 0, -qRound(r[1].height() * coef));
801 p.setClipRegion(region);
803 region += r[1].adjusted(qRound(r[1].width() * coef), 0, 0, 0);
804 region += r[1].adjusted(0, 0, -qRound(r[1].width() * coef), 0);
805 p.setClipRegion(region);
807 p.setClipRegion(region);
808 p.setBrush(Qt::NoBrush);
809 p.setPen(QPen(QColor(c.rgb())));
811 p.drawRect(r[1].adjusted(0, 0, -1, -1));
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831 p.setClipping(
false);
835 p.setBrush(Qt::NoBrush);
839 r[1].adjust(br, br, -br, -br);
840 if (r[1].adjusted(lw, lw, -lw, -lw).isValid()) {
841 QStyleOptionFrame opt;
845 opt.midLineWidth = 1;
847 opt.state |= QStyle::State_Sunken;
849 opt.state |= QStyle::State_Raised;
850 q_ptr->style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, q_ptr);
851 QRect colorRect = r[1].adjusted(lw, lw, -lw, -lw);
852 if (q_ptr->isEnabled()) {
853 p.fillRect(colorRect, c);
854 const QColor frameColor(0, 0, 0, 38);
855 p.setPen(frameColor);
856 p.drawRect(colorRect.adjusted(0, 0, -1, -1));
858
859
860
861
862
863
865
866
867
868
869
876 if (event->button() != Qt::LeftButton)
879 QList<QRect> r = rects(m_point);
880 QPoint clickPos = event->position().toPoint();
882 QPoint posOnField = r[1].topLeft() - QPoint(m_indicatorSpace, m_indicatorSpace);
883 m_clickOffset = posOnField - clickPos;
885 if (!r[1].contains(clickPos))
895 QPoint newPos = event->position().toPoint();
897 QSize fieldSize = q_ptr->geometry().size() -
898 QSize(m_indicatorSize + 2 * m_indicatorSpace - 1, m_indicatorSize + 2 * m_indicatorSpace - 1);
899 QPoint newPosOnField = newPos + m_clickOffset;
900 if (newPosOnField.x() < 0)
901 newPosOnField.setX(0);
902 else if (newPosOnField.x() > fieldSize.width())
903 newPosOnField.setX(fieldSize.width());
904 if (newPosOnField.y() < 0)
905 newPosOnField.setY(0);
906 else if (newPosOnField.y() > fieldSize.height())
907 newPosOnField.setY(fieldSize.height());
909 const double x =
double(newPosOnField.x()) / fieldSize.width();
910 const double y =
double(newPosOnField.y()) / fieldSize.height();
911 m_point = QPointF(x, y);
912 QColor color = colorFromPoint(m_point);
913 if (m_color == color)
916 emit q_ptr->colorChanged(color);
922 if (event->button() != Qt::LeftButton)
930 if (event->button() != Qt::LeftButton)
933 QList<QRect> r = rects(m_point);
934 QPoint clickPos = event->position().toPoint();
935 if (!r[0].contains(clickPos) && !r[2].contains(clickPos))
937 QPoint newPosOnField = clickPos;
938 if (r[2].contains(clickPos))
939 newPosOnField -= QPoint(m_indicatorSize + 2 * m_indicatorSpace - 2, m_indicatorSize + 2 * m_indicatorSpace - 2);
940 QSize fieldSize = q_ptr->geometry().size() -
941 QSize(m_indicatorSize + 2 * m_indicatorSpace - 1, m_indicatorSize + 2 * m_indicatorSpace - 1);
943 const double x =
double(newPosOnField.x()) / fieldSize.width();
944 const double y =
double(newPosOnField.y()) / fieldSize.height();
945 m_point = QPointF(x, y);
946 QColor color = colorFromPoint(m_point);
947 if (m_color == color)
950 emit q_ptr->colorChanged(color);
956QtColorLine::QtColorLine(QWidget *parent)
957 : QWidget(parent), d_ptr(
new QtColorLinePrivate)
961 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
964QtColorLine::~QtColorLine()
968QSize QtColorLine::minimumSizeHint()
const
970 return QSize(d_ptr->m_indicatorSize, d_ptr->m_indicatorSize);
973QSize QtColorLine::sizeHint()
const
975 return QSize(d_ptr->m_indicatorSize, d_ptr->m_indicatorSize);
978void QtColorLine::setColor(QColor color)
980 d_ptr->setColor(color);
983QColor QtColorLine::color()
const
985 return d_ptr->color();
988void QtColorLine::setColorComponent(QtColorLine::ColorComponent component)
990 d_ptr->setColorComponent(component);
993QtColorLine::ColorComponent QtColorLine::colorComponent()
const
995 return d_ptr->colorComponent();
998void QtColorLine::setIndicatorSize(
int size)
1000 d_ptr->setIndicatorSize(size);
1003int QtColorLine::indicatorSize()
const
1005 return d_ptr->indicatorSize();
1008void QtColorLine::setIndicatorSpace(
int space)
1010 d_ptr->setIndicatorSpace(space);
1013int QtColorLine::indicatorSpace()
const
1015 return d_ptr->indicatorSpace();
1018void QtColorLine::setFlip(
bool flip)
1020 d_ptr->setFlip(flip);
1023bool QtColorLine::flip()
const
1025 return d_ptr->flip();
1028void QtColorLine::setBackgroundCheckered(
bool checkered)
1030 d_ptr->setBackgroundCheckered(checkered);
1033bool QtColorLine::isBackgroundCheckered()
const
1035 return d_ptr->isBackgroundCheckered();
1038void QtColorLine::setOrientation(Qt::Orientation orientation)
1040 d_ptr->setOrientation(orientation);
1043Qt::Orientation QtColorLine::orientation()
const
1045 return d_ptr->orientation();
1047void QtColorLine::resizeEvent(QResizeEvent *event)
1049 d_ptr->resizeEvent(event);
1052void QtColorLine::paintEvent(QPaintEvent *event)
1054 d_ptr->paintEvent(event);
1057void QtColorLine::mousePressEvent(QMouseEvent *event)
1059 d_ptr->mousePressEvent(event);
1062void QtColorLine::mouseMoveEvent(QMouseEvent *event)
1064 d_ptr->mouseMoveEvent(event);
1067void QtColorLine::mouseReleaseEvent(QMouseEvent *event)
1069 d_ptr->mouseReleaseEvent(event);
1072void QtColorLine::mouseDoubleClickEvent(QMouseEvent *event)
1074 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)