source: flair-src/trunk/tools/FlairGCS/src/Vector3DSpinBox.cpp@ 313

Last change on this file since 313 was 269, checked in by Sanahuja Guillaume, 6 years ago

flairgcs:
speed up processing time when receiving datas from uav
triger watchdog while receiving datas from uav
(avoids connection lost in uav)

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