Added home directory to the browser model. Unified method to find an index for a path

This commit is contained in:
Martin Dobias 2011-07-12 21:44:08 +02:00
parent 372e8053e8
commit 9c9259a703
5 changed files with 60 additions and 39 deletions

View File

@ -74,7 +74,7 @@ QgsBrowser::QgsBrowser( QWidget *parent, Qt::WFlags flags )
QgsDebugMsg( "lastPath = " + lastPath );
if ( !lastPath.isEmpty() )
{
expand( lastPath );
expandPath( lastPath );
}
}
@ -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 );
}
}
@ -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 );

View File

@ -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 );

View File

@ -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() )
{
@ -162,29 +166,50 @@ 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() );
QModelIndex idx = index( i, 0, theIndex );
QgsDataItem *item = dataItem( idx );
if ( !item )
return QModelIndex(); // an error occurred
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();
return;
}
if ( path.indexOf( item->path() ) == 0 )
{
refresh( path, idx );
break;
}
}
}

View File

@ -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:
@ -78,7 +81,6 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
protected:
QVector<QgsDataItem*> mRootItems;
QIcon mIconDirectory;
};
#endif // QGSBROWSERMODEL_H

View File

@ -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;