MessageFieldItem.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 <QRegExp>
00020 #include <QStringList>
00021 
00022 #include <variant_topic_tools/ArrayDataType.h>
00023 #include <variant_topic_tools/BuiltinDataType.h>
00024 #include <variant_topic_tools/MessageDataType.h>
00025 
00026 #include "rqt_multiplot/MessageFieldItem.h"
00027 
00028 namespace rqt_multiplot {
00029 
00030 /*****************************************************************************/
00031 /* Constructors and Destructor                                               */
00032 /*****************************************************************************/
00033 
00034 MessageFieldItem::MessageFieldItem(const variant_topic_tools::DataType&
00035     dataType, MessageFieldItem* parent, const QString& name) :
00036   parent_(parent),
00037   name_(name),
00038   dataType_(dataType) {
00039   if (dataType_.isMessage()) {
00040     variant_topic_tools::MessageDataType messageType = dataType_;
00041     
00042     for (size_t i = 0; i < messageType.getNumVariableMembers(); ++i)
00043       appendChild(new MessageFieldItem(messageType.getVariableMember(i).
00044         getType(), this, QString::fromStdString(messageType.
00045         getVariableMember(i).getName())));
00046   }
00047   else if (dataType_.isArray()) {
00048     variant_topic_tools::ArrayDataType arrayType = dataType_;
00049     
00050     if (!arrayType.isDynamic()) {
00051       for (size_t i = 0; i < arrayType.getNumMembers(); ++i)
00052         appendChild(new MessageFieldItem(arrayType.getMemberType(), this,
00053           QString::number(i)));
00054     }
00055     else {
00056       for (size_t i = 0; i <= 9; ++i)
00057         appendChild(new MessageFieldItem(arrayType.getMemberType(), this,
00058           QString::number(i)));
00059     }
00060   }
00061 }
00062 
00063 MessageFieldItem::~MessageFieldItem() {
00064   for (QList<MessageFieldItem*>::iterator it = children_.begin();
00065        it != children_.end(); ++it)
00066     delete *it;
00067 }
00068 
00069 /*****************************************************************************/
00070 /* Accessors                                                                 */
00071 /*****************************************************************************/
00072 
00073 MessageFieldItem* MessageFieldItem::getParent() const {
00074   return parent_;
00075 }
00076 
00077 size_t MessageFieldItem::getNumChildren() const {
00078   return children_.count();
00079 }
00080 
00081 MessageFieldItem* MessageFieldItem::getChild(size_t row) const {
00082   return children_.value(row);
00083 }
00084 
00085 MessageFieldItem* MessageFieldItem::getChild(const QString& name) const {
00086   for (QList<MessageFieldItem*>::const_iterator it = children_.begin();
00087       it != children_.end(); ++it) {
00088     if ((*it)->name_ == name)
00089       return *it;
00090   }
00091   
00092   return 0;
00093 }
00094 
00095 MessageFieldItem* MessageFieldItem::getDescendant(const QString& path) const {
00096   QStringList names = path.split("/");
00097   
00098   if (!names.isEmpty()) {
00099     MessageFieldItem* child = getChild(names.first());
00100     
00101     if (child) {
00102       names.removeFirst();
00103       return child->getDescendant(names.join("/"));
00104     }
00105   }
00106   
00107   return 0;
00108 }
00109 
00110 int MessageFieldItem::getRow() const {
00111   if (parent_)
00112     return parent_->children_.indexOf(const_cast<MessageFieldItem*>(this));
00113 
00114   return -1;
00115 }
00116 
00117 size_t MessageFieldItem::getNumColumns() const {
00118   return 1;
00119 }
00120 
00121 const QString& MessageFieldItem::getName() const {
00122   return name_;
00123 }
00124 
00125 const variant_topic_tools::DataType& MessageFieldItem::getDataType() const {
00126   return dataType_;
00127 }
00128 
00129 /*****************************************************************************/
00130 /* Methods                                                                   */
00131 /*****************************************************************************/
00132 
00133 void MessageFieldItem::appendChild(MessageFieldItem* child) {
00134   children_.append(child);
00135 }
00136 
00137 void MessageFieldItem::update(const QString& path) {
00138   QStringList names = path.split("/");
00139 
00140   if (dataType_.isArray() && QRegExp("[1-9][0-9]*").exactMatch(
00141       names.first())) {
00142     variant_topic_tools::ArrayDataType arrayType = dataType_;
00143     
00144     if (arrayType.isDynamic()) {        
00145       if (children_.count() < 11)
00146         appendChild(new MessageFieldItem(arrayType.getMemberType(), this));          
00147       
00148       children_[0]->name_ = names.first();
00149       
00150       for (size_t i = 0; i <= 9; ++i)
00151         children_[i+1]->name_ = names.first()+QString::number(i);
00152     }
00153   }
00154   
00155   for (size_t row = 0; row < children_.count(); ++row) {
00156     MessageFieldItem* child = children_[row];
00157     
00158     if (child->dataType_.isArray()) {
00159       variant_topic_tools::ArrayDataType arrayType = child->dataType_;
00160       
00161       if (arrayType.isDynamic()) {
00162         if (child->children_.count() > 10) {
00163           for (size_t i = 0; i <= 9; ++i)
00164             child->children_[i]->name_ = QString::number(i);
00165           
00166           delete child->children_.last();        
00167           child->children_.removeLast();
00168         }
00169         
00170       }
00171     }
00172   }
00173   
00174   if (!names.isEmpty()) {
00175     MessageFieldItem* child = getChild(names.first());
00176     
00177     if (child) {
00178       names.removeFirst();
00179       child->update(names.join("/"));
00180     }
00181   }
00182 }
00183 
00184 }


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