FreeCAD

Форк
0
/
ActionFunction.cpp 
182 строки · 5.2 Кб
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 "PreCompiled.h"
24

25
#ifndef _PreComp_
26
# include <QAction>
27
# include <QMap>
28
# include <QPointer>
29
# include <QTimer>
30
#endif
31

32
#include "ActionFunction.h"
33

34

35
using namespace Gui;
36

37

38
namespace Gui {
39
class ActionFunctionPrivate
40
{
41
public:
42
    QMap<QAction*, std::function<void()> > triggerMap;
43
    QMap<QAction*, std::function<void(bool)> > toggleMap;
44
    QMap<QAction*, std::function<void()> > hoverMap;
45
};
46
}
47

48
ActionFunction::ActionFunction(QObject* parent)
49
  : QObject(parent), d_ptr(new ActionFunctionPrivate())
50
{
51
}
52

53
ActionFunction::~ActionFunction() = default;
54

55
void ActionFunction::trigger(QAction* action, std::function<void()> func)
56
{
57
    Q_D(ActionFunction);
58

59
    d->triggerMap[action] = func;
60
    connect(action, &QAction::triggered, this, &ActionFunction::triggered);
61
}
62

63
void ActionFunction::triggered()
64
{
65
    Q_D(ActionFunction);
66

67
    auto a = qobject_cast<QAction*>(sender());
68
    QMap<QAction*, std::function<void()> >::iterator it = d->triggerMap.find(a);
69
    if (it != d->triggerMap.end()) {
70
        // invoke the class function here
71
        it.value()();
72
    }
73
}
74

75
void ActionFunction::toggle(QAction* action, std::function<void(bool)> func)
76
{
77
    Q_D(ActionFunction);
78

79
    d->toggleMap[action] = func;
80
    connect(action, &QAction::toggled, this, &ActionFunction::toggled);
81
}
82

83
void ActionFunction::toggled(bool on)
84
{
85
    Q_D(ActionFunction);
86

87
    auto a = qobject_cast<QAction*>(sender());
88
    QMap<QAction*, std::function<void(bool)> >::iterator it = d->toggleMap.find(a);
89
    if (it != d->toggleMap.end()) {
90
        // invoke the class function here
91
        it.value()(on);
92
    }
93
}
94

95
void ActionFunction::hover(QAction* action, std::function<void()> func)
96
{
97
    Q_D(ActionFunction);
98

99
    d->hoverMap[action] = func;
100
    connect(action, &QAction::hovered, this, &ActionFunction::hovered);
101
}
102

103
void ActionFunction::hovered()
104
{
105
    Q_D(ActionFunction);
106

107
    auto a = qobject_cast<QAction*>(sender());
108
    QMap<QAction*, std::function<void()> >::iterator it = d->hoverMap.find(a);
109
    if (it != d->hoverMap.end()) {
110
        // invoke the class function here
111
        it.value()();
112
    }
113
}
114

115
// ----------------------------------------------------------------------------
116

117
namespace Gui {
118
class TimerFunctionPrivate
119
{
120
public:
121
    std::function<void()> timeoutFunc;
122
    std::function<void(QObject*)> timeoutFuncQObject;
123
    std::function<void(QVariant)> timeoutFuncQVariant;
124
    bool autoDelete;
125
    QPointer<QObject> argQObject;
126
    QVariant argQVariant;
127
};
128
}
129

130
TimerFunction::TimerFunction(QObject* parent)
131
  : QObject(parent), d_ptr(new TimerFunctionPrivate())
132
{
133
    d_ptr->autoDelete = false;
134
}
135

136
TimerFunction::~TimerFunction() = default;
137

138
void TimerFunction::setFunction(std::function<void()> func)
139
{
140
    Q_D(TimerFunction);
141
    d->timeoutFunc = func;
142
}
143

144
void TimerFunction::setFunction(std::function<void(QObject*)> func, QObject* args)
145
{
146
    Q_D(TimerFunction);
147
    d->timeoutFuncQObject = func;
148
    d->argQObject = args;
149
}
150

151
void TimerFunction::setFunction(std::function<void(QVariant)> func, QVariant args)
152
{
153
    Q_D(TimerFunction);
154
    d->timeoutFuncQVariant = func;
155
    d->argQVariant = args;
156
}
157

158
void TimerFunction::setAutoDelete(bool on)
159
{
160
    Q_D(TimerFunction);
161
    d->autoDelete = on;
162
}
163

164
void TimerFunction::timeout()
165
{
166
    Q_D(TimerFunction);
167
    if (d->timeoutFunc)
168
        d->timeoutFunc();
169
    else if (d->timeoutFuncQObject)
170
        d->timeoutFuncQObject(d->argQObject);
171
    else if (d->timeoutFuncQVariant)
172
        d->timeoutFuncQVariant(d->argQVariant);
173
    if (d->autoDelete)
174
        deleteLater();
175
}
176

177
void TimerFunction::singleShot(int ms)
178
{
179
    QTimer::singleShot(ms, this, &Gui::TimerFunction::timeout);
180
}
181

182
#include "moc_ActionFunction.cpp"
183

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.