MessageFieldItemModel.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <variant_topic_tools/MessageVariable.h>
20 
22 
23 Q_DECLARE_METATYPE(variant_topic_tools::DataType)
24 
25 namespace rqt_multiplot {
26 
27 /*****************************************************************************/
28 /* Constructors and Destructor */
29 /*****************************************************************************/
30 
31 MessageFieldItemModel::MessageFieldItemModel(QObject* parent) :
32  QAbstractItemModel(parent),
33  rootItem_(0) {
34 }
35 
37  if (rootItem_)
38  delete rootItem_;
39 }
40 
41 /*****************************************************************************/
42 /* Accessors */
43 /*****************************************************************************/
44 
45 void MessageFieldItemModel::setMessageDataType(const variant_topic_tools::
46  MessageDataType& dataType) {
47  if (rootItem_) {
48  delete rootItem_;
49  rootItem_ = 0;
50  }
51 
52  if (dataType.isValid())
53  rootItem_ = new MessageFieldItem(dataType);
54 }
55 
56 variant_topic_tools::MessageDataType MessageFieldItemModel::
58  if (rootItem_)
59  return rootItem_->getDataType();
60  else
61  return variant_topic_tools::MessageDataType();
62 }
63 
64 variant_topic_tools::DataType MessageFieldItemModel::getFieldDataType(const
65  QString& field) const {
66  if (rootItem_) {
67  MessageFieldItem* descendant = rootItem_->getDescendant(field);
68 
69  if (descendant)
70  return descendant->getDataType();
71  }
72 
73  return variant_topic_tools::DataType();
74 }
75 
76 /*****************************************************************************/
77 /* Methods */
78 /*****************************************************************************/
79 
80 int MessageFieldItemModel::rowCount(const QModelIndex& parent) const {
81  if (parent.column() <= 0) {
82  MessageFieldItem* parentItem = 0;
83 
84  if (!parent.isValid())
85  parentItem = rootItem_;
86  else
87  parentItem = static_cast<MessageFieldItem*>(parent.internalPointer());
88 
89  if (parentItem)
90  return parentItem->getNumChildren();
91  }
92 
93  return 0;
94 }
95 
96 int MessageFieldItemModel::columnCount(const QModelIndex& parent) const {
97  if (parent.isValid()) {
98  MessageFieldItem* parentItem = static_cast<MessageFieldItem*>(
99  parent.internalPointer());
100 
101  if (parentItem)
102  return parentItem->getNumColumns();
103  }
104  else if (rootItem_)
105  return rootItem_->getNumColumns();
106 
107 
108  return 0;
109 }
110 
111 QVariant MessageFieldItemModel::data(const QModelIndex& index, int role)
112  const {
113  if (index.isValid()) {
114  if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
115  MessageFieldItem* item = static_cast<MessageFieldItem*>(
116  index.internalPointer());
117 
118  if (item)
119  return item->getName();
120  }
121  }
122 
123  return QVariant();
124 }
125 
126 QModelIndex MessageFieldItemModel::index(int row, int column, const
127  QModelIndex& parent) const {
128  if (hasIndex(row, column, parent)) {
129  MessageFieldItem* parentItem = 0;
130 
131  if (!parent.isValid())
132  parentItem = rootItem_;
133  else
134  parentItem = static_cast<MessageFieldItem*>(parent.internalPointer());
135 
136  if (parentItem) {
137  MessageFieldItem* childItem = parentItem->getChild(row);
138 
139  if (childItem)
140  return createIndex(row, column, childItem);
141  }
142  }
143 
144  return QModelIndex();
145 }
146 
147 QModelIndex MessageFieldItemModel::parent(const QModelIndex& index) const {
148  if (index.isValid()) {
149  MessageFieldItem* childItem = static_cast<MessageFieldItem*>(
150  index.internalPointer());
151 
152  if (childItem) {
153  MessageFieldItem* parentItem = childItem->getParent();
154 
155  if (parentItem != rootItem_)
156  return createIndex(parentItem->getRow(), 0, parentItem);
157  }
158  }
159 
160  return QModelIndex();
161 }
162 
163 void MessageFieldItemModel::update(const QString& path) {
164  if (rootItem_)
165  rootItem_->update(path);
166 }
167 
168 }
const QString & getName() const
MessageFieldItem * getChild(size_t row) const
const variant_topic_tools::DataType & getDataType() const
QModelIndex index(int row, int column, const QModelIndex &parent) const
MessageFieldItem * getParent() const
MessageFieldItem * getDescendant(const QString &path) const
int columnCount(const QModelIndex &parent) const
QModelIndex parent(const QModelIndex &index) const
void update(const QString &path)
int rowCount(const QModelIndex &parent) const
QVariant data(const QModelIndex &index, int role) const
variant_topic_tools::DataType getFieldDataType(const QString &field) const
void setMessageDataType(const variant_topic_tools::MessageDataType &dataType)
variant_topic_tools::MessageDataType getMessageDataType() const


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44