Skip to content
Permalink
Browse files

Added home directory to the browser model. Unified method to find an …

…index for a path
  • Loading branch information
wonder-sk committed Jul 12, 2011
1 parent 372e805 commit 9c9259a703d0f2418a033a16115a5f48e4114a43
Showing with 59 additions and 38 deletions.
  1. +7 −15 src/browser/qgsbrowser.cpp
  2. +1 −1 src/browser/qgsbrowser.h
  3. +45 −20 src/core/qgsbrowsermodel.cpp
  4. +4 −2 src/core/qgsbrowsermodel.h
  5. +2 −0 src/core/qgsdataitem.h
@@ -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 );
@@ -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 );



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

0 comments on commit 9c9259a

Please sign in to comment.
You can’t perform that action at this time.