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
9#include "kis_icon_utils.h"
10#include <qapplication.h>
11#include <qevent.h>
12#include <qpainter.h>
13#include <qpainterpath.h>
14
15static constexpr int ICON_SIZE_OFFSET = 6;
16
17KisSelectionActionsPanelButton::KisSelectionActionsPanelButton(const QString& iconName, const QString &tooltip, int size, QWidget *parent)
18 : QAbstractButton(parent)
19{
20 setIcon(KisIconUtils::loadIcon(iconName));
21 setFixedSize(size, size);
22 setToolTip(tooltip);
23 setCursor(Qt::PointingHandCursor);
24 setIconSize(QSize(size - ICON_SIZE_OFFSET, size - ICON_SIZE_OFFSET));
25 setAttribute(Qt::WA_AcceptTouchEvents);
26}
27
32
34{
35 QRect rect = geometry();
36 //Draw an outline when the button is pressed
37 if(this->isDown()) {
38 QPainterPath path;
39 path.addRoundedRect(rect, 3, 3);
40 QPen pen = qApp->palette().highlight().color();
41 pen.setWidth(2);
42
43 painter.setPen(pen);
44 painter.drawPath(path) ;
45 }
46
47 int padding = ICON_SIZE_OFFSET / 2;
48 icon().paint(&painter, rect.marginsRemoved(QMargins(padding, padding, padding, padding)));
49}
50
51
52//Dummy paint event, so that this class isn't treated like an abstract class
54{
55 Q_UNUSED(e);
56}
57
58// For whatever reason the children of the canvas don't interpret tabletPress/Release events as mouse press/release events
59// So we have to do this manually
61{
62 if (e->type() == QEvent::TabletPress) {
63 QMouseEvent mouseEvent(QEvent::MouseButtonPress,
64 e->posF(),
65 e->globalPosF(),
66 e->button(),
67 e->buttons(),
68 e->modifiers());
69 QAbstractButton::mousePressEvent(&mouseEvent);
70 e->accept();
71 } else if (e->type() == QEvent::TabletRelease) {
72 QMouseEvent mouseEvent(QEvent::MouseButtonRelease,
73 e->posF(),
74 e->globalPosF(),
75 e->button(),
76 e->buttons(),
77 e->modifiers());
78 QAbstractButton::mouseReleaseEvent(&mouseEvent);
79 e->accept();
80 }
81}
82
84{
85 //Manually handle the button being pressed on touch
86 if (e->type() == QEvent::TouchBegin) {
87 QTouchEvent *touchEvent = static_cast<QTouchEvent *>(e);
88#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
89 int pointsCount = touchEvent->points().count();
90#else
91 int pointsCount = touchEvent->touchPoints().count();
92#endif
93 if (pointsCount == 1) {
94 this->animateClick();
95 e->accept();
96 return true;
97 }
98 }
99 return QAbstractButton::event(e);
100}
KisSelectionActionsPanelButton(const QString &iconName, const QString &tooltip, int size, QWidget *parent)
static constexpr int ICON_SIZE_OFFSET
QIcon loadIcon(const QString &name)