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 "Vector3DSpinBox.h"
|
---|
6 | #include <QLineEdit>
|
---|
7 | #include <QMenu>
|
---|
8 | #include <QContextMenuEvent>
|
---|
9 | #include <QFormLayout>
|
---|
10 | #include <QGroupBox>
|
---|
11 | #include "Layout.h"
|
---|
12 |
|
---|
13 | Vector3DSpinBox::Vector3DSpinBox(Layout *parent, int row, int col, QString name,
|
---|
14 | QString value[3], float min, float max,
|
---|
15 | float step, int decimals)
|
---|
16 | : XmlWidget(name, "Vector3DSpinBox", parent) {
|
---|
17 | for (int i = 0; i < 3; i++) {
|
---|
18 | doublespinbox[i].setRange(min, max);
|
---|
19 | doublespinbox[i].setSingleStep(step);
|
---|
20 | doublespinbox[i].setDecimals(decimals);
|
---|
21 | }
|
---|
22 |
|
---|
23 | adjust_decimals(value);
|
---|
24 |
|
---|
25 | for (int i = 0; i < 3; i++) {
|
---|
26 | doublespinbox[i].setValue(value[i].toDouble());
|
---|
27 | doublespinbox_value[i] = doublespinbox[i].cleanText();
|
---|
28 |
|
---|
29 | // event filter for qdoublespinbox and its child (qlinedit and incremnt
|
---|
30 | // qbuttons)
|
---|
31 | doublespinbox[i].installEventFilter(this);
|
---|
32 | QObjectList o_list = doublespinbox[i].children();
|
---|
33 | for (int j = 0; j < o_list.length(); j++) {
|
---|
34 | QLineEdit *cast = qobject_cast<QLineEdit *>(o_list[j]);
|
---|
35 | if (cast)
|
---|
36 | cast->installEventFilter(this);
|
---|
37 | }
|
---|
38 |
|
---|
39 | connect(&doublespinbox[i], SIGNAL(valueChanged(const QString &)), this,
|
---|
40 | SLOT(valuechanged(const QString &)));
|
---|
41 | }
|
---|
42 |
|
---|
43 | SetValues(value);
|
---|
44 |
|
---|
45 | // creation et ajout QGroupBox
|
---|
46 | box = new QGroupBox(name);
|
---|
47 | box->setObjectName(name);
|
---|
48 | visible_widget = box;
|
---|
49 | parent->addWidget(box, row, col);
|
---|
50 | qgridlayout = new QGridLayout(new QWidget());
|
---|
51 | box->setLayout(qgridlayout);
|
---|
52 |
|
---|
53 | AddElement("x:", 0);
|
---|
54 | AddElement("y:", 1);
|
---|
55 | AddElement("z:", 2);
|
---|
56 | }
|
---|
57 |
|
---|
58 | Vector3DSpinBox::~Vector3DSpinBox() {}
|
---|
59 |
|
---|
60 | void Vector3DSpinBox::SetValues(QString value[3]) {
|
---|
61 | SetAttribute("value_x", value[0]);
|
---|
62 | SetAttribute("value_y", value[1]);
|
---|
63 | SetAttribute("value_z", value[2]);
|
---|
64 | }
|
---|
65 |
|
---|
66 | void Vector3DSpinBox::AddElement(QString name, int index) {
|
---|
67 | QWidget *widget = new QWidget();
|
---|
68 | QFormLayout *object_layout = new QFormLayout(widget);
|
---|
69 | object_layout->setHorizontalSpacing(2);
|
---|
70 | object_layout->setVerticalSpacing(2);
|
---|
71 | object_layout->setContentsMargins(2, 2, 2, 2);
|
---|
72 |
|
---|
73 | object_layout->addRow(name, &doublespinbox[index]);
|
---|
74 |
|
---|
75 | qgridlayout->addWidget(widget, index, 0);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void Vector3DSpinBox::adjust_decimals(QString value[3]) {
|
---|
79 | for (int i = 0; i < 3; i++) {
|
---|
80 | // auto adjust decimals
|
---|
81 | QLocale locale;
|
---|
82 | value[i].remove(locale.groupSeparator());
|
---|
83 |
|
---|
84 | QStringList parts = value[i].split(locale.decimalPoint());
|
---|
85 | if (parts.count() == 2) {
|
---|
86 | doublespinbox[i].setDecimals(parts[1].size());
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool Vector3DSpinBox::eventFilter(QObject *object, QEvent *event) {
|
---|
92 | for (int i = 0; i < 3; i++) {
|
---|
93 | if (object == &doublespinbox[i] &&
|
---|
94 | event->type() == QEvent::MouseButtonPress) {
|
---|
95 | if (((QMouseEvent *)event)->button() == Qt::RightButton) {
|
---|
96 | QMenu *menu = new QMenu("menu", doublespinbox);
|
---|
97 | QAction *a, *b, *c, *z;
|
---|
98 |
|
---|
99 | a = menu->addAction("add decimal");
|
---|
100 | b = menu->addAction("remove decimal");
|
---|
101 |
|
---|
102 | if (doublespinbox[i].decimals() == 0)
|
---|
103 | b->setEnabled(false);
|
---|
104 | z = menu->exec(((QMouseEvent *)event)->globalPos());
|
---|
105 |
|
---|
106 | if (z == a)
|
---|
107 | doublespinbox[i].setDecimals(doublespinbox[i].decimals() + 1);
|
---|
108 | if (z == b)
|
---|
109 | doublespinbox[i].setDecimals(doublespinbox[i].decimals() - 1);
|
---|
110 | delete menu;
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | return object->eventFilter(object, event);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Vector3DSpinBox::SetUptodate(void) {
|
---|
119 | ui_to_var();
|
---|
120 | ui_to_xml();
|
---|
121 | visible_widget->setPalette(black_pal);
|
---|
122 | }
|
---|
123 |
|
---|
124 | void Vector3DSpinBox::ui_to_var(void) {
|
---|
125 | for (int i = 0; i < 3; i++) {
|
---|
126 | doublespinbox_value[i] = doublespinbox[i].cleanText();
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | void Vector3DSpinBox::ui_to_xml(void) {
|
---|
131 | SetAttribute("value_x", doublespinbox[0].cleanText());
|
---|
132 | SetAttribute("value_y", doublespinbox[1].cleanText());
|
---|
133 | SetAttribute("value_z", doublespinbox[2].cleanText());
|
---|
134 | }
|
---|
135 |
|
---|
136 | void Vector3DSpinBox::Reset(void) {
|
---|
137 | // le setvalue fait un arrondi pour l'affichage, la variable n'est donc plus
|
---|
138 | // egale
|
---|
139 | // on reprend la valeur de la boite et on force la couleur a noir
|
---|
140 | adjust_decimals(doublespinbox_value);
|
---|
141 | for (int i = 0; i < 3; i++) {
|
---|
142 | doublespinbox[i].setValue(doublespinbox_value[i].toDouble());
|
---|
143 | doublespinbox_value[i] = doublespinbox[i].cleanText();
|
---|
144 | }
|
---|
145 | visible_widget->setPalette(black_pal);
|
---|
146 | }
|
---|
147 |
|
---|
148 | void Vector3DSpinBox::LoadEvent(QDomElement *dom) {
|
---|
149 | if (doublespinbox[0].isEnabled()) {
|
---|
150 | doublespinbox[0].setValue((dom->attribute("value_x")).toDouble());
|
---|
151 | }
|
---|
152 | if (doublespinbox[1].isEnabled()) {
|
---|
153 | doublespinbox[1].setValue((dom->attribute("value_y")).toDouble());
|
---|
154 | }
|
---|
155 | if (doublespinbox[2].isEnabled()) {
|
---|
156 | doublespinbox[2].setValue((dom->attribute("value_z")).toDouble());
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | void Vector3DSpinBox::valuechanged(const QString &value) {
|
---|
161 | for (int i = 0; i < 3; i++) {
|
---|
162 | if ((QDoubleSpinBox *)sender() == &doublespinbox[i] &&
|
---|
163 | value != doublespinbox_value[i]) {
|
---|
164 | visible_widget->setPalette(red_pal);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | visible_widget->setPalette(black_pal);
|
---|
169 | }
|
---|
170 |
|
---|
171 | bool Vector3DSpinBox::IsUptodate(void) {
|
---|
172 | // si le widget n'est pas enabled, sa palette est differente de rouge (greyed)
|
---|
173 | // donc on renvoit true
|
---|
174 | // permet de ne pas envoyer les modifs d'un widget disabled
|
---|
175 | // if(label->palette()==red_pal) return false;
|
---|
176 | if (visible_widget->palette() == red_pal)
|
---|
177 | return false;
|
---|
178 | return true;
|
---|
179 | }
|
---|