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