32 #include <QStringList> 42 : QAbstractItemModel(parent), root_property_(root_property)
72 return property->getViewFlags(index.column());
77 if (parent_index.isValid() && parent_index.column() != 0)
86 return createIndex(row, column, child);
96 if (!child_index.isValid())
108 return QModelIndex();
113 return QModelIndex();
125 if (!index.isValid())
144 if (property->getValue().type() == QVariant::Bool && role == Qt::CheckStateRole)
146 if (property->setValue(value.toInt() != Qt::Unchecked))
152 if (role != Qt::EditRole)
157 return property->setValue(value);
168 if (indexes.count() <= 0)
177 QMimeData*
data =
new QMimeData();
178 QString format = types.at(0);
180 QDataStream stream(&encoded, QIODevice::WriteOnly);
182 QModelIndexList::ConstIterator it = indexes.begin();
183 for (; it != indexes.end(); ++it)
185 if ((*it).column() == 0)
187 void* pointer = (*it).internalPointer();
188 stream.writeRawData((
char*)&pointer,
sizeof(
void*));
192 data->setData(format, encoded);
204 Qt::DropAction action,
207 const QModelIndex& dest_parent)
209 if (!data || action != Qt::MoveAction)
218 QString format = types.at(0);
219 if (!data->hasFormat(format))
223 QByteArray encoded = data->data(format);
224 QDataStream stream(&encoded, QIODevice::ReadOnly);
228 QList<Property*> source_properties;
231 while (!stream.atEnd())
234 if (
sizeof(
void*) != stream.readRawData((
char*)&pointer,
sizeof(
void*)))
236 printf(
"ERROR: dropped mime data has invalid pointer data.\n");
240 if (prop == dest_parent_property || prop->
isAncestorOf(dest_parent_property))
245 source_properties.append(prop);
252 for (
int i = 0; i < source_properties.size(); i++)
254 Property* prop = source_properties.at(i);
262 if (dest_parent_property == prop->
getParent() && dest_row > source_row)
267 dest_parent_property->
addChild(prop, dest_row);
285 return QModelIndex();
296 QModelIndex left_index =
indexOf(property);
297 QModelIndex right_index = createIndex(left_index.row(), 1, left_index.internalPointer());
298 Q_EMIT dataChanged(left_index, right_index);
307 beginInsertRows(
indexOf(parent_property), row_within_parent, row_within_parent + count - 1);
322 beginRemoveRows(
indexOf(parent_property), row_within_parent, row_within_parent + count - 1);
343 QModelIndexList indexes = persistentIndexList();
344 QModelIndexList::ConstIterator it = indexes.begin();
345 for (; it != indexes.end(); ++it)
347 if (!(*it).isValid())
349 printf(
" invalid index\n");
356 printf(
" null property\n");
360 printf(
" prop name '%s'\n", qPrintable(prop->
getName()));
QStringList mimeTypes() const override
Returns a list with just "application/x-rviz-" plus drag_drop_class_.
PropertyTreeModel(Property *root_property, QObject *parent=nullptr)
Constructor.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QModelIndex parentIndex(const Property *child) const
Same as parent() but taking a Property pointer instead of an index.
Property * getParent() const
Return the parent Property.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
bool isAncestorOf(Property *possible_child) const
Returns true if this is an ancestor of possible_child, meaning is the parent or parent of parent etc...
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Return the number of rows under the given parent index.
A single element of a property tree, with a name, value, description, and possibly children...
void printPersistentIndices()
For debugging only. Uses printf() to print the property names of current persistent indices...
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void collapse(const QModelIndex &index)
Emitted when a Property wants to collapse (hide its children).
QModelIndex parent(const QModelIndex &index) const override
void expand(const QModelIndex &index)
Emitted when a Property wants to expand (display its children).
QMimeData * mimeData(const QModelIndexList &indexes) const override
Override from QAbstractItemModel. Returns a (non-standard) mime-encoded version of the given indexes...
int rowNumberInParent() const
Return the row number of this property within its parent, or -1 if it has no parent.
void beginRemove(Property *parent_property, int row_within_parent, int count=1)
void expandProperty(Property *property)
Expand (show the children of) the given Property.
virtual QVariant getViewData(int column, int role) const
Return data appropriate for the given column (0 or 1) and role for this Property. ...
void setModel(PropertyTreeModel *model)
Set the model managing this Property and all its child properties, recursively.
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
~PropertyTreeModel() override
Destructor. Deletes the root property (and thus the entire property tree).
Property * getProp(const QModelIndex &index) const
return the Property at the given index, or the root property if the index is invalid.
virtual QString getName() const
Return the name of this Property as a QString.
QVariant data(const QModelIndex &index, int role) const override
bool shouldBeSaved() const
Returns true if the property is not read-only AND has data worth saving.
void configChanged()
Emitted when a Property which should be saved changes.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int destination_row, int destination_column, const QModelIndex &destination_parent) override
Override from QAbstractItemModel. Takes a (non-standard) mime-encoded version of an index list and dr...
QModelIndex indexOf(Property *property) const
virtual Property * takeChildAt(int index)
Take a child out of the child list, but don't destroy it.
virtual int numChildren() const
Return the number of child objects (Property or otherwise).
Property * childAt(int index) const
Return the child Property with the given index, or NULL if the index is out of bounds or if the child...
virtual void addChild(Property *child, int index=-1)
Add a child property.
void collapseProperty(Property *property)
Collapse (hide the children of) the given Property.
void emitDataChanged(Property *property)
void beginInsert(Property *parent_property, int row_within_parent, int count=1)
Qt::ItemFlags flags(const QModelIndex &index) const override
Property * root_property_