Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPopupWidgetAction.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
3 * SPDX-FileCopyrightText: 2021 Emmet O 'Neill <emmetoneill.pdx@gmail.com>
4 * SPDX-FileCopyrightText: 2021 Eoin O 'Neill <eoinoneill1991@gmail.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
10
11#include <QCursor>
12#include <QMenu>
13#include <QTouchEvent>
14
15#include <klocalizedstring.h>
16
18#include <kis_canvas2.h>
19#include "kis_tool_proxy.h"
20#include "kis_popup_palette.h"
21#include "kis_input_manager.h"
22
23struct SinglePressEventEater : public QObject
24{
25 bool eventFilter(QObject *, QEvent *event) override {
26 if (hungry && event->type() == QEvent::MouseButtonPress) {
27 hungry = false;
28 return true;
29 }
30
31 return false;
32 }
33
34private:
35 bool hungry = true;
36};
37
38
39//=================================================================
40
42 : KisAbstractInputAction("Show Popup Widget")
43{
44 setName(i18n("Show Popup Widget"));
45 setDescription(i18n("Show the current tool's popup widget."));
46}
47
51
52void KisPopupWidgetAction::end(QEvent *event)
53{
54 if (QMenu *popupMenu = inputManager()->toolProxy()->popupActionsMenu()) { // Handle popup menus...
55 QEvent::Type requestingEventType = event ? event->type() : QEvent::None;
56 std::optional<QPointF> basePopupPos = KoPointerEvent::fetchGlobalPositionFromPointerEvent(event);
57
62 QTimer::singleShot(0, this, [popupMenu, requestingEventType, basePopupPos](){
63 if (popupMenu) {
64 QPoint popupPos;
65 QScopedPointer<SinglePressEventEater> eventEater;
66
67 if (basePopupPos && requestingEventType == QEvent::TabletPress) {
68 eventEater.reset(new SinglePressEventEater());
69 popupMenu->installEventFilter(eventEater.data());
70 popupPos = basePopupPos->toPoint() + QPoint(10,10);
71 } else if (basePopupPos) {
72 popupPos = basePopupPos->toPoint();
73 } else {
74 popupPos = QCursor::pos();
75 }
76
77 popupMenu->exec(popupPos);
78 popupMenu->clear();
79 }
80 });
81 } else if (KisPopupWidgetInterface *popupWidget = inputManager()->toolProxy()->popupWidget()) { // Handle other popup widgets...
82 if (!popupWidget->onScreen()) {
83 QPoint pos = eventPos(event);
84 if (pos.isNull()) {
85 pos = inputManager()->canvas()->canvasWidget()->mapFromGlobal(QCursor::pos());
86 }
87 inputManager()->registerPopupWidget(popupWidget);
88
91 popupWidget->popup(pos);
92 } else {
93 popupWidget->dismiss();
94 }
95 }
96}
Abstract base class for input actions.
static KisInputManager * inputManager
QPoint eventPos(const QEvent *event)
void setName(const QString &name)
void setDescription(const QString &description)
KisAbstractCanvasWidget * canvasWidget
KisCanvas2 * canvas() const
QPointer< KisToolProxy > toolProxy() const
void registerPopupWidget(KisPopupWidgetInterface *popupWidget)
void end(QEvent *) override
The PopupWidgetInterface abstract class defines the basic interface that will be used by all popup wi...
static std::optional< QPointF > fetchGlobalPositionFromPointerEvent(QEvent *event)
bool eventFilter(QObject *, QEvent *event) override