/
nodectrl.cpp
151 lines (137 loc) · 4.42 KB
/
nodectrl.cpp
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
#include "nodectrl.h"
#include "mainctrl.h"
typedef relarank::PlugHandle PlugHandle;
NodeCtrl::NodeCtrl(MainCtrl * manager, relarank::NodeHandle node): QObject(manager), m_manager(manager), m_node(node),
m_plugs(QHash < PlugHandle,
QList < PlugHandle >> ())
{
// connect node signals
connect(&m_node, SIGNAL(removalRequested()), this, SLOT(remove()));
connect(&m_node,
SIGNAL(inputConnected
(relarank::PlugHandle, relarank::PlugHandle)), this,
SLOT(inputConnected
(relarank::PlugHandle, relarank::PlugHandle)));
connect(&m_node,
SIGNAL(outputConnected
(relarank::PlugHandle, relarank::PlugHandle)), this,
SLOT(outputConnected
(relarank::PlugHandle, relarank::PlugHandle)));
connect(&m_node,
SIGNAL(inputDisconnected
(relarank::PlugHandle, relarank::PlugHandle)), this,
SLOT(inputDisconnected
(relarank::PlugHandle, relarank::PlugHandle)));
connect(&m_node,
SIGNAL(outputDisconnected
(relarank::PlugHandle, relarank::PlugHandle)), this,
SLOT(outputDisconnected
(relarank::PlugHandle, relarank::PlugHandle)));
}
void NodeCtrl::rename(const QString & name)
{
m_node.rename(name);
}
QString NodeCtrl::renamePlug(const QString & oldName, const QString & newName)
{
if (newName == oldName) {
return oldName;
}
PlugHandle plug = m_node.getPlug(oldName);
if (!plug.isValid()) {
// return the empty string if there is no plug by the given name
return "";
}
// disconnect all connected plugs
if (plug.isIncoming()) {
for (PlugHandle otherPlug : m_plugs.value(plug)) {
m_manager->getCtrlForHandle(otherPlug.getNode())->
outputDisconnected(otherPlug, plug);
}
} else {
for (PlugHandle otherPlug : m_plugs.value(plug)) {
m_manager->getCtrlForHandle(otherPlug.getNode())->
inputDisconnected(otherPlug, plug);
}
}
// rename the plug
QString actualName = plug.rename(newName);
// reconnect other plugs
if (plug.isIncoming()) {
for (PlugHandle otherPlug : m_plugs.value(plug)) {
m_manager->getCtrlForHandle(otherPlug.getNode())->
outputConnected(otherPlug, plug);
}
} else {
for (PlugHandle otherPlug : m_plugs.value(plug)) {
m_manager->getCtrlForHandle(otherPlug.getNode())->
inputConnected(otherPlug, plug);
}
}
return actualName;
}
bool NodeCtrl::togglePlugDirection(const QString & name)
{
PlugHandle plug = m_node.getPlug(name);
if (!plug.isValid() || !plug.toggleDirection()) {
return false;
}
Q_ASSERT(m_plugs.contains(plug));
Q_ASSERT(m_plugs[plug].size() == 0);
return true;
}
bool NodeCtrl::removePlug(const QString & name)
{
// get the plug that is about to be removed
PlugHandle plug = m_node.getPlug(name);
if (!plug.isValid() || !plug.isRemovable()) {
return false;
}
// remove all references to the plug
Q_ASSERT(m_plugs.contains(plug));
Q_ASSERT(m_plugs[plug].size() == 0);
m_plugs.remove(plug);
// remove the plug
bool result = plug.remove();
Q_ASSERT(result);
return result;
}
void NodeCtrl::setSelected(bool isSelected)
{
m_node.setSelected(isSelected);
}
bool NodeCtrl::remove()
{
return m_manager->deleteNode(this);
}
relarank::PlugHandle NodeCtrl::addPlug(const QString & name, bool incoming)
{
PlugHandle
newPlug;
if (incoming) {
newPlug = m_node.createIncomingPlug(name);
} else {
newPlug = m_node.createOutgoingPlug(name);
}
Q_ASSERT(newPlug.isValid());
m_plugs.insert(newPlug, QList < PlugHandle > ());
return newPlug;
}
void NodeCtrl::inputConnected(PlugHandle myInput, PlugHandle otherOutput)
{
m_plugs[myInput].append(otherOutput);
}
void NodeCtrl::outputConnected(PlugHandle myOutput, PlugHandle otherInput)
{
m_plugs[myOutput].append(otherInput);
}
void NodeCtrl::inputDisconnected(PlugHandle myInput, PlugHandle otherOutput)
{
m_plugs[myInput].removeOne(otherOutput);
Q_ASSERT(m_plugs[myInput].count(otherOutput) == 0);
}
void NodeCtrl::outputDisconnected(PlugHandle myOutput, PlugHandle otherInput)
{
m_plugs[myOutput].removeOne(otherInput);
Q_ASSERT(m_plugs[myOutput].count(otherInput) == 0);
}