xmlRpcModel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, C. Dornhege, University of Freiburg
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the University of Freiburg nor the names
14  * of its contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
30 
31 XmlRpcModel::XmlRpcModel(XmlRpc::XmlRpcValue* rootData, const std::string & rootPath, ros::NodeHandle* nh)
32 {
33  _root = new XmlRpcTreeItem(rootData, NULL, rootPath, nh);
34  _maxDisplayLength = 120;
35 }
36 
38 {
39  delete _root;
40 }
41 
42 QModelIndex XmlRpcModel::index(int row, int column, const QModelIndex & parent) const
43 {
44  if(!parent.isValid()) {
45  return createIndex(row, column, _root);
46  }
47 
48  XmlRpcTreeItem* parentItem = static_cast<XmlRpcTreeItem*>(parent.internalPointer());
49  // parent points to the (parent.row, parent.column) child of parentItem and its corresponding tree node
50  // We want to create a child of that.
51  // First check if that (parent.row, parent.column) child of parentItem can be a parent for an index again
52  // Only items in the first column can be parents
53  if(parent.column() > 0)
54  return QModelIndex();
55  // Lookup that child item and use it as parent for the new index
56  XmlRpcTreeItem* childItem = parentItem->child(parent.row());
57  if(childItem) {
58  // only items with children can be parents
59  if(childItem->childCount() == 0)
60  return QModelIndex();
61  // create an index referring to the (row, column) child of childItem
62  return createIndex(row, column, childItem);
63  } else {
64  return QModelIndex();
65  }
66 }
67 
68 QModelIndex XmlRpcModel::parent(const QModelIndex & index) const
69 {
70  if(!index.isValid())
71  return QModelIndex();
72 
73  XmlRpcTreeItem* childItem = static_cast<XmlRpcTreeItem*>(index.internalPointer());
74  XmlRpcTreeItem* parentItem = childItem->parent();
75 
76  // top level
77  if(parentItem == NULL)
78  return QModelIndex();
79 
80  return createIndex(childItem->row(), 0, parentItem);
81 }
82 
83 Qt::ItemFlags XmlRpcModel::flags(const QModelIndex & index) const
84 {
85  if(!index.isValid())
86  return 0;
87 
88  if(index.column() == 1) { // right sides of inner nodes have no flags
89  XmlRpcTreeItem *parentItem = static_cast<XmlRpcTreeItem*>(index.internalPointer());
90  XmlRpcTreeItem *item = parentItem->child(index.row());
91  // index points to index.rows child of parentItem
92  if(item && item->childCount() > 0) {
93  return 0;
94  }
95  }
96 
97  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
98 
99  if(index.column() == 1) { // right side = value, editable
100  // if the data is bool make it checkable
101  XmlRpcTreeItem *item = static_cast<XmlRpcTreeItem*>(index.internalPointer());
102  if(item->isBool(index.row(), index.column())) {
103  flags |= Qt::ItemIsUserCheckable;
104  } else {
105  flags |= Qt::ItemIsEditable;
106  }
107  }
108 
109  return flags;
110 }
111 
112 QVariant XmlRpcModel::data(const QModelIndex & index, int role) const
113 {
114  if (!index.isValid())
115  return QVariant();
116 
117  //printf("data() Role is %d\n", role);
118  if(role != Qt::DisplayRole && role != Qt::CheckStateRole && role != Qt::EditRole)
119  return QVariant();
120 
121  XmlRpcTreeItem* item = static_cast<XmlRpcTreeItem*>(index.internalPointer());
122 
123  if(role == Qt::CheckStateRole) { // only valid for bools
124  if(!item->isBool(index.row(), index.column()))
125  return QVariant();
126  //printf("RET IS %d", item->data(index.row(), index.column()).toBool());
127 
128  // for bool values in CheckStateRole return Qt::Checked/Unchecked instead of true/false
129  return item->data(index.row(), index.column()).toBool() ? Qt::Checked : Qt::Unchecked;
130  }
131 
132  if(role == Qt::DisplayRole) { // no text for bools
133  if(item->isBool(index.row(), index.column()))
134  return QVariant();
135  else {
136  QVariant itemDisplay = item->data(index.row(), index.column());
137  if(itemDisplay.type() != QVariant::String) {
138  return itemDisplay;
139  }
140  // convert here to string to check _maxDisplayLength
141  QString itemStr = itemDisplay.toString();
142  unsigned int maxLength = _maxDisplayLength;
143  if(maxLength < 3)
144  maxLength = 3;
145  if(itemStr.length() > (int)maxLength) {
146  itemStr = itemStr.mid(0, maxLength - 3) + "...";
147  }
148  return itemStr;
149  }
150  }
151 
152  if(role == Qt::EditRole) {
153  return item->data(index.row(), index.column());
154  }
155 
156  // never called
157  return QVariant();
158 }
159 
160 bool XmlRpcModel::setData(const QModelIndex & index, const QVariant & value, int role)
161 {
162  if(!index.isValid())
163  return false;
164  if(role != Qt::EditRole && role != Qt::CheckStateRole)
165  return false;
166 
167  if(index.column() != 1)
168  return false;
169 
170  XmlRpcTreeItem* parentItem = static_cast<XmlRpcTreeItem*>(index.internalPointer());
171  if(parentItem->isBool(index.row(), index.column())) {
172  if(role == Qt::EditRole)
173  return false;
174  }
175  if(!parentItem->isBool(index.row(), index.column())) {
176  if(role == Qt::CheckStateRole)
177  return false;
178  }
179 
180  XmlRpcTreeItem* item = parentItem->child(index.row());
181 
182  // this should always be a leaf, never a struct!
183  if(item->setData(value)) {
184  Q_EMIT dataChanged(index, index);
185  return true;
186  }
187 
188  return false;
189 }
190 
191 QVariant XmlRpcModel::headerData(int section, Qt::Orientation orientation, int role) const
192 {
193  if(role != Qt::DisplayRole || orientation != Qt::Horizontal)
194  return QVariant();
195 
196  if(section == 0)
197  return "Parameter";
198  if(section == 1)
199  return "Value";
200 
201  return QVariant();
202 }
203 
204 int XmlRpcModel::rowCount(const QModelIndex & parent) const
205 {
206  if(!parent.isValid()) {
207  return _root->childCount();
208  }
209 
210  if(parent.column() > 0)
211  return 0;
212 
213  // parent refers to the parent.row'th child of parentItem
214  // get that item
215  XmlRpcTreeItem* parentItem = static_cast<XmlRpcTreeItem*>(parent.internalPointer());
216  XmlRpcTreeItem* item = parentItem->child(parent.row());
217 
218  if(item == NULL)
219  return 0;
220  return item->childCount();
221 }
222 
223 int XmlRpcModel::columnCount(const QModelIndex & parent) const
224 {
225  if(!parent.isValid()) {
226  if(_root->childCount() > 0)
227  return 2;
228  return 0;
229  }
230 
231  if(parent.column() > 0)
232  return 0;
233 
234  // parent is the parent.row'th child of parentItem
235  // get that item
236  XmlRpcTreeItem* parentItem = static_cast<XmlRpcTreeItem*>(parent.internalPointer());
237  XmlRpcTreeItem* item = parentItem->child(parent.row());
238 
239  if(item == NULL)
240  return 0;
241 
242  // with children we address 0->name 1->value
243  if(item->childCount() > 0)
244  return 2;
245  return 0;
246 }
247 
unsigned int _maxDisplayLength
Definition: xmlRpcModel.h:70
A wrapper around the XmlRpcValue including a parent pointer and convenience functions for the qt mode...
XmlRpcModel(XmlRpc::XmlRpcValue *rootData, const std::string &rootPath, ros::NodeHandle *nh)
Definition: xmlRpcModel.cpp:31
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: xmlRpcModel.cpp:42
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
XmlRpcTreeItem * _root
Definition: xmlRpcModel.h:68
XmlRpcTreeItem * parent()
QModelIndex parent(const QModelIndex &index) const
Definition: xmlRpcModel.cpp:68
bool setData(QVariant val)
set data for this item
int rowCount(const QModelIndex &parent=QModelIndex()) const
should return the number of children for a parent item
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
int columnCount(const QModelIndex &parent=QModelIndex()) const
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: xmlRpcModel.cpp:83
bool isBool(int row, int column) const
is the stored data a bool
QVariant data(int row, int column) const
return the data in the map
XmlRpcTreeItem * child(unsigned int i)
int row() const
Figure out which row/nth child we are for the parent.
QVariant data(const QModelIndex &index, int role) const
unsigned int childCount() const
virtual ~XmlRpcModel()
Definition: xmlRpcModel.cpp:37


qt_paramedit
Author(s): Christian Dornhege
autogenerated on Mon Feb 28 2022 23:37:51