File: GeographicViewShowElementInfo.cpp

package info (click to toggle)
tulip 6.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196,224 kB
  • sloc: cpp: 571,851; ansic: 13,983; python: 4,105; sh: 1,555; yacc: 522; xml: 484; makefile: 168; pascal: 148; lex: 55
file content (285 lines) | stat: -rw-r--r-- 11,202 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
 *
 * This file is part of Tulip (https://tulip.labri.fr)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux
 *
 * 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 "GeographicViewShowElementInfo.h"

#include "ui_ElementInformationWidget.h"
#include "GeographicViewInteractors.h"
#include "../../utils/PluginNames.h"

#include <tulip/MouseInteractors.h>
#include <tulip/NodeLinkDiagramComponentInteractor.h>
#include <tulip/TulipItemDelegate.h>
#include <tulip/GraphElementModel.h>
#include <tulip/GlSimpleEntityItemModel.h>
#include <tulip/GlComplexPolygon.h>
#include <tulip/Perspective.h>
#include <tulip/StandardInteractorPriority.h>

#include <QPropertyAnimation>
#include <QLayout>
#include <QStringList>
#include <QVariantList>

using namespace std;
using namespace tlp;

// this class is needed to allow interactive settings
// of some GlComplexPolygon rendering properties
class tlp::GlComplexPolygonItemEditor : public GlSimpleEntityItemEditor {
public:
  GlComplexPolygonItemEditor(GlComplexPolygon *poly) : GlSimpleEntityItemEditor(poly) {}

  // redefined inherited methods from GlSimpleEntityItemEditor
  QStringList propertiesNames() const override {
    return QStringList() << "fillColor"
                         << "outlineColor";
  }

  QVariantList propertiesQVariant() const override {
    return QVariantList() << QVariant::fromValue<Color>(
                                 static_cast<GlComplexPolygon *>(entity)->getFillColor())
                          << QVariant::fromValue<Color>(
                                 static_cast<GlComplexPolygon *>(entity)->getOutlineColor());
  }

  void setProperty(const QString &name, const QVariant &value) override {
    if (name == "fillColor")
      static_cast<GlComplexPolygon *>(entity)->setFillColor(value.value<Color>());
    else if (name == "outlineColor")
      static_cast<GlComplexPolygon *>(entity)->setOutlineColor(value.value<Color>());
  }
};

class GeographicViewInteractorGetInformation : public NodeLinkDiagramComponentInteractor {

public:
  PLUGININFORMATION("GeographicViewInteractorGetInformation", "Tulip Team", "06/2012",
                    "Explore current View", "1.0", "Information")
  /**
   * Default constructor
   */
  GeographicViewInteractorGetInformation(const tlp::PluginContext *)
      : NodeLinkDiagramComponentInteractor(":/tulip/gui/icons/i_explore.png",
                                           "Explore current view",
                                           StandardInteractorPriority::GetInformation) {}

  /**
   * Construct chain of responsibility
   */
  void construct() override {
    setConfigurationWidgetText(
        QString("<h3>Explore graph on the map</h3>") +
        "When the mouse cursor looks like <img src=\":/tulip/gui/icons/i_information.png\">, "
        "indicating it is on top of a graph element (node or edge), "
        "<b>Mouse left</b> click to display a panel showing the element properties.<br/>"
        "As the properties panel is displayed, <b>Mouse left</b> click in a property row to edit the "
        "corresponding value.<br/>"
        "The visible properties can be filtered using the list of properties displayed in the "
        "<b>Options</b> tab.<br/>"
        "If none is filtered, when the element properties panel is displayed, the display of the "
        "visual rendering properties can be then toggled using a dedicated check box.<br/><br/>" +
        "<u>2D Navigation on the map</u><br/><br/>" +
        "Translation: <ul><li><b>Mouse left</b> down + moves</li></ul>" +
#if defined(__APPLE__)
        "Zoom/Unzoom: <ul><li><b>Mouse wheel</b> down/up</li></ul>"
#else
        "Zoom/Unzoom: <ul><li><b>Mouse wheel</b> up/down</li></ul>"
#endif
    );
    push_back(new GeographicViewNavigator);
    push_back(new GeographicViewShowElementInfo);
  }

  bool isCompatible(const string &viewName) const override {
    return (viewName == ViewName::GeographicViewName);
  }
};

PLUGIN(GeographicViewInteractorGetInformation)

GeographicViewShowElementInfo::GeographicViewShowElementInfo()
    : MouseShowElementInfo(false), _editor(nullptr) {}

void GeographicViewShowElementInfo::init() {
  auto gmw =
      static_cast<GeographicView *>(view())->getGeographicViewGraphicsView()->getGeoMapWidget();
  connect(gmw, SIGNAL(mouseMove()), this, SLOT(mouseMove()));
}

void GeographicViewShowElementInfo::clear() {
  auto gmw =
      static_cast<GeographicView *>(view())->getGeographicViewGraphicsView()->getGeoMapWidget();
  connect(gmw, SIGNAL(mouseMove()), this, SLOT(mouseMove()));

  static_cast<GeographicView *>(view())
      ->getGeographicViewGraphicsView()
      ->getGlMainWidget()
      ->setCursor(QCursor());
  _informationWidgetItem->setVisible(false);
}

void GeographicViewShowElementInfo::mouseMove() {
  if (QApplication::mouseButtons() & Qt::LeftButton)
    _informationWidgetItem->setVisible(false);
}

bool GeographicViewShowElementInfo::eventFilter(QObject *widget, QEvent *e) {
  if (widget == _informationWidget &&
      (e->type() == QEvent::Wheel || e->type() == QEvent::MouseButtonPress))
    return true;

  if (_informationWidget->isVisible() && e->type() == QEvent::Wheel) {
    _informationWidgetItem->setVisible(false);
    return false;
  }

  QMouseEvent *qMouseEv = dynamic_cast<QMouseEvent *>(e);

  if (qMouseEv != nullptr) {
    static SelectedEntity selectedEntity;

    GeographicView *geoView = static_cast<GeographicView *>(view());

    if (e->type() == QEvent::MouseMove) {
      if (pick(qMouseEv->pos().x(), qMouseEv->pos().y(), selectedEntity) &&
          (selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED ||
           selectedEntity.getEntityType() == SelectedEntity::EDGE_SELECTED)) {
        geoView->getGeographicViewGraphicsView()->getGlMainWidget()->setCursor(qtWhatsThisCursor);
      } else {
        geoView->getGeographicViewGraphicsView()->getGlMainWidget()->setCursor(QCursor());
      }
    } else if (e->type() == QEvent::MouseButtonPress && qMouseEv->button() == Qt::LeftButton) {
      if (_informationWidgetItem->isVisible()) {
        // Hide widget if we click outside it
        _informationWidgetItem->setVisible(false);
      }
      // Show widget if we click on node or edge
      if (selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED ||
          selectedEntity.getEntityType() == SelectedEntity::EDGE_SELECTED) {
        _informationWidgetItem->setVisible(true);

        QLabel *title = _informationWidget->findChild<QLabel *>();

        if (selectedEntity.getEntityType() == SelectedEntity::NODE_SELECTED) {
          title->setText("Node");
          tableView()->setModel(new GraphNodeElementModel(
              _view->graph(), selectedEntity.getComplexEntityId(), _informationWidget));
        } else {
          title->setText("Edge");
          tableView()->setModel(new GraphEdgeElementModel(
              _view->graph(), selectedEntity.getComplexEntityId(), _informationWidget));
        }

        title->setText(title->text() + " #" + QString::number(selectedEntity.getComplexEntityId()));

        QPoint position = qMouseEv->pos();

        if (position.x() + _informationWidgetItem->rect().width() >
            _view->graphicsView()->sceneRect().width() - 5)
          position.setX(_view->graphicsView()->sceneRect().width() -
                        _informationWidgetItem->rect().width() - 5);

        if (position.y() + _informationWidgetItem->rect().height() >
            _view->graphicsView()->sceneRect().height())
          position.setY(_view->graphicsView()->sceneRect().height() -
                        _informationWidgetItem->rect().height() - 5);

        _informationWidgetItem->setPos(position);
        QPropertyAnimation *animation = new QPropertyAnimation(_informationWidgetItem, "opacity");
        animation->setDuration(100);
        animation->setStartValue(0.);
        animation->setEndValue(1.);
        animation->start();
      } else if (selectedEntity.getEntityType() == SelectedEntity::SIMPLE_ENTITY_SELECTED) {

        GlComplexPolygon *polygon =
            dynamic_cast<GlComplexPolygon *>(selectedEntity.getSimpleEntity());

        if (!polygon)
          return false;

        _informationWidgetItem->setVisible(true);
        QLabel *title = _informationWidget->findChild<QLabel *>();
        title->setText(selectedEntity.getSimpleEntity()
                           ->getParent()
                           ->findKey(selectedEntity.getSimpleEntity())
                           .c_str());

        delete _editor;

        _editor = new GlComplexPolygonItemEditor(polygon);

        tableView()->setModel(new GlSimpleEntityItemModel(_editor, _informationWidget));
        int size = title->height() + _informationWidget->layout()->spacing() +
                   tableView()->rowHeight(0) + tableView()->rowHeight(1) + 10;
        _informationWidget->setMaximumHeight(size);

        QPoint position = qMouseEv->pos();

        if (position.x() + _informationWidgetItem->rect().width() >
            _view->graphicsView()->sceneRect().width())
          position.setX(qMouseEv->pos().x() - _informationWidgetItem->rect().width());

        if (position.y() + _informationWidgetItem->rect().height() >
            _view->graphicsView()->sceneRect().height())
          position.setY(qMouseEv->pos().y() - _informationWidgetItem->rect().height());

        _informationWidgetItem->setPos(position);
        QPropertyAnimation *animation = new QPropertyAnimation(_informationWidgetItem, "opacity");
        animation->setDuration(100);
        animation->setStartValue(0.);
        animation->setEndValue(1.);
        animation->start();
      }
    }
  }

  return false;
}

bool GeographicViewShowElementInfo::pick(int x, int y, SelectedEntity &selectedEntity) {
  GeographicView *geoView = static_cast<GeographicView *>(view());

  if (geoView->getGeographicViewGraphicsView()->getGlMainWidget()->pickNodesEdges(x, y,
                                                                                  selectedEntity))
    return true;

  vector<SelectedEntity> selectedEntities;

  if (geoView->getGeographicViewGraphicsView()->getGlMainWidget()->pickGlEntities(
          x, y, selectedEntities)) {
    selectedEntity = selectedEntities[0];
    return true;
  }

  return false;
}

void GeographicViewShowElementInfo::viewChanged(View *view) {
  if (view == nullptr) {
    _view = nullptr;
    return;
  }

  GeographicView *geoView = static_cast<GeographicView *>(view);
  _view = geoView;
  connect(_view, SIGNAL(graphSet(tlp::Graph *)), _informationWidgetItem, SLOT(close()));
  _view->getGeographicViewGraphicsView()->scene()->addItem(_informationWidgetItem);
}