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
|
/**
*
* 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 "PathFinderComponent.h"
#include <tulip/GlMainWidget.h>
#include <tulip/GlGraphComposite.h>
#include <tulip/GlMainView.h>
#include "highlighters/PathHighlighter.h"
#include "PathFinder.h"
#include <QApplication>
#include <QMouseEvent>
#include <QMessageBox>
using namespace std;
using namespace tlp;
PathFinderComponent::PathFinderComponent(PathFinder *parent)
: lastGraph(nullptr), parent(parent), graphPopable(false) {}
PathFinderComponent::~PathFinderComponent() {
qDeleteAll(highlighters);
}
bool PathFinderComponent::eventFilter(QObject *obj, QEvent *event) {
QMouseEvent *qMouseEv = static_cast<QMouseEvent *>(event);
GlMainWidget *glw = dynamic_cast<GlMainWidget *>(obj);
if (glw == nullptr)
return false;
if (event->type() == QEvent::MouseMove) {
SelectedEntity entity;
if (glw->pickNodesEdges(qMouseEv->pos().x(), qMouseEv->pos().y(), entity) &&
entity.getEntityType() == SelectedEntity::NODE_SELECTED) {
tmp.id = entity.getComplexEntityId();
glw->setCursor(Qt::CrossCursor);
return true;
} else {
tmp = node();
glw->setCursor(Qt::ArrowCursor);
}
} else if (event->type() == QEvent::MouseButtonDblClick && qMouseEv->button() == Qt::LeftButton) {
// double click will deselect all
Observable::holdObservers();
BooleanProperty *selectionProperty =
glw->getScene()->getGlGraphComposite()->getInputData()->getElementSelected();
selectionProperty->setAllNodeValue(false);
selectionProperty->setAllEdgeValue(false);
if (tmp.isValid()) {
// double click on a node
// user is selecting a new path source
src = tmp;
// select it
selectionProperty->setNodeValue(src, true);
} else
src = node();
// invalidate the path target
tgt = node();
Observable::unholdObservers();
return true;
} else if (event->type() == QEvent::MouseButtonPress && qMouseEv->button() == Qt::LeftButton &&
tmp.isValid()) {
if (!src.isValid()) {
// user can select the path source with a simple click
// if no source already exists
Observable::holdObservers();
BooleanProperty *selectionProperty =
glw->getScene()->getGlGraphComposite()->getInputData()->getElementSelected();
selectionProperty->setAllNodeValue(false);
selectionProperty->setAllEdgeValue(false);
// select it
selectionProperty->setNodeValue(src = tmp, true);
Observable::unholdObservers();
} else {
// as the path source already exists
// we assume that the user is selecting a new path target
tgt = tmp;
// but we wait a bit to ensure the current event does not belong to a
// QEvent::MouseButtonDblClick event
obj->startTimer(QApplication::doubleClickInterval() + 5);
}
return true;
} else if (event->type() == QEvent::Timer) {
obj->killTimer(static_cast<QTimerEvent *>(event)->timerId());
// tgt could have been invalidate by a QEvent::MouseButtonDblClick event
if (tgt.isValid()) {
Observable::holdObservers();
BooleanProperty *selectionProperty =
glw->getScene()->getGlGraphComposite()->getInputData()->getElementSelected();
selectionProperty->setAllNodeValue(false);
selectionProperty->setAllEdgeValue(false);
selectPath(glw, glw->getScene()->getGlGraphComposite()->getGraph());
Observable::unholdObservers();
glw->redraw();
}
return true;
}
return false;
}
void PathFinderComponent::selectPath(GlMainWidget *glMainWidget, Graph *graph) {
GlGraphInputData *inputData = glMainWidget->getScene()->getGlGraphComposite()->getInputData();
BooleanProperty *selection = inputData->getElementSelected();
if (src.isValid() && tgt.isValid()) { // We only select a path if source and target are valid
// get active window before launching paths computins
// as a workaround for uncorrected QMessageBox styling
// when paths computing is interrupted on user demand
auto w = QApplication::activeWindow();
Observable::holdObservers();
DoubleProperty *weights = nullptr;
string weightsMetricName = parent->getWeightMetricName();
if (weightsMetricName.compare(NO_METRIC) != 0 && graph->existProperty(weightsMetricName)) {
PropertyInterface *prop = graph->getProperty(weightsMetricName);
if (prop && prop->getTypename().compare("double") == 0)
weights = graph->getProperty<DoubleProperty>(weightsMetricName);
}
bool pathFound =
PathAlgorithm::computePath(graph, parent->getPathsType(), parent->getEdgeOrientation(), src,
tgt, selection, weights, parent->getTolerance());
Observable::unholdObservers();
if (!pathFound) {
selection->setAllNodeValue(false);
selection->setAllEdgeValue(false);
selection->setNodeValue(src, true);
QMessageBox::warning(w, "Path finder", "A path between the selected nodes cannot be found.");
} else {
// register current graph
lastGraph = graph;
// A path has been found: highlight it
runHighlighters(glMainWidget, selection, src, tgt);
}
} else if (src.isValid()) {
selection->setNodeValue(src, true);
}
}
void PathFinderComponent::runHighlighters(GlMainWidget *glMainWidget, BooleanProperty *selection,
node src, node tgt) {
glMainWidget->getScene()->getGlGraphComposite()->getGraph()->push(true);
graphPopable = true;
vector<string> activeHighlighters(parent->getActiveHighlighters());
bool foundEnclosingCircle = false;
for (vector<string>::iterator it = activeHighlighters.begin(); it != activeHighlighters.end();
++it) {
if (*it == "Enclosing circle")
foundEnclosingCircle = true;
PathHighlighter *hler = findHighlighter(*it);
if (hler)
hler->highlight(parent, glMainWidget, selection, src, tgt);
}
if (!foundEnclosingCircle) {
// ensure it is cleared
PathHighlighter *enclosingCircle = findHighlighter("Enclosing circle");
enclosingCircle->clear();
}
}
void PathFinderComponent::clearHighlighters(GlMainWidget *glMainWidget) {
if (graphPopable && glMainWidget->getScene()->getGlGraphComposite()->getGraph()->canPop()) {
glMainWidget->getScene()->getGlGraphComposite()->getGraph()->pop(false);
graphPopable = false;
}
vector<string> activeHighlighters(parent->getHighlighters());
for (vector<string>::iterator it = activeHighlighters.begin(); it != activeHighlighters.end();
++it) {
PathHighlighter *hler = findHighlighter(*it);
if (hler)
hler->clear();
}
}
PathHighlighter *PathFinderComponent::findHighlighter(const string &name) {
for (auto p : highlighters) {
if (p->getName() == name)
return p;
}
return nullptr;
}
void PathFinderComponent::addHighlighter(PathHighlighter *highlighter) {
highlighters.insert(highlighter);
}
QSet<PathHighlighter *> PathFinderComponent::getHighlighters() {
return highlighters;
}
void PathFinderComponent::init() {
// restore enclosing circle if needed
auto graph = view()->graph();
if (lastGraph != graph) {
src = tgt = node();
return;
}
auto hls = parent->getActiveHighlighters();
bool needEnclosingCircle = false;
for (const std::string &hl : hls) {
if ((needEnclosingCircle = (hl == "Enclosing circle")))
break;
}
if (needEnclosingCircle) {
PathHighlighter *enclosingCircle = findHighlighter("Enclosing circle");
if (src.isValid() && tgt.isValid()) {
if (graph->isElement(src) && graph->isElement(tgt)) {
auto glw = static_cast<GlMainView *>(view())->getGlMainWidget();
BooleanProperty *selection =
glw->getScene()->getGlGraphComposite()->getInputData()->getElementSelected();
if (selection->getNodeValue(src) && selection->getNodeValue(tgt))
enclosingCircle->highlight(parent, glw, selection, src, tgt);
}
}
}
}
void PathFinderComponent::clear() {
auto glw = static_cast<GlMainView *>(view())->getGlMainWidget();
glw->setCursor(QCursor());
clearHighlighters(glw);
}
|