forked from clemenssielaff/ZodiacGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeproperties.cpp
169 lines (141 loc) · 5.28 KB
/
nodeproperties.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "nodeproperties.h"
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include "collapsible.h"
#include "nodectrl.h"
QString NodeProperties::s_defaultPlugName = "plug";
NodeProperties::NodeProperties(NodeCtrl *node, Collapsible *parent)
: QWidget(parent)
, m_node(node)
, m_nextPlugIsIncoming(true)
{
// define the main layout
QVBoxLayout* mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(2,2,2,2);
mainLayout->setSpacing(2);
setLayout(mainLayout);
// update the title of the collapsible container
parent->updateTitle(m_node->getName());
// define the name edit
QHBoxLayout* nameLayout = new QHBoxLayout();
m_nameEdit = new QLineEdit(m_node->getName(), this);
connect(m_nameEdit, SIGNAL(editingFinished()), this, SLOT(renameNode()));
nameLayout->addWidget(new QLabel("Name", this));
nameLayout->addWidget(m_nameEdit);
nameLayout->setContentsMargins(0, 4, 0, 0);
mainLayout->addLayout(nameLayout);
// define the add plug button
m_plugLayout = new QGridLayout();
m_plugLayout->setContentsMargins(0, 8, 0, 0); // leave space between the plug list and the name
m_plugLayout->setColumnStretch(1,1); // so the add-plug button always stays on the far right
m_addPlugButton = new QPushButton(this);
m_addPlugButton->setIconSize(QSize(8, 8));
m_addPlugButton->setIcon(QIcon(":/icons/plus.svg"));
m_addPlugButton->setFlat(true);
m_plugLayout->addWidget(new QLabel("Plugs", this), 0, 0, 1, 2, Qt::AlignLeft);
m_plugLayout->addWidget(m_addPlugButton, 0, 2);
connect(m_addPlugButton, SIGNAL(pressed()), this, SLOT(createNewPlug()));
// define the plugs
for(zodiac::PlugHandle& plug : m_node->getPlugHandles()){
addPlugRow(plug);
}
mainLayout->addLayout(m_plugLayout);
}
void NodeProperties::renameNode()
{
QString newName = m_nameEdit->text();
if(m_node->getName() == newName){
return;
}
m_node->rename(newName);
qobject_cast<Collapsible*>(parent())->updateTitle(newName);
}
void NodeProperties::createNewPlug()
{
// duplicate plug names are automatically resolved by the zodiac::Node
if(m_nextPlugIsIncoming){
addPlugRow(m_node->addIncomingPlug(s_defaultPlugName));
} else {
addPlugRow(m_node->addOutgoingPlug(s_defaultPlugName));
}
m_nextPlugIsIncoming = !m_nextPlugIsIncoming;
}
void NodeProperties::addPlugRow(zodiac::PlugHandle plug)
{
int row = m_plugLayout->rowCount();
QPushButton* directionButton = new QPushButton(this);
directionButton->setIconSize(QSize(16, 16));
directionButton->setFlat(true);
directionButton->setStatusTip("Toggle the direction of the Plug from 'incoming' to 'outoing' and vice versa.");
m_plugLayout->addWidget(directionButton, row, 0);
QLineEdit* plugNameEdit = new QLineEdit(plug.getName(), this);
m_plugLayout->addWidget(plugNameEdit, row, 1);
QPushButton* removalButton = new QPushButton(this);
removalButton->setIcon(QIcon(":/icons/minus.svg"));
removalButton->setIconSize(QSize(8, 8));
removalButton->setFlat(true);
removalButton->setStatusTip("Delete the Plug from its Node");
m_plugLayout->addWidget(removalButton, row, 2);
m_plugRows.insert(plug.getName(), new PlugRow(this, plug, plugNameEdit, directionButton, removalButton));
}
void NodeProperties::removePlugRow(const QString& plugName)
{
Q_ASSERT(m_plugRows.contains(plugName));
m_plugRows.remove(plugName);
}
PlugRow::PlugRow(NodeProperties* editor, zodiac::PlugHandle plug,
QLineEdit* nameEdit, QPushButton* directionToggle, QPushButton* removalButton)
: QObject(editor)
, m_editor(editor)
, m_plug(plug)
, m_nameEdit(nameEdit)
, m_directionToggle(directionToggle)
, m_removalButton(removalButton)
{
connect(m_nameEdit, SIGNAL(editingFinished()), this, SLOT(renamePlug()));
connect(m_directionToggle, SIGNAL(clicked()), this, SLOT(togglePlugDirection()));
connect(m_removalButton, SIGNAL(clicked()), this, SLOT(removePlug()));
updateDirectionIcon();
}
void PlugRow::renamePlug()
{
m_nameEdit->setText(m_editor->getNode()->renamePlug(m_plug.getName(), m_nameEdit->text()));
}
void PlugRow::updateDirectionIcon()
{
if(m_plug.isIncoming()){
m_directionToggle->setIcon(QIcon(":/icons/incoming.svg"));
} else {
m_directionToggle->setIcon(QIcon(":/icons/outgoing.svg"));
}
}
void PlugRow::togglePlugDirection()
{
if(!m_editor->getNode()->togglePlugDirection(m_plug.getName())){
return;
}
updateDirectionIcon();
}
void PlugRow::removePlug()
{
// do nothing, if the plug cannot be removed
if(!m_plug.isRemovable()){
return;
}
// unregister from the editor
m_editor->removePlugRow(m_plug.getName());
// remove widgets from the editor
QGridLayout* plugLayout = m_editor->getPlugLayout();
plugLayout->removeWidget(m_directionToggle);
plugLayout->removeWidget(m_nameEdit);
plugLayout->removeWidget(m_removalButton);
// delete the widgets, they are no longer needed
m_directionToggle->deleteLater();
m_nameEdit->deleteLater();
m_removalButton->deleteLater();
// finally, remove the plug from the logical node
m_editor->getNode()->removePlug(m_plug.getName());
}