From 018854360d17dcfb59607e9aefca269cb83e0028 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 30 Mar 2015 23:06:35 +0200 Subject: [PATCH] + implement mechanism to connect QAction signal with class method --- src/Gui/ActionFunction.cpp | 71 +++++++++++++++++++++++++++++++ src/Gui/ActionFunction.h | 86 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 src/Gui/ActionFunction.cpp create mode 100644 src/Gui/ActionFunction.h diff --git a/src/Gui/ActionFunction.cpp b/src/Gui/ActionFunction.cpp new file mode 100644 index 000000000..543d2f0b1 --- /dev/null +++ b/src/Gui/ActionFunction.cpp @@ -0,0 +1,71 @@ +/*************************************************************************** + * Copyright (c) 2015 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#ifndef _PreComp_ +#endif + +#include "ActionFunction.h" + +using namespace Gui; + + +namespace Gui { +class ActionFunctionPrivate +{ +public: + QMap > actionFuncMap; +}; +} + +ActionFunction::ActionFunction(QObject* parent) + : QObject(parent), d_ptr(new ActionFunctionPrivate()) +{ +} + +ActionFunction::~ActionFunction() +{ +} + +void ActionFunction::mapSignal(QAction* action, boost::function func) +{ + Q_D(ActionFunction); + + d->actionFuncMap[action] = func; + connect(action, SIGNAL(triggered()), this, SLOT(trigger())); +} + +void ActionFunction::trigger() +{ + Q_D(ActionFunction); + + QAction* a = qobject_cast(sender()); + QMap >::iterator it = d->actionFuncMap.find(a); + if (it != d->actionFuncMap.end()) { + // invoke the class function here + it.value()(); + } +} + +#include "moc_ActionFunction.cpp" diff --git a/src/Gui/ActionFunction.h b/src/Gui/ActionFunction.h new file mode 100644 index 000000000..329ce2bf8 --- /dev/null +++ b/src/Gui/ActionFunction.h @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (c) 2015 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef GUI_ACTIONFUNCTION_H +#define GUI_ACTIONFUNCTION_H + +#include +#include + +namespace Gui +{ + +class ActionFunctionPrivate; + +/*! + The class connects the triggered() signal of a QAction instance with the method + of a usual C++ class not derived from QObject. + + The class can e.g be used to fill up the context-menu of the view provider classes. + \code + void MyViewProvider::setupContextMenu(QMenu* menu, QObject* receiver, const char* member) + { + Gui::ActionFunction* func = new Gui::ActionFunction(menu); + + QAction* a1 = menu->addAction(QObject::tr("Menu item 1...")); + func->mapSignal(a1, boost::bind(&MyViewProvider::doItem1, this)); + + QAction* a2 = menu->addAction(QObject::tr("Menu item 2...")); + func->mapSignal(a2, boost::bind(&MyViewProvider::doItem2, this)); + + QAction* a3 = menu->addAction(QObject::tr("Menu item 3...")); + func->mapSignal(a3, boost::bind(&MyViewProvider::doItem3, this)); + } + \endcode + + @author Werner Mayer + + http://www.boost.org/doc/libs/1_57_0/libs/bind/bind.html#with_boost_function +*/ +class GuiExport ActionFunction : public QObject +{ + Q_OBJECT + +public: + /// Constructor + ActionFunction(QObject*); + virtual ~ActionFunction(); + + /*! + Connects the QAction's triggered() signal with the function \a func + */ + void mapSignal(QAction* a, boost::function func); + +private Q_SLOTS: + void trigger(); + +private: + QScopedPointer d_ptr; + Q_DISABLE_COPY(ActionFunction) + Q_DECLARE_PRIVATE(ActionFunction) +}; + +} //namespace Gui + + +#endif // GUI_ACTIONFUNCTION_H