YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
xmlloader.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6#include "xmlloader.h"
7#include <QXmlStreamReader>
8#include <QFile>
9
10XmlLoader::XmlLoader(QString fileName, PlotManager *plotManager,QObject *parent) : GenericLoader(parent)
11{
12 plotter = nullptr;
13 this->plotManager = plotManager;
14
15 auto* file = new QFile(fileName);
16 /* If we can't open it, let's show an error message. */
17 if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
18 return;
19 }
20
21 QXmlStreamReader xml(file);
22
23 while(!xml.atEnd() && !xml.hasError()) {
24
25
26 QXmlStreamReader::TokenType token = xml.readNext();
27
28 /* If token is just StartDocument, we'll go to next.*/
29 if(token == QXmlStreamReader::StartDocument) {
30 continue;
31 }
32 if(token == QXmlStreamReader::StartElement) {
33 if(xml.name() == "portscope") {
34 QXmlStreamAttributes attributes = xml.attributes();
35
36 if(attributes.hasAttribute("rows")) {
37 portscope_rows = attributes.value("rows").toInt();
38 }
39 if(attributes.hasAttribute("columns")) {
40 portscope_columns = attributes.value("columns").toInt();
41 }
42 if(attributes.hasAttribute("carrier")) {
43 portscope_carrier = attributes.value("carrier").toString();
44 }
45 if(attributes.hasAttribute("persistent")) {
46 portscope_persistent = QVariant(attributes.value("persistent").toString()).toBool();
47 }
48
49 continue;
50 }
51
52 if (xml.name() == "plot") {
53
54 QXmlStreamAttributes plotAttributes = xml.attributes();
55 int plot_gridx(default_plot_gridx);
56 int plot_gridy(default_plot_gridy);
57 int plot_hspan(default_plot_hspan);
58 int plot_vspan(default_plot_vspan);
59 int plot_size(default_plot_size);
60 float plot_minval(default_plot_minval);
61 float plot_maxval(default_plot_maxval);
62 bool plot_autorescale(default_plot_autorescale);
63 bool plot_realtime(default_plot_realtime);
64 bool plot_triggermode(default_plot_triggermode);
65 QString plot_bgcolor(default_plot_bgcolor);
66 QString plot_title(default_plot_title);
67
68 if(plotAttributes.hasAttribute("title")) {
69 plot_title = plotAttributes.value("title").toString();
70 }
71 if(plotAttributes.hasAttribute("gridx")) {
72 plot_gridx = plotAttributes.value("gridx").toInt();
73 }
74 if(plotAttributes.hasAttribute("gridy")) {
75 plot_gridy = plotAttributes.value("gridy").toInt();
76 }
77 if(plotAttributes.hasAttribute("hspan")) {
78 plot_hspan = plotAttributes.value("hspan").toInt();
79 }
80 if(plotAttributes.hasAttribute("vspan")) {
81 plot_vspan = plotAttributes.value("vspan").toInt();
82 }
83 if(plotAttributes.hasAttribute("minval")) {
84 plot_minval = plotAttributes.value("minval").toFloat();
85 }
86 if(plotAttributes.hasAttribute("maxval")) {
87 plot_maxval = plotAttributes.value("maxval").toFloat();
88 }
89 if(plotAttributes.hasAttribute("size")) {
90 plot_size = plotAttributes.value("size").toInt();
91 }
92 if(plotAttributes.hasAttribute("bgcolor")) {
93 plot_bgcolor = plotAttributes.value("bgcolor").toString();
94 }
95 if(plotAttributes.hasAttribute("autorescale")) {
96 plot_autorescale = QVariant(plotAttributes.value("autorescale").toString()).toBool();
97 }
98 if(plotAttributes.hasAttribute("realtime")) {
99 plot_realtime = QVariant(plotAttributes.value("realtime").toString()).toBool();
100 }
101 if(plotAttributes.hasAttribute("triggermode")) {
102 plot_triggermode = QVariant(plotAttributes.value("triggermode").toString()).toBool();
103 }
104
105 plotter = plotManager->addPlot(plot_title, plot_gridx, plot_gridy, plot_hspan, plot_vspan, plot_minval, plot_maxval, plot_size, plot_bgcolor, plot_autorescale);
106 Q_UNUSED(plot_realtime);
107 Q_UNUSED(plot_triggermode);
108 continue;
109 }
110
111 if (xml.name() == "graph") {
112
113 QXmlStreamAttributes graphAttributes = xml.attributes();
114 QString graph_remote;
115 QString graph_title(default_graph_title);
116 QString graph_color(default_graph_color);
117 QString graph_type(default_graph_type);
118 int graph_index;
119 double graph_y_scale;
120 int graph_size(default_graph_size);
121
122 if(graphAttributes.hasAttribute("remote")) {
123 graph_remote = graphAttributes.value("remote").toString();
124 } else {
125 qCritical(R"(Syntax error while loading %s. "remote" attribute not found in element "graph")",fileName.toLatin1().data());
126 break;
127 }
128 if(graphAttributes.hasAttribute("index")) {
129 graph_index = graphAttributes.value("index").toInt();
130 if(graph_index < 0) {
131 qCritical(R"(Syntax error while loading %s. "index" attribute not found in element "graph")",fileName.toLatin1().data());
132 break;
133 }
134 } else {
135 qCritical(R"(Syntax error while loading %s. "index" attribute in element "graph" should be >= 0)",fileName.toLatin1().data());
136 break;
137 }
138 if(graphAttributes.hasAttribute("title")) {
139 graph_title = graphAttributes.value("title").toString();
140 }
141 if(graphAttributes.hasAttribute("rescale_y_factor")) {
142 graph_y_scale = graphAttributes.value("rescale_y_factor").toDouble();
143 }
144 else
145 {
146 graph_y_scale = 1.0;
147 }
148 if(graphAttributes.hasAttribute("color")) {
149 graph_color = graphAttributes.value("color").toString();
150 }
151 if(graphAttributes.hasAttribute("type")) {
152 graph_type = graphAttributes.value("type").toString();
153 }
154 if(graphAttributes.hasAttribute("size")) {
155 graph_size = graphAttributes.value("size").toInt();
156 if(graph_size <= 0){
157 graph_size = default_graph_size;
158 }
159 }
160 if (plotter)
161 {
162 Graph *graph = plotter->addGraph(graph_remote,"",graph_index,graph_title, graph_color, graph_type, graph_size, graph_y_scale);
163 if(graph){
164 graph->init(graph_remote,"", portscope_carrier, portscope_persistent);
165 }
166 }
167 continue;
168 }
169
170 }
171 }
172}
Base Class for the Loaders.
bool portscope_persistent
const bool default_plot_autorescale
const float default_plot_maxval
const int default_plot_size
QString portscope_carrier
const int default_plot_hspan
const bool default_plot_realtime
const int default_graph_size
const float default_plot_minval
const bool default_plot_triggermode
const QString default_graph_title
const QString default_plot_bgcolor
const int default_plot_gridx
const int default_plot_gridy
const int default_plot_vspan
const QString default_plot_title
const QString default_graph_color
const QString default_graph_type
Class representing a Graph.
Definition plotter.h:26
void init(QString remotePortName, QString localPortName, QString carrier, bool persistent)
Definition plotter.cpp:267
The Manager of the plotters.
Definition plotmanager.h:19
Plotter * addPlot(const QString &title, int gridx, int gridy, int hspan, int vspan, float minval, float maxval, int size, const QString &bgcolor, bool autorescale)
Adds a new Plotter.
Graph * addGraph(QString remotePort, QString localPort, int index, QString title, QString color, QString type, int size, double graph_y_scale=1.0)
Add a Graph to the current Plotter.
Definition plotter.cpp:119
XmlLoader(QString fileName, PlotManager *plotManager, QObject *parent=0)
Definition xmlloader.cpp:10