[browser] Correctly show drives inserted (or removed!) after QGIS launch

Fixes #14481, #9843
This commit is contained in:
Nyall Dawson 2018-09-18 11:39:08 +10:00
parent 035c78be3c
commit af4a1df90d
5 changed files with 91 additions and 12 deletions

View File

@ -129,6 +129,15 @@ notify the provider dialogs of a changed connection
%Docstring
Reload the whole model
%End
void refreshDrives();
%Docstring
Refreshes the list of drive items, removing any corresponding to removed
drives and adding newly added drives.
.. versionadded:: 3.4
%End
void beginInsertItems( QgsDataItem *parent, int first, int last );
void endInsertItems();
void beginRemoveItems( QgsDataItem *parent, int first, int last );

View File

@ -1322,10 +1322,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
QgsApplication::applicationName(),
QgsApplication::organizationName(),
Qgis::QGIS_VERSION );
connect( QgsGui::instance()->nativePlatformInterface(), &QgsNative::usbStorageNotification, this, [ = ]( const QString & path, bool inserted )
{
QgsDebugMsg( QStringLiteral( "USB STORAGE NOTIFICATION! %1 %2" ).arg( path ).arg( inserted ? QStringLiteral( "inserted" ) : QStringLiteral( "removed" ) ) );
} );
connect( QgsGui::instance()->nativePlatformInterface(), &QgsNative::usbStorageNotification, mBrowserModel, &QgsBrowserModel::refreshDrives );
// setup application progress reports from task manager
connect( QgsApplication::taskManager(), &QgsTaskManager::taskAdded, this, []

View File

@ -107,13 +107,14 @@ void QgsBrowserModel::addRootItems()
// add drives
Q_FOREACH ( const QFileInfo &drive, QDir::drives() )
{
QString path = drive.absolutePath();
const QString path = drive.absolutePath();
if ( QgsDirectoryItem::hiddenPath( path ) )
continue;
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
mDriveItems.insert( path, item );
connectItem( item );
mRootItems << item;
@ -173,6 +174,7 @@ void QgsBrowserModel::removeRootItems()
}
mRootItems.clear();
mDriveItems.clear();
}
void QgsBrowserModel::initialize()
@ -348,6 +350,54 @@ void QgsBrowserModel::reload()
endResetModel();
}
void QgsBrowserModel::refreshDrives()
{
const QList< QFileInfo > drives = QDir::drives();
// remove any removed drives
const QStringList existingDrives = mDriveItems.keys();
for ( const QString &drivePath : existingDrives )
{
bool stillExists = false;
for ( const QFileInfo &drive : drives )
{
if ( drivePath == drive.absolutePath() )
{
stillExists = true;
break;
}
}
if ( stillExists )
continue;
// drive has been removed, remove corresponding item
if ( QgsDataItem *driveItem = mDriveItems.value( drivePath ) )
removeRootItem( driveItem );
}
for ( const QFileInfo &drive : drives )
{
const QString path = drive.absolutePath();
if ( QgsDirectoryItem::hiddenPath( path ) )
continue;
// does an item for this drive already exist?
if ( !mDriveItems.contains( path ) )
{
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
mDriveItems.insert( path, item );
connectItem( item );
beginInsertRows( QModelIndex(), mRootItems.count(), mRootItems.count() );
mRootItems << item;
endInsertRows();
}
}
}
QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( column < 0 || column >= columnCount() || row < 0 )
@ -576,10 +626,21 @@ void QgsBrowserModel::hidePath( QgsDataItem *item )
}
else
{
int i = mRootItems.indexOf( item );
beginRemoveRows( QModelIndex(), i, i );
mRootItems.remove( i );
item->deleteLater();
endRemoveRows();
removeRootItem( item );
}
}
void QgsBrowserModel::removeRootItem( QgsDataItem *item )
{
int i = mRootItems.indexOf( item );
beginRemoveRows( QModelIndex(), i, i );
mRootItems.remove( i );
if ( !mDriveItems.key( item ).isEmpty() )
{
mDriveItems.remove( mDriveItems.key( item ) );
}
item->deleteLater();
endRemoveRows();
}

View File

@ -145,6 +145,15 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
public slots:
//! Reload the whole model
void reload();
/**
* Refreshes the list of drive items, removing any corresponding to removed
* drives and adding newly added drives.
*
* \since QGIS 3.4
*/
void refreshDrives();
void beginInsertItems( QgsDataItem *parent, int first, int last );
void endInsertItems();
void beginRemoveItems( QgsDataItem *parent, int first, int last );
@ -192,6 +201,9 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
private:
bool mInitialized = false;
QMap< QString, QgsDataItem * > mDriveItems;
void removeRootItem( QgsDataItem *item );
};
#endif // QGSBROWSERMODEL_H

View File

@ -41,10 +41,10 @@
#include <AppKit/AppKit.h>
#include <Cocoa/Cocoa.h>
class CocoaInitializer::Private
class CocoaInitializer::Private
{
public:
NSAutoreleasePool* autoReleasePool_;
NSAutoreleasePool *autoReleasePool_;
};
CocoaInitializer::CocoaInitializer()