param_item_model.cpp
Go to the documentation of this file.
00001 #include "rqt_mrta/config/param.h"
00002 #include "rqt_mrta/config/params.h"
00003 #include "rqt_mrta/param_item.h"
00004 #include "rqt_mrta/param_item_model.h"
00005 #include "utilities/exception.h"
00006 
00007 Q_DECLARE_METATYPE(rqt_mrta::config::Param*)
00008 Q_DECLARE_METATYPE(rqt_mrta::config::Params*)
00009 
00010 namespace rqt_mrta
00011 {
00012 
00013 ParamItemModel::ParamItemModel(QObject* parent)
00014     : QAbstractItemModel(parent), root_(NULL)
00015 {
00016 }
00017 
00018 ParamItemModel::~ParamItemModel()
00019 {
00020   if (root_)
00021   {
00022     delete root_;
00023     root_ = NULL;
00024   }
00025 }
00026 
00027 void ParamItemModel::setConfig(config::Config* config) {}
00028 
00029 int ParamItemModel::rowCount(const QModelIndex& parent) const
00030 {
00031   if (parent.column() <= 0)
00032   {
00033     ParamItem* item = !parent.isValid()
00034                           ? root_
00035                           : static_cast<ParamItem*>(parent.internalPointer());
00036     if (item)
00037     {
00038       return item->getNumChildren();
00039     }
00040   }
00041   return 0;
00042 }
00043 
00044 int ParamItemModel::columnCount(const QModelIndex& parent) const
00045 {
00046   if (parent.isValid())
00047   {
00048     ParamItem* item = static_cast<ParamItem*>(parent.internalPointer());
00049     if (item)
00050     {
00051       return item->getNumColumns();
00052     }
00053   }
00054   else if (root_)
00055   {
00056     return root_->getNumColumns();
00057   }
00058   return 0;
00059 }
00060 
00061 QVariant ParamItemModel::data(const QModelIndex& index, int role) const
00062 {
00063   if (index.isValid())
00064   {
00065     if (role == Qt::DisplayRole || role == Qt::EditRole)
00066     {
00067       ParamItem* item = static_cast<ParamItem*>(index.internalPointer());
00068       if (item)
00069       {
00070         if (item->isLeaf())
00071         {
00072           config::Param* param = static_cast<config::Param*>(item->getParam());
00073           return QVariant::fromValue<config::Param*>(param);
00074         }
00075         config::Params* params = static_cast<config::Params*>(item->getParam());
00076         return QVariant::fromValue<config::Params*>(params);
00077       }
00078     }
00079   }
00080   return QVariant();
00081 }
00082 
00083 QModelIndex ParamItemModel::index(int row, int column,
00084                                   const QModelIndex& parent) const
00085 {
00086   if (hasIndex(row, column, parent))
00087   {
00088     ParamItem* item = !parent.isValid() ? root_ : static_cast<ParamItem*>(parent.internalPointer());
00089     if (item)
00090     {
00091       item = item->getChild(row);
00092       if (item)
00093       {
00094         return createIndex(row, column, item);
00095       }
00096     }
00097   }
00098   return QModelIndex();
00099 }
00100 
00101 QModelIndex ParamItemModel::parent(const QModelIndex& index) const
00102 {
00103   if (index.isValid())
00104   {
00105     ParamItem* item = static_cast<ParamItem*>(index.internalPointer());
00106     if (item)
00107     {
00108       item = item->getParent();
00109       if (item != root_)
00110       {
00111         return createIndex(item->getRow(), 0, item);
00112       }
00113     }
00114   }
00115   return QModelIndex();
00116 }
00117 }


rqt_mrta
Author(s): Adriano Henrique Rossette Leite
autogenerated on Thu Jun 6 2019 18:50:52