Skip to content

Commit

Permalink
+ extend ActionFunction and use in mesh view provider
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Mar 30, 2015
1 parent bbb3e3f commit 9ce46db
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
56 changes: 49 additions & 7 deletions src/Gui/ActionFunction.cpp
Expand Up @@ -37,7 +37,9 @@ namespace Gui {
class ActionFunctionPrivate
{
public:
QMap<QAction*, boost::function<void()> > actionFuncMap;
QMap<QAction*, boost::function<void()> > triggerMap;
QMap<QAction*, boost::function<void(bool)> > toggleMap;
QMap<QAction*, boost::function<void()> > hoverMap;
};
}

Expand All @@ -50,21 +52,61 @@ ActionFunction::~ActionFunction()
{
}

void ActionFunction::mapSignal(QAction* action, boost::function<void()> func)
void ActionFunction::trigger(QAction* action, boost::function<void()> func)
{
Q_D(ActionFunction);

d->actionFuncMap[action] = func;
connect(action, SIGNAL(triggered()), this, SLOT(trigger()));
d->triggerMap[action] = func;
connect(action, SIGNAL(triggered()), this, SLOT(triggered()));
}

void ActionFunction::trigger()
void ActionFunction::triggered()
{
Q_D(ActionFunction);

QAction* a = qobject_cast<QAction*>(sender());
QMap<QAction*, boost::function<void()> >::iterator it = d->actionFuncMap.find(a);
if (it != d->actionFuncMap.end()) {
QMap<QAction*, boost::function<void()> >::iterator it = d->triggerMap.find(a);
if (it != d->triggerMap.end()) {
// invoke the class function here
it.value()();
}
}

void ActionFunction::toggle(QAction* action, boost::function<void(bool)> func)
{
Q_D(ActionFunction);

d->toggleMap[action] = func;
connect(action, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
}

void ActionFunction::toggled(bool on)
{
Q_D(ActionFunction);

QAction* a = qobject_cast<QAction*>(sender());
QMap<QAction*, boost::function<void(bool)> >::iterator it = d->toggleMap.find(a);
if (it != d->toggleMap.end()) {
// invoke the class function here
it.value()(on);
}
}

void ActionFunction::hover(QAction* action, boost::function<void()> func)
{
Q_D(ActionFunction);

d->hoverMap[action] = func;
connect(action, SIGNAL(hovered()), this, SLOT(hovered()));
}

void ActionFunction::hovered()
{
Q_D(ActionFunction);

QAction* a = qobject_cast<QAction*>(sender());
QMap<QAction*, boost::function<void()> >::iterator it = d->hoverMap.find(a);
if (it != d->hoverMap.end()) {
// invoke the class function here
it.value()();
}
Expand Down
14 changes: 9 additions & 5 deletions src/Gui/ActionFunction.h
Expand Up @@ -45,13 +45,13 @@ class ActionFunctionPrivate;
Gui::ActionFunction* func = new Gui::ActionFunction(menu);
QAction* a1 = menu->addAction(QObject::tr("Menu item 1..."));
func->mapSignal(a1, boost::bind(&MyViewProvider::doItem1, this));
func->triggered(a1, boost::bind(&MyViewProvider::doItem1, this));
QAction* a2 = menu->addAction(QObject::tr("Menu item 2..."));
func->mapSignal(a2, boost::bind(&MyViewProvider::doItem2, this));
func->triggered(a2, boost::bind(&MyViewProvider::doItem2, this));
QAction* a3 = menu->addAction(QObject::tr("Menu item 3..."));
func->mapSignal(a3, boost::bind(&MyViewProvider::doItem3, this));
func->triggered(a3, boost::bind(&MyViewProvider::doItem3, this));
}
\endcode
Expand All @@ -71,10 +71,14 @@ class GuiExport ActionFunction : public QObject
/*!
Connects the QAction's triggered() signal with the function \a func
*/
void mapSignal(QAction* a, boost::function<void()> func);
void trigger(QAction* a, boost::function<void()> func);
void toggle(QAction* a, boost::function<void(bool)> func);
void hover(QAction* a, boost::function<void()> func);

private Q_SLOTS:
void trigger();
void triggered();
void toggled(bool);
void hovered();

private:
QScopedPointer<ActionFunctionPrivate> d_ptr;
Expand Down
21 changes: 19 additions & 2 deletions src/Mod/Mesh/Gui/ViewProvider.cpp
Expand Up @@ -79,6 +79,7 @@
#include <Gui/WaitCursor.h>
#include <Gui/View3DInventor.h>
#include <Gui/View3DInventorViewer.h>
#include <Gui/ActionFunction.h>

#include <Mod/Mesh/App/Core/Algorithm.h>
#include <Mod/Mesh/App/Core/Evaluation.h>
Expand All @@ -92,6 +93,7 @@
#include <Mod/Mesh/App/Mesh.h>
#include <Mod/Mesh/App/MeshFeature.h>
#include <zipios++/gzipoutputstream.h>
#include <boost/bind.hpp>

#include "ViewProvider.h"
#include "SoFCIndexedFaceSet.h"
Expand Down Expand Up @@ -577,8 +579,13 @@ bool ViewProviderMesh::exportToVrml(const char* filename, const MeshCore::Materi
void ViewProviderMesh::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
{
ViewProviderGeometryObject::setupContextMenu(menu, receiver, member);
QAction* act = menu->addAction(QObject::tr("Display components"), receiver, member);
act->setData(QVariant((int)ViewProvider::Color));

// toggle command to display components
Gui::ActionFunction* func = new Gui::ActionFunction(menu);
QAction* act = menu->addAction(QObject::tr("Display components"));
act->setCheckable(true);
act->setChecked(pcMatBinding->value.getValue() == SoMaterialBinding::PER_FACE);
func->toggle(act, boost::bind(&ViewProviderMesh::setHighlightedComponents, this, _1));
}

bool ViewProviderMesh::setEdit(int ModNum)
Expand Down Expand Up @@ -1680,6 +1687,16 @@ void ViewProviderMesh::unhighlightSelection()
pcShapeMaterial->diffuseColor.setValue(c.r,c.g,c.b);
}

void ViewProviderMesh::setHighlightedComponents(bool on)
{
if (on) {
highlightComponents();
}
else {
unhighlightSelection();
}
}

void ViewProviderMesh::highlightComponents()
{
const Mesh::MeshObject& rMesh = static_cast<Mesh::Feature*>(pcObject)->Mesh.getValue();
Expand Down
1 change: 1 addition & 0 deletions src/Mod/Mesh/Gui/ViewProvider.h
Expand Up @@ -171,6 +171,7 @@ class MeshGuiExport ViewProviderMesh : public Gui::ViewProviderGeometryObject
void highlightSelection();
void unhighlightSelection();
void highlightComponents();
void setHighlightedComponents(bool);

virtual SoShape* getShapeNode() const;
virtual SoNode* getCoordNode() const;
Expand Down

0 comments on commit 9ce46db

Please sign in to comment.