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