CVB++ 15.0
detail_integer_property.hpp
1#pragma once
2
3#include "../../global.hpp"
4#include "../../genapi/value_node.hpp"
5#include "../../genapi/integer_node.hpp"
6#include "detail_property.hpp"
7#include "detail_spinbox_slider.hpp"
8#include "detail_custom_lineedit.hpp"
9#include "detail_network.hpp"
10
11namespace Cvb
12{
13
14 CVB_BEGIN_INLINE_NS
15
16 namespace UI
17 {
18 namespace Private
19 {
20
21 class IntegerProperty : public Private::Property
22 {
23
24 Q_OBJECT
25
26 public:
27 IntegerProperty(const ValueNodePtr &node, Property *parent)
28 : Property(node, PT_Integer, parent)
29 {
30 // SFNC
31 ipList_ << "Std::GevCurrentIPAddress" << "Std::GevCurrentSubnetMask" << "Std::GevCurrentDefaultGateway"
32 << "Std::GevPersistentIPAddress" << "Std::GevPersistentSubnetMask"
33 << "Std::GevPersistentDefaultGateway" << "Std::GevMCDA" << "Std::GevSCDA"
34 << "Std::GevPrimaryApplicationIPAddress";
35 // SI
36 ipList_ << "Cust::InterfaceSubnetIP" << "Cust::InterfaceSubnetMask" << "Cust::InterfaceGateway"
37 << "Cust::IPCfg_IP" << "Cust::IPCfg_Subnet" << "Cust::IPCfg_Gateway"
38 << "Cust::DeviceIP" << "Cust::DeviceGateway" << "Cust::StreamDestinationIP";
39
40 // SFNC
41 macList_ << "Std::GevMACAddress";
42 // SI
43 macList_ << "Cust::GevInterfaceMACAddress" << "Cust::InterfaceMAC" << "Cust::IPCfg_MAC" << "Cust::DeviceMAC";
44 }
45
46 QWidget *CreateEditor(QWidget *parent) override
47 {
48 auto intNode = std::dynamic_pointer_cast<IntegerNode>(Node());
49
50 if (!intNode)
51 return EmptyEditor();
52
53 if (!intNode->IsAvailable())
54 return EmptyEditor();
55
56 auto rep = intNode->Representation();
57 if (rep == GenApi::NumberRepresentation::IPv4 || ipList_.contains(UI::CvbToQt(intNode->Name())))
58 return CreateIpEditor(intNode, parent);
59 else if (rep == GenApi::NumberRepresentation::MAC || macList_.contains(UI::CvbToQt(intNode->Name())))
60 return CreateMacEditor(intNode, parent);
61 return CreateIntEditor(intNode, parent);
62 }
63
64 QWidget *CreateIntEditor(const IntegerNodePtr &node, QWidget *parent)
65 {
66 // No sliders necessary if min == max
67 if (node->Min() == node->Max())
68 {
69 auto editor = new SpinBox64(parent); // NOLINT(cppcoreguidelines-owning-memory)
70 editor->setMinimum(node->Min());
71 editor->setMaximum(node->Max());
72 editor->setValue(node->Value());
73 editor->setSingleStep(node->Increment());
74
75 QObject::connect(editor, &SpinBox64::ValueChanged, this, [=](qint64 value) { SetValue(QVariant(value)); });
76 return editor;
77 }
78
79 // Create sliders as well
80 auto editor = new SpinBoxSlider64(parent); // NOLINT(cppcoreguidelines-owning-memory)
81 editor->SetMinimum(node->Min());
82 editor->SetMaximum(node->Max());
83 editor->SetValue(node->Value());
84 editor->SetSingleStep(node->Increment());
85
86 QObject::connect(editor, &SpinBoxSlider64::ValueChanged, this,
87 [=](qint64 value) { SetValue(QVariant(value)); });
88
89 return editor;
90 }
91
92 QWidget *CreateIpEditor(const IntegerNodePtr &node, QWidget *parent)
93 {
94 auto editor = new IpLineEdit(parent); // NOLINT(cppcoreguidelines-owning-memory)
95 IpAddress ip(node->Value());
96 editor->SetText(ip.ToQString());
97
98 QObject::connect(editor, &IpLineEdit::TextChanged, this,
99 [=](const QString &text) { SetValue(QVariant(text)); });
100
101 return editor;
102 }
103
104 QWidget *CreateMacEditor(const IntegerNodePtr &node, QWidget *parent)
105 {
106 auto editor = new MacLineEdit(parent); // NOLINT(cppcoreguidelines-owning-memory)
107 MacAddress mac(node->Value());
108 editor->SetText(mac.ToQString());
109
110 QObject::connect(editor, &MacLineEdit::TextChanged, this,
111 [=](const QString &text) { SetValue(QVariant(text)); });
112
113 return editor;
114 }
115
116 bool SetEditorData(QWidget *editor, const QVariant &data) override
117 {
118 auto intNode = std::dynamic_pointer_cast<IntegerNode>(Node());
119
120 if (!intNode)
121 return false;
122
123 auto rep = intNode->Representation();
124 if (rep == GenApi::NumberRepresentation::IPv4 || ipList_.contains(UI::CvbToQt(intNode->Name())))
125 {
126 auto le = dynamic_cast<IpLineEdit *>(editor);
127 if (le)
128 {
129 le->SetText(data.toString());
130 return true;
131 }
132 }
133 else if (rep == GenApi::NumberRepresentation::MAC || macList_.contains(UI::CvbToQt(intNode->Name())))
134 {
135 auto le = dynamic_cast<MacLineEdit *>(editor);
136 if (le)
137 {
138 le->SetText(data.toString());
139 return true;
140 }
141 }
142 else
143 {
144 auto sb = dynamic_cast<SpinBoxSlider64 *>(editor);
145 if (sb)
146 {
147 sb->SetValue(data.toInt());
148 return true;
149 }
150 }
151 return false;
152 }
153
154 QVariant EditorData(QWidget *editor) override
155 {
156 auto intNode = std::dynamic_pointer_cast<IntegerNode>(Node());
157
158 auto rep = intNode->Representation();
159 if (rep == GenApi::NumberRepresentation::IPv4 || ipList_.contains(UI::CvbToQt(intNode->Name())))
160 {
161 auto le = dynamic_cast<IpLineEdit *>(editor);
162 if (le)
163 return QVariant(le->Text());
164 }
165 else if (rep == GenApi::NumberRepresentation::MAC || macList_.contains(UI::CvbToQt(intNode->Name())))
166 {
167 auto le = dynamic_cast<MacLineEdit *>(editor);
168 if (le)
169 return QVariant(le->Text());
170 }
171 else
172 {
173 auto sb = dynamic_cast<SpinBoxSlider64 *>(editor);
174 if (sb)
175 return QVariant(sb->Value());
176 }
177
178 return QVariant();
179 }
180
181 QVariant Value(int column, int role = Qt::UserRole) override
182 {
183 switch (role)
184 {
185 case Qt::DisplayRole:
186 {
187 if (column == 0)
188 return Data(column, role);
189
190 auto intNode = std::dynamic_pointer_cast<IntegerNode>(Node());
191 if (intNode && intNode->IsAvailable())
192 {
193 int64_t intValue = -1;
194 try
195 {
196 intValue = intNode->Value();
197 }
198 catch (const CvbException &e)
199 {
200 if (e.ErrorCode() == ErrorCodes::CVB_TIMEOUT)
201 TimeoutOccurred(); // send signal
202 return QVariant("-");
203 }
204
205 auto rep = intNode->Representation();
206 if (rep == GenApi::NumberRepresentation::IPv4 || ipList_.contains(UI::CvbToQt(intNode->Name())))
207 {
208 IpAddress ip(intValue);
209 return ip.ToQString();
210 }
211 else if (rep == GenApi::NumberRepresentation::MAC || macList_.contains(UI::CvbToQt(intNode->Name())))
212 {
213 MacAddress mac(intValue);
214 return mac.ToQString();
215 }
216 else
217 {
218 return QVariant::fromValue(intValue);
219 }
220 }
221 }
222 }
223 return QVariant();
224 }
225
226 void SetValue(const QVariant &value) override
227 {
228 if (auto intNode = std::dynamic_pointer_cast<IntegerNode>(Node()))
229 {
230 auto rep = intNode->Representation();
231 if (rep == GenApi::NumberRepresentation::IPv4 || ipList_.contains(UI::CvbToQt(intNode->Name())))
232 {
233 if (value.type() == QVariant::String)
234 {
235 IpAddress ip(value.toString());
236 try
237 {
238 intNode->SetValue(ip.ToInt64());
239 }
240 catch (const CvbException &)
241 {
242 // ignore the exception
243 }
244 }
245 }
246 else if (rep == GenApi::NumberRepresentation::MAC || macList_.contains(UI::CvbToQt(intNode->Name())))
247 {
248 if (value.type() == QVariant::String)
249 {
250 MacAddress mac(value.toString());
251 try
252 {
253 intNode->SetValue(mac.ToInt64());
254 }
255 catch (const CvbException &)
256 {
257 // ignore the exception
258 }
259 }
260 }
261 else
262 {
263 if (value.type() == QVariant::LongLong)
264 {
265 try
266 {
267 intNode->SetValue(value.toLongLong());
268 }
269 catch (const CvbException &)
270 {
271 // ignore the exception
272 }
273 }
274 }
275 }
276 }
277
278 QString HtmlDescription() override
279 {
280 if (auto intNode = std::dynamic_pointer_cast<IntegerNode>(Node()))
281 {
282 auto txt = QString("<br><table>");
283 auto min = QString("-");
284 auto max = QString("-");
285 auto rep = QString("-");
286 auto inc = QString("");
287
288 auto representation = intNode->Representation();
289 if (representation != GenApi::NumberRepresentation::IPv4 && ipList_.contains(UI::CvbToQt(intNode->Name())))
290 rep = UI::CvbToQt(NodeRepresentation(GenApi::NumberRepresentation::IPv4));
291 else if (representation != GenApi::NumberRepresentation::MAC
292 && macList_.contains(UI::CvbToQt(intNode->Name())))
293 rep = UI::CvbToQt(NodeRepresentation(GenApi::NumberRepresentation::MAC));
294
295 inc = QString::number(intNode->Increment());
296 min = QString::number(intNode->Min());
297 max = QString::number(intNode->Max());
298
299 txt.append("<tr><td><b>Minimum:</b></td><td align=\"right\">" + min + "</td></tr>");
300 txt.append("<tr><td><b>Maximum:</b></td><td align=\"right\">" + max + "</td></tr>");
301 if (!inc.isEmpty())
302 txt.append("<tr><td><b>Increment:</b></td><td align=\"right\">" + inc + "</td></tr>");
303 txt.append("<tr><td><b>Representation:</b></td><td align=\"right\">" + rep + "</td></tr>");
304 txt.append("</table>");
305 return txt;
306 }
307 return QString();
308 }
309
310 private:
311 QStringList ipList_;
312 QStringList macList_;
313
314 }; /* class IntegerProperty */
315
316 } /* namespace Private */
317 } /* namespace UI */
318
319 CVB_END_INLINE_NS
320
321} /* namespace Cvb */
T max(T... args)
T min(T... args)
const int CVB_TIMEOUT
Timeout in function.
Definition exception.hpp:29
std::shared_ptr< IntegerNode > IntegerNodePtr
Convenience shared pointer for IntegerNode.
Definition genapi.hpp:63
std::shared_ptr< ValueNode > ValueNodePtr
Convenience shared pointer for ValueNode.
Definition genapi.hpp:91
@ MAC
MAC address in an edit control.
Definition genapi.hpp:159
@ IPv4
IPv4 address in an edit control.
Definition genapi.hpp:157
Namespace for user interface components.
Definition decl_image_scene.hpp:39
QString CvbToQt(const Cvb::String &text) noexcept
Convenience converter for strings.
Definition ui.hpp:257
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17