SettingController.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 RobotTrajectoryDesigner::gui-plugins::Controller::SettingController
17 * \author Max Beddies
18 * \date 2018
19 * \copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 #include "SettingController.h"
23 #include <VirtualRobot/XML/RobotIO.h>
24 #include "../KinematicSolver.h"
25 #include <QFileDialog>
26 
27 namespace armarx
28 {
30  {
31  ARMARX_INFO << "RobotTrajectoryDesigner: SettingController on init";
32 
33  // Disable collision model combo box and list, import and export button
34  enableWidgets(true);
35  enableExportButtons(false);
37  }
38 
40  {
41  ARMARX_INFO << "RobotTrajectoryDesigner: SettingController on connect";
42 
43  // Robot selection button: clicked
44  QObject::connect(guiSettingTab->getSettingTab()->robotSelectionButton,
45  SIGNAL(clicked()), this, SLOT(openRobotSelectionDialog()));
46 
47  // Shortcut button: clicked
48  QObject::connect(guiSettingTab->getSettingTab()->shortcutButton,
49  SIGNAL(clicked()), this, SLOT(openShortcutDialog()));
50 
51  // TCP combo box: index changed
52  QObject::connect(guiSettingTab->getSettingTab()->tcpComboBox,
53  SIGNAL(activated(int)),
54  this, SLOT(selectTCP(int)));
55 
56  // New IK solution button: clicked
57  QObject::connect(guiSettingTab->getSettingTab()->newIKSolutionButton,
58  SIGNAL(clicked()), this, SLOT(newIKSolution()));
59 
60  // Active collision model changed
61  QObject::connect(guiSettingTab->getSettingTab()->collisionModelComboBox,
62  SIGNAL(activated(int)), this, SLOT(selectActiveCM(int)));
63 
64  // Other used collision models changed
65  QObject::connect(guiSettingTab->getSettingTab()->collisionModelList,
66  SIGNAL(itemChanged(QListWidgetItem*)),
67  this, SLOT(setCollisionModelList(QListWidgetItem*)));
68 
69  // Export
70  QObject::connect(guiSettingTab->getSettingTab()->convertToMMMButton,
71  SIGNAL(clicked()), this, SLOT(convertToMMMSlot()));
72 
73  // Import
74  QObject::connect(guiSettingTab->getSettingTab()->importButton,
75  SIGNAL(clicked()), this, SLOT(openImportDialog()));
76  }
77 
79  {
80  ARMARX_INFO << "RobotTrajectoryDesigner: SettingController on disconnect";
81  }
82 
84  {
85  ARMARX_INFO << "RobotTrajectoryDesigner: SettingController on exit";
86  }
87 
89  guiSettingTab(settingTab)
90  {
93  }
94 
96  {
97  return this->guiSettingTab;
98  }
99 
101  {
102  if (guiSettingTab != NULL)
103  {
104  this->guiSettingTab = guiSettingTab;
105  }
106  }
107 
108  void SettingController::openRobotSelectionDialog()
109  {
110  emit openRobotSelection();
111  }
112 
113  void SettingController::openShortcutDialog()
114  {
115  emit openShortcut();
116  }
117 
119  {
120  /*
121  * If index is valid kinematic chain, enable widgets and send signal,
122  * else disable widgets
123  */
124  if (index > 0)
125  {
126  enableWidgets(true);
127  emit changedTCP(this->guiSettingTab->getSettingTab()->
128  tcpComboBox->currentText());
129  }
130  else
131  {
132  enableWidgets(false);
133  }
134  }
135 
137  {
138  QComboBox* combobox = guiSettingTab->getSettingTab()->tcpComboBox;
139  int index = combobox->findText(tcp);
140 
141  // Check if tcp is valid kinematic chain
142  if (index > 0)
143  {
144  combobox->setCurrentIndex(index);
145  }
146  }
147 
148  void SettingController::selectActiveCM(int index)
149  {
150  QListWidget* cmList = this->guiSettingTab->getSettingTab()->collisionModelList;
151  if (index > 0)
152  {
153  // Enable all collision model items but active collision model item
154  for (int i = 0; i < cmList->count(); i++)
155  {
156  QListWidgetItem* item = cmList->item(i);
157  if (i == (index - 1))
158  {
159  item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
160  }
161  else
162  {
163  item->setFlags(item->flags() | Qt::ItemIsEnabled);
164  }
165  }
166  cmList->setEnabled(true);
167  }
168  else
169  {
170  cmList->setEnabled(false);
171  }
172  emit setActiveColModelName(guiSettingTab->getSettingTab()->collisionModelComboBox->itemText(index));
173  }
174 
175  void SettingController::setCollisionModelList(QListWidgetItem* item)
176  {
177  if (item != NULL)
178  {
179  QComboBox* tcp = guiSettingTab->getSettingTab()->tcpComboBox;
180  QComboBox* cm = guiSettingTab->getSettingTab()->collisionModelComboBox;
181 
182  // Check if tcp is selected and active collision model is selected
183  if ((tcp->currentIndex() > 0)
184  && (cm->currentIndex() > 0))
185  {
186  QListWidget* models = guiSettingTab->getSettingTab()->collisionModelList;
187  QStringList bodyCollisionModels;
188 
189  // Add each checked collision model as robot node set pointer to vector
190  for (int i = 0; i < models->count(); i++)
191  {
192  if (models->item(i)->checkState() == Qt::Checked)
193  {
194  bodyCollisionModels.push_back(models->item(i)->text());
195  }
196  }
197  emit setBodyColModelsNames(bodyCollisionModels);
198  }
199  }
200  }
201 
202  void SettingController::exportTrajectorySlot()
203  {
204  emit exportTrajectory();
205  }
206 
207  void SettingController::convertToMMMSlot()
208  {
209  emit convertToMMM();
210  }
211 
212  void SettingController::openImportDialog()
213  {
214  emit openImport();
215  }
216 
217  void SettingController::setLanguage(int index)
218  {
219  /*
220  QVariant language = guiSettingTab->getSettingTab()->languageComboBox->
221  itemData(index, Qt::UserRole);
222  if (language.isValid())
223  {
224  emit changedLanguage(language.toString());
225  }
226  */
227  }
228 
230  {
231  this->guiSettingTab->getSettingTab()->newIKSolutionButton->setEnabled(enable);
232  }
233 
235  {
236  this->guiSettingTab->getSettingTab()->convertToMMMButton->setEnabled(enable);
237  //this->guiSettingTab->getSettingTab()->exportTrajectoryButton->setEnabled(enable);
238  }
239 
241  {
242  this->guiSettingTab->getSettingTab()->collisionModelComboBox->setEnabled(enable);
243  this->guiSettingTab->getSettingTab()->collisionModelList->setEnabled(enable);
244  this->guiSettingTab->getSettingTab()->tcpComboBox->setEnabled(enable);
245  this->guiSettingTab->getSettingTab()->importButton->setEnabled(enable);
246  }
247 
248  void SettingController::newIKSolution()
249  {
250  VirtualRobot::RobotPtr robot = environment->getRobot();
252  VirtualRobot::RobotNodeSetPtr activeKinematicChain = robot->getRobotNodeSet(guiSettingTab->getSettingTab()->tcpComboBox->currentText().toStdString());
253  std::vector<double> newJA = kc->solveIK(activeKinematicChain, PoseBasePtr(new Pose(activeKinematicChain->getTCP()->getGlobalPose())), VirtualRobot::IKSolver::CartesianSelection::All, 50);
254  if (newJA.size() != 0)
255  {
256  activeKinematicChain->setJointValues(std::vector<float>(newJA.begin(), newJA.end()));
257  }
258  }
259 
261  {
262  throw ("not yet implemented");
263  }
264 
266  {
267  guiSettingTab->getSettingTab()->robotSelectionButton->setEnabled(enable);
268  }
269 
271  {
272  guiSettingTab->getSettingTab()->tcpComboBox->clear();
273  guiSettingTab->getSettingTab()->collisionModelComboBox->clear();
274  guiSettingTab->getSettingTab()->collisionModelList->clear();
275  this->environment = environment;
276  // Get robot and initialize child widgets
277  VirtualRobot::RobotPtr robot = environment->getRobot();
278  initTCPComboBox(robot);
279  initCMComboBox(robot);
280  initCMList(robot);
281  }
282 
283 
284  /************************************************************************************/
285  /* Private functions */
286  /************************************************************************************/
287  void SettingController::enableWidgets(bool enable)
288  {
289  this->guiSettingTab->getSettingTab()->collisionModelComboBox->setEnabled(enable);
290  this->guiSettingTab->getSettingTab()->collisionModelList->setEnabled(enable);
291  //Man kann auch importieren ohne einen TCP ausgewählt zu haben.
292  //this->guiSettingTab->getSettingTab()->importButton->setEnabled(enable);
293  }
294 
295  void SettingController::initTCPComboBox(VirtualRobot::RobotPtr robot)
296  {
297  QComboBox* tcpComboBox = this->guiSettingTab->getSettingTab()->tcpComboBox;
298 
299  // Set strong focus and add wheel event filter
300  tcpComboBox->setFocusPolicy(Qt::StrongFocus);
301  tcpComboBox->installEventFilter(new WheelEventFilter(this));
302 
303  if (robot)
304  {
305  tcpComboBox->clear();
306 
307  // Combo box item at first index is no rns
308  tcpComboBox->insertItem(0, QString::fromStdString("- select -"));
309 
310  auto robotNodeSets = robot->getRobotNodeSets();
311 
312  // Add combo box item for each robot node set
313  for (VirtualRobot::RobotNodeSetPtr s : robotNodeSets)
314  {
315  if ((s->isKinematicChain()) && (s->getTCP() != NULL))
316  {
317  ARMARX_DEBUG << "Add item " << s->getName() << " to tcp combo box";
318  tcpComboBox->addItem(QString::fromStdString(s->getName()));
319  }
320  }
321  tcpComboBox->setEnabled(true);
322  tcpComboBox->setCurrentIndex(0);
323  }
324  else
325  {
326  // Error message? Or just label in red etc?
327  tcpComboBox->setEnabled(false);
328  }
329  }
330 
331  void SettingController::initCMComboBox(VirtualRobot::RobotPtr robot)
332  {
333  QComboBox* cmComboBox = this->guiSettingTab->getSettingTab()->
334  collisionModelComboBox;
335 
336  // Set strong focus and add wheel event filter
337  cmComboBox->setFocusPolicy(Qt::StrongFocus);
338  cmComboBox->installEventFilter(new WheelEventFilter(this));
339 
340  if (robot)
341  {
342  cmComboBox->clear();
343 
344  // Combo box item at first index is no collision model
345  cmComboBox->insertItem(0, QString::fromStdString("- select -"));
346 
347  auto robotNodeSets = robot->getRobotNodeSets();
348 
349  // Add combo box item for each robot node set (collision model)
350  for (VirtualRobot::RobotNodeSetPtr s : robotNodeSets)
351  {
352  if (s->isKinematicChain())
353  {
354  ARMARX_DEBUG << "Add item " << s->getName() << " to collision model combo box";
355  cmComboBox->addItem(QString::fromStdString(s->getName()));
356  }
357  }
358  cmComboBox->setCurrentIndex(0);
359  }
360  else
361  {
362  // Error message? Or just label in red etc?
363  cmComboBox->setEnabled(false);
364  }
365  }
366 
367  void SettingController::initCMList(VirtualRobot::RobotPtr robot)
368  {
369  QListWidget* cmList = this->guiSettingTab->getSettingTab()->collisionModelList;
370  if (robot)
371  {
372  cmList->clear();
373 
374  auto robotNodeSets = robot->getRobotNodeSets();
375 
376  for (VirtualRobot::RobotNodeSetPtr s : robotNodeSets)
377  {
378  if (s->isKinematicChain())
379  {
380  QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(s->getName()));
381  item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
382  item->setCheckState(Qt::Unchecked);
383  ARMARX_DEBUG << "Add item " << s->getName() << " to collision model list";
384  cmList->addItem(item);
385  }
386  }
387  }
388  else
389  {
390  // Error message? Or just label in red etc?
391  cmList->setEnabled(false);
392  }
393  }
394 
395 }
armarx::SettingController::SettingController
SettingController(SettingTabPtr guiSettingTab)
Creates a new SettingController and assigns a SettingTab to handle.
Definition: SettingController.cpp:88
armarx::SettingController::onDisconnectComponent
void onDisconnectComponent() override
Definition: SettingController.cpp:78
armarx::SettingController::changedTCP
void changedTCP(QString tcp)
Notifies other controllers about a change of the current TCP.
SettingController.h
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::SettingController::convertToMMM
void convertToMMM()
Notifies other controllers to convert all trajectories to MMM.
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::SettingController::onExitComponent
void onExitComponent() override
Definition: SettingController.cpp:83
armarx::SettingController::retranslateGui
void retranslateGui()
Retranslates the guiSettingTab.
Definition: SettingController.cpp:260
armarx::SettingController::enableImportTCPCollision
void enableImportTCPCollision(bool enable)
Enables or disables the import, tcp and collision buttons.
Definition: SettingController.cpp:240
armarx::SettingController::setGuiSettingTab
void setGuiSettingTab(SettingTabPtr guiSettingTab)
Setter for the SettingTab pointer to guiSettingTab.
Definition: SettingController.cpp:100
armarx::SettingController::exportTrajectory
void exportTrajectory()
Notifies other controllers to export all trajectories to Trajectory.
armarx::EnvironmentPtr
std::shared_ptr< Environment > EnvironmentPtr
Definition: Environment.h:29
armarx::SettingController::openShortcut
void openShortcut()
Notifies other controllers to open a shortcut dialog.
armarx::SettingController::enableIKSolutionButton
void enableIKSolutionButton(bool enable)
Enables or disables the new IK solution button.
Definition: SettingController.cpp:229
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::SettingController::getGuiSettingTab
SettingTabPtr getGuiSettingTab()
Getter for the SettingTab pointer to guiSettingTab.
Definition: SettingController.cpp:95
armarx::SettingController::openRobotSelection
void openRobotSelection()
Notifies other controllers to open a robot selection dialog.
armarx::SettingController::onInitComponent
void onInitComponent() override
Definition: SettingController.cpp:29
armarx::SettingController::setBodyColModelsNames
void setBodyColModelsNames(QStringList bodyColModelsNames)
Sets the body collision models names.
SettingTabPtr
std::shared_ptr< SettingTab > SettingTabPtr
Definition: SettingTab.h:52
armarx::KinematicSolver::getInstance
static std::shared_ptr< KinematicSolver > getInstance(VirtualRobot::ScenePtr scenario, VirtualRobot::RobotPtr robot)
SINGLETON-FEATURES///////////////////////////////////////////////////////////////////////////////////...
Definition: KinematicSolver.cpp:92
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::SettingController::enableSelectRobotButton
void enableSelectRobotButton(bool enable)
Definition: SettingController.cpp:265
armarx::SettingController::environmentChanged
void environmentChanged(EnvironmentPtr environment)
Set the enviroment.
Definition: SettingController.cpp:270
armarx::SettingController::selectTCP
void selectTCP(int index)
Changes the currently selected TCP.
Definition: SettingController.cpp:118
armarx::SettingController::onConnectComponent
void onConnectComponent() override
Definition: SettingController.cpp:39
armarx::KinematicSolverPtr
std::shared_ptr< KinematicSolver > KinematicSolverPtr
Definition: KinematicSolver.h:235
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::SettingController::openImport
void openImport()
Notifies other controllers to open an import dialog.
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::SettingController::enableExportButtons
void enableExportButtons(bool enable)
Enables or disables the export buttons.
Definition: SettingController.cpp:234
armarx::SettingController::setActiveColModelName
void setActiveColModelName(QString activeColModelName)
Sets the active collision model name.