PlotTableConfig.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/PlotTableConfig.h"
00020 
00021 namespace rqt_multiplot {
00022 
00023 /*****************************************************************************/
00024 /* Constructors and Destructor                                               */
00025 /*****************************************************************************/
00026 
00027 PlotTableConfig::PlotTableConfig(QObject* parent, const QColor&
00028     backgroundColor, const QColor& foregroundColor, size_t numRows,
00029     size_t numColumns, bool linkScale, bool linkCursor, bool trackPoints) :
00030   Config(parent),
00031   backgroundColor_(backgroundColor),
00032   foregroundColor_(foregroundColor),
00033   linkScale_(linkScale),
00034   linkCursor_(linkCursor),
00035   trackPoints_(trackPoints) {
00036   if (numRows && numColumns) {
00037     plotConfig_.resize(numRows);
00038     
00039     for (size_t row = 0; row < numRows; ++row) {
00040       plotConfig_[row].resize(numColumns);
00041       
00042       for (size_t column = 0; column < numColumns; ++column) {
00043         plotConfig_[row][column] = new PlotConfig(this);
00044         
00045         connect(plotConfig_[row][column], SIGNAL(changed()), this,
00046           SLOT(plotConfigChanged()));
00047       }
00048     }
00049   }
00050 }
00051 
00052 PlotTableConfig::~PlotTableConfig() {
00053 }
00054 
00055 /*****************************************************************************/
00056 /* Accessors                                                                 */
00057 /*****************************************************************************/
00058 
00059 void PlotTableConfig::setBackgroundColor(const QColor& color) {
00060   if (color != backgroundColor_) {
00061     backgroundColor_ = color;
00062     
00063     emit backgroundColorChanged(color);
00064     emit changed();
00065   }
00066 }
00067 
00068 const QColor& PlotTableConfig::getBackgroundColor() const {
00069   return backgroundColor_;
00070 }
00071 
00072 void PlotTableConfig::setForegroundColor(const QColor& color) {
00073   if (color != foregroundColor_) {
00074     foregroundColor_ = color;
00075     
00076     emit foregroundColorChanged(color);
00077     emit changed();
00078   }
00079 }
00080 
00081 const QColor& PlotTableConfig::getForegroundColor() const {
00082   return foregroundColor_;
00083 }
00084 
00085 void PlotTableConfig::setNumPlots(size_t numRows, size_t numColumns) {
00086   if ((numRows != getNumRows()) || (numColumns != getNumColumns())) {
00087     size_t oldNumRows = getNumRows();
00088     size_t oldNumColumns = getNumColumns();
00089     
00090     if (!numRows || !numColumns) {
00091       numRows = 0;
00092       numColumns = 0;
00093     }
00094     
00095     QVector<QVector<PlotConfig*> > plotConfig(numRows);
00096     
00097     for (size_t row = 0; row < numRows; ++row) {
00098       plotConfig[row].resize(numColumns);
00099       
00100       for (size_t column = 0; column < numColumns; ++column) {
00101         if ((row < oldNumRows) && (column < oldNumColumns))
00102           plotConfig[row][column] = plotConfig_[row][column];
00103         else {
00104           plotConfig[row][column] = new PlotConfig(this);
00105           
00106           connect(plotConfig[row][column], SIGNAL(changed()), this,
00107             SLOT(plotConfigChanged()));
00108         }
00109       }
00110     }
00111     
00112     for (size_t row = 0; row < oldNumRows; ++row)
00113       for (size_t column = 0; column < oldNumColumns; ++column)
00114         if ((row >= numRows) || (column >= numColumns))
00115           delete plotConfig_[row][column];
00116     
00117     plotConfig_ = plotConfig;
00118     
00119     emit numPlotsChanged(numRows, numColumns);
00120     emit changed();
00121   }
00122 }
00123 
00124 void PlotTableConfig::setNumRows(size_t numRows) {
00125   setNumPlots(numRows, getNumColumns());
00126 }
00127 
00128 size_t PlotTableConfig::getNumRows() const {
00129   return plotConfig_.count();
00130 }
00131 
00132 void PlotTableConfig::setNumColumns(size_t numColumns) {
00133   setNumPlots(getNumRows(), numColumns);
00134 }
00135 
00136 size_t PlotTableConfig::getNumColumns() const {
00137   if (!plotConfig_.isEmpty())
00138     return plotConfig_[0].count();
00139   else
00140     return 0;
00141 }
00142 
00143 PlotConfig* PlotTableConfig::getPlotConfig(size_t row, size_t column) const {
00144   if ((row < getNumRows()) && (column < getNumColumns()))
00145     return plotConfig_[row][column];
00146   else
00147     return 0;
00148 }
00149 
00150 void PlotTableConfig::setLinkScale(bool link) {
00151   if (link != linkScale_) {
00152     linkScale_ = link;
00153     
00154     emit linkScaleChanged(link);
00155     emit changed();
00156   }
00157 }
00158 
00159 bool PlotTableConfig::isScaleLinked() const {
00160   return linkScale_;
00161 }
00162 
00163 void PlotTableConfig::setLinkCursor(bool link) {
00164   if (link != linkCursor_) {
00165     linkCursor_ = link;
00166     
00167     emit linkCursorChanged(link);
00168     emit changed();
00169   }
00170 }
00171 
00172 bool PlotTableConfig::isCursorLinked() const {
00173   return linkCursor_;
00174 }
00175 
00176 void PlotTableConfig::setTrackPoints(bool track) {
00177   if (track != trackPoints_) {
00178     trackPoints_ = track;
00179     
00180     emit trackPointsChanged(track);
00181     emit changed();
00182   }
00183 }
00184 
00185 bool PlotTableConfig::arePointsTracked() const {
00186   return trackPoints_;
00187 }
00188 
00189 /*****************************************************************************/
00190 /* Methods                                                                   */
00191 /*****************************************************************************/
00192 
00193 void PlotTableConfig::save(QSettings& settings) const {
00194   settings.setValue("background_color", QVariant::fromValue<QColor>(
00195     backgroundColor_));
00196   settings.setValue("foreground_color", QVariant::fromValue<QColor>(
00197     foregroundColor_));
00198   
00199   settings.beginGroup("plots");
00200   
00201   for (size_t row = 0; row < plotConfig_.count(); ++row) {
00202     settings.beginGroup("row_"+QString::number(row));
00203     
00204     for (size_t column = 0; column < plotConfig_[row].count(); ++column) {
00205       settings.beginGroup("column_"+QString::number(column));
00206       plotConfig_[row][column]->save(settings);
00207       settings.endGroup();
00208     }
00209     
00210     settings.endGroup();
00211   }
00212   
00213   settings.endGroup();
00214   
00215   settings.setValue("link_scale", linkScale_);
00216   settings.setValue("link_cursor", linkCursor_);
00217   settings.setValue("track_points", trackPoints_);
00218 }
00219 
00220 void PlotTableConfig::load(QSettings& settings) {
00221   setBackgroundColor(settings.value("background_color", (QColor)Qt::white).
00222     value<QColor>());
00223   setForegroundColor(settings.value("foreground_color", (QColor)Qt::black).
00224     value<QColor>());
00225   
00226   settings.beginGroup("plots");
00227   
00228   QStringList rowGroups = settings.childGroups();
00229   size_t row = 0;
00230   size_t numColumns = 0;
00231   
00232   for (QStringList::iterator it = rowGroups.begin();
00233       it != rowGroups.end(); ++it) {
00234     if (row >= plotConfig_.count())
00235       setNumRows(row+1);
00236     
00237     settings.beginGroup(*it);
00238     
00239     QStringList columnGroups = settings.childGroups();
00240     size_t column = 0;
00241     
00242     for (QStringList::iterator jt = columnGroups.begin();
00243         jt != columnGroups.end(); ++jt) {    
00244       if (column >= plotConfig_[row].count())
00245         setNumColumns(column+1);
00246       
00247       settings.beginGroup(*jt);
00248       plotConfig_[row][column]->load(settings);
00249       settings.endGroup();
00250       
00251       ++column;
00252     }
00253     
00254     settings.endGroup();
00255     
00256     numColumns = std::max(numColumns, column);
00257     ++row;
00258   }
00259     
00260   settings.endGroup();
00261   
00262   setNumPlots(row, numColumns);
00263   
00264   setLinkScale(settings.value("link_scale", false).toBool());
00265   setLinkCursor(settings.value("link_cursor", false).toBool());
00266   setTrackPoints(settings.value("track_points", false).toBool());
00267 }
00268 
00269 void PlotTableConfig::reset() {
00270   setBackgroundColor(Qt::white);
00271   setForegroundColor(Qt::black);
00272 
00273   setNumPlots(1, 1);
00274   plotConfig_[0][0]->reset();
00275   
00276   setLinkScale(false);
00277   setLinkCursor(false);
00278   setTrackPoints(false);
00279 }
00280 
00281 void PlotTableConfig::write(QDataStream& stream) const {
00282   stream << backgroundColor_;
00283   stream << foregroundColor_;
00284   
00285   stream << (quint64)getNumRows() << (quint64)getNumColumns();
00286   
00287   for (size_t row = 0; row < plotConfig_.count(); ++row)
00288     for (size_t column = 0; column < plotConfig_[row].count(); ++column)
00289       plotConfig_[row][column]->write(stream);
00290   
00291   stream << linkScale_;
00292   stream << linkCursor_;
00293   stream << trackPoints_;
00294 }
00295 
00296 void PlotTableConfig::read(QDataStream& stream) {
00297   QColor backgroundColor, foregroundColor;
00298   bool linkScale, linkCursor, trackPoints;
00299   quint64 numRows, numColumns;
00300   
00301   stream >> backgroundColor;
00302   setBackgroundColor(backgroundColor);
00303   stream >> foregroundColor;
00304   setForegroundColor(foregroundColor);
00305   
00306   stream >> numRows >> numColumns;
00307   setNumPlots(numRows, numColumns);
00308   for (size_t row = 0; row < plotConfig_.count(); ++row)
00309     for (size_t column = 0; column < plotConfig_[row].count(); ++column)
00310       plotConfig_[row][column]->read(stream);
00311   
00312   stream >> linkScale;
00313   setLinkScale(linkScale);
00314   stream >> linkCursor;
00315   setLinkCursor(linkCursor);
00316   stream >> trackPoints;
00317   setTrackPoints(trackPoints);
00318 }
00319 
00320 /*****************************************************************************/
00321 /* Operators                                                                 */
00322 /*****************************************************************************/
00323 
00324 PlotTableConfig& PlotTableConfig::operator=(const PlotTableConfig& src) {
00325   setBackgroundColor(src.backgroundColor_);
00326   setForegroundColor(src.foregroundColor_);
00327   
00328   setNumPlots(src.getNumRows(), src.getNumColumns());
00329   
00330   for (size_t row = 0; row < getNumRows(); ++row)
00331     for (size_t column = 0; column < getNumColumns(); ++column)
00332       *plotConfig_[row][column] = *src.plotConfig_[row][column];
00333 
00334   setLinkScale(src.linkScale_);
00335   setLinkCursor(src.linkCursor_);
00336   setTrackPoints(src.trackPoints_);
00337     
00338   return *this;
00339 }
00340 
00341 /*****************************************************************************/
00342 /* Slots                                                                     */
00343 /*****************************************************************************/
00344 
00345 void PlotTableConfig::plotConfigChanged() {
00346   emit changed();
00347 }
00348 
00349 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Tue May 9 2017 02:16:02