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 outerLayout = new QHBoxLayout();
18 outerLayout->setContentsMargins(0, 0, 0, 0);
19 innerWidget = new QComboBox();
20 outerLayout->addWidget(innerWidget);
21
22 for (const auto& [name, value] : enumPtr->getAcceptedValueMap())
23 {
24 innerWidget->addItem(QString::fromStdString(toDisplayText(name, value)));
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
39 {
40 return dynamic_cast<IntEnumWidget*>(widget);
41 }
42
45 {
46 if (!elem)
47 {
48 return nullptr;
49 }
50 auto* casted = DynamicCast(elem);
52 return casted;
53 }
54
55 bool
57 {
58 return parseToAron().first;
59 }
60
61 std::string
62 IntEnumWidget::toDisplayText(const std::string& name, int value)
63 {
64 return name + " (" + std::to_string(value) + ")";
65 }
66
67 std::optional<std::pair<std::string, int>>
68 IntEnumWidget::resolveText(const std::string& text) const
69 {
70 const auto valueMap = element_type->getAcceptedValueMap();
71
72 // display format or plain name
73 for (const auto& [name, value] : valueMap)
74 {
75 if (text == toDisplayText(name, value) || text == name)
76 {
77 return std::make_pair(name, value);
78 }
79 }
80
81 // plain number
82 try
83 {
84 const int parsed = simox::alg::to_<int>(text);
85 for (const auto& [name, value] : valueMap)
86 {
87 if (value == parsed)
88 {
89 return std::make_pair(name, value);
90 }
91 }
92 }
93 catch (const simox::error::SimoxError&)
94 {
95 }
96
97 return std::nullopt;
98 }
99
100 void
101 IntEnumWidget::setText(const QString& text)
102 {
103 // if the text identifies an enum entry, show it in the display format;
104 // this only affects the GUI, parseToAron() resolves it back to the int value
105 if (const auto resolved = resolveText(text.toStdString()))
106 {
107 innerWidget->setEditText(
108 QString::fromStdString(toDisplayText(resolved->first, resolved->second)));
109 }
110 else
111 {
112 innerWidget->setEditText(text);
113 }
114 highlightUnparsableEntries();
115 }
116
117 QString
119 {
120 return innerWidget->currentText();
121 }
122
123 std::pair<bool, aron::data::IntPtr>
125 {
126 // accepts the display format ("Name (value)"), a plain name, or a plain
127 // number; the resulting aron data is always the plain int value
128 const std::string toParse = getText().toStdString();
129 const auto resolved = resolveText(toParse);
130 if (not resolved.has_value())
131 {
132 ARMARX_VERBOSE << "Failed to parse IntEnum: \'" << toParse
133 << "\' is neither an accepted name nor an accepted value.";
134 return {false, nullptr};
135 }
136
137 auto crAron = std::make_shared<aron::data::Int>(element_type->getPath());
138 crAron->setValue(resolved->second);
139 return {true, crAron};
140 }
141
142 void
143 IntEnumWidget::highlightUnparsableEntries()
144 {
145 auto parsed = parseToAron().first;
146 if (parsed)
147 {
148 innerWidget->setPalette(gui_color_palette::getNormalPalette());
149 }
150 else
151 {
152 innerWidget->setPalette(gui_color_palette::getErrorPalette());
153 }
154 }
155
156 void
157 IntEnumWidget::textChanged(const QString&)
158 {
159 setSupressSignals(true);
160 highlightUnparsableEntries();
161 setSupressSignals(false);
162 // fire change signal
164 }
165} // 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:185
std::shared_ptr< IntEnum > IntEnumPtr
Definition IntEnum.h:36