7#include <QtWidgets/qstyleoption.h>
9#include <QtGui/qevent.h>
10#include <QtGui/qpainter.h>
11#include <QtGui/qregion.h>
17 QtColorLine *q_ptr =
nullptr;
18 Q_DECLARE_PUBLIC(QtColorLine)
51 bool isMainPixmapValid()
const;
53 void recreateMainPixmap();
54 QSize pixmapSizeFromGeometrySize(QSize geometrySize)
const;
55 QPixmap gradientPixmap(
int size, Qt::Orientation orientation,
56 QColor begin, QColor end,
bool flipped =
false)
const;
57 QPixmap gradientPixmap(Qt::Orientation orientation,
58 QColor begin, QColor end,
bool flipped =
false)
const;
59 QPixmap hueGradientPixmap(
int size, Qt::Orientation orientation,
bool flipped =
false,
60 int saturation = 0xFF,
int value = 0xFF,
int alpha = 0xFF)
const;
61 QPixmap hueGradientPixmap(Qt::Orientation orientation,
bool flipped =
false,
62 int saturation = 0xFF,
int value = 0xFF,
int alpha = 0xFF)
const;
64 QList<QRect> rects(QPointF point)
const;
66 QColor colorFromPoint(QPointF point)
const;
67 QPointF pointFromColor(QColor color)
const;
71 bool m_flipped =
false;
72 bool m_backgroundCheckered =
true;
74 bool m_dragging =
false;
75 bool m_combiningAlpha =
false;
76 int m_indicatorSize = 22;
77 int m_indicatorSpace = 0;
82 QPixmap m_alphalessPixmap;
83 QPixmap m_semiAlphaPixmap;
84 QSize m_pixmapSize{0, 0};
89 QtColorLine::ColorComponent component;
91 Qt::Orientation orientation;
94 PixData m_lastValidMainPixmapData;
98 : m_point(pointFromColor(m_color))
104 if (m_color == color)
106 if (!color.isValid())
112 m_point = pointFromColor(m_color);
123 if (m_component == component)
127 m_component = component;
129 m_point = pointFromColor(m_color);
144 if (m_indicatorSize == size)
146 m_indicatorSize = size;
147 m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size());
149 q_ptr->updateGeometry();
154 return m_indicatorSize;
163 if (m_indicatorSpace == space)
165 m_indicatorSpace = space;
166 m_pixmapSize = pixmapSizeFromGeometrySize(q_ptr->contentsRect().size());
172 return m_indicatorSpace;
179 if (m_flipped == flip)
182 m_point = pointFromColor(m_color);
193 if (m_backgroundCheckered == checkered)
195 m_backgroundCheckered = checkered;
201 return m_backgroundCheckered;
208 if (m_orientation == orientation)
211 m_orientation = orientation;
212 if (!q_ptr->testAttribute(Qt::WA_WState_OwnSizePolicy)) {
213 QSizePolicy sp = q_ptr->sizePolicy();
215 q_ptr->setSizePolicy(sp);
216 q_ptr->setAttribute(Qt::WA_WState_OwnSizePolicy,
false);
218 m_point = pointFromColor(m_color);
220 q_ptr->updateGeometry();
225 return m_orientation;
230 switch (m_component) {
231 case QtColorLine::Red:
232 case QtColorLine::Green:
233 case QtColorLine::Blue:
234 if (m_color.spec() != QColor::Rgb)
235 m_color = m_color.toRgb();
237 case QtColorLine::Hue:
238 case QtColorLine::Saturation:
239 case QtColorLine::Value:
240 if (m_color.spec() != QColor::Hsv)
241 m_color = m_color.toHsv();
246 if (m_color.spec() == QColor::Hsv) {
247 if (m_color.hue() == 360 || m_color.hue() == -1) {
248 m_color.setHsvF(0.0, m_color.saturationF(), m_color.valueF(), m_color.alphaF());
255 if (m_mainPixmap.isNull()) {
256 if (m_pixmapSize.isEmpty())
261 if (m_lastValidMainPixmapData.component != m_component)
263 if (m_lastValidMainPixmapData.size != m_pixmapSize)
265 if (m_lastValidMainPixmapData.flipped != m_flipped)
267 if (m_lastValidMainPixmapData.orientation != m_orientation)
269 if (m_lastValidMainPixmapData.color == m_color)
271 switch (m_component) {
272 case QtColorLine::Red:
273 if (m_color.green() == m_lastValidMainPixmapData.color.green() &&
274 m_color.blue() == m_lastValidMainPixmapData.color.blue() &&
275 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
278 case QtColorLine::Green:
279 if (m_color.red() == m_lastValidMainPixmapData.color.red() &&
280 m_color.blue() == m_lastValidMainPixmapData.color.blue() &&
281 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
284 case QtColorLine::Blue:
285 if (m_color.red() == m_lastValidMainPixmapData.color.red() &&
286 m_color.green() == m_lastValidMainPixmapData.color.green() &&
287 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
290 case QtColorLine::Hue:
291 if (m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
292 m_color.value() == m_lastValidMainPixmapData.color.value() &&
293 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
296 case QtColorLine::Saturation:
297 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
298 m_color.value() == m_lastValidMainPixmapData.color.value() &&
299 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
302 case QtColorLine::Value:
303 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
304 m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
305 (!m_combiningAlpha || m_color.alpha() == m_lastValidMainPixmapData.color.alpha()))
308 case QtColorLine::Alpha:
309 if (m_color.hue() == m_lastValidMainPixmapData.color.hue() &&
310 m_color.saturation() == m_lastValidMainPixmapData.color.saturation() &&
311 m_color.value() == m_lastValidMainPixmapData.color.value())
319 if (isMainPixmapValid())
322 recreateMainPixmap();
326 QColor begin, QColor end,
bool flipped)
const
328 int size = m_pixmapSize.width();
329 if (orientation == Qt::Vertical)
330 size = m_pixmapSize.height();
331 return gradientPixmap(size, orientation, begin, end, flipped);
335 QColor begin, QColor end,
bool flipped)
const
341 if (orientation == Qt::Horizontal) {
354 QLinearGradient lg(0, 0, gradW, gradH);
355 lg.setColorAt(0, c1);
356 lg.setColorAt(1, c2);
357 QImage img(w, h, QImage::Format_ARGB32);
359 p.setCompositionMode(QPainter::CompositionMode_Source);
360 p.fillRect(QRect(0, 0, w, h), lg);
361 return QPixmap::fromImage(img);
364QPixmap
QtColorLinePrivate::hueGradientPixmap(Qt::Orientation orientation,
bool flipped,
365 int saturation,
int value,
int alpha)
const
367 int size = m_pixmapSize.width();
368 if (orientation == Qt::Vertical)
369 size = m_pixmapSize.height();
370 return hueGradientPixmap(size, orientation, flipped, saturation, value, alpha);
373QPixmap
QtColorLinePrivate::hueGradientPixmap(
int size, Qt::Orientation orientation,
bool flipped,
374 int saturation,
int value,
int alpha)
const
376 int gradW = size + 1;
377 int gradH = size + 1;
380 if (orientation == Qt::Horizontal) {
387 QList<QColor> colorList;
388 colorList << QColor::fromHsv(0, saturation, value, alpha);
389 colorList << QColor::fromHsv(60, saturation, value, alpha);
390 colorList << QColor::fromHsv(120, saturation, value, alpha);
391 colorList << QColor::fromHsv(180, saturation, value, alpha);
392 colorList << QColor::fromHsv(240, saturation, value, alpha);
393 colorList << QColor::fromHsv(300, saturation, value, alpha);
394 colorList << QColor::fromHsv(0, saturation, value, alpha);
395 QLinearGradient lg(0, 0, gradW, gradH);
396 for (
int i = 0; i <= 6; i++)
397 lg.setColorAt(
double(i) / 6.0, flipped ? colorList.at(6 - i) : colorList.at(i));
398 QImage img(w, h, QImage::Format_ARGB32);
400 p.setCompositionMode(QPainter::CompositionMode_Source);
401 p.fillRect(QRect(0, 0, w, h), lg);
402 return QPixmap::fromImage(img);
407 m_lastValidMainPixmapData.size = m_pixmapSize;
408 m_lastValidMainPixmapData.component = m_component;
409 m_lastValidMainPixmapData.color = m_color;
410 m_lastValidMainPixmapData.flipped = m_flipped;
411 m_lastValidMainPixmapData.orientation = m_orientation;
413 if (m_pixmapSize.isEmpty()) {
414 m_mainPixmap = QPixmap();
415 m_alphalessPixmap = QPixmap();
416 m_semiAlphaPixmap = QPixmap();
420 if (m_mainPixmap.size() != m_pixmapSize) {
421 m_mainPixmap = QPixmap(m_pixmapSize);
422 m_alphalessPixmap = QPixmap(m_pixmapSize);
423 m_semiAlphaPixmap = QPixmap(m_pixmapSize);
426 Qt::Orientation orient = m_orientation;
427 const bool flip = m_flipped;
429 const int r = m_color.red();
430 const int g = m_color.green();
431 const int b = m_color.blue();
432 const int h = m_color.hue();
433 const int s = m_color.saturation();
434 const int v = m_color.value();
435 const int a = m_color.alpha();
436 const double coef = 0.5;
437 const int semi = qRound(a * coef + 0xFF * (1.0 - coef));
439 if (m_component == QtColorLine::Hue) {
440 m_alphalessPixmap = hueGradientPixmap(orient, flip, s, v, 0xFF);
441 if (m_combiningAlpha) {
442 m_mainPixmap = hueGradientPixmap(orient, flip, s, v, a);
443 m_semiAlphaPixmap = hueGradientPixmap(orient, flip, s, v, semi);
445 }
else if (m_component == QtColorLine::Saturation) {
446 m_alphalessPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, 0xFF), QColor::fromHsv(h, 0xFF, v, 0xFF), flip);
447 if (m_combiningAlpha) {
448 m_mainPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, a), QColor::fromHsv(h, 0xFF, v, a), flip);
449 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromHsv(h, 0, v, semi), QColor::fromHsv(h, 0xFF, v, semi), flip);
451 }
else if (m_component == QtColorLine::Value) {
452 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, 0xFF), QColor::fromHsv(h, s, 0xFF, 0xFF), flip);
453 if (m_combiningAlpha) {
454 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, a), QColor::fromHsv(h, s, 0xFF, a), flip);
455 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(0, 0, 0, semi), QColor::fromHsv(h, s, 0xFF, semi), flip);
457 }
else if (m_component == QtColorLine::Red) {
458 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, 0xFF), QColor::fromRgb(0xFF, g, b, 0xFF), flip);
459 if (m_combiningAlpha) {
460 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, a), QColor::fromRgb(0xFF, g, b, a), flip);
461 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(0, g, b, semi), QColor::fromRgb(0xFF, g, b, semi), flip);
463 }
else if (m_component == QtColorLine::Green) {
464 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, 0xFF), QColor::fromRgb(r, 0xFF, b, 0xFF), flip);
465 if (m_combiningAlpha) {
466 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, a), QColor::fromRgb(r, 0xFF, b, a), flip);
467 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(r, 0, b, semi), QColor::fromRgb(r, 0xFF, b, semi), flip);
469 }
else if (m_component == QtColorLine::Blue) {
470 m_alphalessPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, 0xFF), QColor::fromRgb(r, g, 0xFF, 0xFF), flip);
471 if (m_combiningAlpha) {
472 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, a), QColor::fromRgb(r, g, 0xFF, a), flip);
473 m_semiAlphaPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, 0, semi), QColor::fromRgb(r, g, 0xFF, semi), flip);
475 }
else if (m_component == QtColorLine::Alpha) {
476 m_mainPixmap = gradientPixmap(orient, QColor::fromRgb(r, g, b, 0), QColor::fromRgb(r, g, b, 0xFF), flip);
481 if (!m_combiningAlpha && m_component != QtColorLine::Alpha)
482 m_mainPixmap = m_alphalessPixmap;
487 QSize size(m_indicatorSize + 2 * m_indicatorSpace - 1,
488 m_indicatorSize + 2 * m_indicatorSpace - 1);
489 if (m_orientation == Qt::Horizontal)
493 return geometrySize - size;
509 if (m_orientation == Qt::Vertical)
515 switch (m_component) {
516 case QtColorLine::Red:
517 c.setRgbF(pos, m_color.greenF(), m_color.blueF(), m_color.alphaF());
519 case QtColorLine::Green:
520 c.setRgbF(m_color.redF(), pos, m_color.blueF(), m_color.alphaF());
522 case QtColorLine::Blue:
523 c.setRgbF(m_color.redF(), m_color.greenF(), pos, m_color.alphaF());
525 case QtColorLine::Hue:
527 hue *= 35999.0 / 36000.0;
528 c.setHsvF(hue, m_color.saturationF(), m_color.valueF(), m_color.alphaF());
530 case QtColorLine::Saturation:
531 c.setHsvF(m_color.hueF(), pos, m_color.valueF(), m_color.alphaF());
533 case QtColorLine::Value:
534 c.setHsvF(m_color.hueF(), m_color.saturationF(), pos, m_color.alphaF());
536 case QtColorLine::Alpha:
537 c.setHsvF(m_color.hueF(), m_color.saturationF(), m_color.valueF(), pos);
545 qreal hue = color.hueF();
546 if (color.hue() == 360)
549 hue *= 36000.0 / 35999.0;
552 switch (m_component) {
553 case QtColorLine::Red:
556 case QtColorLine::Green:
557 pos = color.greenF();
559 case QtColorLine::Blue:
562 case QtColorLine::Hue:
565 case QtColorLine::Saturation:
566 pos = color.saturationF();
568 case QtColorLine::Value:
569 pos = color.valueF();
571 case QtColorLine::Alpha:
572 pos = color.alphaF();
578 if (m_orientation == Qt::Horizontal)
587 QRect r = q_ptr->geometry();
590 int x1 = (
int)((r.width() - m_indicatorSize - 2 * m_indicatorSpace) * point.x() + 0.5);
591 int x2 = x1 + m_indicatorSize + 2 * m_indicatorSpace;
592 int y1 = (
int)((r.height() - m_indicatorSize - 2 * m_indicatorSpace) * point.y() + 0.5);
593 int y2 = y1 + m_indicatorSize + 2 * m_indicatorSpace;
596 if (m_orientation == Qt::Horizontal) {
598 QRect r0(0, 0, x1, r.height());
599 QRect r1(x1 + m_indicatorSpace, 0, m_indicatorSize, r.height());
600 QRect r2(x2, 0, r.width() - x2, r.height());
602 rects << r0 << r1 << r2;
607 QRect r0(0, 0, r.width(), y1);
608 QRect r1(0, y1 + m_indicatorSpace, r.width(), m_indicatorSize);
609 QRect r2(0, y2, r.width(), r.height() - y2);
611 rects << r0 << r1 << r2;
618 m_pixmapSize = pixmapSizeFromGeometrySize(event->size());
623 QRect rect = q_ptr->rect();
625 QList<QRect> r = rects(m_point);
627 QColor c = colorFromPoint(m_point);
628 if (!m_combiningAlpha && m_component != QtColorLine::Alpha)
632 if (q_ptr->isEnabled()) {
633 if (m_backgroundCheckered) {
635 QPixmap pm(2 * pixSize, 2 * pixSize);
637 pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
638 pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
639 pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
640 pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
643 p.setBrushOrigin((rect.width() % pixSize + pixSize) / 2, (rect.height() % pixSize + pixSize) / 2);
645 QRegion region(r[1].adjusted(4, 4, -4, -4));
646 region += QRect(rect.topLeft(),
QPoint(r[1].left() + 0, rect.bottom()));
647 region += QRect(
QPoint(r[1].right() - 0, rect.top()), rect.bottomRight());
648 region += QRect(rect.topLeft(),
QPoint(rect.right(), r[1].top() + 0));
649 region += QRect(
QPoint(rect.left(), r[1].bottom() - 0), rect.bottomRight());
650 p.setClipRegion(region);
651 p.fillRect(rect, pm);
652 p.setBrushOrigin(0, 0);
653 p.setClipping(
false);
658 QSize fieldSize = pixmapSizeFromGeometrySize(q_ptr->geometry().size());
660 QPoint posOnField = r[1].topLeft() - QPoint(m_indicatorSpace, m_indicatorSpace);
661 int x = posOnField.x();
662 int y = posOnField.y();
663 int w = fieldSize.width();
664 int h = fieldSize.height();
667 if (m_orientation == Qt::Horizontal) {
668 r0 = QRect(0, 0, x, m_pixmapSize.height());
669 r2 = QRect(x + 1, 0, w - x - 1, m_pixmapSize.height());
671 r0 = QRect(0, 0, m_pixmapSize.width(), y);
672 r2 = QRect(0, y + 1, m_pixmapSize.width(), h - y - 1);
675 p.setBrush(m_mainPixmap);
677 if (r[0].isValid()) {
680 if (r[2].isValid()) {
681 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
684 if (m_indicatorSpace) {
686 if (m_orientation == Qt::Horizontal) {
687 p.drawRect(r[1].adjusted(-m_indicatorSpace, 0, -r[1].width(), 0));
688 p.drawRect(r[1].adjusted(r[1].width(), 0, m_indicatorSpace, 0));
690 p.drawRect(r[1].adjusted(0, -m_indicatorSpace, 0, -r[1].height()));
691 p.drawRect(r[1].adjusted(0, r[1].height(), 0, m_indicatorSpace));
697 p.setBrush(Qt::NoBrush);
698 if (r[1].isValid()) {
699 p.drawRect(r[1].adjusted(0, 0, -1, -1));
702 double coef = 9.0 / 10;
704 if (m_component != QtColorLine::Alpha && m_combiningAlpha) {
705 p.setBrush(m_alphalessPixmap);
706 if (r[0].isValid()) {
707 p.setBrushOrigin(QPoint(0, 0));
708 QRect thinRect1 = r[0];
709 QRect thinRect2 = r[0];
710 QRect thinRect = r[0];
711 if (m_orientation == Qt::Horizontal) {
712 thinRect1.adjust(0, qRound(thinRect1.height() * coef), 0, 0);
713 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef));
714 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
716 thinRect1.adjust(qRound(thinRect1.width() * coef), 0, 0, 0);
717 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef), 0);
718 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
720 p.drawRect(thinRect1);
721 p.drawRect(thinRect2);
724 if (r[2].isValid()) {
725 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
726 QRect thinRect1 = r[2];
727 QRect thinRect2 = r[2];
728 QRect thinRect = r[2];
729 if (m_orientation == Qt::Horizontal) {
730 thinRect1.adjust(0, qRound(thinRect1.height() * coef), 0, 0);
731 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef));
732 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
734 thinRect1.adjust(qRound(thinRect1.width() * coef), 0, 0, 0);
735 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef), 0);
736 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
738 p.drawRect(thinRect1);
739 p.drawRect(thinRect2);
743
744
752 p.setBrush(m_semiAlphaPixmap);
753 if (r[0].isValid()) {
754 p.setBrushOrigin(QPoint(0, 0));
755 QRect thinRect1 = r[0];
756 QRect thinRect2 = r[0];
757 QRect thinRect = r[0];
758 if (m_orientation == Qt::Horizontal) {
759 thinRect1.adjust(0, qRound(thinRect1.height() * coef) - 1, 0, 0);
760 thinRect1.setBottom(thinRect1.top());
761 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef) + 1);
762 thinRect2.setTop(thinRect2.bottom());
763 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
765 thinRect1.adjust(qRound(thinRect1.width() * coef) - 1, 0, 0, 0);
766 thinRect1.setRight(thinRect1.left());
767 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef) + 1, 0);
768 thinRect2.setLeft(thinRect2.right());
769 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
771 p.drawRect(thinRect1);
772 p.drawRect(thinRect2);
775 if (r[2].isValid()) {
776 p.setBrushOrigin(r[2].topLeft() - r2.topLeft());
777 QRect thinRect1 = r[2];
778 QRect thinRect2 = r[2];
779 QRect thinRect = r[2];
780 if (m_orientation == Qt::Horizontal) {
781 thinRect1.adjust(0, qRound(thinRect1.height() * coef) - 1, 0, 0);
782 thinRect1.setBottom(thinRect1.top());
783 thinRect2.adjust(0, 0, 0, -qRound(thinRect2.height() * coef) + 1);
784 thinRect2.setTop(thinRect2.bottom());
785 thinRect.adjust(0, qRound(thinRect.height() * coef), 0, -qRound(thinRect.height() * coef));
787 thinRect1.adjust(qRound(thinRect1.width() * coef) - 1, 0, 0, 0);
788 thinRect1.setRight(thinRect1.left());
789 thinRect2.adjust(0, 0, -qRound(thinRect2.width() * coef) + 1, 0);
790 thinRect2.setLeft(thinRect2.right());
791 thinRect.adjust(qRound(thinRect.width() * coef), 0, -qRound(thinRect.width() * coef), 0);
793 p.drawRect(thinRect1);
794 p.drawRect(thinRect2);
797 p.setBrush(m_alphalessPixmap);
799 if (m_orientation == Qt::Horizontal) {
800 region += r[1].adjusted(0, qRound(r[1].height() * coef), 0, 0);
801 region += r[1].adjusted(0, 0, 0, -qRound(r[1].height() * coef));
802 p.setClipRegion(region);
804 region += r[1].adjusted(qRound(r[1].width() * coef), 0, 0, 0);
805 region += r[1].adjusted(0, 0, -qRound(r[1].width() * coef), 0);
806 p.setClipRegion(region);
808 p.setClipRegion(region);
809 p.setBrush(Qt::NoBrush);
810 p.setPen(QPen(QColor(c.rgb())));
812 p.drawRect(r[1].adjusted(0, 0, -1, -1));
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832 p.setClipping(
false);
836 p.setBrush(Qt::NoBrush);
840 r[1].adjust(br, br, -br, -br);
841 if (r[1].adjusted(lw, lw, -lw, -lw).isValid()) {
842 QStyleOptionFrame opt;
846 opt.midLineWidth = 1;
848 opt.state |= QStyle::State_Sunken;
850 opt.state |= QStyle::State_Raised;
851 q_ptr->style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, q_ptr);
852 QRect colorRect = r[1].adjusted(lw, lw, -lw, -lw);
853 if (q_ptr->isEnabled()) {
854 p.fillRect(colorRect, c);
855 const QColor frameColor(0, 0, 0, 38);
856 p.setPen(frameColor);
857 p.drawRect(colorRect.adjusted(0, 0, -1, -1));
859
860
861
862
863
864
866
867
868
869
870
877 if (event->button() != Qt::LeftButton)
880 QList<QRect> r = rects(m_point);
881 QPoint clickPos = event->position().toPoint();
883 QPoint posOnField = r[1].topLeft() - QPoint(m_indicatorSpace, m_indicatorSpace);
884 m_clickOffset = posOnField - clickPos;
886 if (!r[1].contains(clickPos))
896 QPoint newPos = event->position().toPoint();
898 QSize fieldSize = q_ptr->geometry().size() -
899 QSize(m_indicatorSize + 2 * m_indicatorSpace - 1, m_indicatorSize + 2 * m_indicatorSpace - 1);
900 QPoint newPosOnField = newPos + m_clickOffset;
901 if (newPosOnField.x() < 0)
902 newPosOnField.setX(0);
903 else if (newPosOnField.x() > fieldSize.width())
904 newPosOnField.setX(fieldSize.width());
905 if (newPosOnField.y() < 0)
906 newPosOnField.setY(0);
907 else if (newPosOnField.y() > fieldSize.height())
908 newPosOnField.setY(fieldSize.height());
910 const double x =
double(newPosOnField.x()) / fieldSize.width();
911 const double y =
double(newPosOnField.y()) / fieldSize.height();
912 m_point = QPointF(x, y);
913 QColor color = colorFromPoint(m_point);
914 if (m_color == color)
917 emit q_ptr->colorChanged(color);
923 if (event->button() != Qt::LeftButton)
931 if (event->button() != Qt::LeftButton)
934 QList<QRect> r = rects(m_point);
935 QPoint clickPos = event->position().toPoint();
936 if (!r[0].contains(clickPos) && !r[2].contains(clickPos))
938 QPoint newPosOnField = clickPos;
939 if (r[2].contains(clickPos))
940 newPosOnField -= QPoint(m_indicatorSize + 2 * m_indicatorSpace - 2, m_indicatorSize + 2 * m_indicatorSpace - 2);
941 QSize fieldSize = q_ptr->geometry().size() -
942 QSize(m_indicatorSize + 2 * m_indicatorSpace - 1, m_indicatorSize + 2 * m_indicatorSpace - 1);
944 const double x =
double(newPosOnField.x()) / fieldSize.width();
945 const double y =
double(newPosOnField.y()) / fieldSize.height();
946 m_point = QPointF(x, y);
947 QColor color = colorFromPoint(m_point);
948 if (m_color == color)
951 emit q_ptr->colorChanged(color);
957QtColorLine::QtColorLine(QWidget *parent)
958 : QWidget(parent), d_ptr(
new QtColorLinePrivate)
962 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
965QtColorLine::~QtColorLine()
969QSize QtColorLine::minimumSizeHint()
const
971 return QSize(d_ptr->m_indicatorSize, d_ptr->m_indicatorSize);
974QSize QtColorLine::sizeHint()
const
976 return QSize(d_ptr->m_indicatorSize, d_ptr->m_indicatorSize);
979void QtColorLine::setColor(QColor color)
981 d_ptr->setColor(color);
984QColor QtColorLine::color()
const
986 return d_ptr->color();
989void QtColorLine::setColorComponent(QtColorLine::ColorComponent component)
991 d_ptr->setColorComponent(component);
994QtColorLine::ColorComponent QtColorLine::colorComponent()
const
996 return d_ptr->colorComponent();
999void QtColorLine::setIndicatorSize(
int size)
1001 d_ptr->setIndicatorSize(size);
1004int QtColorLine::indicatorSize()
const
1006 return d_ptr->indicatorSize();
1009void QtColorLine::setIndicatorSpace(
int space)
1011 d_ptr->setIndicatorSpace(space);
1014int QtColorLine::indicatorSpace()
const
1016 return d_ptr->indicatorSpace();
1019void QtColorLine::setFlip(
bool flip)
1021 d_ptr->setFlip(flip);
1024bool QtColorLine::flip()
const
1026 return d_ptr->flip();
1029void QtColorLine::setBackgroundCheckered(
bool checkered)
1031 d_ptr->setBackgroundCheckered(checkered);
1034bool QtColorLine::isBackgroundCheckered()
const
1036 return d_ptr->isBackgroundCheckered();
1039void QtColorLine::setOrientation(Qt::Orientation orientation)
1041 d_ptr->setOrientation(orientation);
1044Qt::Orientation QtColorLine::orientation()
const
1046 return d_ptr->orientation();
1048void QtColorLine::resizeEvent(QResizeEvent *event)
1050 d_ptr->resizeEvent(event);
1053void QtColorLine::paintEvent(QPaintEvent *event)
1055 d_ptr->paintEvent(event);
1058void QtColorLine::mousePressEvent(QMouseEvent *event)
1060 d_ptr->mousePressEvent(event);
1063void QtColorLine::mouseMoveEvent(QMouseEvent *event)
1065 d_ptr->mouseMoveEvent(event);
1068void QtColorLine::mouseReleaseEvent(QMouseEvent *event)
1070 d_ptr->mouseReleaseEvent(event);
1073void QtColorLine::mouseDoubleClickEvent(QMouseEvent *event)
1075 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)