Debian Codegraph @

src/Gui/ActionFunction.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QAction>
24 #include <QMap>
25 #include <QPointer>
26 #include <QTimer>
27
28 #include "ActionFunction.h"
29
30
31 using namespace Gui;
32
33
34 namespace Gui
35 {
36 class ActionFunctionPrivate
37 {
38 public:
39 QMap<QAction*, std::function<void()>> triggerMap;
40 QMap<QAction*, std::function<void(bool)>> toggleMap;
41 QMap<QAction*, std::function<void()>> hoverMap;
42 };
43 } // namespace Gui
44
45 ActionFunction::ActionFunction(QObject* parent)
46 : QObject(parent)
47 , d_ptr(new ActionFunctionPrivate())
48 {}
49
50 ActionFunction::~ActionFunction() = default;
51
52 void ActionFunction::trigger(QAction* action, std::function<void()> func)
53 {
54 Q_D(ActionFunction);
55
56 d->triggerMap[action] = func;
57 connect(action, &QAction::triggered, this, &ActionFunction::triggered);
58 }
59
60 void ActionFunction::triggered()
61 {
62 Q_D(ActionFunction);
63
64 auto a = qobject_cast<QAction*>(sender());
65 QMap<QAction*, std::function<void()>>::iterator it = d->triggerMap.find(a);
66 if (it != d->triggerMap.end()) {
67 // invoke the class function here
68 it.value()();
69 }
70 }
71
72 void ActionFunction::toggle(QAction* action, std::function<void(bool)> func)
73 {
74 Q_D(ActionFunction);
75
76 d->toggleMap[action] = func;
77 connect(action, &QAction::toggled, this, &ActionFunction::toggled);
78 }
79
80 void ActionFunction::toggled(bool on)
81 {
82 Q_D(ActionFunction);
83
84 auto a = qobject_cast<QAction*>(sender());
85 QMap<QAction*, std::function<void(bool)>>::iterator it = d->toggleMap.find(a);
86 if (it != d->toggleMap.end()) {
87 // invoke the class function here
88 it.value()(on);
89 }
90 }
91
92 void ActionFunction::hover(QAction* action, std::function<void()> func)
93 {
94 Q_D(ActionFunction);
95
96 d->hoverMap[action] = func;
97 connect(action, &QAction::hovered, this, &ActionFunction::hovered);
98 }
99
100 void ActionFunction::hovered()
101 {
102 Q_D(ActionFunction);
103
104 auto a = qobject_cast<QAction*>(sender());
105 QMap<QAction*, std::function<void()>>::iterator it = d->hoverMap.find(a);
106 if (it != d->hoverMap.end()) {
107 // invoke the class function here
108 it.value()();
109 }
110 }
111
112 // ----------------------------------------------------------------------------
113
114 namespace Gui
115 {
116 class TimerFunctionPrivate
117 {
118 public:
119 std::function<void()> timeoutFunc;
120 std::function<void(QObject*)> timeoutFuncQObject;
121 std::function<void(QVariant)> timeoutFuncQVariant;
122 bool autoDelete;
123 QPointer<QObject> argQObject;
124 QVariant argQVariant;
125 };
126 } // namespace Gui
127
128 TimerFunction::TimerFunction(QObject* parent)
129 : QObject(parent)
130 , d_ptr(new TimerFunctionPrivate())
131 {
132 d_ptr->autoDelete = false;
133 }
134
135 TimerFunction::~TimerFunction() = default;
136
137 void TimerFunction::setFunction(std::function<void()> func)
138 {
139 Q_D(TimerFunction);
140 d->timeoutFunc = func;
141 }
142
143 void TimerFunction::setFunction(std::function<void(QObject*)> func, QObject* args)
144 {
145 Q_D(TimerFunction);
146 d->timeoutFuncQObject = func;
147 d->argQObject = args;
148 }
149
150 void TimerFunction::setFunction(std::function<void(QVariant)> func, QVariant args)
151 {
152 Q_D(TimerFunction);
153 d->timeoutFuncQVariant = func;
154 d->argQVariant = args;
155 }
156
157 void TimerFunction::setAutoDelete(bool on)
158 {
159 Q_D(TimerFunction);
160 d->autoDelete = on;
161 }
162
163 void TimerFunction::timeout()
164 {
165 Q_D(TimerFunction);
166 if (d->timeoutFunc) {
167 d->timeoutFunc();
168 }
169 else if (d->timeoutFuncQObject) {
170 d->timeoutFuncQObject(d->argQObject);
171 }
172 else if (d->timeoutFuncQVariant) {
173 d->timeoutFuncQVariant(d->argQVariant);
174 }
175 if (d->autoDelete) {
176 deleteLater();
177 }
178 }
179
180 void TimerFunction::singleShot(int ms)
181 {
182 QTimer::singleShot(ms, this, &Gui::TimerFunction::timeout);
183 }
184
185 #include "moc_ActionFunction.cpp"