-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodecombobox.cpp
More file actions
191 lines (144 loc) · 4.92 KB
/
nodecombobox.cpp
File metadata and controls
191 lines (144 loc) · 4.92 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "nodecombobox.h"
#include <QMessageBox>
#include <QInputDialog>
QString NodeComboBox::NONE_VALUE = "None";
NodeComboBox::NodeComboBox(const QString& description, QList<QString>* values, QWidget *parent) :
QComboBox(parent)
,m_StringCreate("Create new ")
,m_StringRename("Rename current ")
,m_StringDelete("Delete current ")
,m_StringSeparator("_________________________ ")
{
m_Description = description;
m_Values = values;
m_DefaultValues.append(m_StringSeparator);
m_StringRename = m_StringRename + m_Description + " ";
m_DefaultValues.append(m_StringRename);
m_StringCreate = m_StringCreate + m_Description + " ";
m_DefaultValues.append(m_StringCreate);
m_StringDelete = m_StringDelete + m_Description + " ";
m_DefaultValues.append(m_StringDelete);
this->connect(this, SIGNAL(currentIndexChanged(QString)), this, SLOT(SlotSelectionChanged(QString)) );
this->Update();
}
NodeComboBox::~NodeComboBox()
{
}
void NodeComboBox::Update(const QString &indexAtText)
{
this->disconnect(this, SIGNAL(currentIndexChanged(QString)), this, SLOT(SlotSelectionChanged(QString)) );
this->clear();
this->addItem(NONE_VALUE);
int i=0;
for(i=0; i < m_Values->size(); ++i)
{
this->addItem(m_Values->at(i));
}
for(i=0; i < m_DefaultValues.size(); ++i)
{
this->addItem(m_DefaultValues.at(i));
}
this->SetCurrentIndex(indexAtText);
this->connect(this, SIGNAL(currentIndexChanged(QString)), this, SLOT(SlotSelectionChanged(QString)) );
if(this->IsSelectedEntryNode(this->currentText()))
m_LastSelectedNode = this->currentText();
}
void NodeComboBox::SetCurrentIndex(const QString& text)
{
this->disconnect(this, SIGNAL(currentIndexChanged(QString)), this, SLOT(SlotSelectionChanged(QString)) );
int textIDX = this->findText(text);
if(textIDX != -1)
this->setCurrentIndex(textIDX);
else
this->setCurrentIndex(0);
this->connect(this, SIGNAL(currentIndexChanged(QString)), this, SLOT(SlotSelectionChanged(QString)) );
}
bool NodeComboBox::IsSelectedEntryNode(const QString& name)
{
bool result = true;
if(m_DefaultValues.contains(name))
result = false;
return result;
}
void NodeComboBox::SlotSelectionChanged(const QString& selected)
{
if(selected.compare(m_StringSeparator) == 0)
{
this->SetCurrentIndex(m_LastSelectedNode);
return;
}
if(!m_DefaultValues.contains(selected))
emit SignalSelected(selected);
else
{
if(selected.compare(m_StringCreate) == 0)
{
QString name = QInputDialog::getText(NULL,"Create "+m_Description,"Name:");
name = name.trimmed();
if(this->CheckNameAlreadyExists(name,true))
{
this->SetCurrentIndex(m_LastSelectedNode);
return;
}
if(!name.isEmpty())
emit SignalCreate(name);
else
this->SetCurrentIndex(m_LastSelectedNode);
}
if(m_LastSelectedNode.compare(NONE_VALUE) != 0)
{
this->SetCurrentIndex(m_LastSelectedNode);
if(selected.compare(m_StringRename) == 0)
{
QString rename = QInputDialog::getText(NULL,"Rename "+m_Description,"Name:",QLineEdit::Normal, m_LastSelectedNode);
rename = rename.trimmed();
if(rename.compare(m_LastSelectedNode) != 0)
{
if(this->CheckNameAlreadyExists(rename,true))
return;
if(!rename.isEmpty())
emit SignalRename(m_LastSelectedNode,rename);
}
}
else if(selected.compare(m_StringDelete) == 0)
{
int ret = QMessageBox::question(NULL,"Delete "+m_Description, "Are you sure you want to delete "+m_LastSelectedNode+" ?",QMessageBox::Yes,QMessageBox::Cancel);
if(ret == QMessageBox::Yes)
emit SignalDelete(m_LastSelectedNode);
}
}
else
this->SetCurrentIndex(m_LastSelectedNode);
}
}
void NodeComboBox::HideOperator(Operator op)
{
switch(op)
{
case CREATE:
m_DefaultValues.removeOne(m_StringCreate);
break;
case RENAME:
m_DefaultValues.removeOne(m_StringRename);
break;
case DELETE:
m_DefaultValues.removeOne(m_StringDelete);
break;
case SEPPARATOR:
m_DefaultValues.removeOne(m_StringSeparator);
break;
}
}
bool NodeComboBox::CheckNameAlreadyExists(const QString& name, bool showWarning)
{
for(int i=0; i < this->count(); ++i)
{
if(this->itemText(i).compare(name) == 0)
{
if(showWarning)
QMessageBox::warning(NULL,"Duplicate Name","A "+m_Description+" with this name already exists!");
return true;
}
}
return false;
}