| 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 |
|
| 24 |
#ifndef GUI_ACTIONFUNCTION_H |
| 25 |
#define GUI_ACTIONFUNCTION_H |
| 26 |
|
| 27 |
#include <QObject> |
| 28 |
#include <QVariant> |
| 29 |
#include <functional> |
| 30 |
#include <FCGlobal.h> |
| 31 |
|
| 32 |
class QAction; |
| 33 |
|
| 34 |
namespace Gui |
| 35 |
{ |
| 36 |
|
| 37 |
class ActionFunctionPrivate; |
| 38 |
|
| 39 |
/*! |
| 40 |
The class connects the triggered() signal of a QAction instance with the method |
| 41 |
of a usual C++ class not derived from QObject. |
| 42 |
|
| 43 |
The class can e.g be used to fill up the context-menu of the view provider classes. |
| 44 |
\code |
| 45 |
void MyViewProvider::setupContextMenu(QMenu* menu, QObject* receiver, const char* member) |
| 46 |
{ |
| 47 |
Gui::ActionFunction* func = new Gui::ActionFunction(menu); |
| 48 |
|
| 49 |
QAction* a1 = menu->addAction(QObject::tr("Menu item 1...")); |
| 50 |
func->triggered(a1, std::bind(&MyViewProvider::doItem1, this)); |
| 51 |
|
| 52 |
QAction* a2 = menu->addAction(QObject::tr("Menu item 2...")); |
| 53 |
func->triggered(a2, std::bind(&MyViewProvider::doItem2, this)); |
| 54 |
|
| 55 |
QAction* a3 = menu->addAction(QObject::tr("Menu item 3...")); |
| 56 |
func->triggered(a3, std::bind(&MyViewProvider::doItem3, this)); |
| 57 |
} |
| 58 |
\endcode |
| 59 |
|
| 60 |
@author Werner Mayer |
| 61 |
|
| 62 |
http://www.boost.org/doc/libs/1_57_0/libs/bind/bind.html#with_boost_function |
| 63 |
*/ |
| 64 |
class GuiExport ActionFunction: public QObject |
| 65 |
{ |
| 66 |
Q_OBJECT |
| 67 |
|
| 68 |
public: |
| 69 |
/// Constructor |
| 70 |
explicit ActionFunction(QObject*); |
| 71 |
~ActionFunction() override; |
| 72 |
|
| 73 |
/*! |
| 74 |
Connects the QAction's triggered() signal with the function \a func |
| 75 |
*/ |
| 76 |
void trigger(QAction* a, std::function<void()> func); |
| 77 |
void toggle(QAction* a, std::function<void(bool)> func); |
| 78 |
void hover(QAction* a, std::function<void()> func); |
| 79 |
|
| 80 |
private Q_SLOTS: |
| 81 |
void triggered(); |
| 82 |
void toggled(bool); |
| 83 |
void hovered(); |
| 84 |
|
| 85 |
private: |
| 86 |
QScopedPointer<ActionFunctionPrivate> d_ptr; |
| 87 |
Q_DISABLE_COPY(ActionFunction) |
| 88 |
Q_DECLARE_PRIVATE(ActionFunction) |
| 89 |
}; |
| 90 |
|
| 91 |
class TimerFunctionPrivate; |
| 92 |
|
| 93 |
class GuiExport TimerFunction: public QObject |
| 94 |
{ |
| 95 |
Q_OBJECT |
| 96 |
|
| 97 |
public: |
| 98 |
/// Constructor |
| 99 |
explicit TimerFunction(QObject* = nullptr); |
| 100 |
~TimerFunction() override; |
| 101 |
|
| 102 |
void setFunction(std::function<void()> func); |
| 103 |
void setFunction(std::function<void(QObject*)> func, QObject* args); |
| 104 |
void setFunction(std::function<void(QVariant)> func, QVariant args); |
| 105 |
void setAutoDelete(bool); |
| 106 |
void singleShot(int ms); |
| 107 |
|
| 108 |
private Q_SLOTS: |
| 109 |
void timeout(); |
| 110 |
|
| 111 |
private: |
| 112 |
QScopedPointer<TimerFunctionPrivate> d_ptr; |
| 113 |
Q_DISABLE_COPY(TimerFunction) |
| 114 |
Q_DECLARE_PRIVATE(TimerFunction) |
| 115 |
}; |
| 116 |
|
| 117 |
} // namespace Gui |
| 118 |
|
| 119 |
|
| 120 |
#endif // GUI_ACTIONFUNCTION_H |