IntEnumWidget.cpp
Go to the documentation of this file.
1 #include "IntEnumWidget.h"
2 
3 #include <QHBoxLayout>
4 
5 #include <SimoxUtility/algorithm/string/string_conversion.h>
6 #include <SimoxUtility/error/SimoxError.h>
7 
8 #include "../ColorPalettes.h"
9 #include "../visitors/AronTreeWidgetConverter.h"
10 
11 namespace armarx::skills::gui
12 {
14  QTreeWidgetItem* currentItem) :
15  CustomWidget(currentItem), element_type(enumPtr)
16  {
17  auto names = enumPtr->getAcceptedValueNames();
18  auto outerLayout = new QHBoxLayout();
19  outerLayout->setContentsMargins(0, 0, 0, 0);
20  innerWidget = new QComboBox();
21  outerLayout->addWidget(innerWidget);
22 
23  for (const auto& n : names)
24  {
25  innerWidget->addItem(n.c_str());
26  }
27  innerWidget->setInsertPolicy(QComboBox::NoInsert);
28  innerWidget->setEditable(true);
29  innerWidget->setCurrentIndex(0);
30 
31  setLayout(outerLayout);
32  ARMARX_CHECK(connect(innerWidget,
33  SIGNAL(currentTextChanged(const QString&)),
34  this,
35  SLOT(textChanged(const QString&))));
36  }
37 
39  IntEnumWidget::DynamicCast(QWidget* widget)
40  {
41  return dynamic_cast<IntEnumWidget*>(widget);
42  }
43 
46  {
47  if (!elem)
48  {
49  return nullptr;
50  }
51  auto* casted = DynamicCast(elem);
52  ARMARX_CHECK_NOT_NULL(casted);
53  return casted;
54  }
55 
56  bool
58  {
59  return parseToAron().first;
60  }
61 
62  void
63  IntEnumWidget::setText(const QString& text)
64  {
65  innerWidget->setEditText(text);
66  highlightUnparsableEntries();
67  }
68 
69  QString
71  {
72  return innerWidget->currentText();
73  }
74 
75  std::pair<bool, aron::data::IntPtr>
77  {
78  auto crAron = std::make_shared<aron::data::Int>(element_type->getPath());
79 
80  // first look for strings in value map
81  auto valueMap = element_type->getAcceptedValueMap();
82  std::string toParse = getText().toStdString();
83 
84  auto searchRes = valueMap.find(toParse);
85  if (searchRes != valueMap.end())
86  {
87  crAron->setValue(searchRes->second);
88  return {true, crAron};
89  }
90  // maybe its the number directly
91  auto accVals = element_type->getAcceptedValues();
92  int res;
93  try
94  {
95  res = simox::alg::to_<int>(toParse);
96  }
97  catch (const simox::error::SimoxError& err)
98  {
99  ARMARX_VERBOSE << "Failed to parse IntEnum: Could not convert \'" << toParse
100  << "\' to an int. It also was not one of the accepted strings.";
101  return {false, nullptr};
102  }
103  if (std::find(accVals.begin(), accVals.end(), res) != accVals.end())
104  {
105 
106  crAron->setValue(res);
107  }
108  else
109  {
110  ARMARX_VERBOSE << "Parsed int " << res
111  << "but this int is not part of the accepted values.";
112  return {false, nullptr};
113  }
114  return {true, crAron};
115  }
116 
117  void
118  IntEnumWidget::highlightUnparsableEntries()
119  {
120  auto parsed = parseToAron().first;
121  if (parsed)
122  {
123  innerWidget->setPalette(gui_color_palette::getNormalPalette());
124  }
125  else
126  {
127  innerWidget->setPalette(gui_color_palette::getErrorPalette());
128  }
129  }
130 
131  void
132  IntEnumWidget::textChanged(const QString&)
133  {
134  setSupressSignals(true);
135  highlightUnparsableEntries();
136  setSupressSignals(false);
137  // fire change signal
139  }
140 } // namespace armarx::skills::gui
armarx::skills::gui::IntEnumWidget::hasParseErrors
bool hasParseErrors()
Definition: IntEnumWidget.cpp:57
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::skills::gui::IntEnumWidget
Definition: IntEnumWidget.h:16
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::skills::gui::IntEnumWidget::DynamicCast
static IntEnumWidget * DynamicCast(QWidget *)
Definition: IntEnumWidget.cpp:39
armarx::skills::gui::CustomWidget::overlayingItem
QTreeWidgetItem * overlayingItem
Definition: CustomWidget.h:21
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
IntEnumWidget.h
armarx::skills::gui::IntEnumWidget::DynamicCastAndCheck
static IntEnumWidget * DynamicCastAndCheck(QWidget *)
Definition: IntEnumWidget.cpp:45
armarx::skills::gui::IntEnumWidget::setText
void setText(const QString &text)
Definition: IntEnumWidget.cpp:63
armarx::skills::gui::IntEnumWidget::parseToAron
std::pair< bool, aron::data::IntPtr > parseToAron()
Definition: IntEnumWidget.cpp:76
armarx::skills::gui
Definition: PeriodicUpdateWidget.cpp:11
armarx::skills::gui::CustomWidget::elemChanged
void elemChanged(QTreeWidgetItem *elem, int col)
armarx::skills::gui::CustomWidget::setSupressSignals
virtual void setSupressSignals(bool doSupress)
Definition: CustomWidget.cpp:36
armarx::skills::gui::CustomWidget
Definition: CustomWidget.h:9
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:13
armarx::skills::gui::IntEnumWidget::getText
QString getText()
Definition: IntEnumWidget.cpp:70
armarx::skills::gui::gui_color_palette::getErrorPalette
QPalette getErrorPalette()
Definition: ColorPalettes.cpp:6
armarx::aron::type::IntEnumPtr
std::shared_ptr< IntEnum > IntEnumPtr
Definition: IntEnum.h:36
armarx::skills::gui::IntEnumWidget::IntEnumWidget
IntEnumWidget(const aron::type::IntEnumPtr &, QTreeWidgetItem *currentItem)
Definition: IntEnumWidget.cpp:13
armarx::skills::gui::gui_color_palette::getNormalPalette
QPalette getNormalPalette()
Definition: ColorPalettes.cpp:14