mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-13 00:07:27 -05:00
[browser] Correctly show drives inserted (or removed!) after QGIS launch
Fixes #14481, #9843
This commit is contained in:
parent
035c78be3c
commit
af4a1df90d
@ -129,6 +129,15 @@ notify the provider dialogs of a changed connection
|
|||||||
%Docstring
|
%Docstring
|
||||||
Reload the whole model
|
Reload the whole model
|
||||||
%End
|
%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 beginInsertItems( QgsDataItem *parent, int first, int last );
|
||||||
void endInsertItems();
|
void endInsertItems();
|
||||||
void beginRemoveItems( QgsDataItem *parent, int first, int last );
|
void beginRemoveItems( QgsDataItem *parent, int first, int last );
|
||||||
|
|||||||
@ -1322,10 +1322,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
|
|||||||
QgsApplication::applicationName(),
|
QgsApplication::applicationName(),
|
||||||
QgsApplication::organizationName(),
|
QgsApplication::organizationName(),
|
||||||
Qgis::QGIS_VERSION );
|
Qgis::QGIS_VERSION );
|
||||||
connect( QgsGui::instance()->nativePlatformInterface(), &QgsNative::usbStorageNotification, this, [ = ]( const QString & path, bool inserted )
|
connect( QgsGui::instance()->nativePlatformInterface(), &QgsNative::usbStorageNotification, mBrowserModel, &QgsBrowserModel::refreshDrives );
|
||||||
{
|
|
||||||
QgsDebugMsg( QStringLiteral( "USB STORAGE NOTIFICATION! %1 %2" ).arg( path ).arg( inserted ? QStringLiteral( "inserted" ) : QStringLiteral( "removed" ) ) );
|
|
||||||
} );
|
|
||||||
|
|
||||||
// setup application progress reports from task manager
|
// setup application progress reports from task manager
|
||||||
connect( QgsApplication::taskManager(), &QgsTaskManager::taskAdded, this, []
|
connect( QgsApplication::taskManager(), &QgsTaskManager::taskAdded, this, []
|
||||||
|
|||||||
@ -107,13 +107,14 @@ void QgsBrowserModel::addRootItems()
|
|||||||
// add drives
|
// add drives
|
||||||
Q_FOREACH ( const QFileInfo &drive, QDir::drives() )
|
Q_FOREACH ( const QFileInfo &drive, QDir::drives() )
|
||||||
{
|
{
|
||||||
QString path = drive.absolutePath();
|
const QString path = drive.absolutePath();
|
||||||
|
|
||||||
if ( QgsDirectoryItem::hiddenPath( path ) )
|
if ( QgsDirectoryItem::hiddenPath( path ) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
|
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
|
||||||
item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
|
item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
|
||||||
|
mDriveItems.insert( path, item );
|
||||||
|
|
||||||
connectItem( item );
|
connectItem( item );
|
||||||
mRootItems << item;
|
mRootItems << item;
|
||||||
@ -173,6 +174,7 @@ void QgsBrowserModel::removeRootItems()
|
|||||||
}
|
}
|
||||||
|
|
||||||
mRootItems.clear();
|
mRootItems.clear();
|
||||||
|
mDriveItems.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QgsBrowserModel::initialize()
|
void QgsBrowserModel::initialize()
|
||||||
@ -348,6 +350,54 @@ void QgsBrowserModel::reload()
|
|||||||
endResetModel();
|
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
|
QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
|
||||||
{
|
{
|
||||||
if ( column < 0 || column >= columnCount() || row < 0 )
|
if ( column < 0 || column >= columnCount() || row < 0 )
|
||||||
@ -576,10 +626,21 @@ void QgsBrowserModel::hidePath( QgsDataItem *item )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int i = mRootItems.indexOf( item );
|
removeRootItem( item );
|
||||||
beginRemoveRows( QModelIndex(), i, i );
|
|
||||||
mRootItems.remove( i );
|
|
||||||
item->deleteLater();
|
|
||||||
endRemoveRows();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
public slots:
|
||||||
//! Reload the whole model
|
//! Reload the whole model
|
||||||
void reload();
|
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 beginInsertItems( QgsDataItem *parent, int first, int last );
|
||||||
void endInsertItems();
|
void endInsertItems();
|
||||||
void beginRemoveItems( QgsDataItem *parent, int first, int last );
|
void beginRemoveItems( QgsDataItem *parent, int first, int last );
|
||||||
@ -192,6 +201,9 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool mInitialized = false;
|
bool mInitialized = false;
|
||||||
|
QMap< QString, QgsDataItem * > mDriveItems;
|
||||||
|
|
||||||
|
void removeRootItem( QgsDataItem *item );
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QGSBROWSERMODEL_H
|
#endif // QGSBROWSERMODEL_H
|
||||||
|
|||||||
@ -44,7 +44,7 @@
|
|||||||
class CocoaInitializer::Private
|
class CocoaInitializer::Private
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NSAutoreleasePool* autoReleasePool_;
|
NSAutoreleasePool *autoReleasePool_;
|
||||||
};
|
};
|
||||||
|
|
||||||
CocoaInitializer::CocoaInitializer()
|
CocoaInitializer::CocoaInitializer()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user