Skip to content

Commit af4a1df

Browse files
committedSep 25, 2018
[browser] Correctly show drives inserted (or removed!) after QGIS launch
Fixes #14481, #9843
1 parent 035c78b commit af4a1df

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed
 

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ notify the provider dialogs of a changed connection
129129
%Docstring
130130
Reload the whole model
131131
%End
132+
133+
void refreshDrives();
134+
%Docstring
135+
Refreshes the list of drive items, removing any corresponding to removed
136+
drives and adding newly added drives.
137+
138+
.. versionadded:: 3.4
139+
%End
140+
132141
void beginInsertItems( QgsDataItem *parent, int first, int last );
133142
void endInsertItems();
134143
void beginRemoveItems( QgsDataItem *parent, int first, int last );

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,10 +1322,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
13221322
QgsApplication::applicationName(),
13231323
QgsApplication::organizationName(),
13241324
Qgis::QGIS_VERSION );
1325-
connect( QgsGui::instance()->nativePlatformInterface(), &QgsNative::usbStorageNotification, this, [ = ]( const QString & path, bool inserted )
1326-
{
1327-
QgsDebugMsg( QStringLiteral( "USB STORAGE NOTIFICATION! %1 %2" ).arg( path ).arg( inserted ? QStringLiteral( "inserted" ) : QStringLiteral( "removed" ) ) );
1328-
} );
1325+
connect( QgsGui::instance()->nativePlatformInterface(), &QgsNative::usbStorageNotification, mBrowserModel, &QgsBrowserModel::refreshDrives );
13291326

13301327
// setup application progress reports from task manager
13311328
connect( QgsApplication::taskManager(), &QgsTaskManager::taskAdded, this, []

‎src/core/qgsbrowsermodel.cpp

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,14 @@ void QgsBrowserModel::addRootItems()
107107
// add drives
108108
Q_FOREACH ( const QFileInfo &drive, QDir::drives() )
109109
{
110-
QString path = drive.absolutePath();
110+
const QString path = drive.absolutePath();
111111

112112
if ( QgsDirectoryItem::hiddenPath( path ) )
113113
continue;
114114

115115
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
116116
item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
117+
mDriveItems.insert( path, item );
117118

118119
connectItem( item );
119120
mRootItems << item;
@@ -173,6 +174,7 @@ void QgsBrowserModel::removeRootItems()
173174
}
174175

175176
mRootItems.clear();
177+
mDriveItems.clear();
176178
}
177179

178180
void QgsBrowserModel::initialize()
@@ -348,6 +350,54 @@ void QgsBrowserModel::reload()
348350
endResetModel();
349351
}
350352

353+
void QgsBrowserModel::refreshDrives()
354+
{
355+
const QList< QFileInfo > drives = QDir::drives();
356+
// remove any removed drives
357+
const QStringList existingDrives = mDriveItems.keys();
358+
for ( const QString &drivePath : existingDrives )
359+
{
360+
bool stillExists = false;
361+
for ( const QFileInfo &drive : drives )
362+
{
363+
if ( drivePath == drive.absolutePath() )
364+
{
365+
stillExists = true;
366+
break;
367+
}
368+
}
369+
370+
if ( stillExists )
371+
continue;
372+
373+
// drive has been removed, remove corresponding item
374+
if ( QgsDataItem *driveItem = mDriveItems.value( drivePath ) )
375+
removeRootItem( driveItem );
376+
}
377+
378+
for ( const QFileInfo &drive : drives )
379+
{
380+
const QString path = drive.absolutePath();
381+
382+
if ( QgsDirectoryItem::hiddenPath( path ) )
383+
continue;
384+
385+
// does an item for this drive already exist?
386+
if ( !mDriveItems.contains( path ) )
387+
{
388+
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
389+
item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
390+
391+
mDriveItems.insert( path, item );
392+
connectItem( item );
393+
394+
beginInsertRows( QModelIndex(), mRootItems.count(), mRootItems.count() );
395+
mRootItems << item;
396+
endInsertRows();
397+
}
398+
}
399+
}
400+
351401
QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
352402
{
353403
if ( column < 0 || column >= columnCount() || row < 0 )
@@ -576,10 +626,21 @@ void QgsBrowserModel::hidePath( QgsDataItem *item )
576626
}
577627
else
578628
{
579-
int i = mRootItems.indexOf( item );
580-
beginRemoveRows( QModelIndex(), i, i );
581-
mRootItems.remove( i );
582-
item->deleteLater();
583-
endRemoveRows();
629+
removeRootItem( item );
584630
}
585631
}
632+
633+
634+
void QgsBrowserModel::removeRootItem( QgsDataItem *item )
635+
{
636+
int i = mRootItems.indexOf( item );
637+
beginRemoveRows( QModelIndex(), i, i );
638+
mRootItems.remove( i );
639+
if ( !mDriveItems.key( item ).isEmpty() )
640+
{
641+
mDriveItems.remove( mDriveItems.key( item ) );
642+
}
643+
item->deleteLater();
644+
endRemoveRows();
645+
}
646+

‎src/core/qgsbrowsermodel.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
145145
public slots:
146146
//! Reload the whole model
147147
void reload();
148+
149+
/**
150+
* Refreshes the list of drive items, removing any corresponding to removed
151+
* drives and adding newly added drives.
152+
*
153+
* \since QGIS 3.4
154+
*/
155+
void refreshDrives();
156+
148157
void beginInsertItems( QgsDataItem *parent, int first, int last );
149158
void endInsertItems();
150159
void beginRemoveItems( QgsDataItem *parent, int first, int last );
@@ -192,6 +201,9 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
192201

193202
private:
194203
bool mInitialized = false;
204+
QMap< QString, QgsDataItem * > mDriveItems;
205+
206+
void removeRootItem( QgsDataItem *item );
195207
};
196208

197209
#endif // QGSBROWSERMODEL_H

‎src/native/mac/cocoainitializer.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
#include <AppKit/AppKit.h>
4242
#include <Cocoa/Cocoa.h>
4343

44-
class CocoaInitializer::Private
44+
class CocoaInitializer::Private
4545
{
4646
public:
47-
NSAutoreleasePool* autoReleasePool_;
47+
NSAutoreleasePool *autoReleasePool_;
4848
};
4949

5050
CocoaInitializer::CocoaInitializer()

0 commit comments

Comments
 (0)
Please sign in to comment.