PeriDyno 1.2.1
Loading...
Searching...
No Matches
QPiecewiseDoubleSpinBox.h
Go to the documentation of this file.
1
19#pragma once
20//Qt
21#include <QGroupBox>
22#include <QPushButton>
23#include <QSpinBox>
24#include <QDialog>
25#include <QLineEdit>
26#include <QMouseEvent>
27#include <QWheelEvent>
28#include <QLabel>
29#include <QVBoxLayout>
30
31//PeriDyno
32#include "Format.h"
33#include "FCallBackFunc.h"
34#include "QtGUI/Common.h"
35#include "Core/Vector.h"
36
37//C++
38#include <memory>
39#include <sstream>
40
41namespace dyno
42{
43 class Node;
44 class Module;
45 class FBase;
46
47 class QDoubleSpinner;
48 class QDoubleSlider;
50 class QValueDialog;
51
52 class QPiecewiseDoubleSpinBox : public QDoubleSpinBox
53 {
54 Q_OBJECT
55 public:
56 explicit QPiecewiseDoubleSpinBox(QWidget* parent = nullptr);
57
58 QPiecewiseDoubleSpinBox(Real v, QWidget* parent = nullptr);
59
60 double getRealValue()
61 {
62 return realValue;
63 }
64
65
66 QLineEdit* getLineEdit()
67 {
68 return this->lineEdit();
69 }
70
72
73
74
75 private:
76 //Prohibited to use
77 void wheelEvent(QWheelEvent* event);
78
79 void mousePressEvent(QMouseEvent* event) override;
80
81 void mouseReleaseEvent(QMouseEvent* event) override;
82
83 void mouseMoveEvent(QMouseEvent* event) override;
84
85 void contextMenuEvent(QContextMenuEvent* event) override;
86
87
88
89 protected:
90
91 void stepBy(int steps) override
92 {
93 double val = realValue;
94 double step = singleStep();
95
96 double newVal = val + steps * step;
97
98 if (newVal > maximum())
99 newVal = maximum();
100 else if (newVal < minimum())
101 newVal = minimum();
102
104 }
105
106 QValidator::State validate(QString& input, int& pos) const override
107 {
108 Q_UNUSED(pos);
109
110 if (input.isEmpty() || input == "-" || input == "+" || input == "." || input == "-." || input == "+.")
111 {
112 return QValidator::Intermediate;
113 }
114
115 bool ok = false;
116 double val = input.toDouble(&ok);
117 if (!ok)
118 {
119 return QValidator::Invalid;
120 }
121
122 if (val < minimum() || val > maximum())
123 {
124 return QValidator::Intermediate;
125 }
126
127 return QValidator::Acceptable;
128 }
129
130 void fixup(QString& input) const override
131 {
132 bool ok = false;
133 double val = input.toDouble(&ok);
134 if (!ok)
135 return;
136
137 if (val < minimum())
138 input = QString::number(minimum(), 'g', decimalsMax);
139 else if (val > maximum())
140 input = QString::number(maximum(), 'g', decimalsMax);
141 }
142
143 virtual QString textFromValue(double val) const override
144 {
145 auto qstr = QString::number(realValue, 10, displayDecimals);
146
147 return qstr;
148 }
149
150 void focusOutEvent(QFocusEvent* event) override
151 {
152 interpretText();
154 QDoubleSpinBox::focusOutEvent(event);
155 }
156
157 virtual double valueFromText(const QString& text) const override
158 {
159 if (istoggle)
160 {
161 return realValue;
162 }
163 else
164 {
165 return text.toDouble();
166 }
167 }
168
169
170 bool eventFilter(QObject* obj, QEvent* event) override;
171
172 void createValueDialog();
173
174 public:
175
176 signals:
177
178 void editingFinishedWithValue(double value);
179 public slots:
180
181 double setRealValue(double val);
182
184 {
185 this->setRealValue(this->value());
187 }
188
189 void triggerEditingFinished(double value)
190 {
191 this->setValue(value);
192 this->interpretText();
193 this->setRealValue(value);
194 emit editingFinished();
195 }
196
197 void toggleDecimals(bool v)
198 {
199 if (v)
201 else
203
204 istoggle = true;
205
206 auto t = QString::number(realValue, 10, displayDecimals);
207 this->lineEdit()->setText(t);
208
209 istoggle = false;
210 }
211
212
213
214 private:
215 int decimalsMin = 3;
216 int decimalsMax = 8;
218 double realValue = 0;
219 bool istoggle = false;
220 };
221
222
223 class QToggleLabel : public QLabel
224 {
225 Q_OBJECT
226 public:
227
228 explicit QToggleLabel(QWidget* parent = nullptr)
229 : QLabel(parent)
230 {
231
232 }
233 explicit QToggleLabel(std::string text,QWidget* parent = nullptr)
234 : QLabel(parent)
235 {
236 this->setText(QString(text.c_str()));
237 }
238
239 Q_SIGNALS:
240 void toggle(bool high);
241
242 protected:
243 void mousePressEvent(QMouseEvent* event) override
244 {
245 current = !current;
246 emit toggle(current);
247 }
248
249
250
251 private:
252
253 bool current = false;
254 };
255
256
257 class mVec3fWidget : public QWidget
258 {
259 Q_OBJECT
260 public:
261
262 explicit mVec3fWidget(Vec3f v,std::string name,QWidget* parent = nullptr)
263 {
264 this->setContentsMargins(0, 0, 0, 0);
265 QHBoxLayout* layout = new QHBoxLayout;
266
267 nameLabel = new QToggleLabel(name.c_str());
268
269 nameLabel->setMinimumWidth(90);
270
271 this->setLayout(layout);
272
273 v0 = new QPiecewiseDoubleSpinBox(parent);
274 v1 = new QPiecewiseDoubleSpinBox(parent);
275 v2 = new QPiecewiseDoubleSpinBox(parent);
276
277 setRange(-999999,999999);
278
279 v0->setValue(v[0]);
280 v1->setValue(v[1]);
281 v2->setValue(v[2]);
282
283 v0->setMinimumWidth(90);
284 v1->setMinimumWidth(90);
285 v2->setMinimumWidth(90);
286
287 layout->addWidget(nameLabel);
288 layout->addWidget(v0);
289 layout->addWidget(v1);
290 layout->addWidget(v2);
291
292 QObject::connect(v0, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double value) {emit vec3fChange(); });
293 QObject::connect(v1, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double value) {emit vec3fChange(); });
294 QObject::connect(v2, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double value) {emit vec3fChange(); });
295
296 QObject::connect(nameLabel, SIGNAL(toggle(bool)), v0, SLOT(toggleDecimals(bool)));
297 QObject::connect(nameLabel, SIGNAL(toggle(bool)), v1, SLOT(toggleDecimals(bool)));
298 QObject::connect(nameLabel, SIGNAL(toggle(bool)), v2, SLOT(toggleDecimals(bool)));
299 }
300
302 {
303 delete v0;
304 delete v1;
305 delete v2;
306 }
307
309 {
310 return Vec3f(v0->getRealValue(), v1->getRealValue(), v2->getRealValue());
311 };
312
314 {
315 nameLabel->setMinimumWidth(max);
316 }
317
318 void setRange(double min,double max)
319 {
320 v0->setRange(min, max);
321 v1->setRange(min, max);
322 v2->setRange(min, max);
323 }
324
325 void setRange(double min0, double max0, double min1, double max1, double min2, double max2)
326 {
327 v0->setRange(min0, max0);
328 v1->setRange(min1, max1);
329 v2->setRange(min2, max2);
330 }
331
332 Q_SIGNALS:
333
335
336 protected:
337
338 private:
339
343
345
346 };
347
348
349
350 class mPiecewiseDoubleSpinBox : public QWidget
351 {
352 Q_OBJECT
353 public:
354
355 explicit mPiecewiseDoubleSpinBox(double value, std::string name, QWidget* parent = nullptr)
356 {
357 this->setContentsMargins(0, 0, 0, 0);
358 QHBoxLayout* layout = new QHBoxLayout;
359
360 nameLabel = new QToggleLabel(name.c_str());
361 nameLabel->setMinimumWidth(90);
362
363 this->setLayout(layout);
364
365 spinBox = new QPiecewiseDoubleSpinBox(parent);
366 spinBox->setValue(value);
367
368 spinBox->setRange(-99999999, 99999999);
369
370 layout->addWidget(nameLabel);
371 layout->addWidget(spinBox);
372
373 QObject::connect(spinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double value) {emit valueChange(); });
374
375 QObject::connect(nameLabel, SIGNAL(toggle(bool)), spinBox, SLOT(toggleDecimals(bool)));
376
377 }
378
380 {
381 delete spinBox;
382 delete nameLabel;
383 }
384
385 double getValue()
386 {
387 return spinBox->getRealValue();
388 };
389
391 {
392 nameLabel->setMinimumWidth(max);
393 }
394
395 void setRange(double min,double max)
396 {
397 spinBox->setRange(min,max);
398 }
399
400 Q_SIGNALS:
401
403
404 protected:
405
406 private:
407
409
411 };
412
413
414
415 class QVec3fWidget : public QWidget
416 {
417 Q_OBJECT
418 public:
419
420 explicit QVec3fWidget(Vec3f v, QWidget* parent = nullptr)
421 {
422 this->setContentsMargins(0, 0, 0, 0);
423 QHBoxLayout* layout = new QHBoxLayout;
424
425 this->setLayout(layout);
426
427 v0 = new QPiecewiseDoubleSpinBox(parent);
428 v1 = new QPiecewiseDoubleSpinBox(parent);
429 v2 = new QPiecewiseDoubleSpinBox(parent);
430
431 v0->setValue(v[0]);
432 v1->setValue(v[1]);
433 v2->setValue(v[2]);
434
435
436 layout->addWidget(v0);
437 layout->addWidget(v1);
438 layout->addWidget(v2);
439
440 QObject::connect(v0, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double value) {emit vec3fChange(); });
441 QObject::connect(v1, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double value) {emit vec3fChange(); });
442 QObject::connect(v2, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=](double value) {emit vec3fChange(); });
443
444 }
445
447 {
448 delete v0;
449 delete v1;
450 delete v2;
451 }
452
454 {
455 return Vec3f(v0->getRealValue(), v1->getRealValue(), v2->getRealValue());
456 };
457
458
459
460 Q_SIGNALS:
461
463
464 protected:
465
466 private:
467
471
472
473 };
474
475
476}
double Real
Definition Typedef.inl:23
void fixup(QString &input) const override
QPiecewiseDoubleSpinBox(QWidget *parent=nullptr)
void focusOutEvent(QFocusEvent *event) override
void contextMenuEvent(QContextMenuEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
bool eventFilter(QObject *obj, QEvent *event) override
void mouseReleaseEvent(QMouseEvent *event) override
void editingFinishedWithValue(double value)
virtual QString textFromValue(double val) const override
virtual double valueFromText(const QString &text) const override
QValidator::State validate(QString &input, int &pos) const override
QToggleLabel(QWidget *parent=nullptr)
void mousePressEvent(QMouseEvent *event) override
QToggleLabel(std::string text, QWidget *parent=nullptr)
void toggle(bool high)
QPiecewiseDoubleSpinBox * v2
QVec3fWidget(Vec3f v, QWidget *parent=nullptr)
QPiecewiseDoubleSpinBox * v1
QPiecewiseDoubleSpinBox * v0
void setRange(double min, double max)
mPiecewiseDoubleSpinBox(double value, std::string name, QWidget *parent=nullptr)
void setRange(double min0, double max0, double min1, double max1, double min2, double max2)
void setRange(double min, double max)
QPiecewiseDoubleSpinBox * v0
mVec3fWidget(Vec3f v, std::string name, QWidget *parent=nullptr)
QPiecewiseDoubleSpinBox * v2
QPiecewiseDoubleSpinBox * v1
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
DYN_FUNC T minimum(const T &v0, const T &v1)
Definition SimpleMath.h:120
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
DYN_FUNC T maximum(const T &v0, const T &v1)
Definition SimpleMath.h:160
#define max(x, y)
Definition svd3_cuda.h:41