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