Skip to content

Commit

Permalink
[browser] Add API to access the drive data items
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 26, 2018
1 parent 572664c commit 1072ebb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
10 changes: 10 additions & 0 deletions python/core/auto_generated/qgsbrowsermodel.sip.in
Expand Up @@ -128,6 +128,16 @@ items, i.e. it does not fetch children.
Returns true if the model has been initialized.

.. seealso:: :py:func:`initialize`
%End

QMap<QString, QgsDirectoryItem *> driveItems() const;
%Docstring
Returns a map of the root drive items shown in the browser.

These correspond to the top-level directory items shown, e.g. on Windows the C:\, D:\, etc,
and on Linux the "/" root directory.

.. versionadded:: 3.6
%End

signals:
Expand Down
16 changes: 11 additions & 5 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -173,6 +173,11 @@ void QgsBrowserModel::removeRootItems()
mDriveItems.clear();
}

QMap<QString, QgsDirectoryItem *> QgsBrowserModel::driveItems() const
{
return mDriveItems;
}

void QgsBrowserModel::initialize()
{
if ( ! mInitialized )
Expand Down Expand Up @@ -401,7 +406,7 @@ void QgsBrowserModel::refreshDrives()
continue;

// drive has been removed, remove corresponding item
if ( QgsDataItem *driveItem = mDriveItems.value( drivePath ) )
if ( QgsDirectoryItem *driveItem = mDriveItems.value( drivePath ) )
removeRootItem( driveItem );
}

Expand Down Expand Up @@ -528,7 +533,7 @@ void QgsBrowserModel::setupItemConnections( QgsDataItem *item )
this, &QgsBrowserModel::itemStateChanged );

// if it's a collection item, also forwards connectionsChanged
QgsDataCollectionItem *collectionItem = dynamic_cast<QgsDataCollectionItem *>( item );
QgsDataCollectionItem *collectionItem = qobject_cast<QgsDataCollectionItem *>( item );
if ( collectionItem )
connect( collectionItem, &QgsDataCollectionItem::connectionsChanged, this, &QgsBrowserModel::connectionsChanged );
}
Expand Down Expand Up @@ -628,7 +633,7 @@ void QgsBrowserModel::addFavoriteDirectory( const QString &directory, const QStr

void QgsBrowserModel::removeFavorite( const QModelIndex &index )
{
QgsDirectoryItem *item = dynamic_cast<QgsDirectoryItem *>( dataItem( index ) );
QgsDirectoryItem *item = qobject_cast<QgsDirectoryItem *>( dataItem( index ) );
if ( !item )
return;

Expand Down Expand Up @@ -674,9 +679,10 @@ void QgsBrowserModel::removeRootItem( QgsDataItem *item )
int i = mRootItems.indexOf( item );
beginRemoveRows( QModelIndex(), i, i );
mRootItems.remove( i );
if ( !mDriveItems.key( item ).isEmpty() )
QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem * >( item );
if ( !mDriveItems.key( dirItem ).isEmpty() )
{
mDriveItems.remove( mDriveItems.key( item ) );
mDriveItems.remove( mDriveItems.key( dirItem ) );
}
item->deleteLater();
endRemoveRows();
Expand Down
12 changes: 11 additions & 1 deletion src/core/qgsbrowsermodel.h
Expand Up @@ -148,6 +148,16 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
*/
bool initialized() const { return mInitialized; }

/**
* Returns a map of the root drive items shown in the browser.
*
* These correspond to the top-level directory items shown, e.g. on Windows the C:\, D:\, etc,
* and on Linux the "/" root directory.
*
* \since QGIS 3.6
*/
QMap<QString, QgsDirectoryItem *> driveItems() const;

signals:
//! Emitted when item children fetch was finished
void stateChanged( const QModelIndex &index, QgsDataItem::State oldState );
Expand Down Expand Up @@ -225,7 +235,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel

private:
bool mInitialized = false;
QMap< QString, QgsDataItem * > mDriveItems;
QMap< QString, QgsDirectoryItem * > mDriveItems;

void setupItemConnections( QgsDataItem *item );

Expand Down
15 changes: 15 additions & 0 deletions tests/src/core/testqgsbrowsermodel.cpp
Expand Up @@ -37,6 +37,7 @@ class TestQgsBrowserModel : public QObject
void cleanup() {} // will be called after every testfunction.

void testModel();
void driveItems();

};

Expand Down Expand Up @@ -161,5 +162,19 @@ void TestQgsBrowserModel::testModel()
QCOMPARE( model.data( model.index( 0, 0, root2Index ) ).toString(), QStringLiteral( "Child4" ) );
}

void TestQgsBrowserModel::driveItems()
{
// an unapologetically linux-directed test ;)
QgsBrowserModel model;
QVERIFY( model.driveItems().empty() );

model.initialize();
QVERIFY( !model.driveItems().empty() );
QVERIFY( model.driveItems().contains( QStringLiteral( "/" ) ) );
QgsDirectoryItem *rootItem = model.driveItems().value( QStringLiteral( "/" ) );
QVERIFY( rootItem );
QCOMPARE( rootItem->path(), QStringLiteral( "/" ) );
}

QGSTEST_MAIN( TestQgsBrowserModel )
#include "testqgsbrowsermodel.moc"

0 comments on commit 1072ebb

Please sign in to comment.