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"
10
11namespace 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
40 {
41 return dynamic_cast<IntEnumWidget*>(widget);
42 }
43
46 {
47 if (!elem)
48 {
49 return nullptr;
50 }
51 auto* casted = DynamicCast(elem);
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
void elemChanged(QTreeWidgetItem *elem, int col)
virtual void setSupressSignals(bool doSupress)
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