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