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

Last change on this file since 9 was 9, checked in by Sanahuja Guillaume, 7 years ago

gcs

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