Skip to content
Permalink
Browse files

Share QgsBrowserModel among browser instances

  • Loading branch information
elpaso committed Sep 10, 2017
1 parent 9349ae8 commit 3dba091b8623cd8afb553ee05b6d3d2b0209eba5
@@ -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:
@@ -184,6 +195,7 @@ Populates the model
%End
void removeRootItems();


};

/************************************************************************
@@ -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
@@ -78,6 +78,7 @@
#include "qgslayerstylingwidget.h"
#include "qgstaskmanager.h"
#include "qgsziputils.h"
#include "qgsbrowsermodel.h"

#include <QNetworkReply>
#include <QNetworkProxy>
@@ -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();
@@ -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();
@@ -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 );
@@ -123,6 +123,8 @@ class QgsLayerStylingWidget;
class QgsDiagramProperties;
class QgsLocatorWidget;
class QgsDataSourceManagerDialog;
class QgsBrowserModel;


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

QgsBrowserModel *mBrowserModel = nullptr;

friend class TestQgisAppPython;
};

@@ -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()
@@ -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
{
@@ -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();
@@ -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
@@ -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 )
{
@@ -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
@@ -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 );
@@ -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 ),
@@ -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" ) );
@@ -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
@@ -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();

/**

0 comments on commit 3dba091

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