27 #include <QFileDialog>
35 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_RenameStateDialog.h>
46 sourceGroup(sourceGroup),
47 sourceState(sourceState),
48 validStateNameRegExp(
"[a-zA-Z][a-zA-Z0-9]*"),
49 validInstanceNameRegExp(
"[a-zA-Z][a-zA-Z0-9_]*"),
51 validInstanceNames(true),
52 colorGreen(QColor::fromRgb(120, 255, 120)),
53 colorRed(QColor::fromRgb(255, 120, 120)),
54 colorYellow(QColor::fromRgb(255, 200, 0)),
56 alreadyChecking(false)
59 setOkButtonsEnabled(
false);
60 ui->editOldName->setText(sourceState->getStateName());
61 ui->editOldName->setReadOnly(
true);
64 ui->editNewName->setValidator(
new QRegExpValidator(validStateNameRegExp,
this));
66 connect(ui->btnCancel, SIGNAL(clicked()),
this, SLOT(reject()));
69 connect(ui->btnDontSaveAndProceed, SIGNAL(clicked()),
this, SLOT(accept()));
70 connect(ui->editNewName, SIGNAL(textChanged(QString)),
this, SLOT(
verifyNewName(QString)));
71 connect(ui->statesWidget,
72 SIGNAL(itemChanged(QTableWidgetItem*)),
76 localStates = sourceGroup->getAllStates(
true);
86 if (!node->isState() || !node->getState())
91 auto state = node->getState();
95 if (!instance->getStateClass())
100 if (instance->getStateClass()->getUUID() == sourceState->getUUID())
102 instanceRenameInfos.push_back({group,
104 instance->getInstanceName(),
105 instance->getInstanceName()});
111 std::sort(instanceRenameInfos.begin(),
112 instanceRenameInfos.end(),
116 if (info1.group->getName() == info2.group->getName())
118 if (info1.parentState->getState()->getStateName() ==
119 info2.parentState->getState()->getStateName())
121 return info1.fromName.compare(info2.toName);
124 return info1.parentState->getState()->getStateName().compare(
125 info2.parentState->getState()->getStateName());
128 return info1.
group->getName().compare(info2.
group->getName());
131 ui->statesWidget->clear();
132 ui->statesWidget->setRowCount(instanceRenameInfos.size());
133 ui->statesWidget->setColumnCount(3);
134 ui->statesWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
135 ui->statesWidget->setHorizontalHeaderLabels(
136 {
"Statechart Group",
"Parent State",
"Instance Name"});
140 for (
const auto& info : instanceRenameInfos)
142 QTableWidgetItem* groupItem =
new QTableWidgetItem(info.group->getName());
143 groupItem->setFlags(groupItem->flags() ^ Qt::ItemIsEditable);
144 ui->statesWidget->setItem(i, 0, groupItem);
146 QTableWidgetItem* stateItem =
147 new QTableWidgetItem(info.parentState->getState()->getStateName());
148 stateItem->setFlags(stateItem->flags() ^ Qt::ItemIsEditable);
149 ui->statesWidget->setItem(i, 1, stateItem);
151 QTableWidgetItem* newInstanceItem =
new QTableWidgetItem(info.toName);
152 newInstanceItem->setBackgroundColor(colorGreen);
153 ui->statesWidget->setItem(i, 2, newInstanceItem);
159 RenameStateDialog::~RenameStateDialog()
165 RenameStateDialog::setOkButtonsEnabled(
bool enabled)
167 ui->btnDontSaveAndProceed->setEnabled(
enabled);
168 ui->btnSaveAndProceed->setEnabled(
enabled);
172 RenameStateDialog::getGroup()
const
178 RenameStateDialog::getState()
const
183 QVector<StateRenamer::InstanceRenameInfo>
184 RenameStateDialog::getInstanceRenameInfos()
const
186 return instanceRenameInfos;
190 RenameStateDialog::isSaveAllRequested()
const
196 RenameStateDialog::getNewStateName()
const
198 return ui->editNewName->text();
202 RenameStateDialog::saveAllProceedButtonClicked()
209 RenameStateDialog::verifyInstanceName(QTableWidgetItem* item)
211 const int r = item->row();
212 const int c = item->column();
216 if (
c != 2 || ui->statesWidget->columnCount() < 3 || ui->statesWidget->rowCount() == 0 ||
222 alreadyChecking =
true;
226 bool hasNameCollisions =
false;
228 for (
int i = 0; i < ui->statesWidget->rowCount(); ++i)
230 const auto& itemCheck = ui->statesWidget->item(i, 2);
239 bool collision =
false;
241 for (
int j = 0; j < ui->statesWidget->rowCount() && !collision; ++j)
243 const auto& itemOther = ui->statesWidget->item(j, 2);
245 if (i == j || !itemOther)
252 if (infoCheck.
parentState->getState()->getUUID() ==
255 ARMARX_INFO_S <<
" checking: " << itemCheck->text() <<
" " << itemOther->text();
256 collision = itemCheck->text() == itemOther->text();
260 hasNameCollisions |= collision;
261 itemCheck->setBackgroundColor(collision ? colorRed : colorGreen);
264 bool validName = validInstanceNameRegExp.exactMatch(item->text());
268 info.
toName = item->text();
271 item->setBackgroundColor(!validName ? colorRed : item->backgroundColor());
272 validInstanceNames = validName && !hasNameCollisions;
273 setOkButtonsEnabled(validInstanceNames && validNewName);
274 alreadyChecking =
false;
278 RenameStateDialog::verifyNewName(QString newName)
282 for (
const auto&
s : localStates)
284 if (newName ==
s->getStateName())
295 ui->labelNewNameError->setText(
"Name already in use");
296 colorToUse = colorRed;
300 ui->labelNewNameError->setText(
"Valid name");
301 colorToUse = colorGreen;
304 validNewName = (!inUse && newName.size() != 0);
305 setOkButtonsEnabled(validNewName && validInstanceNames);
307 QPalette p(ui->labelNewNameError->palette());
308 p.setColor(ui->labelNewNameError->backgroundRole(), colorToUse);
309 ui->labelNewNameError->setPalette(p);