Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 af4a1df
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgsbrowsermodel.sip.in
Expand Up @@ -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 );
Expand Down
5 changes: 1 addition & 4 deletions src/app/qgisapp.cpp
Expand Up @@ -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, []
Expand Down
73 changes: 67 additions & 6 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -173,6 +174,7 @@ void QgsBrowserModel::removeRootItems()
}

mRootItems.clear();
mDriveItems.clear();
}

void QgsBrowserModel::initialize()
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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();
}

12 changes: 12 additions & 0 deletions src/core/qgsbrowsermodel.h
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/native/mac/cocoainitializer.mm
Expand Up @@ -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()
Expand Down

0 comments on commit af4a1df

Please sign in to comment.