1 /***************************************************************************
2 * Copyright (C) 2017 Kai Uwe Broulik <kde@privat.broulik.de> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "middleclickactioneventfilter.h"
25 #include <QMouseEvent>
28 MiddleClickActionEventFilter::MiddleClickActionEventFilter(QObject
*parent
) : QObject(parent
)
33 MiddleClickActionEventFilter::~MiddleClickActionEventFilter() = default;
35 bool MiddleClickActionEventFilter::eventFilter(QObject
*watched
, QEvent
*event
)
37 if (event
->type() == QEvent::MouseButtonPress
38 || event
->type() == QEvent::MouseButtonRelease
) {
39 QMouseEvent
*me
= static_cast<QMouseEvent
*>(event
);
41 if (me
->button() == Qt::MiddleButton
) {
42 QToolBar
*toolBar
= qobject_cast
<QToolBar
*>(watched
);
44 QAction
*action
= toolBar
->actionAt(me
->pos());
46 if (event
->type() == QEvent::MouseButtonPress
) {
47 m_lastMiddlePressedAction
= action
;
48 } else if (event
->type() == QEvent::MouseButtonRelease
) {
49 if (m_lastMiddlePressedAction
== action
) {
50 emit
actionMiddleClicked(action
);
52 m_lastMiddlePressedAction
= nullptr;
56 QMenu
*menu
= qobject_cast
<QMenu
*>(watched
);
58 QAction
*action
= menu
->actionAt(me
->pos());
60 if (event
->type() == QEvent::MouseButtonPress
) {
61 m_lastMiddlePressedAction
= action
;
62 } else if (event
->type() == QEvent::MouseButtonRelease
) {
63 if (m_lastMiddlePressedAction
== action
) {
64 emit
actionMiddleClicked(action
);
67 m_lastMiddlePressedAction
= nullptr;
74 return QObject::eventFilter(watched
, event
);