Krita Source Code Documentation
Loading...
Searching...
No Matches
WGColorPreviewToolTip.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Mathias Wein <lynx.mw+kde@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
8
9#include <QApplication>
10#include <QScreen>
11#include <QFile>
12#include <QScreen>
13#include <QPainter>
14#include <QWindow>
15
16#include <cmath>
17
19 : QWidget(parent)
20 , m_color(0, 0, 0)
21 , m_previousColor(0, 0, 0, 0)
22 , m_lastUsedColor(0, 0, 0, 0)
23{
24 setWindowFlags(Qt::ToolTip | Qt::NoDropShadowWindowHint);
25 setAttribute(Qt::WA_TranslucentBackground);
26 resize(100, 150);
27 QString iconFile(":/dark_krita_tool_freehand.svg");
28 if (QFile(iconFile).exists()) {
29 m_brushIcon.addFile(iconFile, QSize(16, 16));
30 }
31 iconFile = ":/light_krita_tool_freehand.svg";
32 if (QFile(iconFile).exists()) {
33 m_brushIcon.addFile(iconFile, QSize(16, 16), QIcon::Normal, QIcon::On);
34 }
35}
36
37void WGColorPreviewToolTip::updatePosition(const QWidget *focus)
38{
39 const QWidget *parent = focus ? focus : parentWidget();
40 if (!parent) {
41 return;
42 }
43
44 QPoint parentPos = parent->mapToGlobal(QPoint(0,0));
45 const QRect availRect = this->screen()->availableGeometry();
46 QPoint targetPos;
47 if (parentPos.x() - width() > availRect.x()) {
48 targetPos = QPoint(parentPos.x() - width(), parentPos.y());
49 } else if (parentPos.x() + parent->width() + width() < availRect.right()) {
50 targetPos = parent->mapToGlobal(QPoint(parent->width(), 0));
51 } else if (parentPos.y() - height() > availRect.y()) {
52 targetPos = QPoint(parentPos.x(), parentPos.y() - height());
53 } else {
54 targetPos = QPoint(parentPos.x(), parentPos.y() + parent->height());
55 }
56 move(targetPos);
57}
58
60{
61 // a cheap approximation of luma assuming sRGB with gamma = 2.0
62 return std::sqrt(col.redF() * col.redF() * 0.21 +
63 col.greenF() * col.greenF() * 0.71 +
64 col.blueF() * col.blueF() * 0.08);
65}
66
68 Q_UNUSED(e)
69 QPainter painter(this);
70 painter.fillRect(0, 0, width(), width(), m_color);
71 painter.fillRect(0, width(), width()/2, height(), m_previousColor);
72 painter.fillRect(width()/2, width(), width(), height(), m_lastUsedColor);
73
74 QPixmap icon;
75 QWindow *window = windowHandle();
76 if (window && m_lastUsedColor.alpha() > 0) {
77 QIcon::State iconState;
78 iconState = estimateBrightness(m_lastUsedColor) > 0.5 ? QIcon::Off : QIcon::On;
79#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
80 icon = m_brushIcon.pixmap(window, QSize(16, 16), QIcon::Normal, iconState);
81#else
82 icon = m_brushIcon.pixmap(QSize(16, 16), window->devicePixelRatio(), QIcon::Normal, iconState);
83#endif
84 }
85 if (!icon.isNull()) {
86 painter.drawPixmap(width() - 18, height() - 18, icon);
87 }
88}
WGColorPreviewToolTip(QWidget *parent=nullptr)
void updatePosition(const QWidget *focus)
static qreal estimateBrightness(QColor col)
void paintEvent(QPaintEvent *e) override