Skip to content

Commit

Permalink
Share QgsBrowserModel among browser instances
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Sep 12, 2017
1 parent 9349ae8 commit 3dba091
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 14 deletions.
12 changes: 12 additions & 0 deletions python/core/qgsbrowsermodel.sip
Original file line number Diff line number Diff line change
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( );
%Docstring
Returns true if the model has been initialized
:rtype: bool
%End

void init();
%Docstring
Delayied initialization
%End

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


};

/************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion python/gui/qgsbrowserdockwidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class QgsBrowserDockWidget : QgsDockWidget
%End
public:

explicit QgsBrowserDockWidget( const QString &name, QWidget *parent /TransferThis/ = 0 );
explicit QgsBrowserDockWidget( const QString &name, QgsBrowserModel *model, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsBrowserDockWidget
\param name name of the widget
Expand Down
8 changes: 5 additions & 3 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
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,7 +867,8 @@ 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();
Expand All @@ -876,7 +878,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
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();
Expand Down Expand Up @@ -1509,7 +1511,7 @@ void QgisApp::dataSourceManager( const QString &pageName )
{
if ( ! mDataSourceManagerDialog )
{
mDataSourceManagerDialog = new QgsDataSourceManagerDialog( this, mapCanvas() );
mDataSourceManagerDialog = new QgsDataSourceManagerDialog( mBrowserModel, this, mapCanvas() );
// Forward signals to this
connect( this, &QgisApp::connectionsChanged, mDataSourceManagerDialog, &QgsDataSourceManagerDialog::refresh );
connect( mDataSourceManagerDialog, &QgsDataSourceManagerDialog::connectionsChanged, this, &QgisApp::connectionsChanged );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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::init()
{
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
Original file line number Diff line number Diff line change
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( ) { return mInitialized; }

//! Delayied initialization
void init();

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
11 changes: 8 additions & 3 deletions src/gui/qgsbrowserdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@

#include <QDragEnterEvent>

QgsBrowserDockWidget::QgsBrowserDockWidget( const QString &name, QWidget *parent )
QgsBrowserDockWidget::QgsBrowserDockWidget( const QString &name, QgsBrowserModel *model, QWidget *parent )
: QgsDockWidget( parent )
, mModel( model )
, 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->init();
}
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
2 changes: 1 addition & 1 deletion src/gui/qgsbrowserdockwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
* \param name name of the widget
* \param parent parent widget
*/
explicit QgsBrowserDockWidget( const QString &name, QWidget *parent SIP_TRANSFERTHIS = nullptr );
explicit QgsBrowserDockWidget( const QString &name, QgsBrowserModel *model, 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
Original file line number Diff line number Diff line change
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
3 changes: 2 additions & 1 deletion src/gui/qgsdatasourcemanagerdialog.h
Original file line number Diff line number Diff line change
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 @@ -52,7 +53,7 @@ class GUI_EXPORT QgsDataSourceManagerDialog : public QgsOptionsDialogBase, priva
* \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 3dba091

Please sign in to comment.