File indexing completed on 2024-12-22 04:12:45

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2012 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  * SPDX-FileCopyrightText: 2021 Emmet O 'Neill <emmetoneill.pdx@gmail.com>
0004  * SPDX-FileCopyrightText: 2021 Eoin O 'Neill <eoinoneill1991@gmail.com>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "KisPopupWidgetAction.h"
0010 
0011 #include <QCursor>
0012 #include <QMenu>
0013 
0014 #include <klocalizedstring.h>
0015 
0016 #include <kis_favorite_resource_manager.h>
0017 #include <kis_canvas2.h>
0018 #include "kis_tool_proxy.h"
0019 #include "kis_popup_palette.h"
0020 #include "kis_input_manager.h"
0021 
0022 struct SinglePressEventEater : public QObject
0023 {
0024     bool eventFilter(QObject *, QEvent *event) override {
0025         if (hungry && event->type() == QEvent::MouseButtonPress) {
0026             hungry = false;
0027             return true;
0028         }
0029 
0030         return false;
0031     }
0032 
0033 private:
0034     bool hungry = true;
0035 };
0036 
0037 
0038 //=================================================================
0039 
0040 KisPopupWidgetAction::KisPopupWidgetAction()
0041     : KisAbstractInputAction("Show Popup Widget"),
0042       m_requestedWithStylus(false)
0043 {
0044     setName(i18n("Show Popup Widget"));
0045     setDescription(i18n("Show the current tool's popup widget."));
0046 }
0047 
0048 KisPopupWidgetAction::~KisPopupWidgetAction()
0049 {
0050 }
0051 
0052 void KisPopupWidgetAction::end(QEvent *event)
0053 {
0054     if (QMenu *popupMenu = inputManager()->toolProxy()->popupActionsMenu()) { // Handle popup menus...
0055         m_requestedWithStylus = event && event->type() == QEvent::TabletPress;
0056 
0057         /**
0058          * Opening a menu changes the focus of the windows, so we should not open it
0059          * inside the filtering loop. Just raise it using the timer.
0060          */
0061         QTimer::singleShot(0, this, [this, popupMenu](){
0062             if (popupMenu) {
0063                 QPoint stylusOffset;
0064                 QScopedPointer<SinglePressEventEater> eventEater;
0065 
0066                 if (m_requestedWithStylus) {
0067                     eventEater.reset(new SinglePressEventEater());
0068                     popupMenu->installEventFilter(eventEater.data());
0069                     stylusOffset += QPoint(10,10);
0070                 }
0071 
0072                 popupMenu->exec(QCursor::pos() + stylusOffset);
0073                 popupMenu->clear();
0074             }
0075         });
0076     } else if (KisPopupWidgetInterface *popupWidget = inputManager()->toolProxy()->popupWidget()) { // Handle other popup widgets...
0077         if (!popupWidget->onScreen()) {
0078             QPoint pos = eventPos(event);
0079             if (pos.isNull()) {
0080                 pos = inputManager()->canvas()->canvasWidget()->mapFromGlobal(QCursor::pos());
0081             }
0082             inputManager()->registerPopupWidget(popupWidget);
0083             popupWidget->popup(pos);
0084         } else {
0085             popupWidget->dismiss();
0086         }
0087     }
0088 }