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
15#include <cmath>
16
18 : QWidget(parent)
19 , m_color(0, 0, 0)
20 , m_previousColor(0, 0, 0, 0)
21 , m_lastUsedColor(0, 0, 0, 0)
22{
23 setWindowFlags(Qt::ToolTip | Qt::NoDropShadowWindowHint);
24 setAttribute(Qt::WA_TranslucentBackground);
25 resize(100, 150);
26 QString iconFile(":/dark_krita_tool_freehand.svg");
27 if (QFile(iconFile).exists()) {
28 m_brushIcon.addFile(iconFile, QSize(16, 16));
29 }
30 iconFile = ":/light_krita_tool_freehand.svg";
31 if (QFile(iconFile).exists()) {
32 m_brushIcon.addFile(iconFile, QSize(16, 16), QIcon::Normal, QIcon::On);
33 }
34}
35
36void WGColorPreviewToolTip::updatePosition(const QWidget *focus)
37{
38 const QWidget *parent = focus ? focus : parentWidget();
39 if (!parent) {
40 return;
41 }
42
43 QPoint parentPos = parent->mapToGlobal(QPoint(0,0));
44 const QRect availRect = this->screen()->availableGeometry();
45 QPoint targetPos;
46 if (parentPos.x() - width() > availRect.x()) {
47 targetPos = QPoint(parentPos.x() - width(), parentPos.y());
48 } else if (parentPos.x() + parent->width() + width() < availRect.right()) {
49 targetPos = parent->mapToGlobal(QPoint(parent->width(), 0));
50 } else if (parentPos.y() - height() > availRect.y()) {
51 targetPos = QPoint(parentPos.x(), parentPos.y() - height());
52 } else {
53 targetPos = QPoint(parentPos.x(), parentPos.y() + parent->height());
54 }
55 move(targetPos);
56}
57
59{
60 // a cheap approximation of luma assuming sRGB with gamma = 2.0
61 return std::sqrt(col.redF() * col.redF() * 0.21 +
62 col.greenF() * col.greenF() * 0.71 +
63 col.blueF() * col.blueF() * 0.08);
64}
65
67 Q_UNUSED(e)
68 QPainter painter(this);
69 painter.fillRect(0, 0, width(), width(), m_color);
70 painter.fillRect(0, width(), width()/2, height(), m_previousColor);
71 painter.fillRect(width()/2, width(), width(), height(), m_lastUsedColor);
72
73 QPixmap icon;
74 QWindow *window = windowHandle();
75 if (window && m_lastUsedColor.alpha() > 0) {
76 QIcon::State iconState;
77 iconState = estimateBrightness(m_lastUsedColor) > 0.5 ? QIcon::Off : QIcon::On;
78 icon = m_brushIcon.pixmap(window, QSize(16, 16), QIcon::Normal, iconState);
79 }
80 if (!icon.isNull()) {
81 painter.drawPixmap(width() - 18, height() - 18, icon);
82 }
83}
WGColorPreviewToolTip(QWidget *parent=nullptr)
void updatePosition(const QWidget *focus)
static qreal estimateBrightness(QColor col)
void paintEvent(QPaintEvent *e) override