|
14 | 14 | QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
|
15 | 15 | QAbstractItemModel( parent )
|
16 | 16 | {
|
| 17 | + |
| 18 | + // give the home directory a prominent first place |
| 19 | + QgsDirectoryItem *item = new QgsDirectoryItem( NULL, tr( "Home" ), QDir::homePath() ); |
17 | 20 | QStyle *style = QApplication::style();
|
18 |
| - mIconDirectory = QIcon( style->standardPixmap( QStyle::SP_DirClosedIcon ) ); |
19 |
| - mIconDirectory.addPixmap( style->standardPixmap( QStyle::SP_DirOpenIcon ), |
20 |
| - QIcon::Normal, QIcon::On ); |
| 21 | + QIcon homeIcon( style->standardPixmap( QStyle::QStyle::SP_DirHomeIcon ) ); |
| 22 | + item->setIcon( homeIcon ); |
| 23 | + connectItem( item ); |
| 24 | + mRootItems << item; |
21 | 25 |
|
22 | 26 | foreach( QFileInfo drive, QDir::drives() )
|
23 | 27 | {
|
@@ -162,30 +166,51 @@ int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
|
162 | 166 | return 1;
|
163 | 167 | }
|
164 | 168 |
|
165 |
| -/* Refresh dir path */ |
166 |
| -void QgsBrowserModel::refresh( QString path, const QModelIndex &theIndex ) |
| 169 | +QModelIndex QgsBrowserModel::findPath( QString path ) |
167 | 170 | {
|
168 |
| - QStringList paths = path.split( '/' ); |
169 |
| - for ( int i = 0; i < rowCount( theIndex ); i++ ) |
| 171 | + QModelIndex theIndex; // starting from root |
| 172 | + bool foundChild = true; |
| 173 | + |
| 174 | + while ( foundChild ) |
170 | 175 | {
|
171 |
| - QModelIndex idx = index( i, 0, theIndex ); |
172 |
| - QgsDataItem *item = dataItem( idx ); |
173 |
| - if ( !item ) |
174 |
| - break; |
| 176 | + foundChild = false; // assume that the next child item will not be found |
175 | 177 |
|
176 |
| - if ( item->path() == path ) |
| 178 | + for ( int i = 0; i < rowCount( theIndex ); i++ ) |
177 | 179 | {
|
178 |
| - QgsDebugMsg( "Arrived " + item->path() ); |
179 |
| - item->refresh(); |
180 |
| - return; |
181 |
| - } |
| 180 | + QModelIndex idx = index( i, 0, theIndex ); |
| 181 | + QgsDataItem *item = dataItem( idx ); |
| 182 | + if ( !item ) |
| 183 | + return QModelIndex(); // an error occurred |
182 | 184 |
|
183 |
| - if ( path.indexOf( item->path() ) == 0 ) |
184 |
| - { |
185 |
| - refresh( path, idx ); |
186 |
| - break; |
| 185 | + if ( item->path() == path ) |
| 186 | + { |
| 187 | + QgsDebugMsg( "Arrived " + item->path() ); |
| 188 | + return idx; // we have found the item we have been looking for |
| 189 | + } |
| 190 | + |
| 191 | + if ( path.startsWith( item->path() ) ) |
| 192 | + { |
| 193 | + // we have found a preceding item: stop searching on this level and go deeper |
| 194 | + foundChild = true; |
| 195 | + theIndex = idx; |
| 196 | + break; |
| 197 | + } |
187 | 198 | }
|
188 | 199 | }
|
| 200 | + |
| 201 | + return QModelIndex(); // not found |
| 202 | +} |
| 203 | + |
| 204 | +/* Refresh dir path */ |
| 205 | +void QgsBrowserModel::refresh( QString path ) |
| 206 | +{ |
| 207 | + QModelIndex idx = findPath( path ); |
| 208 | + if ( idx.isValid() ) |
| 209 | + { |
| 210 | + QgsDataItem* item = dataItem( idx ); |
| 211 | + if ( item ) |
| 212 | + item->refresh(); |
| 213 | + } |
189 | 214 | }
|
190 | 215 |
|
191 | 216 | QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
|
|
0 commit comments