Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_selection_actions_panel_button.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Luna Lovecraft <ciubix8514@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
8
10#include "kis_icon_utils.h"
11#include <qapplication.h>
12#include <qevent.h>
13#include <qpainter.h>
14#include <qpainterpath.h>
16
17static constexpr int ICON_SIZE_OFFSET = 6;
18
19KisSelectionActionsPanelButton::KisSelectionActionsPanelButton(const QString& iconName, const QString &tooltip, int size, QWidget *parent)
20 : QAbstractButton(parent)
21{
22 setIcon(KisIconUtils::loadIcon(iconName));
23 setFixedSize(size, size);
24 setToolTip(tooltip);
25 setCursor(Qt::PointingHandCursor);
26 setIconSize(QSize(size - ICON_SIZE_OFFSET, size - ICON_SIZE_OFFSET));
27 setAttribute(Qt::WA_AcceptTouchEvents);
29}
30
35
36void KisSelectionActionsPanelButton::draw(QPainter &painter, const KoColorDisplayRendererInterface *displayRendererInterface)
37{
38 QRect rect = geometry();
39 //Draw an outline when the button is pressed
40
41
42 QPainterPath rectAroundButton;
43 rectAroundButton.addRoundedRect(rect, 3, 3);
44
45 if (isCheckable() && isChecked()) {
46 painter.save();
47 QBrush brush = displayRendererInterface->systemPaletteForDisplayColorSpace().base();
48 QColor color = brush.color();
49 color.setAlphaF(0.5);
50 brush.setColor(color);
51 painter.setBrush(brush);
52 painter.setPen(Qt::NoPen);
53 painter.drawPath(rectAroundButton);
54 painter.restore();
55 }
56
57 if (!isCheckable() && isDown()) {
58 QPen pen = displayRendererInterface->systemPaletteForDisplayColorSpace().highlight().color();
59 pen.setWidth(2);
60
61 painter.setPen(pen);
62 painter.drawPath(rectAroundButton);
63 }
64
65 int padding = ICON_SIZE_OFFSET / 2;
66
67#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
68 icon().paint(&painter, rect.marginsRemoved(QMargins(padding, padding, padding, padding)));
69#else
70 QImage ic = displayRendererInterface->convertImageToDisplayColorSpace(icon().pixmap(rect.width()-(padding*2), rect.height()-(padding*2)).toImage());
71 QRect target = QRect(QPoint(0, 0), ic.deviceIndependentSize().toSize());
72 target.moveCenter(rect.center());
73 painter.drawImage(target.intersected(rect.marginsRemoved(QMargins(padding, padding, padding, padding))), ic);
74#endif
75
76}
77
78
79//Dummy paint event, so that this class isn't treated like an abstract class
81{
82 Q_UNUSED(e);
83}
84
85// For whatever reason the children of the canvas don't interpret tabletPress/Release events as mouse press/release events
86// So we have to do this manually
88{
89 if (e->type() == QEvent::TabletPress) {
90 QMouseEvent mouseEvent(QEvent::MouseButtonPress,
91 e->posF(),
92 e->globalPosF(),
93 e->button(),
94 e->buttons(),
95 e->modifiers());
96 QAbstractButton::mousePressEvent(&mouseEvent);
97 e->accept();
98 } else if (e->type() == QEvent::TabletRelease) {
99 QMouseEvent mouseEvent(QEvent::MouseButtonRelease,
100 e->posF(),
101 e->globalPosF(),
102 e->button(),
103 e->buttons(),
104 e->modifiers());
105 QAbstractButton::mouseReleaseEvent(&mouseEvent);
106 e->accept();
107 }
108}
109
111{
112 //Manually handle the button being pressed on touch
113 if (e->type() == QEvent::TouchBegin) {
114 QTouchEvent *touchEvent = static_cast<QTouchEvent *>(e);
115#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
116 int pointsCount = touchEvent->points().count();
117#else
118 int pointsCount = touchEvent->touchPoints().count();
119#endif
120 if (pointsCount == 1) {
121 this->animateClick();
122 e->accept();
123 return true;
124 }
125 }
126 return QAbstractButton::event(e);
127}
128
130{
131 Q_EMIT customContextMenuRequested(mapToGlobal(event->pos()));
132 event->accept();
133}
134
136{
137 //Do not propagate the rmb event, to prevent other tool context menus from appearing
138 if (event->button() == Qt::RightButton) {
139 event->accept();
140 } else {
141 QAbstractButton::mousePressEvent(event);
142 }
143}
KisMagneticGraph::vertex_descriptor target(typename KisMagneticGraph::edge_descriptor e, KisMagneticGraph g)
static constexpr char ENABLED_PROPERTY[]
void draw(QPainter &painter, const KoColorDisplayRendererInterface *displayRendererInterface)
void mousePressEvent(QMouseEvent *event) override
KisSelectionActionsPanelButton(const QString &iconName, const QString &tooltip, int size, QWidget *parent)
void contextMenuEvent(QContextMenuEvent *event) override
virtual QImage convertImageToDisplayColorSpace(const QImage source) const =0
convertImageToDisplayColorSpace
virtual QPalette systemPaletteForDisplayColorSpace() const =0
systemPaletteForDisplayColorSpace
static constexpr int ICON_SIZE_OFFSET
QIcon loadIcon(const QString &name)