File: NodeEditor.cpp

package info (click to toggle)
bornagain 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,912 kB
  • sloc: cpp: 518,586; python: 42,512; xml: 805; awk: 630; sh: 470; ansic: 37; makefile: 25
file content (119 lines) | stat: -rw-r--r-- 4,077 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/coregui/Views/SampleDesigner/NodeEditor.cpp
//! @brief     Implements class NodeEditor
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/coregui/Views/SampleDesigner/NodeEditor.h"
#include "GUI/coregui/Views/SampleDesigner/DesignerView.h"
#include "GUI/coregui/Views/SampleDesigner/NodeEditorConnection.h"
#include "GUI/coregui/Views/SampleDesigner/NodeEditorPort.h"
#include <QEvent>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>

NodeEditor::NodeEditor(QObject* parent) : QObject(parent), m_scene(0), m_conn(0) {}

void NodeEditor::install(QGraphicsScene* scene)
{
    scene->installEventFilter(this);
    m_scene = scene;
}

QGraphicsItem* NodeEditor::itemAt(const QPointF& pos)
{
    QList<QGraphicsItem*> items = m_scene->items(QRectF(pos - QPointF(1, 1), QSize(3, 3)));

    for (QGraphicsItem* item : items)
        if (item->type() > QGraphicsItem::UserType)
            return item;

    return nullptr;
}

bool NodeEditor::eventFilter(QObject* object, QEvent* event)
{
    QGraphicsSceneMouseEvent* mouseEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(event);
    if (!mouseEvent)
        return QObject::eventFilter(object, event);

    bool isProcessedEvent(false);

    if (event->type() == QEvent::GraphicsSceneMousePress) {
        isProcessedEvent = processMousePress(mouseEvent);
    } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
        isProcessedEvent = processMouseMove(mouseEvent);
    } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
        isProcessedEvent = processMouseRelease(mouseEvent);
    }
    return isProcessedEvent ? isProcessedEvent : QObject::eventFilter(object, event);
}

bool NodeEditor::processMousePress(QGraphicsSceneMouseEvent* event)
{
    bool result(false);

    if (m_conn == 0 && event->button() == Qt::LeftButton) {
        QGraphicsItem* item = itemAt(event->scenePos());
        if (item && item->type() == ViewTypes::NODE_EDITOR_PORT) {
            emit selectionModeChangeRequest(DesignerView::SIMPLE_SELECTION);
            m_conn = new NodeEditorConnection(0, m_scene);
            m_conn->setPort1((NodeEditorPort*)item);
            m_conn->setPos1(item->scenePos());
            m_conn->setPos2(event->scenePos());
            m_conn->updatePath();

            result = true;
        }
    }
    return result;
}

bool NodeEditor::processMouseMove(QGraphicsSceneMouseEvent* event)
{
    bool result(false);

    if (m_conn) {
        m_conn->setPos2(event->scenePos());
        m_conn->updatePath();
        result = true;
    }
    return result;
}

bool NodeEditor::processMouseRelease(QGraphicsSceneMouseEvent* event)
{
    bool result(false);

    if (m_conn && event->button() == Qt::LeftButton) {
        emit selectionModeChangeRequest(DesignerView::RUBBER_SELECTION);

        QGraphicsItem* item = itemAt(event->scenePos());
        if (item && item->type() == ViewTypes::NODE_EDITOR_PORT) {
            NodeEditorPort* port1 = m_conn->port1();
            NodeEditorPort* port2 = (NodeEditorPort*)item;

            if (port1->parentItem() != port2->parentItem() && port1->isOutput() != port2->isOutput()
                && !port1->isConnected(port2) && port1->getPortType() == port2->getPortType()) {
                m_conn->setPos2(port2->scenePos());
                m_conn->setPort2(port2);
                m_conn->updatePath();
                emit connectionIsEstablished(m_conn);
                m_conn = 0;
                return true;
            }
        }
        delete m_conn;
        m_conn = 0;
        result = true;
    }
    return result;
}