QT6 color handling changes

* use typedef for floating point value used in color related API
* changes in screen grabbing API used by color picker
This commit is contained in:
Kārlis Seņko 2021-04-13 21:44:58 +03:00 committed by karliss
parent 988918a038
commit 2799390314
3 changed files with 38 additions and 12 deletions

View File

@ -51,9 +51,9 @@ ColorThemeWorker::ColorThemeWorker(QObject *parent) : QObject(parent)
QColor ColorThemeWorker::mergeColors(const QColor &upper, const QColor &lower) const QColor ColorThemeWorker::mergeColors(const QColor &upper, const QColor &lower) const
{ {
qreal r1, g1, b1, a1; qhelpers::ColorFloat r1, g1, b1, a1;
qreal r2, g2, b2, a2; qhelpers::ColorFloat r2, g2, b2, a2;
qreal r, g, b, a; qhelpers::ColorFloat r, g, b, a;
upper.getRgbF(&r1, &g1, &b1, &a1); upper.getRgbF(&r1, &g1, &b1, &a1);
lower.getRgbF(&r2, &g2, &b2, &a2); lower.getRgbF(&r2, &g2, &b2, &a2);

View File

@ -85,6 +85,12 @@ CUTTER_EXPORT void selectIndexByData(QComboBox *comboBox, QVariant data, int def
CUTTER_EXPORT void emitColumnChanged(QAbstractItemModel *model, int column); CUTTER_EXPORT void emitColumnChanged(QAbstractItemModel *model, int column);
CUTTER_EXPORT bool filterStringContains(const QString &string, const QSortFilterProxyModel *model); CUTTER_EXPORT bool filterStringContains(const QString &string, const QSortFilterProxyModel *model);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
using ColorFloat = float;
#else
using ColorFloat = qreal;
#endif
} // qhelpers } // qhelpers
#endif // HELPERS_H #endif // HELPERS_H

View File

@ -1,11 +1,16 @@
#include "ColorPicker.h" #include "ColorPicker.h"
#include "ui_ColorPicker.h" #include "ui_ColorPicker.h"
#include "common/Helpers.h"
#include <QPaintEvent> #include <QPaintEvent>
#include <QPainter> #include <QPainter>
#include <QPainterPath> #include <QPainterPath>
#include <QMouseEvent> #include <QMouseEvent>
#include <QDesktopWidget> #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
# include <QDesktopWidget>
#else
# include <QWindow>
#endif
#include <QPixmap> #include <QPixmap>
#include <QCursor> #include <QCursor>
#include <QScreen> #include <QScreen>
@ -23,7 +28,7 @@ void ColorPickArea::paintEvent(QPaintEvent *event)
for (int x = event->rect().x(); x <= event->rect().right(); x++) { for (int x = event->rect().x(); x <= event->rect().right(); x++) {
for (int y = event->rect().y(); y <= event->rect().bottom(); y++) { for (int y = event->rect().y(); y <= event->rect().bottom(); y++) {
qreal h, s, v; qhelpers::ColorFloat h, s, v;
QColor c = pointToColor(x, y); QColor c = pointToColor(x, y);
c.getHsvF(&h, &s, &v); c.getHsvF(&h, &s, &v);
c.setHsvF(h, s, 1); c.setHsvF(h, s, 1);
@ -74,7 +79,7 @@ void ColorPickerWidget::mouseMoveEvent(QMouseEvent *event)
QColor ColorPickArea::pointToColor(int x, int y) const QColor ColorPickArea::pointToColor(int x, int y) const
{ {
QColor color; QColor color;
qreal h, s, v, a; qhelpers::ColorFloat h, s, v, a;
currColor.getHsvF(&h, &s, &v, &a); currColor.getHsvF(&h, &s, &v, &a);
color.setHsvF(qreal(x) / width(), 1.0 - qreal(y) / height(), v, a); color.setHsvF(qreal(x) / width(), 1.0 - qreal(y) / height(), v, a);
return color; return color;
@ -82,7 +87,7 @@ QColor ColorPickArea::pointToColor(int x, int y) const
QPoint ColorPickArea::colorToPoint(const QColor &color) const QPoint ColorPickArea::colorToPoint(const QColor &color) const
{ {
qreal h, s, v; qhelpers::ColorFloat h, s, v;
color.getHsvF(&h, &s, &v); color.getHsvF(&h, &s, &v);
return QPointF(h * width(), (1.0 - s) * height()).toPoint(); return QPointF(h * width(), (1.0 - s) * height()).toPoint();
} }
@ -114,7 +119,7 @@ void ColorValueBar::paintEvent(QPaintEvent *event)
{ {
QPainter p(this); QPainter p(this);
QColor color = currColor; QColor color = currColor;
qreal h, s, v; qhelpers::ColorFloat h, s, v;
currColor.getHsvF(&h, &s, &v); currColor.getHsvF(&h, &s, &v);
v = 1.0 - v; v = 1.0 - v;
@ -147,7 +152,7 @@ QColor ColorValueBar::pointToColor(int x, int y) const
{ {
Q_UNUSED(x) Q_UNUSED(x)
QColor color = currColor; QColor color = currColor;
qreal h, s, v, a; qhelpers::ColorFloat h, s, v, a;
color.getHsvF(&h, &s, &v, &a); color.getHsvF(&h, &s, &v, &a);
color.setHsvF(h, s, 1.0 - qreal(y) / height(), a); color.setHsvF(h, s, 1.0 - qreal(y) / height(), a);
return color; return color;
@ -155,7 +160,7 @@ QColor ColorValueBar::pointToColor(int x, int y) const
QPoint ColorValueBar::colorToPoint(const QColor &color) const QPoint ColorValueBar::colorToPoint(const QColor &color) const
{ {
qreal h, s, v; qhelpers::ColorFloat h, s, v;
color.getHsvF(&h, &s, &v); color.getHsvF(&h, &s, &v);
return QPoint(rect().x(), int((1.0 - v) * height())); return QPoint(rect().x(), int((1.0 - v) * height()));
} }
@ -308,12 +313,27 @@ void ColorPicker::mouseMoveEvent(QMouseEvent *event)
QColor ColorPicker::getColorAtMouse() QColor ColorPicker::getColorAtMouse()
{ {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const QDesktopWidget *desktop = QApplication::desktop(); const QDesktopWidget *desktop = QApplication::desktop();
const QPixmap pixmap = const QPixmap pixmap =
QGuiApplication::screens() QGuiApplication::screens()
.at(desktop->screenNumber()) .at(desktop->screenNumber())
->grabWindow(desktop->winId(), QCursor::pos().x(), QCursor::pos().y(), 1, 1); ->grabWindow(desktop->winId(), QCursor::pos().x(), QCursor::pos().y(), 1, 1);
return QColor(pixmap.toImage().pixel(0, 0)); return QColor(pixmap.toImage().pixel(0, 0));
#else
QPoint pos = QCursor::pos();
auto screen = QGuiApplication::screenAt(pos);
if (!screen) {
screen = QGuiApplication::primaryScreen();
}
if (screen) {
auto screenRelativePos = pos - screen->geometry().topLeft();
const QPixmap pixmap =
screen->grabWindow(0, screenRelativePos.x(), screenRelativePos.y(), 1, 1);
return QColor(pixmap.toImage().pixel(0, 0));
}
return QColorConstants::Red;
#endif
} }
bool ColorPicker::isPickingFromScreen() const bool ColorPicker::isPickingFromScreen() const
@ -378,7 +398,7 @@ void AlphaChannelBar::paintEvent(QPaintEvent *event)
QPainter p(this); QPainter p(this);
QRect barRect = rect(); QRect barRect = rect();
qreal h, s, v, a; qhelpers::ColorFloat h, s, v, a;
currColor.getHsvF(&h, &s, &v, &a); currColor.getHsvF(&h, &s, &v, &a);
a = 1.0 - a; a = 1.0 - a;
const int triangleSize = 10; const int triangleSize = 10;
@ -416,7 +436,7 @@ QColor AlphaChannelBar::pointToColor(int x, int y) const
{ {
Q_UNUSED(x) Q_UNUSED(x)
QColor color = currColor; QColor color = currColor;
qreal h, s, v; qhelpers::ColorFloat h, s, v;
color.getHsvF(&h, &s, &v); color.getHsvF(&h, &s, &v);
color.setHsvF(h, s, v, 1.0 - qreal(y) / height()); color.setHsvF(h, s, v, 1.0 - qreal(y) / height());
return color; return color;