PlotConfig.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
20 
21 namespace rqt_multiplot {
22 
23 /*****************************************************************************/
24 /* Constructors and Destructor */
25 /*****************************************************************************/
26 
27 PlotConfig::PlotConfig(QObject* parent, const QString& title, double
28  plotRate) :
29  Config(parent),
30  title_(title),
31  axesConfig_(new PlotAxesConfig(this)),
32  legendConfig_(new PlotLegendConfig(this)),
33  plotRate_(plotRate) {
34  connect(axesConfig_, SIGNAL(changed()), this,
35  SLOT(axesConfigChanged()));
36  connect(legendConfig_, SIGNAL(changed()), this,
37  SLOT(legendConfigChanged()));
38 }
39 
41 }
42 
43 /*****************************************************************************/
44 /* Accessors */
45 /*****************************************************************************/
46 
47 void PlotConfig::setTitle(const QString& title) {
48  if (title != title_) {
49  title_ = title;
50 
51  emit titleChanged(title);
52  emit changed();
53  }
54 }
55 
56 const QString& PlotConfig::getTitle() const {
57  return title_;
58 }
59 
60 void PlotConfig::setNumCurves(size_t numCurves) {
61  while (curveConfig_.count() > numCurves)
62  removeCurve(curveConfig_.count()-1);
63 
64  while (curveConfig_.count() < numCurves)
65  addCurve();
66 }
67 
68 size_t PlotConfig::getNumCurves() const {
69  return curveConfig_.count();
70 }
71 
73  if (index < curveConfig_.count())
74  return curveConfig_[index];
75  else
76  return 0;
77 }
78 
80  return axesConfig_;
81 }
82 
84  return legendConfig_;
85 }
86 
87 void PlotConfig::setPlotRate(double rate) {
88  if (rate != plotRate_) {
89  plotRate_ = rate;
90 
91  emit plotRateChanged(rate);
92  emit changed();
93  }
94 }
95 
96 double PlotConfig::getPlotRate() const {
97  return plotRate_;
98 }
99 
100 /*****************************************************************************/
101 /* Methods */
102 /*****************************************************************************/
103 
105  CurveConfig* curveConfig = new CurveConfig(this);
106  curveConfig->getColorConfig()->setAutoColorIndex(curveConfig_.count());
107 
108  curveConfig_.append(curveConfig);
109 
110  connect(curveConfig, SIGNAL(changed()), this,
111  SLOT(curveConfigChanged()));
112  connect(curveConfig, SIGNAL(destroyed()), this,
113  SLOT(curveConfigDestroyed()));
114 
115  emit curveAdded(curveConfig_.count()-1);
116  emit changed();
117 
118  return curveConfig;
119 }
120 
122  int index = curveConfig_.indexOf(curveConfig);
123 
124  if (index >= 0)
125  removeCurve(index);
126 }
127 
128 void PlotConfig::removeCurve(size_t index) {
129  if (index < curveConfig_.count())
130  delete curveConfig_[index];
131 }
132 
134  if (!curveConfig_.isEmpty()) {
135  for (size_t i = 0; i < curveConfig_.count(); ++i)
136  delete curveConfig_[i];
137 
138  curveConfig_.clear();
139 
140  emit curvesCleared();
141  emit changed();
142  }
143 }
144 
145 QVector<CurveConfig*> PlotConfig::findCurves(const QString& title) const {
146  QVector<CurveConfig*> curves;
147 
148  for (size_t i = 0; i < curveConfig_.count(); ++i)
149  if (curveConfig_[i]->getTitle() == title)
150  curves.append(curveConfig_[i]);
151 
152  return curves;
153 }
154 
155 void PlotConfig::save(QSettings& settings) const {
156  settings.setValue("title", title_);
157 
158  settings.beginGroup("curves");
159 
160  for (size_t index = 0; index < curveConfig_.count(); ++index) {
161  settings.beginGroup("curve_"+QString::number(index));
162  curveConfig_[index]->save(settings);
163  settings.endGroup();
164  }
165 
166  settings.endGroup();
167 
168  settings.beginGroup("axes");
169  axesConfig_->save(settings);
170  settings.endGroup();
171 
172  settings.beginGroup("legend");
173  legendConfig_->save(settings);
174  settings.endGroup();
175 
176  settings.setValue("plot_rate", plotRate_);
177 }
178 
179 void PlotConfig::load(QSettings& settings) {
180  setTitle(settings.value("title", "Untitled Curve").toString());
181 
182  settings.beginGroup("curves");
183 
184  QStringList groups = settings.childGroups();
185  size_t index = 0;
186 
187  for (QStringList::iterator it = groups.begin(); it != groups.end(); ++it) {
188  CurveConfig* curveConfig = 0;
189 
190  if (index < curveConfig_.count())
191  curveConfig = curveConfig_[index];
192  else
193  curveConfig = addCurve();
194 
195  settings.beginGroup(*it);
196  curveConfig->load(settings);
197  settings.endGroup();
198 
199  ++index;
200  }
201 
202  settings.endGroup();
203 
204  while (index < curveConfig_.count())
205  removeCurve(index);
206 
207  settings.beginGroup("axes");
208  axesConfig_->load(settings);
209  settings.endGroup();
210 
211  settings.beginGroup("legend");
212  legendConfig_->load(settings);
213  settings.endGroup();
214 
215  setPlotRate(settings.value("plot_rate", 30.0).toDouble());
216 }
217 
219  setTitle("Untitled Plot");
220 
221  clearCurves();
222 
223  axesConfig_->reset();
224  legendConfig_->reset();
225 
226  setPlotRate(30.0);
227 }
228 
229 void PlotConfig::write(QDataStream& stream) const {
230  stream << title_;
231 
232  stream << (quint64)getNumCurves();
233  for (size_t index = 0; index < curveConfig_.count(); ++index)
234  curveConfig_[index]->write(stream);
235 
236  axesConfig_->write(stream);
237  legendConfig_->write(stream);
238 
239  stream << plotRate_;
240 }
241 
242 void PlotConfig::read(QDataStream& stream) {
243  QString title;
244  quint64 numCurves;
245  double plotRate;
246 
247  stream >> title;
248  setTitle(title);
249 
250  stream >> numCurves;
251  setNumCurves(numCurves);
252  for (size_t index = 0; index < curveConfig_.count(); ++index)
253  curveConfig_[index]->read(stream);
254 
255  axesConfig_->write(stream);
256  legendConfig_->write(stream);
257 
258  stream >> plotRate;
259  setPlotRate(plotRate);
260 }
261 
262 /*****************************************************************************/
263 /* Operators */
264 /*****************************************************************************/
265 
267  setTitle(src.title_);
268 
269  while (curveConfig_.count() < src.curveConfig_.count())
270  addCurve();
271  while (curveConfig_.count() > src.curveConfig_.count())
272  removeCurve(curveConfig_.count()-1);
273 
274  for (size_t index = 0; index < curveConfig_.count(); ++index)
275  *curveConfig_[index] = *src.curveConfig_[index];
276 
277  *axesConfig_ = *src.axesConfig_;
279 
280  setPlotRate(src.plotRate_);
281 
282  return *this;
283 }
284 
285 /*****************************************************************************/
286 /* Slots */
287 /*****************************************************************************/
288 
290  for (size_t index = 0; index < curveConfig_.count(); ++index) {
291  if (curveConfig_[index] == sender()) {
292  emit curveConfigChanged(index);
293 
294  break;
295  }
296  }
297 
298  emit changed();
299 }
300 
302  int index = curveConfig_.indexOf(static_cast<CurveConfig*>(sender()));
303 
304  if (index >= 0) {
305  curveConfig_.remove(index);
306 
307  for (size_t i = 0; i < curveConfig_.count(); ++i)
308  curveConfig_[i]->getColorConfig()->setAutoColorIndex(i);
309 
310  emit curveRemoved(index);
311  emit changed();
312  }
313 }
314 
316  emit changed();
317 }
318 
320  emit changed();
321 }
322 
323 }
void write(QDataStream &stream) const
Definition: PlotConfig.cpp:229
PlotAxesConfig * getAxesConfig() const
Definition: PlotConfig.cpp:79
QVector< CurveConfig * > findCurves(const QString &title) const
Definition: PlotConfig.cpp:145
void write(QDataStream &stream) const
PlotConfig & operator=(const PlotConfig &src)
Definition: PlotConfig.cpp:266
void write(QDataStream &stream) const
CurveConfig * addCurve()
Definition: PlotConfig.cpp:104
PlotConfig(QObject *parent=0, const QString &title="Untitled Plot", double plotRate=30.0)
Definition: PlotConfig.cpp:27
QVector< CurveConfig * > curveConfig_
Definition: PlotConfig.h:75
void setNumCurves(size_t numCurves)
Definition: PlotConfig.cpp:60
PlotLegendConfig * getLegendConfig() const
Definition: PlotConfig.cpp:83
void load(QSettings &settings)
void read(QDataStream &stream)
Definition: PlotConfig.cpp:242
void setAutoColorIndex(unsigned char index)
PlotAxesConfig * axesConfig_
Definition: PlotConfig.h:76
void save(QSettings &settings) const
Definition: PlotConfig.cpp:155
void load(QSettings &settings)
const QString & getTitle() const
Definition: PlotConfig.cpp:56
void save(QSettings &settings) const
double getPlotRate() const
Definition: PlotConfig.cpp:96
void curveRemoved(size_t index)
PlotLegendConfig * legendConfig_
Definition: PlotConfig.h:77
void setPlotRate(double rate)
Definition: PlotConfig.cpp:87
CurveConfig * getCurveConfig(size_t index) const
Definition: PlotConfig.cpp:72
void load(QSettings &settings)
size_t getNumCurves() const
Definition: PlotConfig.cpp:68
void setTitle(const QString &title)
Definition: PlotConfig.cpp:47
void removeCurve(CurveConfig *curveConfig)
Definition: PlotConfig.cpp:121
void save(QSettings &settings) const
CurveColorConfig * getColorConfig() const
Definition: CurveConfig.cpp:82
void titleChanged(const QString &title)
void curveAdded(size_t index)
void load(QSettings &settings)
Definition: PlotConfig.cpp:179
void plotRateChanged(double rate)


rqt_multiplot_plugin
Author(s): Ralf Kaestner
autogenerated on Fri Jan 15 2021 03:47:53