CVB++ 15.0
Loading...
Searching...
No Matches
detail_float_property.hpp
1#pragma once
2
3#pragma warning(push)
4#pragma warning(disable : 4800)
5#pragma warning(disable : 4251)
6#pragma warning(disable : 4244)
7#include <QDoubleSpinBox>
8#include <QEvent>
9#include <QKeyEvent>
10#pragma warning(pop)
11
12#include "../../global.hpp"
13#include "../../genapi/value_node.hpp"
14#include "../../genapi/float_node.hpp"
15#include "detail_property.hpp"
16
17namespace Cvb
18{
19
20 CVB_BEGIN_INLINE_NS
21
22 namespace UI
23 {
24 namespace Private
25 {
26
27 class FloatProperty : public Private::Property
28 {
29
30 Q_OBJECT
31
32 public:
33 FloatProperty(const ValueNodePtr &node, Property *parent)
34 : Property(node, PT_Float, parent)
35 {
36 }
37
38 QWidget *CreateEditor(QWidget *parent) override
39 {
40 auto floatNode = std::dynamic_pointer_cast<FloatNode>(Node());
41 if (floatNode && floatNode->IsAvailable())
42 {
43 auto editor = new QDoubleSpinBox(parent); // NOLINT(cppcoreguidelines-owning-memory)
44 editor->setDecimals(6);
45 editor->setMinimum(floatNode->Min());
46 editor->setMaximum(floatNode->Max());
47 editor->setValue(floatNode->Value());
48 editor->setSingleStep(0.1);
49
50 QObject::connect(editor, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this,
51 [=](double value) { SetValue(QVariant(value)); });
52
53 return editor;
54 }
55 return EmptyEditor();
56 }
57
58 bool SetEditorData(QWidget *editor, const QVariant &data) override
59 {
60 if (auto dsb = qobject_cast<QDoubleSpinBox *>(editor))
61 {
62 dsb->setValue(data.toDouble());
63 return true;
64 }
65 return false;
66 }
67
68 QVariant EditorData(QWidget *editor) override
69 {
70 if (auto dsb = qobject_cast<QDoubleSpinBox *>(editor))
71 {
72 return QVariant(dsb->value());
73 }
74 else
75 {
76 return QVariant();
77 }
78 }
79
80 QVariant Value(int column, int role = Qt::UserRole) override
81 {
82 switch (role)
83 {
84 case Qt::DisplayRole:
85 {
86 if (column == 0)
87 return Data(column, role);
88
89 auto floatNode = std::dynamic_pointer_cast<FloatNode>(Node());
90 if (floatNode && floatNode->IsAvailable())
91 {
92 try
93 {
94 return QVariant::fromValue(floatNode->Value());
95 }
96 catch (const CvbException &e)
97 {
98 if (e.ErrorCode() == ErrorCodes::CVB_TIMEOUT)
99 TimeoutOccurred(); // send signal
100 }
101 }
102 return QVariant("-");
103 }
104 }
105 return QVariant();
106 }
107
108 void SetValue(const QVariant &value) override
109 {
110 if (auto floatNode = std::dynamic_pointer_cast<FloatNode>(Node()))
111 {
112 try
113 {
114 floatNode->SetValue(value.toFloat());
115 }
116 catch (const CvbException &)
117 {
118 // ignore the exception
119 }
120 }
121 }
122
123 QString HtmlDescription() override
124 {
125 if (auto floatNode = std::dynamic_pointer_cast<Cvb::FloatNode>(Node()))
126 {
127 auto txt = QString("<br><table>");
128 auto min = QString("-");
129 auto max = QString("-");
130 auto rep = QString("-");
131 auto unit = QString("");
132
133 rep = UI::CvbToQt(NodeRepresentation(floatNode->Representation()));
134 unit = UI::CvbToQt(floatNode->Unit());
135
136 min = QString::number(floatNode->Min());
137 max = QString::number(floatNode->Max());
138
139 if (!unit.isEmpty())
140 txt.append("<tr><td><b>Unit:</b></td><td align=\"right\">" + unit + "</td></tr>");
141 txt.append("<tr><td><b>Minimum:</b></td><td align=\"right\">" + min + "</td></tr>");
142 txt.append("<tr><td><b>Maximum:</b></td><td align=\"right\">" + max + "</td></tr>");
143 txt.append("<tr><td><b>Representation:</b></td><td align=\"right\">" + rep + "</td></tr>");
144 txt.append("</table>");
145 return txt;
146 }
147 return QString();
148 }
149
150 }; /* class FloatProperty */
151
152 } /* namespace Private */
153 } /* namespace UI */
154
155 CVB_END_INLINE_NS
156
157} /* namespace Cvb */
T max(T... args)
T min(T... args)
const int CVB_TIMEOUT
Timeout in function.
Definition exception.hpp:29
std::shared_ptr< ValueNode > ValueNodePtr
Convenience shared pointer for ValueNode.
Definition genapi.hpp:91
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 version.hpp:11