diff --git a/qt/file-tree.cc b/qt/file-tree.cc index c0f580933..82ff20489 100644 --- a/qt/file-tree.cc +++ b/qt/file-tree.cc @@ -41,16 +41,31 @@ enum ***** ****/ +QHash& +FileTreeItem :: getMyChildRows() +{ + // if necessary, revalidate the name-to-row mapping + if( myChildRowsDirty ) { + myChildRows.clear(); + for (size_t i=0, n=childCount(); iname(), i); + myChildRowsDirty = false; + } + + return myChildRows; +} + + FileTreeItem :: ~FileTreeItem( ) { assert( myChildren.isEmpty( ) ); - if( myParent ) { - const int pos = myParent->myChildren.indexOf( this ); - if( pos >= 0 ) - myParent->myChildren.removeAt( pos ); - else - assert( 0 && "failed to remove" ); + const int pos = row(); + if( pos < 0 ) + assert (0 && "failed to remove"); + else { + myParent->myChildren.removeAt( pos ); + myParent->myChildRowsDirty = true; } } @@ -58,26 +73,35 @@ void FileTreeItem :: appendChild( FileTreeItem * child ) { child->myParent = this; - myChildren.append( child ); + const size_t n = childCount(); + myChildren.append (child); + myChildRows.insert (child->name(), n); } FileTreeItem * -FileTreeItem :: child( const QString& filename ) +FileTreeItem :: child (const QString& filename ) { - foreach( FileTreeItem * c, myChildren ) - if( c->name() == filename ) - return c; + FileTreeItem * item(0); - return 0; + const int row = getMyChildRows().value (filename, -1); + if (row != -1) + { + item = child (row); + assert (filename == item->name()); + } + + return item; } int FileTreeItem :: row( ) const { - int i(0); + int i(-1); if( myParent ) - i = myParent->myChildren.indexOf( const_cast(this) ); + { + i = myParent->getMyChildRows().value (name(), -1); + } return i; } diff --git a/qt/file-tree.h b/qt/file-tree.h index 74d41996d..ab9373504 100644 --- a/qt/file-tree.h +++ b/qt/file-tree.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -43,7 +44,8 @@ class FileTreeItem: public QObject FileTreeItem( int fileIndex, const QString& name="" ): myIndex(fileIndex), myParent(0), myName(name), myPriority(0), myIsWanted(0), - myHaveSize(0), myTotalSize(0) { } + myHaveSize(0), myTotalSize(0), + myChildRowsDirty(false) { } public: void appendChild( FileTreeItem *child ); @@ -72,11 +74,14 @@ class FileTreeItem: public QObject int myIndex; FileTreeItem * myParent; QList myChildren; + QHash myChildRows; + QHash& getMyChildRows(); const QString myName; int myPriority; bool myIsWanted; uint64_t myHaveSize; uint64_t myTotalSize; + bool myChildRowsDirty; }; class FileTreeModel: public QAbstractItemModel