bool BtTreeModel::renameFolder(BtFolder* victim, QString newName) { QModelIndex ndx = findFolder(victim->fullPath(), 0, false); QModelIndex pInd; QString targetPath = newName % "/" % victim->name(); QPair<QString,BtTreeItem*> f; QList<QPair<QString, BtTreeItem*> > folders; // This space is important ^ int i, kids,src; if ( ! ndx.isValid() ) return false; pInd = parent(ndx); if ( ! pInd.isValid() ) return false; BtTreeItem* start = item(ndx); f.first = targetPath; f.second = start; folders.append(f); while ( ! folders.isEmpty() ) { // This looks weird, but it is needed for later f = folders.takeFirst(); targetPath = f.first; BtTreeItem* target = f.second; // As we move things, childCount changes. This makes sure we loop // through all of the kids kids = target->childCount(); src = 0; // Ok. We have a start and an index. for (i=0; i < kids; ++i) { // This looks weird and it is. As we move children out, the 0 items // changes to the next child. In the case of a folder, though, we // don't move it, so we need to get the item beyond that. BtTreeItem* next = target->child(src); // If a folder, push it onto the folders stack for latter processing if ( next->type() == BtTreeItem::FOLDER ) { QPair<QString,BtTreeItem*> newTarget; newTarget.first = targetPath % "/" % next->name(); newTarget.second = next; folders.append(newTarget); src++; } else // Leafnode next->thing()->setFolder(targetPath); } } // Last thing is to remove the victim. i = start->childNumber(); return removeRows(i, 1, pInd); }
bool BtTreeModel::renameFolder(BtFolder* victim, QString newName) { QModelIndex ndx = findFolder(victim->fullPath(), 0, false); QModelIndex pInd; QString targetPath = newName % "/" % victim->name(); QPair<QString,BtTreeItem*> f; QList<QPair<QString, BtTreeItem*> > folders; // This space is important ^ int i; if ( ! ndx.isValid() ) return false; pInd = parent(ndx); if ( ! pInd.isValid() ) return false; BtTreeItem* start = item(ndx); f.first = targetPath; f.second = start; folders.append(f); while ( ! folders.isEmpty() ) { // This looks weird, but it is needed for later f = folders.takeFirst(); targetPath = f.first; BtTreeItem* target = f.second; // Ok. We have a start and an index. for (i=0; i < target->childCount(); ++i) { BtTreeItem* next = target->child(i); // If a folder, push it onto the folders stack for latter processing if ( next->type() == BtTreeItem::FOLDER ) { QPair<QString,BtTreeItem*> newTarget; newTarget.first = targetPath % "/" % next->name(); newTarget.second = next; folders.append(newTarget); } else // Leafnode next->thing()->setFolder(targetPath); } } // Last thing is to remove the victim. i = start->childNumber(); return removeRows(i, 1, pInd); }