24 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_RenameStateDialog.h>
27 #include <QFileDialog>
42 sourceGroup(sourceGroup),
43 sourceState(sourceState),
44 validStateNameRegExp(
"[a-zA-Z][a-zA-Z0-9]*"),
45 validInstanceNameRegExp(
"[a-zA-Z][a-zA-Z0-9_]*"),
47 validInstanceNames(true),
48 colorGreen(QColor::fromRgb(120, 255, 120)),
49 colorRed(QColor::fromRgb(255, 120, 120)),
50 colorYellow(QColor::fromRgb(255, 200, 0)),
52 alreadyChecking(false)
55 setOkButtonsEnabled(
false);
56 ui->editOldName->setText(sourceState->getStateName());
57 ui->editOldName->setReadOnly(
true);
60 ui->editNewName->setValidator(
new QRegExpValidator(validStateNameRegExp,
this));
62 connect(ui->btnCancel, SIGNAL(clicked()),
this, SLOT(reject()));
64 connect(ui->btnDontSaveAndProceed, SIGNAL(clicked()),
this, SLOT(accept()));
65 connect(ui->editNewName, SIGNAL(textChanged(QString)),
this, SLOT(
verifyNewName(QString)));
66 connect(ui->statesWidget, SIGNAL(itemChanged(QTableWidgetItem*)),
this, SLOT(
verifyInstanceName(QTableWidgetItem*)));
68 localStates = sourceGroup->getAllStates(
true);
78 if (!node->isState() || !node->getState())
83 auto state = node->getState();
87 if (!instance->getStateClass())
92 if (instance->getStateClass()->getUUID() == sourceState->getUUID())
94 instanceRenameInfos.push_back({group, node, instance->getInstanceName(), instance->getInstanceName()});
102 if (info1.group->getName() == info2.group->getName())
104 if (info1.parentState->getState()->getStateName() == info2.parentState->getState()->getStateName())
106 return info1.fromName.compare(info2.toName);
109 return info1.parentState->getState()->getStateName().compare(info2.parentState->getState()->getStateName());
112 return info1.
group->getName().compare(info2.
group->getName());
115 ui->statesWidget->clear();
116 ui->statesWidget->setRowCount(instanceRenameInfos.size());
117 ui->statesWidget->setColumnCount(3);
118 ui->statesWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
119 ui->statesWidget->setHorizontalHeaderLabels({
"Statechart Group",
"Parent State",
"Instance Name"});
123 for (
const auto& info : instanceRenameInfos)
125 QTableWidgetItem* groupItem =
new QTableWidgetItem(info.group->getName());
126 groupItem->setFlags(groupItem->flags() ^ Qt::ItemIsEditable);
127 ui->statesWidget->setItem(i, 0, groupItem);
129 QTableWidgetItem* stateItem =
new QTableWidgetItem(info.parentState->getState()->getStateName());
130 stateItem->setFlags(stateItem->flags() ^ Qt::ItemIsEditable);
131 ui->statesWidget->setItem(i, 1, stateItem);
133 QTableWidgetItem* newInstanceItem =
new QTableWidgetItem(info.toName);
134 newInstanceItem->setBackgroundColor(colorGreen);
135 ui->statesWidget->setItem(i, 2, newInstanceItem);
141 RenameStateDialog::~RenameStateDialog()
146 void RenameStateDialog::setOkButtonsEnabled(
bool enabled)
148 ui->btnDontSaveAndProceed->setEnabled(
enabled);
149 ui->btnSaveAndProceed->setEnabled(
enabled);
162 QVector<StateRenamer::InstanceRenameInfo> RenameStateDialog::getInstanceRenameInfos()
const
164 return instanceRenameInfos;
167 bool RenameStateDialog::isSaveAllRequested()
const
172 QString RenameStateDialog::getNewStateName()
const
174 return ui->editNewName->text();
177 void RenameStateDialog::saveAllProceedButtonClicked()
183 void RenameStateDialog::verifyInstanceName(QTableWidgetItem* item)
185 const int r = item->row();
186 const int c = item->column();
190 if (
c != 2 || ui->statesWidget->columnCount() < 3 || ui->statesWidget->rowCount() == 0 || alreadyChecking)
195 alreadyChecking =
true;
199 bool hasNameCollisions =
false;
201 for (
int i = 0; i < ui->statesWidget->rowCount(); ++i)
203 const auto& itemCheck = ui->statesWidget->item(i, 2);
212 bool collision =
false;
214 for (
int j = 0; j < ui->statesWidget->rowCount() && !collision; ++j)
216 const auto& itemOther = ui->statesWidget->item(j, 2);
218 if (i == j || !itemOther)
227 ARMARX_INFO_S <<
" checking: " << itemCheck->text() <<
" " << itemOther->text();
228 collision = itemCheck->text() == itemOther->text();
232 hasNameCollisions |= collision;
233 itemCheck->setBackgroundColor(collision ? colorRed : colorGreen);
236 bool validName = validInstanceNameRegExp.exactMatch(item->text());
240 info.
toName = item->text();
243 item->setBackgroundColor(!validName ? colorRed : item->backgroundColor());
244 validInstanceNames = validName && !hasNameCollisions;
245 setOkButtonsEnabled(validInstanceNames && validNewName);
246 alreadyChecking =
false;
249 void RenameStateDialog::verifyNewName(QString newName)
253 for (
const auto&
s : localStates)
255 if (newName ==
s->getStateName())
266 ui->labelNewNameError->setText(
"Name already in use");
267 colorToUse = colorRed;
271 ui->labelNewNameError->setText(
"Valid name");
272 colorToUse = colorGreen;
275 validNewName = (!inUse && newName.size() != 0);
276 setOkButtonsEnabled(validNewName && validInstanceNames);
278 QPalette p(ui->labelNewNameError->palette());
279 p.setColor(ui->labelNewNameError->backgroundRole(), colorToUse);
280 ui->labelNewNameError->setPalette(p);