File: InteractorPluginExample.cpp

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (109 lines) | stat: -rwxr-xr-x 4,377 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip 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 General Public License for more details.
 *
 */
#include "InteractorPluginExample.h"

#include <QtGui/QMouseEvent>

#include <tulip/MouseInteractors.h>
#include <tulip/GlMainWidget.h>

using namespace std;
using namespace tlp;

// INTERACTORPLUGIN(ClassName, "InteractorName", "Authors", "date", "Long interactor plugin name", "plugin_release");
INTERACTORPLUGIN(InteractorPluginExample, "InteractorPluginExample", "Tulip developers", "30/11/2009", "Interactor Plugin Example", "0.1");

// InteractorChainOfResponsibility parameters :
//  - first : icon to use for this interactor (in this case we use Qt ressources system : see InteractorPluginRessources.qrc)
//  - second : name of the interactor (is visible in the tooltip of the interactor)
InteractorPluginExample::InteractorPluginExample():InteractorChainOfResponsibility(":/i_iconexample.png","Select one node/edge") {

  // Set the priority of the interactor
  //   - priority is used to sort interactors icons on tool bar
  //     for example an interactor with 5 (or more) in priority appear at left side of the toolbar
  //     an interactor with 0 appear at right side
  setPriority(0);

  // Set the text appear on the configuration widget of this interactor
  setConfigurationWidgetText(QString("<h3>Example Interactor Plugin</h3>")+
                             "<b>Mouse left</b> click on an element to select it");
}

// Contruct the interactor
void InteractorPluginExample::construct() {
  // In this case we put two interactor component on this interactor
  //   - If InteractorPluginComponent don't catch the event it will be passed to the MouseNKeysNavigator
  pushInteractorComponent(new MouseNKeysNavigator);
  pushInteractorComponent(new InteractorPluginComponent);
}

// Event filter of the interactor component (main function of the interactor component)
bool InteractorPluginComponent::eventFilter(QObject *widget, QEvent *e) {

  if (e->type() == QEvent::MouseButtonRelease) {
    QMouseEvent * qMouseEv = (QMouseEvent *) e;
    GlMainWidget *glMainWidget = (GlMainWidget *) widget;

    if (qMouseEv->button()== Qt::LeftButton) {
      // Enter here if we have released the left button of the mouse

      // doSelect function return node/edge under the mouse
      node tmpNode;
      edge tmpEdge;
      ElementType type;
      bool result = glMainWidget->doSelect(qMouseEv->x(), qMouseEv->y(), type, tmpNode, tmpEdge);

      if (result) {
        // Enter here if we have node/edge under the mouse

        // Store selection property
        Graph *graph=glMainWidget->getScene()->getGlGraphComposite()->getInputData()->getGraph();
        string selectionPropertyName=glMainWidget->getScene()->getGlGraphComposite()->getInputData()->getElementSelectedPropName();
        BooleanProperty* selection=graph->getProperty<BooleanProperty>(selectionPropertyName);

        // Before do any think on the graph, we push the current state of the graph (this activate the undo/redo system)
        graph->push();

        // Deselect all nodes/edges
        selection->setAllNodeValue(false);
        selection->setAllEdgeValue(false);

        switch(type) {
        case NODE:
          // Set selection at true for selected node
          selection->setNodeValue(tmpNode, true);
          break;

        case EDGE:
          // Set selection at false for selected edge
          selection->setEdgeValue(tmpEdge, true);
          break;
        }

        // We have treated the event so we return true
        // (this event will not be passed to others interactorComponent)
        return true;
      }
    }
  }

  // We don't have treated the event so we return false
  // (this event will be passed to others interactorComponent)
  return false;
}