1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | #include "DoubleSpinBox.h"
|
---|
6 | #include "Layout.h"
|
---|
7 | #include <QLineEdit>
|
---|
8 | #include <QMenu>
|
---|
9 | #include <QContextMenuEvent>
|
---|
10 | #include <QDoubleSpinBox>
|
---|
11 | #include <QFormLayout>
|
---|
12 |
|
---|
13 | DoubleSpinBox::DoubleSpinBox(Layout *parent, int row, int col, QString name,
|
---|
14 | QString suffix, QString value, float min,
|
---|
15 | float max, float step, int decimals)
|
---|
16 | : FormLayout(parent, row, col, name, "DoubleSpinBox") {
|
---|
17 | doublespinbox = new QDoubleSpinBox();
|
---|
18 |
|
---|
19 | doublespinbox->setRange(min, max);
|
---|
20 | doublespinbox->setSingleStep(step);
|
---|
21 | doublespinbox->setDecimals(decimals);
|
---|
22 | if (suffix != "")
|
---|
23 | doublespinbox->setSuffix(suffix);
|
---|
24 | adjust_decimals(value);
|
---|
25 | doublespinbox->setValue(value.toDouble());
|
---|
26 | doublespinbox_value = doublespinbox->cleanText();
|
---|
27 |
|
---|
28 | // event filter for qdoublespinbox and its child (qlinedit and incremnt
|
---|
29 | // qbuttons)
|
---|
30 | doublespinbox->installEventFilter(this);
|
---|
31 | QObjectList o_list = doublespinbox->children();
|
---|
32 | for (int i = 0; i < o_list.length(); i++) {
|
---|
33 | QLineEdit *cast = qobject_cast<QLineEdit *>(o_list[i]);
|
---|
34 | if (cast)
|
---|
35 | cast->installEventFilter(this);
|
---|
36 | }
|
---|
37 |
|
---|
38 | object_layout->addRow(name, doublespinbox);
|
---|
39 |
|
---|
40 | connect(doublespinbox, SIGNAL(valueChanged(const QString &)), this,
|
---|
41 | SLOT(valuechanged(const QString &)));
|
---|
42 |
|
---|
43 | SetValue(value);
|
---|
44 | }
|
---|
45 |
|
---|
46 | DoubleSpinBox::~DoubleSpinBox() { delete doublespinbox; }
|
---|
47 |
|
---|
48 | void DoubleSpinBox::adjust_decimals(QString value) {
|
---|
49 | // auto adjust decimals
|
---|
50 | QLocale locale;
|
---|
51 | value.remove(locale.groupSeparator());
|
---|
52 |
|
---|
53 | QStringList parts = value.split(locale.decimalPoint());
|
---|
54 | if (parts.count() == 2) {
|
---|
55 | doublespinbox->setDecimals(parts[1].size());
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | bool DoubleSpinBox::eventFilter(QObject *object, QEvent *event) {
|
---|
60 | if (object == doublespinbox && event->type() == QEvent::MouseButtonPress) {
|
---|
61 | if (((QMouseEvent *)event)->button() == Qt::RightButton) {
|
---|
62 | QMenu *menu = new QMenu("menu", doublespinbox);
|
---|
63 | QAction *a, *b, *c, *z;
|
---|
64 |
|
---|
65 | a = menu->addAction("add decimal");
|
---|
66 | b = menu->addAction("remove decimal");
|
---|
67 |
|
---|
68 | if (doublespinbox->decimals() == 0)
|
---|
69 | b->setEnabled(false);
|
---|
70 | z = menu->exec(((QMouseEvent *)event)->globalPos());
|
---|
71 |
|
---|
72 | if (z == a)
|
---|
73 | doublespinbox->setDecimals(doublespinbox->decimals() + 1);
|
---|
74 | if (z == b)
|
---|
75 | doublespinbox->setDecimals(doublespinbox->decimals() - 1);
|
---|
76 | delete menu;
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | return object->eventFilter(object, event);
|
---|
82 | }
|
---|
83 |
|
---|
84 | void DoubleSpinBox::SetUptodate(void) {
|
---|
85 | ui_to_var();
|
---|
86 | ui_to_xml();
|
---|
87 | visible_widget->setPalette(black_pal);
|
---|
88 | }
|
---|
89 |
|
---|
90 | void DoubleSpinBox::ui_to_var(void) {
|
---|
91 | doublespinbox_value = doublespinbox->cleanText();
|
---|
92 | }
|
---|
93 |
|
---|
94 | void DoubleSpinBox::ui_to_xml(void) { SetValue(doublespinbox->cleanText()); }
|
---|
95 |
|
---|
96 | void DoubleSpinBox::Reset(void) {
|
---|
97 | // le setvalue fait un arrondi pour l'affichage, la variable n'est donc plus
|
---|
98 | // egale
|
---|
99 | // on reprend la valeur de la boite et on force la couleur a noir
|
---|
100 | adjust_decimals(doublespinbox_value);
|
---|
101 | doublespinbox->setValue(doublespinbox_value.toDouble());
|
---|
102 | doublespinbox_value = doublespinbox->cleanText();
|
---|
103 | visible_widget->setPalette(black_pal);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void DoubleSpinBox::LoadEvent(QDomElement dom) {
|
---|
107 | if (doublespinbox->isEnabled() == true) {
|
---|
108 | doublespinbox->setValue((dom.attribute("value")).toDouble());
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | void DoubleSpinBox::valuechanged(const QString &value) {
|
---|
113 | if (value != doublespinbox_value) {
|
---|
114 | visible_widget->setPalette(red_pal);
|
---|
115 | } else {
|
---|
116 | visible_widget->setPalette(black_pal);
|
---|
117 | }
|
---|
118 | }
|
---|