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