This example gives a minimalistic approach of interaction with a graphIn this example, we are making an interactor plugin for Node link diagram view that will activate zoom and pan and display informations when you click on a node/edge.
The code contains a step-by-step explanation of the process, please refer to classes documentation for more informations about their usage.
#include <QtCore/QPropertyAnimation>
#include <QtGui/QGraphicsView>
#include <QtGui/QLabel>
#include <QtGui/QGraphicsProxyWidget>
#include <tulip/MouseInteractors.h>
#include <tulip/Interactor.h>
#include <tulip/GlMainView.h>
#include <tulip/GlMainWidget.h>
#include <tulip/GlScene.h>
using namespace tlp;
class InteractorPluginComponent : public InteractorComponent {
QGraphicsProxyWidget *_informationWidgetItem;
QLabel *_informationLabel;
public :
InteractorPluginComponent() {
_informationLabel=new QLabel();
_informationWidgetItem=new QGraphicsProxyWidget();
_informationWidgetItem->setWidget(_informationLabel);
_informationWidgetItem->setVisible(false);
}
void viewChanged(
View * view) {
view->
graphicsView()->scene()->addItem(_informationWidgetItem);
}
bool eventFilter(QObject *, QEvent* e) {
if(_informationWidgetItem->isVisible() && e->type()==QEvent::Wheel) {
_informationWidgetItem->setVisible(false);
return false;
}
QMouseEvent * qMouseEv = dynamic_cast<QMouseEvent *>(e);
if(qMouseEv != NULL) {
if (e->type() == QEvent::MouseButtonPress && qMouseEv->button() == Qt::LeftButton) {
if(_informationWidgetItem->isVisible()) {
_informationWidgetItem->setVisible(false);
}
else {
if (glMainView->
getGlMainWidget()->
pickNodesEdges(qMouseEv->x(),qMouseEv->y(),selectedEntity)) {
QString text("Click on ");
if(selectedEntity.
getEntityType() == SelectedEntity::NODE_SELECTED)
text+="node ";
else
text+="edge ";
text+=
" id : "+QString::number(selectedEntity.
getComplexEntityId());
_informationLabel->setText(text);
_informationLabel->adjustSize();
_informationWidgetItem->setPos(qMouseEv->pos());
_informationWidgetItem->setVisible(true);
QPropertyAnimation *animation = new QPropertyAnimation(_informationWidgetItem, "opacity");
animation->setDuration(100);
animation->setStartValue(0.);
animation->setEndValue(0.99);
animation->start();
return true;
}
}
}
}
return false;
}
};
QLabel *_configurationLabel;
public:
PLUGININFORMATIONS(
"InteractorPlugin",
"Tulip Team",
"05/10/2012",
"Demo Interactor",
"1.0",
"Information")
_configurationLabel=new QLabel(QString("This is a demo interactor.<br>")
+"You can zoom and pan and display informations if you click on a node/edge.");
}
~InteractorPlugin() {
delete _configurationLabel;
}
void construct() {
push_back(new MouseNKeysNavigator);
push_back(new InteractorPluginComponent);
}
bool isCompatible(const std::string &viewName) {
if(viewName=="Node Link Diagram view")
return true;
return false;
}
QWidget* configurationWidget() const {
return _configurationLabel;
}
unsigned int priority() const {
return 5;
}
};