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

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

lic

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