Skip to content
Permalink
Browse files

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

Fixes #14481, #9843
  • Loading branch information
nyalldawson committed Sep 25, 2018
1 parent 035c78b commit af4a1df90d621794b63ae84d475513d96ed2d6fc
@@ -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 );
@@ -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, []
@@ -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();
}

@@ -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
@@ -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()

0 comments on commit af4a1df

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