Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' of github.com:qgis/Quantum-GIS
  • Loading branch information
pka committed Jul 12, 2011
2 parents 540fb7a + 9c9259a commit 8fcd587
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 38 deletions.
22 changes: 7 additions & 15 deletions src/browser/qgsbrowser.cpp
Expand Up @@ -74,7 +74,7 @@ QgsBrowser::QgsBrowser( QWidget *parent, Qt::WFlags flags )
QgsDebugMsg( "lastPath = " + lastPath );
if ( !lastPath.isEmpty() )
{
expand( lastPath );
expandPath( lastPath );
}
}

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

}

void QgsBrowser::expand( QString path, const QModelIndex& index )
void QgsBrowser::expandPath( QString path )
{
QStringList paths = path.split( '/' );
for ( int i = 0; i < mModel->rowCount( index ); i++ )
QModelIndex idx = mModel->findPath( path );
if ( idx.isValid() )
{
QModelIndex idx = mModel->index( i, 0, index );
QgsDataItem *item = mModel->dataItem( idx );

if ( item && path.indexOf( item->path() ) == 0 )
{
treeView->expand( idx );
treeView->scrollTo( idx, QAbstractItemView::PositionAtTop );
expand( path, idx );
break;
}
treeView->expand( idx );
treeView->scrollTo( idx, QAbstractItemView::PositionAtTop );
}
}

Expand Down Expand Up @@ -302,7 +294,7 @@ void QgsBrowser::newVectorLayer()
if ( !fileName.isEmpty() )
{
QgsDebugMsg( "New vector layer: " + fileName );
expand( fileName );
expandPath( fileName );
QFileInfo fileInfo( fileName );
QString dirPath = fileInfo.absoluteDir().path();
mModel->refresh( dirPath );
Expand Down
2 changes: 1 addition & 1 deletion src/browser/qgsbrowser.h
Expand Up @@ -34,7 +34,7 @@ class QgsBrowser : public QMainWindow, private Ui::QgsBrowserBase
~QgsBrowser();

// Expand to given path
void expand( QString path, const QModelIndex& index = QModelIndex() );
void expandPath( QString path );



Expand Down
65 changes: 45 additions & 20 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -14,10 +14,14 @@
QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
QAbstractItemModel( parent )
{

// give the home directory a prominent first place
QgsDirectoryItem *item = new QgsDirectoryItem( NULL, tr( "Home" ), QDir::homePath() );
QStyle *style = QApplication::style();
mIconDirectory = QIcon( style->standardPixmap( QStyle::SP_DirClosedIcon ) );
mIconDirectory.addPixmap( style->standardPixmap( QStyle::SP_DirOpenIcon ),
QIcon::Normal, QIcon::On );
QIcon homeIcon( style->standardPixmap( QStyle::QStyle::SP_DirHomeIcon ) );
item->setIcon( homeIcon );
connectItem( item );
mRootItems << item;

foreach( QFileInfo drive, QDir::drives() )
{
Expand Down Expand Up @@ -162,30 +166,51 @@ int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
return 1;
}

/* Refresh dir path */
void QgsBrowserModel::refresh( QString path, const QModelIndex &theIndex )
QModelIndex QgsBrowserModel::findPath( QString path )
{
QStringList paths = path.split( '/' );
for ( int i = 0; i < rowCount( theIndex ); i++ )
QModelIndex theIndex; // starting from root
bool foundChild = true;

while ( foundChild )
{
QModelIndex idx = index( i, 0, theIndex );
QgsDataItem *item = dataItem( idx );
if ( !item )
break;
foundChild = false; // assume that the next child item will not be found

if ( item->path() == path )
for ( int i = 0; i < rowCount( theIndex ); i++ )
{
QgsDebugMsg( "Arrived " + item->path() );
item->refresh();
return;
}
QModelIndex idx = index( i, 0, theIndex );
QgsDataItem *item = dataItem( idx );
if ( !item )
return QModelIndex(); // an error occurred

if ( path.indexOf( item->path() ) == 0 )
{
refresh( path, idx );
break;
if ( item->path() == path )
{
QgsDebugMsg( "Arrived " + item->path() );
return idx; // we have found the item we have been looking for
}

if ( path.startsWith( item->path() ) )
{
// we have found a preceding item: stop searching on this level and go deeper
foundChild = true;
theIndex = idx;
break;
}
}
}

return QModelIndex(); // not found
}

/* Refresh dir path */
void QgsBrowserModel::refresh( QString path )
{
QModelIndex idx = findPath( path );
if ( idx.isValid() )
{
QgsDataItem* item = dataItem( idx );
if ( item )
item->refresh();
}
}

QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsbrowsermodel.h
Expand Up @@ -57,11 +57,14 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
bool hasChildren( const QModelIndex &parent = QModelIndex() ) const;

// Refresh item specified by path
void refresh( QString path, const QModelIndex &index = QModelIndex() );
void refresh( QString path );

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

//! return index of a path
QModelIndex findPath( QString path );

void connectItem( QgsDataItem *item );

signals:
Expand All @@ -78,7 +81,6 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel

protected:
QVector<QgsDataItem*> mRootItems;
QIcon mIconDirectory;
};

#endif // QGSBROWSERMODEL_H
2 changes: 2 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -106,6 +106,8 @@ class CORE_EXPORT QgsDataItem : public QObject
QString name() const { return mName; }
QString path() const { return mPath; }

void setIcon( QIcon icon ) { mIcon = icon; }

protected:

Type mType;
Expand Down

0 comments on commit 8fcd587

Please sign in to comment.