Utils: Improve QtColorButton

By making it more theme aware, using a contrast color to draw the
outline; using the whole button area for the color (had to implement a
custom focus rect for that); Tidying up the checkerboard code a bit.

Change-Id: I9855c07668f920caf371a03fef7be2795feb2a08
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Alessandro Portale
2023-03-24 18:09:04 +01:00
parent ed8f0fb03e
commit 83759e8083

View File

@@ -3,12 +3,13 @@
#include "qtcolorbutton.h" #include "qtcolorbutton.h"
#include <QMimeData>
#include <QApplication> #include <QApplication>
#include <QColorDialog> #include <QColorDialog>
#include <QDragEnterEvent>
#include <QPainter>
#include <QDrag> #include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QPainter>
#include <QStyleOption>
namespace Utils { namespace Utils {
@@ -157,51 +158,36 @@ bool QtColorButton::isDialogOpen() const
void QtColorButton::paintEvent(QPaintEvent *event) void QtColorButton::paintEvent(QPaintEvent *event)
{ {
QToolButton::paintEvent(event); Q_UNUSED(event)
if (!isEnabled())
return;
const int pixSize = 10;
QBrush br(d_ptr->shownColor());
if (d_ptr->m_backgroundCheckered) {
QPixmap pm(2 * pixSize, 2 * pixSize);
QPainter pmp(&pm);
pmp.fillRect(0, 0, pixSize, pixSize, Qt::white);
pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::white);
pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, d_ptr->shownColor());
br = QBrush(pm);
}
QPainter p(this); QPainter p(this);
const int corr = 5; if (isEnabled()) {
QRect r = rect().adjusted(corr, corr, -corr, -corr); QBrush br(d_ptr->shownColor());
p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr); if (d_ptr->m_backgroundCheckered) {
p.fillRect(r, br); const int pixSize = 10;
QPixmap pm(2 * pixSize, 2 * pixSize);
pm.fill(Qt::white);
QPainter pmp(&pm);
pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::black);
pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::black);
pmp.fillRect(pm.rect(), d_ptr->shownColor());
br = QBrush(pm);
p.setBrushOrigin((width() - pixSize) / 2, (height() - pixSize) / 2);
}
p.fillRect(rect(), br);
}
//const int adjX = qRound(r.width() / 4.0); if (hasFocus()) {
//const int adjY = qRound(r.height() / 4.0); QPen pen;
//p.fillRect(r.adjusted(adjX, adjY, -adjX, -adjY), pen.setBrush(Qt::white);
// QColor(d_ptr->shownColor().rgb())); pen.setStyle(Qt::DotLine);
/* p.setPen(pen);
p.fillRect(r.adjusted(0, r.height() * 3 / 4, 0, 0), p.setCompositionMode(QPainter::CompositionMode_Difference);
QColor(d_ptr->shownColor().rgb())); } else {
p.fillRect(r.adjusted(0, 0, 0, -r.height() * 3 / 4), p.setPen(palette().text().color());
QColor(d_ptr->shownColor().rgb())); p.setOpacity(0.25);
*/ }
/* p.drawRect(rect().adjusted(0, 0, -1, -1));
const QColor frameColor0(0, 0, 0, qRound(0.2 * (0xFF - d_ptr->shownColor().alpha())));
p.setPen(frameColor0);
p.drawRect(r.adjusted(adjX, adjY, -adjX - 1, -adjY - 1));
*/
const QColor frameColor1(0, 0, 0, 26);
p.setPen(frameColor1);
p.drawRect(r.adjusted(1, 1, -2, -2));
const QColor frameColor2(0, 0, 0, 51);
p.setPen(frameColor2);
p.drawRect(r.adjusted(0, 0, -1, -1));
} }
void QtColorButton::mousePressEvent(QMouseEvent *event) void QtColorButton::mousePressEvent(QMouseEvent *event)