QModelIndex QvisPluginManagerAttributesDataModel::index(int row, int column, const QModelIndex & parent) const { if(!hasIndex(row, column, parent)) return QModelIndex(); TreeItem parentItem; if(!parent.isValid()) parentItem = TreeItem(-1); // root else parentItem = TreeItem((int)parent.internalId()); TreeItem childItem(parentItem.child(row)); if(childItem.exists()) return createIndex(row, column, (quint32)childItem.index); else return QModelIndex(); }
QModelIndex QvisPluginManagerAttributesDataModel::parent(const QModelIndex &index) const { if(!index.isValid()) return QModelIndex(); TreeItem childItem((int)index.internalId()); TreeItem parentItem(childItem.parent()); if(parentItem == TreeItem(-1)) return QModelIndex(); return createIndex(parentItem.row(), 0, (int)parentItem.index); }
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent) { QList<TreeItem*> parents; QList<int> indentations; parents << parent; indentations << 0; int number = 0; while (number < lines.count()) { int position = 0; while (position < lines[number].length()) { if (lines[number].mid(position, 1) != " ") break; position++; } QString lineData = lines[number].mid(position).trimmed(); if (!lineData.isEmpty()) { // Read the column data from the rest of the line. QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); QList<QVariant> columnData; for (int column = 0; column < columnStrings.count(); ++column) columnData << columnStrings[column]; if (position > indentations.last()) { // The last child of the current parent is now the new parent // unless the current parent has no children. if (parents.last()->childCount() > 0) { parents << parents.last()->child(parents.last()->childCount()-1); indentations << position; } } else { while (position < indentations.last() && parents.count() > 0) { parents.pop_back(); indentations.pop_back(); } } // Append a new item to the current parent's list of children. parents.last()->appendChild(TreeItem(columnData, parents.last())); } ++number; } }
TreeItem child(int row) const { return TreeItem(row); }
TreeItem parent() const { return TreeItem(-1); }