MessageFieldItemModel.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 <variant_topic_tools/MessageVariable.h>
00020 
00021 #include "rqt_multiplot/MessageFieldItemModel.h"
00022 
00023 Q_DECLARE_METATYPE(variant_topic_tools::DataType)
00024 
00025 namespace rqt_multiplot {
00026 
00027 /*****************************************************************************/
00028 /* Constructors and Destructor                                               */
00029 /*****************************************************************************/
00030 
00031 MessageFieldItemModel::MessageFieldItemModel(QObject* parent) :
00032   QAbstractItemModel(parent),
00033   rootItem_(0) {
00034 }
00035 
00036 MessageFieldItemModel::~MessageFieldItemModel() {
00037   if (rootItem_)
00038     delete rootItem_;
00039 }
00040 
00041 /*****************************************************************************/
00042 /* Accessors                                                                 */
00043 /*****************************************************************************/
00044 
00045 void MessageFieldItemModel::setMessageDataType(const variant_topic_tools::
00046     MessageDataType& dataType) {
00047   if (rootItem_) {
00048     delete rootItem_;
00049     rootItem_ = 0;
00050   }
00051   
00052   if (dataType.isValid())
00053     rootItem_ = new MessageFieldItem(dataType);
00054 }
00055 
00056 variant_topic_tools::MessageDataType MessageFieldItemModel::
00057     getMessageDataType() const {
00058   if (rootItem_)
00059     return rootItem_->getDataType();
00060   else
00061     return variant_topic_tools::MessageDataType();
00062 }
00063 
00064 variant_topic_tools::DataType MessageFieldItemModel::getFieldDataType(const
00065     QString& field) const {
00066   if (rootItem_) {
00067     MessageFieldItem* descendant = rootItem_->getDescendant(field);
00068     
00069     if (descendant)
00070       return descendant->getDataType();
00071   }
00072   
00073   return variant_topic_tools::DataType();
00074 }
00075 
00076 /*****************************************************************************/
00077 /* Methods                                                                   */
00078 /*****************************************************************************/
00079 
00080 int MessageFieldItemModel::rowCount(const QModelIndex& parent) const {
00081   if (parent.column() <= 0) {
00082     MessageFieldItem* parentItem = 0;
00083   
00084     if (!parent.isValid())
00085       parentItem = rootItem_;
00086     else
00087       parentItem = static_cast<MessageFieldItem*>(parent.internalPointer());
00088 
00089     if (parentItem)
00090       return parentItem->getNumChildren();
00091   }
00092 
00093   return 0;
00094 }
00095 
00096 int MessageFieldItemModel::columnCount(const QModelIndex& parent) const {
00097   if (parent.isValid()) {
00098     MessageFieldItem* parentItem = static_cast<MessageFieldItem*>(
00099       parent.internalPointer());
00100     
00101     if (parentItem)
00102       return parentItem->getNumColumns();
00103   }
00104   else if (rootItem_)
00105     return rootItem_->getNumColumns();
00106   
00107 
00108   return 0;
00109 }
00110 
00111 QVariant MessageFieldItemModel::data(const QModelIndex& index, int role)
00112     const {
00113   if (index.isValid()) {
00114     if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
00115       MessageFieldItem* item = static_cast<MessageFieldItem*>(
00116         index.internalPointer());
00117 
00118       if (item)
00119         return item->getName();
00120     }
00121   }
00122 
00123   return QVariant();
00124 }
00125 
00126 QModelIndex MessageFieldItemModel::index(int row, int column, const
00127     QModelIndex& parent) const {
00128   if (hasIndex(row, column, parent)) {
00129     MessageFieldItem* parentItem = 0;
00130 
00131     if (!parent.isValid())
00132       parentItem = rootItem_;
00133     else
00134       parentItem = static_cast<MessageFieldItem*>(parent.internalPointer());
00135 
00136     if (parentItem) {
00137       MessageFieldItem* childItem = parentItem->getChild(row);
00138       
00139       if (childItem)
00140         return createIndex(row, column, childItem);
00141     }
00142   }
00143 
00144   return QModelIndex();
00145 }
00146 
00147 QModelIndex MessageFieldItemModel::parent(const QModelIndex& index) const {
00148   if (index.isValid()) {
00149     MessageFieldItem* childItem = static_cast<MessageFieldItem*>(
00150       index.internalPointer());
00151 
00152     if (childItem) {
00153       MessageFieldItem* parentItem = childItem->getParent();
00154 
00155       if (parentItem != rootItem_)
00156         return createIndex(parentItem->getRow(), 0, parentItem);
00157     }
00158   }
00159   
00160   return QModelIndex();
00161 }
00162 
00163 void MessageFieldItemModel::update(const QString& path) {
00164   if (rootItem_)
00165     rootItem_->update(path);    
00166 }
00167 
00168 }


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