Skip to content

Commit

Permalink
Merge pull request #5164 from boundlessgeo/browser_shared_model
Browse files Browse the repository at this point in the history
[bugfix] Browser shared model
  • Loading branch information
elpaso committed Sep 12, 2017
2 parents 9dcdefd + 850fed5 commit 5614df4
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 18 deletions.
9 changes: 9 additions & 0 deletions doc/api_break.dox
Expand Up @@ -557,6 +557,15 @@ QgsBrowserModel {#qgis_api_break_3_0_QgsBrowserModel}

- addFavouriteDirectory has been renamed to addFavoriteDirectory
- removeFavourite has been renamed to removeFavorite
- initialize has been added
- initialized has been added


QgsBrowserDockWidget {#qgis_api_break_3_0_QgsBrowserDockWidget}
--------------------

- The constructor for QgsBrowserDockWidget has changed: a QgsBrowserModel instance is now an argument.


QgsCachedFeatureIterator {#qgis_api_break_3_0_QgsQgsCachedFeatureIterator}
------------------------
Expand Down
12 changes: 12 additions & 0 deletions python/core/qgsbrowsermodel.sip
Expand Up @@ -175,6 +175,17 @@ Reload the whole model
void hidePath( QgsDataItem *item );
%Docstring
Hide the given path in the browser model
%End

bool initialized( ) const;
%Docstring
Returns true if the model has been initialized
:rtype: bool
%End

void initialize();
%Docstring
Delayed initialization, needed because the provider registry must be already populated
%End

protected:
Expand All @@ -184,6 +195,7 @@ Populates the model
%End
void removeRootItems();


};

/************************************************************************
Expand Down
3 changes: 2 additions & 1 deletion python/gui/qgsbrowserdockwidget.sip
Expand Up @@ -21,10 +21,11 @@ class QgsBrowserDockWidget : QgsDockWidget
%End
public:

explicit QgsBrowserDockWidget( const QString &name, QWidget *parent /TransferThis/ = 0 );
explicit QgsBrowserDockWidget( const QString &name, QgsBrowserModel *browserModel, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsBrowserDockWidget
\param name name of the widget
\param browserModel instance of the (shared) browser model
\param parent parent widget
%End
~QgsBrowserDockWidget();
Expand Down
13 changes: 7 additions & 6 deletions src/app/qgisapp.cpp
Expand Up @@ -78,6 +78,7 @@
#include "qgslayerstylingwidget.h"
#include "qgstaskmanager.h"
#include "qgsziputils.h"
#include "qgsbrowsermodel.h"

#include <QNetworkReply>
#include <QNetworkProxy>
Expand Down Expand Up @@ -866,23 +867,24 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
}
endProfile();

mBrowserWidget = new QgsBrowserDockWidget( tr( "Browser Panel" ), this );
mBrowserModel = new QgsBrowserModel( this );
mBrowserWidget = new QgsBrowserDockWidget( tr( "Browser Panel" ), mBrowserModel, this );
mBrowserWidget->setObjectName( QStringLiteral( "Browser" ) );
addDockWidget( Qt::LeftDockWidgetArea, mBrowserWidget );
mBrowserWidget->hide();
connect( this, &QgisApp::newProject, mBrowserWidget, &QgsBrowserDockWidget::updateProjectHome );
// Only connect the first widget: the model is shared, there is no need to refresh multiple times.
connect( this, &QgisApp::connectionsChanged, mBrowserWidget, &QgsBrowserDockWidget::refresh );
connect( mBrowserWidget, &QgsBrowserDockWidget::connectionsChanged, this, &QgisApp::connectionsChanged );
connect( mBrowserWidget, &QgsBrowserDockWidget::openFile, this, &QgisApp::openFile );
connect( mBrowserWidget, &QgsBrowserDockWidget::handleDropUriList, this, &QgisApp::handleDropUriList );

mBrowserWidget2 = new QgsBrowserDockWidget( tr( "Browser Panel (2)" ), this );
mBrowserWidget2 = new QgsBrowserDockWidget( tr( "Browser Panel (2)" ), mBrowserModel, this );
mBrowserWidget2->setObjectName( QStringLiteral( "Browser2" ) );
addDockWidget( Qt::LeftDockWidgetArea, mBrowserWidget2 );
mBrowserWidget2->hide();
connect( this, &QgisApp::newProject, mBrowserWidget2, &QgsBrowserDockWidget::updateProjectHome );
connect( mBrowserWidget2, &QgsBrowserDockWidget::connectionsChanged, this, &QgisApp::connectionsChanged );
connect( this, &QgisApp::connectionsChanged, mBrowserWidget2, &QgsBrowserDockWidget::refresh );
connect( mBrowserWidget2, &QgsBrowserDockWidget::openFile, this, &QgisApp::openFile );
connect( mBrowserWidget2, &QgsBrowserDockWidget::handleDropUriList, this, &QgisApp::handleDropUriList );

Expand Down Expand Up @@ -1509,9 +1511,8 @@ void QgisApp::dataSourceManager( const QString &pageName )
{
if ( ! mDataSourceManagerDialog )
{
mDataSourceManagerDialog = new QgsDataSourceManagerDialog( this, mapCanvas() );
// Forward signals to this
connect( this, &QgisApp::connectionsChanged, mDataSourceManagerDialog, &QgsDataSourceManagerDialog::refresh );
mDataSourceManagerDialog = new QgsDataSourceManagerDialog( mBrowserModel, this, mapCanvas() );
// Forward signals to this. No need to connect refresh: the browser model is shared
connect( mDataSourceManagerDialog, &QgsDataSourceManagerDialog::connectionsChanged, this, &QgisApp::connectionsChanged );
connect( mDataSourceManagerDialog, SIGNAL( addRasterLayer( QString const &, QString const &, QString const & ) ),
this, SLOT( addRasterLayer( QString const &, QString const &, QString const & ) ) );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -123,6 +123,8 @@ class QgsLayerStylingWidget;
class QgsDiagramProperties;
class QgsLocatorWidget;
class QgsDataSourceManagerDialog;
class QgsBrowserModel;


#include <QMainWindow>
#include <QToolBar>
Expand Down Expand Up @@ -2074,6 +2076,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QTimer mRenderProgressBarTimer;
QMetaObject::Connection mRenderProgressBarTimerConnection;

QgsBrowserModel *mBrowserModel = nullptr;

friend class TestQgisAppPython;
};

Expand Down
14 changes: 11 additions & 3 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -45,9 +45,6 @@ QgsBrowserModel::QgsBrowserModel( QObject *parent )
: QAbstractItemModel( parent )

{
connect( QgsProject::instance(), &QgsProject::readProject, this, &QgsBrowserModel::updateProjectHome );
connect( QgsProject::instance(), &QgsProject::writeProject, this, &QgsBrowserModel::updateProjectHome );
addRootItems();
}

QgsBrowserModel::~QgsBrowserModel()
Expand Down Expand Up @@ -172,6 +169,17 @@ void QgsBrowserModel::removeRootItems()
mRootItems.clear();
}

void QgsBrowserModel::initialize()
{
if ( ! mInitialized )
{
connect( QgsProject::instance(), &QgsProject::readProject, this, &QgsBrowserModel::updateProjectHome );
connect( QgsProject::instance(), &QgsProject::writeProject, this, &QgsBrowserModel::updateProjectHome );
addRootItems();
mInitialized = true;
}
}


Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex &index ) const
{
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsbrowsermodel.h
Expand Up @@ -167,6 +167,12 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
//! Hide the given path in the browser model
void hidePath( QgsDataItem *item );

//! Returns true if the model has been initialized
bool initialized( ) const { return mInitialized; }

//! Delayed initialization, needed because the provider registry must be already populated
void initialize();

protected:
//! Populates the model
void addRootItems();
Expand All @@ -175,6 +181,9 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
QVector<QgsDataItem *> mRootItems;
QgsFavoritesItem *mFavorites = nullptr;
QgsDirectoryItem *mProjectHome = nullptr;

private:
bool mInitialized = false;
};

#endif // QGSBROWSERMODEL_H
7 changes: 6 additions & 1 deletion src/core/qgsdataitem.cpp
Expand Up @@ -765,7 +765,6 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
}

}

return children;
}

Expand All @@ -781,6 +780,7 @@ void QgsDirectoryItem::setState( State state )
mFileSystemWatcher->addPath( mDirPath );
connect( mFileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, &QgsDirectoryItem::directoryChanged );
}
mLastScan = QDateTime::currentDateTime();
}
else if ( state == NotPopulated )
{
Expand All @@ -794,6 +794,11 @@ void QgsDirectoryItem::setState( State state )

void QgsDirectoryItem::directoryChanged()
{
// If the last scan was less than 10 seconds ago, skip this
if ( mLastScan.msecsTo( QDateTime::currentDateTime() ) < QgsSettings().value( QStringLiteral( "browser/minscaninterval" ), 10000 ).toInt() )
{
return;
}
if ( state() == Populating )
{
// schedule to refresh later, because refresh() simply returns if Populating
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -28,6 +28,7 @@
#include <QString>
#include <QTreeWidget>
#include <QVector>
#include <QDateTime>

#include "qgsmaplayer.h"
#include "qgscoordinatereferencesystem.h"
Expand Down Expand Up @@ -487,6 +488,7 @@ class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
private:
QFileSystemWatcher *mFileSystemWatcher = nullptr;
bool mRefreshLater;
QDateTime mLastScan;
};

/** \ingroup core
Expand Down
11 changes: 8 additions & 3 deletions src/gui/qgsbrowserdockwidget.cpp
Expand Up @@ -38,8 +38,10 @@

#include <QDragEnterEvent>

QgsBrowserDockWidget::QgsBrowserDockWidget( const QString &name, QWidget *parent )
QgsBrowserDockWidget::QgsBrowserDockWidget( const QString &name, QgsBrowserModel *browserModel, QWidget *parent )
: QgsDockWidget( parent )
, mModel( browserModel )
, mProxyModel( nullptr )
, mPropertiesWidgetEnabled( false )
, mPropertiesWidgetHeight( 0 )
{
Expand Down Expand Up @@ -110,9 +112,12 @@ QgsBrowserDockWidget::~QgsBrowserDockWidget()
void QgsBrowserDockWidget::showEvent( QShowEvent *e )
{
// delayed initialization of the model
if ( !mModel )
if ( !mModel->initialized( ) )
{
mModel->initialize();
}
if ( ! mProxyModel )
{
mModel = new QgsBrowserModel( mBrowserView );
mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
mProxyModel->setBrowserModel( mModel );
mBrowserView->setSettingsSection( objectName().toLower() ); // to distinguish 2 or more instances of the browser
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsbrowserdockwidget.h
Expand Up @@ -47,9 +47,10 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
/**
* Constructor for QgsBrowserDockWidget
* \param name name of the widget
* \param browserModel instance of the (shared) browser model
* \param parent parent widget
*/
explicit QgsBrowserDockWidget( const QString &name, QWidget *parent SIP_TRANSFERTHIS = nullptr );
explicit QgsBrowserDockWidget( const QString &name, QgsBrowserModel *browserModel, QWidget *parent SIP_TRANSFERTHIS = nullptr );
~QgsBrowserDockWidget();
//! Add directory to favorites
void addFavoriteDirectory( const QString &favDir );
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsdatasourcemanagerdialog.cpp
Expand Up @@ -28,7 +28,7 @@
#include "qgsmessagelog.h"
#include "qgsgui.h"

QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QWidget *parent, QgsMapCanvas *canvas, Qt::WindowFlags fl ) :
QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QgsBrowserModel *browserModel, QWidget *parent, QgsMapCanvas *canvas, Qt::WindowFlags fl ) :
QgsOptionsDialogBase( QStringLiteral( "Data Source Manager" ), parent, fl ),
ui( new Ui::QgsDataSourceManagerDialog ),
mPreviousRow( -1 ),
Expand All @@ -48,7 +48,7 @@ QgsDataSourceManagerDialog::QgsDataSourceManagerDialog( QWidget *parent, QgsMapC
connect( ui->mOptionsListWidget, &QListWidget::currentRowChanged, this, &QgsDataSourceManagerDialog::setCurrentPage );

// BROWSER Add the browser widget to the first stacked widget page
mBrowserWidget = new QgsBrowserDockWidget( QStringLiteral( "Browser" ), this );
mBrowserWidget = new QgsBrowserDockWidget( QStringLiteral( "Browser" ), browserModel, this );
mBrowserWidget->setFeatures( QDockWidget::NoDockWidgetFeatures );
ui->mOptionsStackedWidget->addWidget( mBrowserWidget );
mPageNames.append( QStringLiteral( "browser" ) );
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgsdatasourcemanagerdialog.h
Expand Up @@ -32,6 +32,7 @@ class QgsBrowserDockWidget;
class QgsRasterLayer;
class QgsMapCanvas;
class QgsAbstractDataSourceWidget;
class QgsBrowserModel;

/** \ingroup gui
* The QgsDataSourceManagerDialog class embeds the browser panel and all
Expand All @@ -48,11 +49,12 @@ class GUI_EXPORT QgsDataSourceManagerDialog : public QgsOptionsDialogBase, priva
public:

/** QgsDataSourceManagerDialog constructor
* \param browserModel instance of the (shared) browser model
* \param parent the object
* \param canvas a pointer to the map canvas
* \param fl window flags
*/
explicit QgsDataSourceManagerDialog( QWidget *parent = nullptr, QgsMapCanvas *canvas = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
explicit QgsDataSourceManagerDialog( QgsBrowserModel *browserModel, QWidget *parent = nullptr, QgsMapCanvas *canvas = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags );
~QgsDataSourceManagerDialog();

/**
Expand Down

0 comments on commit 5614df4

Please sign in to comment.