From 37fa842f41311e71d4c94eaa380ebfa0965fa25d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Martins?= Date: Thu, 11 Dec 2014 20:08:51 +0000 Subject: QColorDialog: Fix picking screen colors inside QColorPicker If you click "Pick screen color" and move mouse over QColorPicker (the square with gradients) unexpected things will happen: black will be picked half the times. This is because QColorPicker's black cross is painted under our mouse cursor. Although "pick screen color"'s use case is to pick colors in other windows, there's no reason to not make it work on QColorDialog itself too. Task-number: QTBUG-43288 Change-Id: I03ca02148cc15ad41d545723d4ac4f5a82842b4b Reviewed-by: Friedemann Kleint --- src/widgets/dialogs/qcolordialog.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src/widgets/dialogs/qcolordialog.cpp') diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index b92e1a24ad..33b374c1bc 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -627,6 +627,7 @@ public: QColorPicker(QWidget* parent); ~QColorPicker(); + void setCrossVisible(bool visible); public slots: void setCol(int h, int s); @@ -650,6 +651,7 @@ private: void setCol(const QPoint &pt); QPixmap pix; + bool crossVisible; }; static int pWidth = 220; @@ -805,6 +807,7 @@ void QColorPicker::setCol(const QPoint &pt) QColorPicker::QColorPicker(QWidget* parent) : QFrame(parent) + , crossVisible(true) { hue = 0; sat = 0; setCol(150, 255); @@ -817,6 +820,14 @@ QColorPicker::~QColorPicker() { } +void QColorPicker::setCrossVisible(bool visible) +{ + if (crossVisible != visible) { + crossVisible = visible; + update(); + } +} + QSize QColorPicker::sizeHint() const { return QSize(pWidth + 2*frameWidth(), pHeight + 2*frameWidth()); @@ -858,12 +869,13 @@ void QColorPicker::paintEvent(QPaintEvent* ) QRect r = contentsRect(); p.drawPixmap(r.topLeft(), pix); - QPoint pt = colPt() + r.topLeft(); - p.setPen(Qt::black); - - p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black); - p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black); + if (crossVisible) { + QPoint pt = colPt() + r.topLeft(); + p.setPen(Qt::black); + p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black); + p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black); + } } void QColorPicker::resizeEvent(QResizeEvent *ev) @@ -1578,6 +1590,7 @@ void QColorDialogPrivate::_q_pickScreenColor() void QColorDialogPrivate::releaseColorPicking() { Q_Q(QColorDialog); + cp->setCrossVisible(true); q->removeEventFilter(colorPickingEventFilter); q->releaseMouse(); q->releaseKeyboard(); @@ -2174,6 +2187,9 @@ void QColorDialog::changeEvent(QEvent *e) bool QColorDialogPrivate::handleColorPickingMouseMove(QMouseEvent *e) { + // If the cross is visible the grabbed color will be black most of the times + cp->setCrossVisible(!cp->geometry().contains(e->pos())); + const QPoint globalPos = e->globalPos(); const QColor color = grabScreenColor(globalPos); // QTBUG-39792, do not change standard, custom color selectors while moving as -- cgit v1.2.3 From 2746fe4243c03c4411f1f279df85db5cdd4c01b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Martins?= Date: Mon, 15 Dec 2014 21:02:39 +0000 Subject: QColorDialog: Fix matching against predefined colors. The grids are filled top to bottom, left to right, so use division to get the column. Task-number: QTBUG-43371 Change-Id: I02ad518512858ed71e0e3a0cae8c4e02d537a9b9 Reviewed-by: Friedemann Kleint --- src/widgets/dialogs/qcolordialog.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/widgets/dialogs/qcolordialog.cpp') diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index 33b374c1bc..ee3ece2d77 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -1489,8 +1489,8 @@ bool QColorDialogPrivate::selectColor(const QColor &col) const QRgb *match = std::find(standardColors, standardColorsEnd, color); if (match != standardColorsEnd) { const int index = int(match - standardColors); - const int row = index / standardColorRows; - const int column = index % standardColorRows; + const int column = index / standardColorRows; + const int row = index % standardColorRows; _q_newStandard(row, column); standard->setCurrent(row, column); standard->setSelected(row, column); @@ -1505,8 +1505,8 @@ bool QColorDialogPrivate::selectColor(const QColor &col) const QRgb *match = std::find(customColors, customColorsEnd, color); if (match != customColorsEnd) { const int index = int(match - customColors); - const int row = index / customColorRows; - const int column = index % customColorRows; + const int column = index / customColorRows; + const int row = index % customColorRows; _q_newCustom(row, column); custom->setCurrent(row, column); custom->setSelected(row, column); -- cgit v1.2.3