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

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

gcs

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