source: flair-src/trunk/tools/FlairGCS/src/Vector3DSpinBox.cpp@ 15

Last change on this file since 15 was 15, checked in by Bayard Gildas, 9 years ago

sources reformatted with flair-format-dir script

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