Skip to content

Commit 8fcd587

Browse files
committedJul 12, 2011
Merge branch 'master' of github.com:qgis/Quantum-GIS
2 parents 540fb7a + 9c9259a commit 8fcd587

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed
 

‎src/browser/qgsbrowser.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ QgsBrowser::QgsBrowser( QWidget *parent, Qt::WFlags flags )
7474
QgsDebugMsg( "lastPath = " + lastPath );
7575
if ( !lastPath.isEmpty() )
7676
{
77-
expand( lastPath );
77+
expandPath( lastPath );
7878
}
7979
}
8080

@@ -83,21 +83,13 @@ QgsBrowser::~QgsBrowser()
8383

8484
}
8585

86-
void QgsBrowser::expand( QString path, const QModelIndex& index )
86+
void QgsBrowser::expandPath( QString path )
8787
{
88-
QStringList paths = path.split( '/' );
89-
for ( int i = 0; i < mModel->rowCount( index ); i++ )
88+
QModelIndex idx = mModel->findPath( path );
89+
if ( idx.isValid() )
9090
{
91-
QModelIndex idx = mModel->index( i, 0, index );
92-
QgsDataItem *item = mModel->dataItem( idx );
93-
94-
if ( item && path.indexOf( item->path() ) == 0 )
95-
{
96-
treeView->expand( idx );
97-
treeView->scrollTo( idx, QAbstractItemView::PositionAtTop );
98-
expand( path, idx );
99-
break;
100-
}
91+
treeView->expand( idx );
92+
treeView->scrollTo( idx, QAbstractItemView::PositionAtTop );
10193
}
10294
}
10395

@@ -302,7 +294,7 @@ void QgsBrowser::newVectorLayer()
302294
if ( !fileName.isEmpty() )
303295
{
304296
QgsDebugMsg( "New vector layer: " + fileName );
305-
expand( fileName );
297+
expandPath( fileName );
306298
QFileInfo fileInfo( fileName );
307299
QString dirPath = fileInfo.absoluteDir().path();
308300
mModel->refresh( dirPath );

‎src/browser/qgsbrowser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class QgsBrowser : public QMainWindow, private Ui::QgsBrowserBase
3434
~QgsBrowser();
3535

3636
// Expand to given path
37-
void expand( QString path, const QModelIndex& index = QModelIndex() );
37+
void expandPath( QString path );
3838

3939

4040

‎src/core/qgsbrowsermodel.cpp

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
1515
QAbstractItemModel( parent )
1616
{
17+
18+
// give the home directory a prominent first place
19+
QgsDirectoryItem *item = new QgsDirectoryItem( NULL, tr( "Home" ), QDir::homePath() );
1720
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;
2125

2226
foreach( QFileInfo drive, QDir::drives() )
2327
{
@@ -162,30 +166,51 @@ int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
162166
return 1;
163167
}
164168

165-
/* Refresh dir path */
166-
void QgsBrowserModel::refresh( QString path, const QModelIndex &theIndex )
169+
QModelIndex QgsBrowserModel::findPath( QString path )
167170
{
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 )
170175
{
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
175177

176-
if ( item->path() == path )
178+
for ( int i = 0; i < rowCount( theIndex ); i++ )
177179
{
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
182184

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+
}
187198
}
188199
}
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+
}
189214
}
190215

191216
QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const

‎src/core/qgsbrowsermodel.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
5757
bool hasChildren( const QModelIndex &parent = QModelIndex() ) const;
5858

5959
// Refresh item specified by path
60-
void refresh( QString path, const QModelIndex &index = QModelIndex() );
60+
void refresh( QString path );
6161

6262
// Refresh item childs
6363
void refresh( const QModelIndex &index = QModelIndex() );
6464

65+
//! return index of a path
66+
QModelIndex findPath( QString path );
67+
6568
void connectItem( QgsDataItem *item );
6669

6770
signals:
@@ -78,7 +81,6 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
7881

7982
protected:
8083
QVector<QgsDataItem*> mRootItems;
81-
QIcon mIconDirectory;
8284
};
8385

8486
#endif // QGSBROWSERMODEL_H

‎src/core/qgsdataitem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class CORE_EXPORT QgsDataItem : public QObject
106106
QString name() const { return mName; }
107107
QString path() const { return mPath; }
108108

109+
void setIcon( QIcon icon ) { mIcon = icon; }
110+
109111
protected:
110112

111113
Type mType;

0 commit comments

Comments
 (0)