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 if(this->isDown()) {
41 QPainterPath path;
42 path.addRoundedRect(rect, 3, 3);
43 QPen pen = displayRendererInterface->systemPaletteForDisplayColorSpace().highlight().color();
44 pen.setWidth(2);
45
46 painter.setPen(pen);
47 painter.drawPath(path) ;
48 }
49
50 int padding = ICON_SIZE_OFFSET / 2;
51 QImage ic = displayRendererInterface->convertImageToDisplayColorSpace(icon().pixmap(rect.width()-(padding*2), rect.height()-(padding*2)).toImage());
52#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
53 QRect target = QRect(QPoint(0, 0), ic.size()/ic.devicePixelRatioF());
54#else
55 QRect target = QRect(QPoint(0, 0), ic.deviceIndependentSize().toSize());
56#endif
57 target.moveCenter(rect.center());
58 painter.drawImage(target.intersected(rect.marginsRemoved(QMargins(padding, padding, padding, padding))), ic);
59}
60
61
62//Dummy paint event, so that this class isn't treated like an abstract class
64{
65 Q_UNUSED(e);
66}
67
68// For whatever reason the children of the canvas don't interpret tabletPress/Release events as mouse press/release events
69// So we have to do this manually
71{
72 if (e->type() == QEvent::TabletPress) {
73 QMouseEvent mouseEvent(QEvent::MouseButtonPress,
74 e->posF(),
75 e->globalPosF(),
76 e->button(),
77 e->buttons(),
78 e->modifiers());
79 QAbstractButton::mousePressEvent(&mouseEvent);
80 e->accept();
81 } else if (e->type() == QEvent::TabletRelease) {
82 QMouseEvent mouseEvent(QEvent::MouseButtonRelease,
83 e->posF(),
84 e->globalPosF(),
85 e->button(),
86 e->buttons(),
87 e->modifiers());
88 QAbstractButton::mouseReleaseEvent(&mouseEvent);
89 e->accept();
90 }
91}
92
94{
95 //Manually handle the button being pressed on touch
96 if (e->type() == QEvent::TouchBegin) {
97 QTouchEvent *touchEvent = static_cast<QTouchEvent *>(e);
98#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
99 int pointsCount = touchEvent->points().count();
100#else
101 int pointsCount = touchEvent->touchPoints().count();
102#endif
103 if (pointsCount == 1) {
104 this->animateClick();
105 e->accept();
106 return true;
107 }
108 }
109 return QAbstractButton::event(e);
110}
111
113{
114 Q_EMIT customContextMenuRequested(mapToGlobal(event->pos()));
115 event->accept();
116}
117
119{
120 //Do not propagate the rmb event, to prevent other tool context menus from appearing
121 if (event->button() == Qt::RightButton) {
122 event->accept();
123 } else {
124 QAbstractButton::mousePressEvent(event);
125 }
126}
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)