Skip to content

Commit 1072ebb

Browse files
committedNov 26, 2018
[browser] Add API to access the drive data items
1 parent 572664c commit 1072ebb

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed
 

‎python/core/auto_generated/qgsbrowsermodel.sip.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ items, i.e. it does not fetch children.
128128
Returns true if the model has been initialized.
129129

130130
.. seealso:: :py:func:`initialize`
131+
%End
132+
133+
QMap<QString, QgsDirectoryItem *> driveItems() const;
134+
%Docstring
135+
Returns a map of the root drive items shown in the browser.
136+
137+
These correspond to the top-level directory items shown, e.g. on Windows the C:\, D:\, etc,
138+
and on Linux the "/" root directory.
139+
140+
.. versionadded:: 3.6
131141
%End
132142

133143
signals:

‎src/core/qgsbrowsermodel.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ void QgsBrowserModel::removeRootItems()
173173
mDriveItems.clear();
174174
}
175175

176+
QMap<QString, QgsDirectoryItem *> QgsBrowserModel::driveItems() const
177+
{
178+
return mDriveItems;
179+
}
180+
176181
void QgsBrowserModel::initialize()
177182
{
178183
if ( ! mInitialized )
@@ -401,7 +406,7 @@ void QgsBrowserModel::refreshDrives()
401406
continue;
402407

403408
// drive has been removed, remove corresponding item
404-
if ( QgsDataItem *driveItem = mDriveItems.value( drivePath ) )
409+
if ( QgsDirectoryItem *driveItem = mDriveItems.value( drivePath ) )
405410
removeRootItem( driveItem );
406411
}
407412

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

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

629634
void QgsBrowserModel::removeFavorite( const QModelIndex &index )
630635
{
631-
QgsDirectoryItem *item = dynamic_cast<QgsDirectoryItem *>( dataItem( index ) );
636+
QgsDirectoryItem *item = qobject_cast<QgsDirectoryItem *>( dataItem( index ) );
632637
if ( !item )
633638
return;
634639

@@ -674,9 +679,10 @@ void QgsBrowserModel::removeRootItem( QgsDataItem *item )
674679
int i = mRootItems.indexOf( item );
675680
beginRemoveRows( QModelIndex(), i, i );
676681
mRootItems.remove( i );
677-
if ( !mDriveItems.key( item ).isEmpty() )
682+
QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem * >( item );
683+
if ( !mDriveItems.key( dirItem ).isEmpty() )
678684
{
679-
mDriveItems.remove( mDriveItems.key( item ) );
685+
mDriveItems.remove( mDriveItems.key( dirItem ) );
680686
}
681687
item->deleteLater();
682688
endRemoveRows();

‎src/core/qgsbrowsermodel.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
148148
*/
149149
bool initialized() const { return mInitialized; }
150150

151+
/**
152+
* Returns a map of the root drive items shown in the browser.
153+
*
154+
* These correspond to the top-level directory items shown, e.g. on Windows the C:\, D:\, etc,
155+
* and on Linux the "/" root directory.
156+
*
157+
* \since QGIS 3.6
158+
*/
159+
QMap<QString, QgsDirectoryItem *> driveItems() const;
160+
151161
signals:
152162
//! Emitted when item children fetch was finished
153163
void stateChanged( const QModelIndex &index, QgsDataItem::State oldState );
@@ -225,7 +235,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
225235

226236
private:
227237
bool mInitialized = false;
228-
QMap< QString, QgsDataItem * > mDriveItems;
238+
QMap< QString, QgsDirectoryItem * > mDriveItems;
229239

230240
void setupItemConnections( QgsDataItem *item );
231241

‎tests/src/core/testqgsbrowsermodel.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class TestQgsBrowserModel : public QObject
3737
void cleanup() {} // will be called after every testfunction.
3838

3939
void testModel();
40+
void driveItems();
4041

4142
};
4243

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

165+
void TestQgsBrowserModel::driveItems()
166+
{
167+
// an unapologetically linux-directed test ;)
168+
QgsBrowserModel model;
169+
QVERIFY( model.driveItems().empty() );
170+
171+
model.initialize();
172+
QVERIFY( !model.driveItems().empty() );
173+
QVERIFY( model.driveItems().contains( QStringLiteral( "/" ) ) );
174+
QgsDirectoryItem *rootItem = model.driveItems().value( QStringLiteral( "/" ) );
175+
QVERIFY( rootItem );
176+
QCOMPARE( rootItem->path(), QStringLiteral( "/" ) );
177+
}
178+
164179
QGSTEST_MAIN( TestQgsBrowserModel )
165180
#include "testqgsbrowsermodel.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.