PlotConfig.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2015 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include "rqt_multiplot/PlotConfig.h"
00020 
00021 namespace rqt_multiplot {
00022 
00023 /*****************************************************************************/
00024 /* Constructors and Destructor                                               */
00025 /*****************************************************************************/
00026 
00027 PlotConfig::PlotConfig(QObject* parent, const QString& title, double
00028     plotRate) :
00029   Config(parent),
00030   title_(title),
00031   axesConfig_(new PlotAxesConfig(this)),
00032   legendConfig_(new PlotLegendConfig(this)),
00033   plotRate_(plotRate) {
00034   connect(axesConfig_, SIGNAL(changed()), this,
00035     SLOT(axesConfigChanged()));
00036   connect(legendConfig_, SIGNAL(changed()), this,
00037     SLOT(legendConfigChanged()));
00038 }
00039 
00040 PlotConfig::~PlotConfig() {
00041 }
00042 
00043 /*****************************************************************************/
00044 /* Accessors                                                                 */
00045 /*****************************************************************************/
00046 
00047 void PlotConfig::setTitle(const QString& title) {
00048   if (title != title_) {
00049     title_ = title;
00050     
00051     emit titleChanged(title);
00052     emit changed();
00053   }
00054 }
00055 
00056 const QString& PlotConfig::getTitle() const {
00057   return title_;
00058 }
00059 
00060 void PlotConfig::setNumCurves(size_t numCurves) {
00061   while (curveConfig_.count() > numCurves)
00062     removeCurve(curveConfig_.count()-1);
00063   
00064   while (curveConfig_.count() < numCurves)
00065     addCurve();
00066 }
00067 
00068 size_t PlotConfig::getNumCurves() const {
00069   return curveConfig_.count();
00070 }
00071 
00072 CurveConfig* PlotConfig::getCurveConfig(size_t index) const {
00073   if (index < curveConfig_.count())
00074     return curveConfig_[index];
00075   else
00076     return 0;
00077 }
00078 
00079 PlotAxesConfig* PlotConfig::getAxesConfig() const {
00080   return axesConfig_;
00081 }
00082 
00083 PlotLegendConfig* PlotConfig::getLegendConfig() const {
00084   return legendConfig_;
00085 }
00086 
00087 void PlotConfig::setPlotRate(double rate) {
00088   if (rate != plotRate_) {
00089     plotRate_ = rate;
00090     
00091     emit plotRateChanged(rate);
00092     emit changed();
00093   }
00094 }
00095 
00096 double PlotConfig::getPlotRate() const {
00097   return plotRate_;
00098 }
00099 
00100 /*****************************************************************************/
00101 /* Methods                                                                   */
00102 /*****************************************************************************/
00103 
00104 CurveConfig* PlotConfig::addCurve() {
00105   CurveConfig* curveConfig = new CurveConfig(this);
00106   curveConfig->getColorConfig()->setAutoColorIndex(curveConfig_.count());
00107   
00108   curveConfig_.append(curveConfig);
00109   
00110   connect(curveConfig, SIGNAL(changed()), this,
00111     SLOT(curveConfigChanged()));
00112   connect(curveConfig, SIGNAL(destroyed()), this,
00113     SLOT(curveConfigDestroyed()));
00114   
00115   emit curveAdded(curveConfig_.count()-1);
00116   emit changed();
00117   
00118   return curveConfig;
00119 }
00120 
00121 void PlotConfig::removeCurve(CurveConfig* curveConfig) {
00122   int index = curveConfig_.indexOf(curveConfig);
00123   
00124   if (index >= 0)
00125     removeCurve(index);
00126 }
00127 
00128 void PlotConfig::removeCurve(size_t index) {
00129   if (index < curveConfig_.count())
00130     delete curveConfig_[index];
00131 }
00132 
00133 void PlotConfig::clearCurves() {
00134   if (!curveConfig_.isEmpty()) {
00135     for (size_t i = 0; i < curveConfig_.count(); ++i)
00136       delete curveConfig_[i];
00137     
00138     curveConfig_.clear();
00139     
00140     emit curvesCleared();
00141     emit changed();
00142   }
00143 }
00144 
00145 QVector<CurveConfig*> PlotConfig::findCurves(const QString& title) const {
00146   QVector<CurveConfig*> curves;
00147   
00148   for (size_t i = 0; i < curveConfig_.count(); ++i)
00149     if (curveConfig_[i]->getTitle() == title)
00150       curves.append(curveConfig_[i]);
00151   
00152   return curves;
00153 }
00154 
00155 void PlotConfig::save(QSettings& settings) const {
00156   settings.setValue("title", title_);
00157   
00158   settings.beginGroup("curves");
00159   
00160   for (size_t index = 0; index < curveConfig_.count(); ++index) {
00161     settings.beginGroup("curve_"+QString::number(index));
00162     curveConfig_[index]->save(settings);
00163     settings.endGroup();
00164   }
00165   
00166   settings.endGroup();
00167 
00168   settings.beginGroup("axes");
00169   axesConfig_->save(settings);
00170   settings.endGroup();
00171   
00172   settings.beginGroup("legend");
00173   legendConfig_->save(settings);
00174   settings.endGroup();
00175   
00176   settings.setValue("plot_rate", plotRate_);  
00177 }
00178 
00179 void PlotConfig::load(QSettings& settings) {
00180   setTitle(settings.value("title", "Untitled Curve").toString());
00181   
00182   settings.beginGroup("curves");
00183   
00184   QStringList groups = settings.childGroups();
00185   size_t index = 0;
00186   
00187   for (QStringList::iterator it = groups.begin(); it != groups.end(); ++it) {
00188     CurveConfig* curveConfig = 0;
00189     
00190     if (index < curveConfig_.count())
00191       curveConfig = curveConfig_[index];
00192     else
00193       curveConfig = addCurve();
00194     
00195     settings.beginGroup(*it);
00196     curveConfig->load(settings);
00197     settings.endGroup();
00198     
00199     ++index;
00200   }
00201 
00202   settings.endGroup();
00203   
00204   while (index < curveConfig_.count())
00205     removeCurve(index);
00206   
00207   settings.beginGroup("axes");
00208   axesConfig_->load(settings);
00209   settings.endGroup();
00210   
00211   settings.beginGroup("legend");
00212   legendConfig_->load(settings);
00213   settings.endGroup();
00214   
00215   setPlotRate(settings.value("plot_rate", 30.0).toDouble());
00216 }
00217 
00218 void PlotConfig::reset() {
00219   setTitle("Untitled Plot");
00220   
00221   clearCurves();
00222   
00223   axesConfig_->reset();
00224   legendConfig_->reset();
00225   
00226   setPlotRate(30.0);
00227 }
00228 
00229 void PlotConfig::write(QDataStream& stream) const {
00230   stream << title_;
00231   
00232   stream << (quint64)getNumCurves();  
00233   for (size_t index = 0; index < curveConfig_.count(); ++index)
00234     curveConfig_[index]->write(stream);
00235   
00236   axesConfig_->write(stream);
00237   legendConfig_->write(stream);
00238   
00239   stream << plotRate_;
00240 }
00241 
00242 void PlotConfig::read(QDataStream& stream) {
00243   QString title;
00244   quint64 numCurves;
00245   double plotRate;
00246   
00247   stream >> title;
00248   setTitle(title);
00249   
00250   stream >> numCurves;
00251   setNumCurves(numCurves);  
00252   for (size_t index = 0; index < curveConfig_.count(); ++index)
00253     curveConfig_[index]->read(stream);
00254   
00255   axesConfig_->write(stream);
00256   legendConfig_->write(stream);
00257   
00258   stream >> plotRate;
00259   setPlotRate(plotRate);
00260 }
00261 
00262 /*****************************************************************************/
00263 /* Operators                                                                 */
00264 /*****************************************************************************/
00265 
00266 PlotConfig& PlotConfig::operator=(const PlotConfig& src) {
00267   setTitle(src.title_);
00268   
00269   while (curveConfig_.count() < src.curveConfig_.count())
00270     addCurve();
00271   while (curveConfig_.count() > src.curveConfig_.count())
00272     removeCurve(curveConfig_.count()-1);
00273   
00274   for (size_t index = 0; index < curveConfig_.count(); ++index)
00275     *curveConfig_[index] = *src.curveConfig_[index];
00276   
00277   *axesConfig_ = *src.axesConfig_;
00278   *legendConfig_ = *src.legendConfig_;
00279   
00280   setPlotRate(src.plotRate_);
00281   
00282   return *this;
00283 }
00284 
00285 /*****************************************************************************/
00286 /* Slots                                                                     */
00287 /*****************************************************************************/
00288 
00289 void PlotConfig::curveConfigChanged() {
00290   for (size_t index = 0; index < curveConfig_.count(); ++index) {
00291     if (curveConfig_[index] == sender()) {
00292       emit curveConfigChanged(index);
00293       
00294       break;
00295     }
00296   }
00297   
00298   emit changed();
00299 }
00300 
00301 void PlotConfig::curveConfigDestroyed() {
00302   int index = curveConfig_.indexOf(static_cast<CurveConfig*>(sender()));
00303 
00304   if (index >= 0) {
00305     curveConfig_.remove(index);
00306     
00307     for (size_t i = 0; i < curveConfig_.count(); ++i)
00308       curveConfig_[i]->getColorConfig()->setAutoColorIndex(i);
00309     
00310     emit curveRemoved(index);
00311     emit changed();
00312   }
00313 }
00314 
00315 void PlotConfig::axesConfigChanged() {
00316   emit changed();
00317 }
00318 
00319 void PlotConfig::legendConfigChanged() {
00320   emit changed();
00321 }
00322 
00323 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Thu Jun 6 2019 21:49:11