CVB++ 15.0
detail_label_slider.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 <QFrame>
8#include <QSlider>
9#include <QToolButton>
10#include <QLabel>
11#include <QGridLayout>
12#pragma warning(pop)
13
14#include "../../global.hpp"
15#include "../../utilities/system_info.hpp"
16
17namespace Cvb
18{
19
20 CVB_BEGIN_INLINE_NS
21
22 namespace UI
23 {
24 namespace Private
25 {
26
27 class LabelSlider : public QFrame
28 {
29
30 Q_OBJECT
31
32 public:
33 explicit LabelSlider(Qt::Orientation orientation = Qt::Vertical, bool enableButtons = false,
34 QWidget *parent = 0)
35 : QFrame(parent)
36 , buttonSize_(15, 15)
37 , boldLabel_(false)
38 {
39 auto layout = new QGridLayout(this); // NOLINT(cppcoreguidelines-owning-memory)
40
41 slider_ = new QSlider; // NOLINT
42 slider_->setOrientation(orientation);
43
44 QObject::connect(
45 slider_, &QSlider::sliderReleased, this,
46 [=]() {
47 SetValue(slider_->value());
48 ValueChanged(slider_->value()); // send signal
49 },
50 Qt::QueuedConnection);
51
52 maxLabel_ =
53 new QLabel(QString::number(std::numeric_limits<int>::max())); // NOLINT(cppcoreguidelines-owning-memory)
54 minLabel_ =
55 new QLabel(QString::number(std::numeric_limits<int>::min())); // NOLINT(cppcoreguidelines-owning-memory)
56
57 auto upButton_ = new QToolButton; // NOLINT
58 upButton_->setArrowType(Qt::UpArrow);
59 upButton_->setMaximumSize(buttonSize_);
60
61 auto downButton_ = new QToolButton; // NOLINT
62 downButton_->setArrowType(Qt::DownArrow);
63 downButton_->setMaximumSize(buttonSize_);
64
65 QObject::connect(
66 upButton_, &QToolButton::clicked, this,
67 [=]() {
68 auto value = slider_->value();
69 value += slider_->singleStep();
70 if (value > Maximum())
71 value = Maximum();
72 SetValue(value);
73 },
74 Qt::QueuedConnection);
75
76 QObject::connect(
77 downButton_, &QToolButton::clicked, this,
78 [=]() {
79 auto value = slider_->value();
80 value -= slider_->singleStep();
81 if (value < Minimum())
82 value = Minimum();
83 SetValue(value);
84 },
85 Qt::QueuedConnection);
86
87 layout->addWidget(maxLabel_, 0, 0, Qt::AlignRight);
88 layout->addWidget(slider_, 1, 1, Qt::AlignCenter);
89 layout->addWidget(minLabel_, 2, 0, Qt::AlignRight);
90
91 if (enableButtons)
92 {
93 layout->addWidget(upButton_, 0, 1, Qt::AlignCenter);
94 layout->addWidget(downButton_, 2, 1, Qt::AlignCenter);
95 }
96 }
97
98 void SetMinimum(int value)
99 {
100 slider_->setMinimum(value);
101 SetLabel(minLabel_, QString::number(value));
102 }
103
104 void SetMaximum(int value)
105 {
106 slider_->setMaximum(value);
107 SetLabel(maxLabel_, QString::number(value));
108 }
109
110 void SetValue(int value)
111 {
112 // Get a valid value (depending on singleStep)
113 value = ValidValue(value);
114
115 // Set the value
116 if (slider_->value() != value)
117 slider_->setValue(value);
118
119 ValueChanged(value); // send signal
120 }
121
122 void SetSingleStep(int value)
123 {
124 slider_->setSingleStep(value);
125 }
126
127 int Minimum()
128 {
129 return slider_->minimum();
130 }
131
132 int Maximum()
133 {
134 return slider_->maximum();
135 }
136
137 int Value()
138 {
139 return slider_->value();
140 }
141
142 int SingleStep()
143 {
144 return slider_->singleStep();
145 }
146
147 void SetBoldLabel(bool value)
148 {
149 boldLabel_ = value;
150 SetLabel(maxLabel_, maxLabel_->text());
151 SetLabel(minLabel_, minLabel_->text());
152 }
153
154 Q_SIGNALS:
155
156 void ValueChanged(int value);
157
158 private:
159 void SetLabel(QLabel *label, const QString &text)
160 {
161 if (boldLabel_)
162 label->setText("<b>" + text + "</b>");
163 else
164 label->setText(text);
165 }
166
167 int ValidValue(int value)
168 {
169 if (value == Maximum())
170 {
171 while (value % SingleStep() != 0)
172 --value;
173 return value;
174 }
175 else if (value == Minimum())
176 {
177 while (value % SingleStep() != 0)
178 ++value;
179 return value;
180 }
181
182 auto modulo = value % SingleStep();
183 if (modulo != 0)
184 {
185 // move to upper value
186 if (modulo >= (SingleStep() / 2))
187 {
188 while (value % SingleStep() != 0)
189 ++value;
190 }
191 // move to lower value
192 else
193 {
194 while (value % SingleStep() != 0)
195 --value;
196 }
197 }
198
199 return value;
200 }
201
202 private:
203 QSlider *slider_;
204 QLabel *maxLabel_;
205 QLabel *minLabel_;
206 QSize buttonSize_;
207 bool boldLabel_;
208
209 }; /* class ValueScrollBar */
210
211 } /* namespace Private */
212 } /* namespace UI */
213
214 CVB_END_INLINE_NS
215
216} /* namespace Cvb */
T max(T... args)
T min(T... args)
Namespace for user interface components.
Definition decl_image_scene.hpp:39
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17