source: flair-src/trunk/tools/FlairGCS/src/Layout.cpp

Last change on this file was 455, checked in by Sanahuja Guillaume, 14 months ago

gcs: add compatibility with programs without nb_buffering

File size: 10.6 KB
Line 
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}
5#include "Layout.h"
6#include "TabWidget.h"
7#include "GroupBox.h"
8#include "GridLayout.h"
9#include "SpinBox.h"
10#include "DoubleSpinBox.h"
11#include "Vector3DSpinBox.h"
12#include "CheckBox.h"
13#include "ComboBox.h"
14#include "PushButton.h"
15#include "TextEdit.h"
16#include "Label.h"
17#include "DataPlot1D.h"
18#include "DataPlot2D.h"
19#include "RangeFinderPlot.h"
20#include "UsSensorPlot.h"
21#include "Picture.h"
22#include "Map.h"
23#include <QGeoCoordinate>
24#include <QGridLayout>
25#include "ListWidget.h"
26#include <QStringList>
27
28Layout::Layout(QWidget *parent, XmlWidget *xml, QString name, QString type)
29 : XmlWidget(name, type, xml) {
30 qgridlayout = new QGridLayout(parent);
31 Constructor(name);
32}
33
34Layout::Layout(Layout *parent, QString name, QString type)
35 : XmlWidget(name, type, parent) {
36 qgridlayout = new QGridLayout(new QWidget());
37 Constructor(name);
38}
39
40void Layout::Constructor(QString name) {
41 visible_widget = qgridlayout->parentWidget();
42 // if(visible_widget==NULL) fprintf(stderr,"null\n");
43 qgridlayout->setObjectName(name);
44 // fprintf(stderr,"layout\n%s\n",XmlDoc().toString().toLocal8Bit().constData());
45 SetIsContainer(true);
46 SetIsExpandable(true);
47
48 qgridlayout->setHorizontalSpacing(2);
49 qgridlayout->setVerticalSpacing(2);
50 qgridlayout->setContentsMargins(2, 2, 2, 2);
51
52 // splitter = new QSplitter();
53 // addWidget(splitter);
54 /*
55 QWidget *widget = new QWidget();
56 //QHBoxLayout *parentLayout = new QHBoxLayout();
57 widget->setLayout(this);
58 parent->addWidget(widget);
59 //QTabWidget *tabWidget = new QTabWidget();
60 //parentLayout->addWidget(tabWidget);*/
61}
62
63Layout::~Layout() { delete qgridlayout; }
64
65void Layout::addWidget(QWidget *widget, int row, int column) {
66 qgridlayout->addWidget(widget, row, column);
67}
68
69QGridLayout *Layout::getQGridLayout() { return qgridlayout; }
70
71void Layout::XmlEvent(QDomElement *dom) {
72 XmlWidget *widget = NULL;
73 QString type = dom->tagName();
74 QString name = dom->attribute("name");
75 QString old_name = dom->attribute("old_name");
76 int row = dom->attribute("row").toInt();
77 int col = dom->attribute("col").toInt();
78
79 // initially rowCount()=1 and columnCount()=1 but count()=0 !
80 // new row
81 if (row == -1 && col == -1) {
82 row = qgridlayout->rowCount();
83 if (qgridlayout->count() == 0)
84 row = 0;
85 col = 0;
86 }
87 // last row last col
88 if (row == -1 && col == 0) {
89 row = qgridlayout->rowCount() - 1;
90 int i;
91 for (i = 0; i <= qgridlayout->columnCount(); i++) {
92 if (qgridlayout->itemAtPosition(row, i) == NULL)
93 break;
94 }
95 col = i;
96 }
97 // if an item already exists at this position, put it on a new row
98 if (qgridlayout->itemAtPosition(row, col) != NULL) {
99 fprintf(stderr,"widget %s: (%i,%i) already exists\n", name.toLocal8Bit().constData(),row, col);
100 row = qgridlayout->rowCount();
101 fprintf(stderr,"using (%i,%i) for %s\n", row, col,name.toLocal8Bit().constData());
102 }
103
104 if (type == "TabWidget") {
105 int position = dom->attribute("position").toInt();
106 widget =
107 new TabWidget(this, row, col, name, (QTabWidget::TabPosition)position);
108 }
109 if (type == "GroupBox") {
110 widget = new GroupBox(this, row, col, name);
111 }
112 if (type == "GridLayout") {
113 widget = new GridLayout(this, row, col, name);
114 }
115 if (type == "SpinBox") {
116 int value = dom->attribute("value").toInt();
117 QString suffix = dom->attribute("suffix");
118 int min = dom->attribute("min").toInt();
119 int max = dom->attribute("max").toInt();
120 int step = dom->attribute("step").toInt();
121 widget = new SpinBox(this, row, col, name, suffix, value, min, max, step);
122 }
123 if (type == "DoubleSpinBox") {
124 QString value = dom->attribute("value");
125 QString suffix = dom->attribute("suffix");
126 double min = dom->attribute("min").toDouble();
127 double max = dom->attribute("max").toDouble();
128 double step = dom->attribute("step").toDouble();
129 uint16_t decimals = dom->attribute("decimals").toUShort();
130 widget = new DoubleSpinBox(this, row, col, name, suffix, value, min, max,
131 step, decimals);
132 }
133 if (type == "Vector3DSpinBox") {
134 QString value[3];
135 value[0] = dom->attribute("value_x");
136 value[1] = dom->attribute("value_y");
137 value[2] = dom->attribute("value_z");
138 double min = dom->attribute("min").toDouble();
139 double max = dom->attribute("max").toDouble();
140 double step = dom->attribute("step").toDouble();
141 uint16_t decimals = dom->attribute("decimals").toUShort();
142 widget = new Vector3DSpinBox(this, row, col, name, value, min, max, step,
143 decimals);
144 }
145 if (type == "CheckBox") {
146 int value = dom->attribute("value").toInt();
147 widget = new CheckBox(this, row, col, name, value);
148 }
149 if (type == "ComboBox") {
150 uint16_t value = dom->attribute("value").toUShort();
151 widget = new ComboBox(this, row, col, name, value);
152 }
153 if (type == "PushButton") {
154 widget = new PushButton(this, row, col, name);
155 }
156 if (type == "DataPlot1D") {
157 float ymin = dom->attribute("min").toFloat();
158 float ymax = dom->attribute("max").toFloat();
159 int enabled = dom->attribute("enabled").toInt();
160 uint16_t period = dom->attribute("period").toUShort();
161 uint16_t nb_buffering = dom->attribute("nb_buf").toUShort();
162 if(period==0) period=100;
163 if (enabled == 1) {
164 widget = new DataPlot1D(this, row, col, name, ymin, ymax, true, period,nb_buffering);
165 } else {
166 widget = new DataPlot1D(this, row, col, name, ymin, ymax, false, period,nb_buffering);
167 }
168 }
169 if (type == "DataPlot2D") {
170 float xmin = dom->attribute("xmin").toFloat();
171 float xmax = dom->attribute("xmax").toFloat();
172 float ymin = dom->attribute("ymin").toFloat();
173 float ymax = dom->attribute("ymax").toFloat();
174 QString x_name = dom->attribute("x_name");
175 QString y_name = dom->attribute("y_name");
176 int enabled = dom->attribute("enabled").toInt();
177 uint16_t period = dom->attribute("period").toUShort();
178 uint16_t nb_buffering = dom->attribute("nb_buf").toUShort();
179 if(period==0) period=100;
180 if (enabled == 1) {
181 widget = new DataPlot2D(this, row, col, name, x_name, y_name, xmin, xmax,
182 ymin, ymax, true, period,nb_buffering);
183 } else {
184 widget = new DataPlot2D(this, row, col, name, x_name, y_name, xmin, xmax,
185 ymin, ymax, false, period,nb_buffering);
186 }
187 }
188 if (type == "UsSensorPlot") {
189 float ymin = dom->attribute("ymin").toFloat();
190 float ymax = dom->attribute("ymax").toFloat();
191 QString y_name = dom->attribute("y_name");
192 int enabled = dom->attribute("enabled").toInt();
193 uint16_t period = dom->attribute("period").toUShort();
194 QString type = dom->attribute("type");
195 int samples = dom->attribute("samples").toInt();
196 if(period==0) period=100;
197 if (enabled == 1) {
198 widget = new UsSensorPlot(this, row, col, name, y_name,
199 ymin, ymax, samples,type,true, period);
200 } else {
201 widget = new UsSensorPlot(this, row, col, name, y_name,
202 ymin, ymax, samples,type,false, period);
203 }
204 }
205 if (type == "RangeFinderPlot") {
206 float xmin = dom->attribute("xmin").toFloat();
207 float xmax = dom->attribute("xmax").toFloat();
208 float ymin = dom->attribute("ymin").toFloat();
209 float ymax = dom->attribute("ymax").toFloat();
210 QString x_name = dom->attribute("x_name");
211 QString y_name = dom->attribute("y_name");
212 QString data_type = dom->attribute("type");
213 float start_angle = dom->attribute("start_angle").toFloat();
214 float end_angle = dom->attribute("end_angle").toFloat();
215 uint32_t nb_samples = dom->attribute("nb_samples").toUInt();
216 int enabled = dom->attribute("enabled").toInt();
217 uint16_t period = dom->attribute("period").toUShort();
218 int invert_axis = dom->attribute("invert_axis").toInt();
219 bool invert_axis_bool;
220 if (invert_axis == 0) {
221 invert_axis_bool = false;
222 } else {
223 invert_axis_bool = true;
224 }
225 if(period==0) period=100;
226 if (enabled == 1) {
227 widget =
228 new RangeFinderPlot(this, row, col, name, x_name, y_name, xmin, xmax,
229 ymin, ymax, start_angle, end_angle, nb_samples,
230 data_type, invert_axis, true, period);
231 } else {
232 widget =
233 new RangeFinderPlot(this, row, col, name, x_name, y_name, xmin, xmax,
234 ymin, ymax, start_angle, end_angle, nb_samples,
235 data_type, invert_axis, false, period);
236 }
237 }
238 if (type == "Picture") {
239 int width = dom->attribute("width").toInt();
240 int height = dom->attribute("height").toInt();
241 int enabled = dom->attribute("enabled").toInt();
242 uint16_t period = dom->attribute("period").toUShort();
243 if(period==0) period=100;
244 if (enabled == 1) {
245 widget = new Picture(this, row, col, name, width, height, true, period);
246 } else {
247 widget = new Picture(this, row, col, name, width, height, false, period);
248 }
249 }
250 if (type == "Map") {
251 uint16_t period = dom->attribute("period").toUShort();
252 int enabled = dom->attribute("enabled").toInt();
253 int i = 0;
254 QList<QtMobility::QGeoCoordinate> coordinates;
255 while (dom->hasAttribute("lat" + QString::number(i))) {
256 double latitude = dom->attribute("lat" + QString::number(i)).toDouble();
257 double longitude = dom->attribute("long" + QString::number(i)).toDouble();
258 double alt = dom->attribute("alt" + QString::number(i)).toDouble();
259 QtMobility::QGeoCoordinate coordinate =
260 QtMobility::QGeoCoordinate(latitude, longitude, alt);
261 coordinates.append(coordinate);
262 i++;
263 }
264 if(period==0) period=100;
265 if (enabled == 1) {
266 widget = new Map(this, row, col, name, coordinates, true, period);
267 } else {
268 widget = new Map(this, row, col, name, coordinates, false, period);
269 }
270 }
271 if (type == "TextEdit") {
272 widget = new TextEdit(this, row, col, name);
273 }
274 if (type == "Label") {
275 widget = new Label(this, row, col, name);
276 }
277 if (type == "ListWidget") {
278 // Parse the item list
279 QStringList items;
280 int count = 0;
281 while (dom->hasAttribute("item" + QString::number(count))) {
282 QString item = dom->attribute("item" + QString::number(count));
283 items.append(item);
284 count++;
285 }
286 widget = new ListWidget(this, row, col, name, items);
287 }
288
289 if (widget != NULL) {
290 if (old_name != "") {
291 widget->RenamedFrom(old_name);
292 }
293 }
294}
295
296bool Layout::IsUptodate(void) {
297 for (int i = 0; i < childs->count(); i++) {
298 if (childs->at(i)->IsUptodate() == false)
299 return false;
300 }
301 return true;
302}
Note: See TracBrowser for help on using the repository browser.