TransitionController.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::TransitionController
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 "TransitionController.h"
23 #include "../Interpolation/InterpolationType.h"
24 #include <iostream>
25 
26 namespace armarx
27 {
28 
30  {
31  ARMARX_INFO << "RobotTrajectoryDesigner: TransitionController on init";
32 
33  // Fill interpolation combo box with items
34  initInterpolationComboBox();
35 
36  // Set single selection and clear transition list
37  this->guiTransitionTab->getTransitionTab()->transitionList->
38  setSelectionMode(QAbstractItemView::SingleSelection);
39  this->guiTransitionTab->getTransitionTab()->transitionList->clear();
40 
41  // Add double validator to duration line edit
42  QDoubleValidator* validator = new QDoubleValidator(
43  -1000.000,
44  1000.000,
45  3,
46  guiTransitionTab.get());
47  validator->setNotation(QDoubleValidator::StandardNotation);
48  this->guiTransitionTab->getTransitionTab()->durationLineEdit->setValidator(validator);
49  }
50 
52  {
53  ARMARX_INFO << "RobotTrajectoryDesigner: TransitionController on connect";
54 
55  // Transition list: changed selected item
56  QObject::connect(guiTransitionTab->getTransitionTab()->transitionList,
57  SIGNAL(itemClicked(QListWidgetItem*)), this,
58  SLOT(updateSelectedTransition(QListWidgetItem*)));
59 
60  // Transition duration: changed duration
61  QObject::connect(guiTransitionTab->getTransitionTab()->durationLineEdit,
62  SIGNAL(editingFinished()),
63  this, SLOT(setDurationValue()));
64 
65  // Transition interpolation: changed interpolation
66  QObject::connect(guiTransitionTab->getTransitionTab()->interpolationComboBox,
67  SIGNAL(activated(int)),
68  this, SLOT(setInterpolation(int)));
69  }
70 
72  {
73  ARMARX_INFO << "RobotTrajectoryDesigner: TransitionController on disconnect";
74  }
75 
77  {
78  ARMARX_INFO << "RobotTrajectoryDesigner: TransitionController on exit";
79  }
80 
82  guiTransitionTab(guiTransitionTab)
83  {
86  }
87 
89  {
90  return this->guiTransitionTab;
91  }
92 
94  {
95  if (guiTransitionTab != NULL)
96  {
97  this->guiTransitionTab = guiTransitionTab;
98  }
99  }
100 
102  int index,
103  double duration,
104  double start,
105  int interpolation)
106  {
107  // Create struct representing transition
108  struct GuiTransition transition;
109  transition.duration = duration;
110  transition.start = start;
111  transition.it = interpolation;
112 
113  if (transition.isValid())
114  {
115  // Create qvariant from struct
116  QVariant data;
117  data.setValue(transition);
118 
119  // add data to item and insert
120  QListWidgetItem* item = new QListWidgetItem(QString(("Transition: " + std::to_string(index)).c_str()));
121  item->setData(Qt::UserRole, data);
122  QListWidget* transitions = guiTransitionTab->getTransitionTab()->transitionList;
123 
124  transitions->insertItem(index, item);
126  }
127  changeTextListWidgetItems();
128  }
129 
131  {
132  QListWidget* transitions = guiTransitionTab->getTransitionTab()->transitionList;
133 
134  // If index is within range, delete transition at index
135  if (transitions->count() > index)
136  {
137  delete transitions->takeItem(index);
138  }
139  changeTextListWidgetItems();
140  }
141 
142  void TransitionController::updateSelectedTransition(QListWidgetItem* item)
143  {
144  GuiTransition transition = item->data(Qt::UserRole).value<GuiTransition>();
145 
146  // Duration line edit
147  guiTransitionTab->getTransitionTab()->durationLineEdit->
148  setText(QString::number(transition.duration));
149  // Start value label
150  guiTransitionTab->getTransitionTab()->startValueLabel->
151  setText(QString::number(transition.start));
152  // End value label
153  guiTransitionTab->getTransitionTab()->EndValueLabel->
154  setText(QString::number(transition.start + transition.duration));
155  // Interpolation combo box
156  guiTransitionTab->getTransitionTab()->interpolationComboBox->
157  setCurrentIndex(transition.it);
158 
159  // Notify other controllers about changes of the selected transition
160  QListWidget* transitions = guiTransitionTab->getTransitionTab()->transitionList;
161  emit selectedTransitionChanged(transitions->currentIndex().row());
162 
163  }
164 
166  {
167  GuiTransition transition = guiTransitionTab->getTransitionTab()->transitionList->
168  item(index)->data(Qt::UserRole).value<GuiTransition>();
169 
170  // Duration line edit
171  guiTransitionTab->getTransitionTab()->durationLineEdit->
172  setText(QString::number(transition.duration));
173  // Start value label
174  guiTransitionTab->getTransitionTab()->startValueLabel->
175  setText(QString::number(transition.start));
176  // End value label
177  guiTransitionTab->getTransitionTab()->EndValueLabel->
178  setText(QString::number(transition.start + transition.duration));
179  // Interpolation combo box
180  guiTransitionTab->getTransitionTab()->interpolationComboBox->
181  setCurrentIndex(transition.it);
182  }
183 
184  void TransitionController::setDurationValue()
185  {
186  if (this->guiTransitionTab->getTransitionTab()->durationLineEdit->hasAcceptableInput())
187  {
188  QListWidget* transitions = guiTransitionTab->getTransitionTab()->transitionList;
189  QListWidgetItem* item = transitions->currentItem();
190 
191  // Check if list widget has any item
192  if (item != NULL)
193  {
194  GuiTransition transition = item->data(Qt::UserRole).value<GuiTransition>();
195  double dur = this->guiTransitionTab->getTransitionTab()->
196  durationLineEdit->text().toDouble();
197 
198  // Check if duration of item has changed
199  if ((dur >= 0) && (dur != transition.duration))
200  {
201  emit changedDuration(transitions->currentRow(), dur);
202  }
203  }
204 
205  }
206  }
207 
208  void TransitionController::setInterpolation(int index)
209  {
210  QListWidget* transitions = guiTransitionTab->getTransitionTab()->transitionList;
211  QListWidgetItem* item = transitions->currentItem();
212  // Check if list widget has any item
213  if (item != NULL)
214  {
215  GuiTransition transition = item->data(Qt::UserRole).value<GuiTransition>();
216 
217  // Check if interpolation of item has changed
218  if ((index >= 0) && (index != transition.it))
219  {
220  emit changedInterpolation(transitions->currentRow(), index);
221  }
222  }
223  }
224 
225  void TransitionController::enableAll(bool enable)
226  {
227  this->guiTransitionTab->getTransitionTab()->durationLineEdit->setEnabled(enable);
228  this->guiTransitionTab->getTransitionTab()->interpolationComboBox->setEnabled(enable);
229  this->guiTransitionTab->getTransitionTab()->transitionList->setEnabled(enable);
230  }
231 
233  int index,
234  double duration,
235  double start,
236  int it)
237  {
238  // Create struct holding relevant data
239  struct GuiTransition transition;
240  transition.duration = duration;
241  transition.start = start;
242  transition.it = it;
243 
244  if (transition.isValid())
245  {
246  // Create QVariant from struct
247  QVariant data;
248  data.setValue(transition);
249 
250  QListWidget* transitions = guiTransitionTab->getTransitionTab()->transitionList;
251  QListWidgetItem* item = transitions->item(index);
252 
253  // Update data
254  item->setData(Qt::UserRole, data);
255  // Display updated data
256  if (transitions->currentRow() == index)
257  {
259  }
260  }
261  }
262 
264  {
265  throw ("not yet implemented");
266  }
267 
269  {
270  // Clear transition list
271  this->guiTransitionTab->getTransitionTab()->transitionList->clear();
272  // Clear duration line edit
273  this->guiTransitionTab->getTransitionTab()->durationLineEdit->clear();
274  // Clear start and end label
275  this->guiTransitionTab->getTransitionTab()->startValueLabel->clear();
276  this->guiTransitionTab->getTransitionTab()->EndValueLabel->clear();
277  // Reset interpolation index
278  this->guiTransitionTab->getTransitionTab()->interpolationComboBox->setCurrentIndex(0);
279  }
280 
281  bool TransitionController::contains(QListWidget* list, GuiTransition transition)
282  {
283  for (int i = 0; i < list->count(); i++)
284  {
285  if (list->item(i)->data(Qt::UserRole).value<GuiTransition>() == transition)
286  {
287  return true;
288  }
289  }
290  return false;
291  }
292 
293  void TransitionController::initInterpolationComboBox()
294  {
295  QComboBox* interpolation = guiTransitionTab->getTransitionTab()->
296  interpolationComboBox;
297 
298  // Set strong focus and add wheel event filter
299  interpolation->setFocusPolicy(Qt::StrongFocus);
300  interpolation->installEventFilter(new WheelEventFilter(this));
301 
302  // Clear combo box and insert items
303  interpolation->clear();
304  interpolation->addItem(QString::fromStdString("Linear Interpolation"));
305  interpolation->addItem(QString::fromStdString("Spline interpolation"));
306  interpolation->setEnabled(true);
307  interpolation->setCurrentIndex(0);
308  }
309 
310  void TransitionController::changeTextListWidgetItems()
311  {
312  QListWidget* transitions = this->guiTransitionTab->getTransitionTab()->transitionList;
313  for (int i = 0; i < transitions->count(); i++)
314  {
315  transitions->item(i)->setText(QString::fromStdString(("Transition: " + std::to_string(i))));
316  }
317  }
318 }
armarx::GuiTransition::start
double start
Definition: TransitionController.h:43
armarx::GuiTransition
Struct defining a transition which can be stored as QVariant in a QListWidgetItem.
Definition: TransitionController.h:40
index
uint8_t index
Definition: EtherCATFrame.h:59
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
armarx::GuiTransition::isValid
bool isValid()
Checks whether all values of a transition are greater than or equal to zero.
Definition: TransitionController.h:63
armarx::TransitionController::getGuiTransitionTab
TransitionTabPtr getGuiTransitionTab()
Getter for the TransitionTab pointer to guiTransitionTab.
Definition: TransitionController.cpp:88
armarx::TransitionController::addTransition
void addTransition(int index, double duration, double start, int interpolation)
Adds a new transition to the list widget.
Definition: TransitionController.cpp:101
armarx::armem::contains
bool contains(const MemoryID &general, const MemoryID &specific)
Indicates whether general is "less specific" than, or equal to, specific, i.e.
Definition: MemoryID.cpp:558
armarx::TransitionController::onDisconnectComponent
void onDisconnectComponent() override
Definition: TransitionController.cpp:71
armarx::TransitionController::onExitComponent
void onExitComponent() override
Definition: TransitionController.cpp:76
TransitionController.h
armarx::TransitionController::retranslateGui
void retranslateGui()
Retranslates the guiTransitionTab.
Definition: TransitionController.cpp:263
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::TransitionController::selectedTransitionChanged
void selectedTransitionChanged(int index)
TransitionTabPtr
std::shared_ptr< TransitionTab > TransitionTabPtr
Definition: TransitionTab.h:50
armarx::TransitionController::setGuiTransitionTab
void setGuiTransitionTab(TransitionTabPtr guiTransitionTab)
Setter for the TransitionTab pointer to guiTransitionTab.
Definition: TransitionController.cpp:93
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::TransitionController::clearTransitionList
void clearTransitionList()
Removes all items of the transition list.
Definition: TransitionController.cpp:268
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::TransitionController::TransitionController
TransitionController(TransitionTabPtr guiTransitionTab)
Creates a new TransitionController and assigns a TransitionTab to handle.
Definition: TransitionController.cpp:81
armarx::TransitionController::updateSelectedTransition
void updateSelectedTransition(int index)
Updates the currently selected transition.
Definition: TransitionController.cpp:165
armarx::TransitionController::onInitComponent
void onInitComponent() override
Definition: TransitionController.cpp:29
armarx::GuiTransition::duration
double duration
Definition: TransitionController.h:42
armarx::TransitionController::removeTransition
void removeTransition(int index)
Removes the transition at a given index from the list widget.
Definition: TransitionController.cpp:130
armarx::TransitionController::changedInterpolation
void changedInterpolation(int index, int it)
Notifies other controllers about changes of the interpolation type of the given transition.
armarx::TransitionController::setTransitionData
void setTransitionData(int index, double duration, double start, int it)
Connected with signals from other controllers, sets all values of the transition at a given index.
Definition: TransitionController.cpp:232
armarx::GuiTransition
struct armarx::GuiTransition GuiTransition
Struct defining a transition which can be stored as QVariant in a QListWidgetItem.
armarx::TransitionController::onConnectComponent
void onConnectComponent() override
Definition: TransitionController.cpp:51
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::GuiTransition::it
int it
Definition: TransitionController.h:44
armarx::TransitionController::changedDuration
void changedDuration(int transition, double duration)
Notifies other controllers about changes of the duration of the given transition.