]> cloud.milkyroute.net Git - dolphin.git/blob - src/middleclickactioneventfilter.cpp
Add navigation history to forward/back buttons
[dolphin.git] / src / middleclickactioneventfilter.cpp
1 /***************************************************************************
2 * Copyright (C) 2017 Kai Uwe Broulik <kde@privat.broulik.de> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "middleclickactioneventfilter.h"
21
22 #include <QAction>
23 #include <QEvent>
24 #include <QMenu>
25 #include <QMouseEvent>
26 #include <QToolBar>
27
28 MiddleClickActionEventFilter::MiddleClickActionEventFilter(QObject *parent) : QObject(parent)
29 {
30
31 }
32
33 MiddleClickActionEventFilter::~MiddleClickActionEventFilter() = default;
34
35 bool MiddleClickActionEventFilter::eventFilter(QObject *watched, QEvent *event)
36 {
37 if (event->type() == QEvent::MouseButtonPress
38 || event->type() == QEvent::MouseButtonRelease) {
39 QMouseEvent *me = static_cast<QMouseEvent *>(event);
40
41 if (me->button() == Qt::MiddleButton) {
42 QToolBar *toolBar = qobject_cast<QToolBar *>(watched);
43 if (toolBar) {
44 QAction *action = toolBar->actionAt(me->pos());
45 if (action) {
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);
51 }
52 m_lastMiddlePressedAction = nullptr;
53 }
54 }
55 }
56 QMenu *menu = qobject_cast<QMenu *>(watched);
57 if (menu) {
58 QAction *action = menu->actionAt(me->pos());
59 if (action) {
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);
65 return true;
66 }
67 m_lastMiddlePressedAction = nullptr;
68 }
69 }
70 }
71 }
72 }
73
74 return QObject::eventFilter(watched, event);
75 }