RenameGroupDialog.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Valerij Wittenbeck (valerij.wittenbeck at student dot kit dot edu
18 * @date 2015
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "RenameGroupDialog.h"
24 #include <ArmarXGui/gui-plugins/StatechartEditorPlugin/view/dialogs/ui_RenameGroupDialog.h>
25 
27 #include <QFileDialog>
28 #include <QTimer>
29 
32 
33 #include <QtGui>
34 
35 namespace armarx
36 {
37 
38  RenameGroupDialog::RenameGroupDialog(const StateTreeNodePtr& rootNode, const StateTreeModelPtr& treeModel, const StatechartGroupPtr& group, const GroupRenamerPtr& groupRenamer, QWidget* parent) :
39  QDialog(parent),
40  ui(new Ui::RenameGroupDialog),
41  group(group),
42  groupRenamer(groupRenamer),
43  validGroupName("([a-zA-Z][a-zA-Z0-9]*)"),
44  colorGreen(QColor::fromRgb(120, 255, 120)),
45  colorRed(QColor::fromRgb(255, 120, 120)),
46  colorYellow(QColor::fromRgb(255, 200, 0)),
47  saveAll(false)
48  {
49  ui->setupUi(this);
50  setOkButtonsEnabled(false);
51  ui->editOldName->setText(group->getName());
52  ui->editOldName->setReadOnly(true);
53  // ui->editOldName->setEnabled(false);
54 
55  ui->editNewName->setValidator(new QRegExpValidator(validGroupName, this));
56 
57  connect(ui->btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
58  connect(ui->btnSaveAndProceed, SIGNAL(clicked()), this, SLOT(saveAllProceedButtonClicked()));
59  connect(ui->btnDontSaveAndProceed, SIGNAL(clicked()), this, SLOT(accept()));
60  connect(ui->editNewName, SIGNAL(textChanged(QString)), this, SLOT(verifyNewName(QString)));
61 
62  for (const StateTreeNodePtr& c : rootNode->getChildren())
63  {
64  if (!c->isGroup())
65  {
66  continue;
67  }
68 
69  auto childGroup = c->getGroup();
70  allGroups.push_back(childGroup);
71 
72  QVector<statechartmodel::StatePtr> allGroupStates = childGroup->getAllStates(false);
73  auto dependencies = GroupCloner::GetGroupsFromStates(treeModel, allGroupStates);
74  bool isDependent = std::find_if(dependencies.constBegin(), dependencies.constEnd(), [&](const StatechartGroupPtr & g)
75  {
76  return g->getGroupPath() == group->getGroupPath();
77  }) != dependencies.constEnd();
78 
79  if (isDependent && childGroup->getGroupPath() != group->getGroupPath())
80  {
81  dependantGroups.push_back(childGroup);
82  }
83  }
84 
85  ui->groupsWidget->setRowCount(dependantGroups.size() + 1);
86  ui->groupsWidget->setColumnCount(1);
87  ui->groupsWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
88  ui->groupsWidget->setHorizontalHeaderLabels({"Affected Groups"});
89 
90  QTableWidgetItem* groupItem = new QTableWidgetItem(group->getName());
91  groupItem->setFlags(groupItem->flags() ^ Qt::ItemIsEditable);
92  ui->groupsWidget->setItem(0, 0, groupItem);
93 
94  for (int i = 0; i < dependantGroups.size(); ++i)
95  {
96  QTableWidgetItem* item = new QTableWidgetItem(dependantGroups[i]->getName());
97  item->setFlags(item->flags() ^ Qt::ItemIsEditable);
98  ui->groupsWidget->setItem(i + 1, 0, item);
99  }
100  }
101 
102  void RenameGroupDialog::setOkButtonsEnabled(bool enabled)
103  {
104  ui->btnDontSaveAndProceed->setEnabled(enabled);
105  ui->btnSaveAndProceed->setEnabled(enabled);
106  }
107 
109  {
110  delete ui;
111  }
112 
114  {
115  return group;
116  }
117 
119  {
120  return groupRenamer;
121  }
122 
124  {
125  return saveAll;
126  }
127 
128  QVector<StatechartGroupPtr> RenameGroupDialog::getDependantGroups() const
129  {
130  return dependantGroups;
131  }
132 
133  QVector<StatechartGroupPtr> RenameGroupDialog::getAllGroups() const
134  {
135  return allGroups;
136  }
137 
139  {
140  return ui->editNewName->text();
141  }
142 
144  {
145  saveAll = true;
146  accept();
147  }
148 
149  void RenameGroupDialog::verifyNewName(QString newName)
150  {
151  bool inUse = false;
152 
153  for (const auto& g : allGroups)
154  {
155  if (newName == g->getName())
156  {
157  inUse = true;
158  break;
159  }
160  }
161 
162  QColor colorToUse;
163 
164  if (inUse)
165  {
166  ui->labelNewNameError->setText("Name already in use");
167  colorToUse = colorRed;
168  setOkButtonsEnabled(false);
169  }
170  else
171  {
172  ui->labelNewNameError->setText("Valid name");
173  colorToUse = colorGreen;
174  setOkButtonsEnabled(true);
175  }
176 
177  QPalette p(ui->labelNewNameError->palette());
178  p.setColor(ui->labelNewNameError->backgroundRole(), colorToUse);
179  ui->labelNewNameError->setPalette(p);
180  }
181 
182 } // namespace armarx
armarx::StateTreeModelPtr
std::shared_ptr< StateTreeModel > StateTreeModelPtr
Definition: StateTreeModel.h:44
armarx::RenameGroupDialog::getGroupRenamer
GroupRenamerPtr getGroupRenamer() const
Definition: RenameGroupDialog.cpp:118
armarx::StateTreeNodePtr
std::shared_ptr< StateTreeNode > StateTreeNodePtr
Definition: StatechartGroupDefs.h:31
armarx::RenameGroupDialog::getGroup
StatechartGroupPtr getGroup() const
Definition: RenameGroupDialog.cpp:113
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::GroupCloner::GetGroupsFromStates
static QVector< StatechartGroupPtr > GetGroupsFromStates(const StateTreeModelPtr &treeModel, const QVector< statechartmodel::StatePtr > &states)
Definition: GroupCloner.cpp:415
armarx::RenameGroupDialog::~RenameGroupDialog
~RenameGroupDialog() override
Definition: RenameGroupDialog.cpp:108
armarx::RenameGroupDialog::getDependantGroups
QVector< StatechartGroupPtr > getDependantGroups() const
Definition: RenameGroupDialog.cpp:128
Ui
ArmarX Headers.
Definition: ArmarXMainWindow.h:54
armarx::RenameGroupDialog
Definition: RenameGroupDialog.h:40
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
armarx::RenameGroupDialog::verifyNewName
void verifyNewName(QString newName)
Definition: RenameGroupDialog.cpp:149
RenameGroupDialog.h
armarx::StatechartGroupPtr
std::shared_ptr< StatechartGroup > StatechartGroupPtr
Definition: StatechartGroupDefs.h:34
GroupCloner.h
armarx::RenameGroupDialog::getNewName
QString getNewName() const
Definition: RenameGroupDialog.cpp:138
armarx::RenameGroupDialog::saveAllProceedButtonClicked
void saveAllProceedButtonClicked()
Definition: RenameGroupDialog.cpp:143
armarx::GroupRenamerPtr
std::shared_ptr< GroupRenamer > GroupRenamerPtr
Definition: GroupRenamer.h:33
armarx::RenameGroupDialog::isSaveAllRequested
bool isSaveAllRequested() const
Definition: RenameGroupDialog.cpp:123
Logging.h
armarx::RenameGroupDialog::getAllGroups
QVector< StatechartGroupPtr > getAllGroups() const
Definition: RenameGroupDialog.cpp:133
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RenameGroupDialog::RenameGroupDialog
RenameGroupDialog(const StateTreeNodePtr &rootNode, const StateTreeModelPtr &treeModel, const StatechartGroupPtr &group, const GroupRenamerPtr &groupRenamer, QWidget *parent=0)
Definition: RenameGroupDialog.cpp:38