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
11
12namespace 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
41 {
42 return dynamic_cast<IntEnumWidget*>(widget);
43 }
44
47 {
48 if (!elem)
49 {
50 return nullptr;
51 }
52 auto* casted = DynamicCast(elem);
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
void elemChanged(QTreeWidgetItem *elem, int col)
virtual void setSupressSignals(bool doSupress)
QTreeWidgetItem * overlayingItem
CustomWidget(QTreeWidgetItem *overlayingItem)
void setText(const QString &text)
IntEnumWidget(const aron::type::IntEnumPtr &, QTreeWidgetItem *currentItem)
static IntEnumWidget * DynamicCastAndCheck(QWidget *)
std::pair< bool, aron::data::IntPtr > parseToAron()
static IntEnumWidget * DynamicCast(QWidget *)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#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...
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::shared_ptr< IntEnum > IntEnumPtr
Definition IntEnum.h:36
This file offers overloads of toIce() and fromIce() functions for STL container types.