Пример #1
0
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;
        }
    }
}
Пример #2
0
bool GraphDraw::tryToMakeConnection(const GraphConnectionEndpoint &thisEp)
{
    QPointer<GraphConnection> conn;
    const auto &lastEp = _lastClickSelectEp;

    //valid keys, attempt to make a connection when the endpoints differ in direction
    if (thisEp.isValid() and lastEp.isValid() and //both are valid
        lastEp.getKey().isInput() != thisEp.getKey().isInput()) //directions differ
    {
        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)
        {
            static auto &logger = Poco::Logger::get("PothosFlow.GraphDraw");
            logger.warning("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 regardless of failure
        _lastClickSelectEp = GraphConnectionEndpoint();
        this->deselectAllObjs();
    }

    //if this is a signal or slot connection
    //open the properties panel for configuration
    if (conn and conn->isSignalOrSlot())
    {
        emit this->modifyProperties(conn);
    }

    return bool(conn);
}