virtual WModelIndex parent(const WModelIndex& index) const { if (!index.isValid() || index.internalId() == 0) { return WModelIndex(); // treeData_[0] is the tree root } else { const Tree& item = treeData_[index.internalId()]; return createIndex(item.index(), 0, item.parentId()); } }
WModelIndex GitModel::parent(const WModelIndex& index) const { // treeData_[0] indicates the top-level parent. if (!index.isValid() || index.internalId() == 0) return WModelIndex(); else { // get the item that corresponds to the parent ... const Tree& item = treeData_[index.internalId()]; // ... and construct that identifies the parent: // row = child index in the grand parent // internalId = id of the grand parent return createIndex(item.index(), 0, item.parentId()); } }
int GitModel::rowCount(const WModelIndex& index) const { // we are looking for the git SHA1 id of a tree object (since only folders // may contain children). Git::ObjectId objectId; int treeId; if (index.isValid()) { // only column 0 items may contain children if (index.column() != 0) return 0; Git::Object o = getObject(index); if (o.type == Git::Tree) { objectId = o.id; treeId = getTreeId(index.internalId(), index.row()); } else // not a folder: no children return 0; } else { treeId = 0; // the index corresponds to the root object if (treeData_.empty()) // model not yet loaded ! return 0; else objectId = treeData_[0].treeObject(); } return treeData_[treeId].rowCount(); }
virtual WModelIndex index(int row, int column, const WModelIndex& parent = WModelIndex()) const { int parentId; if (!parent.isValid()) parentId = 0; else { int grandParentId = parent.internalId(); parentId = getTreeId(grandParentId, parent.row()); } return createIndex(row, column, parentId); }
virtual int rowCount(const WModelIndex& parent = WModelIndex()) const { int treeId; if (parent.isValid()) { if (parent.column() != 0) return 0; Git::Object o = getObject(parent); if (o.type == Git::Tree) { // is a folder treeId = getTreeId(parent.internalId(), parent.row()); } else // is a file return 0; } else { treeId = 0; } return treeData_[treeId].rowCount(); }
WModelIndex GitModel::index(int row, int column, const WModelIndex& parent) const { int parentId; // the top-level parent has id=0. if (!parent.isValid()) parentId = 0; else { // the internal id of the parent identifies the grand parent int grandParentId = parent.internalId(); // lookup the parent id for the parent himself, based on grand parent // and child-index (=row) within the grand parent parentId = getTreeId(grandParentId, parent.row()); } return createIndex(row, column, parentId); }
/* * Gets the Git::Object that corresponds to an index. */ Git::Object getObject(const WModelIndex& index) const { int parentId = index.internalId(); const Tree& parentItem = treeData_[parentId]; return git_.treeGetObject(parentItem.treeObject(), index.row()); }