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