GraphConnectionEndpoint GraphDraw::mousedEndpoint(const QPoint &pos) { auto objs = this->items(pos); if (_connectLineItem) objs.removeOne(_connectLineItem.get()); if (objs.empty()) return GraphConnectionEndpoint(); auto topObj = dynamic_cast<GraphObject *>(objs.front()); if (topObj == nullptr) return GraphConnectionEndpoint(); const auto point = topObj->mapFromParent(this->mapToScene(pos)); return GraphConnectionEndpoint(topObj, topObj->isPointingToConnectable(point)); }
void GraphDraw::doClickSelection(const QPointF &point) { const bool ctrlDown = QApplication::keyboardModifiers() & Qt::ControlModifier; const auto objs = this->items(this->mapFromScene(point)); //nothing selected, clear the last selected endpoint if (objs.empty()) _lastClickSelectEp = GraphConnectionEndpoint(); //connection creation logic if (not ctrlDown and not objs.empty()) { auto topObj = dynamic_cast<GraphObject *>(objs.front()); if (topObj == nullptr) return; GraphConnectionEndpoint thisEp(topObj, topObj->isPointingToConnectable(topObj->mapFromParent(point))); //valid keys, attempt to make a connection QPointer<GraphConnection> conn; if (thisEp.isValid() and _lastClickSelectEp.isValid() and not (thisEp == _lastClickSelectEp) and //end points valid (_lastClickSelectEp.getConnectableAttrs().direction == GRAPH_CONN_OUTPUT or _lastClickSelectEp.getConnectableAttrs().direction == GRAPH_CONN_SIGNAL) and //last endpoint is output (thisEp.getConnectableAttrs().direction == GRAPH_CONN_INPUT or thisEp.getConnectableAttrs().direction == GRAPH_CONN_SLOT)) //this click endpoint is input { try { conn = this->getGraphEditor()->makeConnection(thisEp, _lastClickSelectEp); this->getGraphEditor()->handleStateChange(GraphState("connect-arrow", tr("Connect %1[%2] to %3[%4]").arg( conn->getOutputEndpoint().getObj()->getId(), conn->getOutputEndpoint().getKey().id, conn->getInputEndpoint().getObj()->getId(), conn->getInputEndpoint().getKey().id ))); } catch (const Pothos::Exception &ex) { poco_warning(Poco::Logger::get("PothosGui.GraphDraw.connect"), Poco::format("Cannot connect port %s[%s] to port %s[%s]: %s", _lastClickSelectEp.getObj()->getId().toStdString(), _lastClickSelectEp.getKey().id.toStdString(), thisEp.getObj()->getId().toStdString(), thisEp.getKey().id.toStdString(), ex.message())); } } //cleanup after new connection if (not conn.isNull()) { _lastClickSelectEp = GraphConnectionEndpoint(); this->deselectAllObjs(); } //otherwise save the click select else { _lastClickSelectEp = thisEp; } } }