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