Skip to content
Permalink
Browse files

layer tree model can emit messages which will be shown in the message…

… bar
  • Loading branch information
3nids committed May 5, 2020
1 parent 202ae7b commit 01f766255b562be58ca8867981a4f0549ec91c85
@@ -321,6 +321,9 @@ displays.
.. versionadded:: 3.6
%End

signals:
void messageEmitted( const QString &message );

protected slots:
void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
@@ -9,6 +9,7 @@




class QgsLayerTreeView : QTreeView
{
%Docstring
@@ -211,6 +212,13 @@ Set width of contextual menu mark, at right of layer node items.
.. seealso:: :py:func:`layerMarkWidth`

.. versionadded:: 3.8
%End

void setMessageBar( QgsMessageBar *messageBar );
%Docstring
Set the message bar to display messages from the layer tree

.. versionadded:: 3.14
%End

signals:
@@ -1681,12 +1681,13 @@ QgisApp::QgisApp()
mMapCanvas = new QgsMapCanvas();
connect( mMapCanvas, &QgsMapCanvas::messageEmitted, this, &QgisApp::displayMessage );
QgsCanvasRefreshBlocker refreshBlocker;
mLayerTreeView = new QgsLayerTreeView( this );
QgsLayerTreeView( this );
QgsLayerTreeModel *model = new QgsLayerTreeModel( QgsProject::instance()->layerTreeRoot(), this );
mLayerTreeView->setModel( model );
mUndoWidget = new QgsUndoWidget( nullptr, mMapCanvas );
mUserInputDockWidget = new QgsUserInputWidget( this );
mInfoBar = new QgsMessageBar( centralWidget() );
mLayerTreeView->setMessageBar( mInfoBar );
mAdvancedDigitizingDockWidget = new QgsAdvancedDigitizingDockWidget( mMapCanvas, this );
mPanelMenu = new QMenu( this );
mProgressBar = new QProgressBar( this );
@@ -4590,6 +4591,8 @@ void QgisApp::initLayerTreeView()
model->setAutoCollapseLegendNodes( 10 );

mLayerTreeView->setModel( model );
mLayerTreeView->setMessageBar( mInfoBar );

mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider( mLayerTreeView, mMapCanvas ) );
new QgsLayerTreeViewFilterIndicatorProvider( mLayerTreeView ); // gets parented to the layer view
new QgsLayerTreeViewEmbeddedIndicatorProvider( mLayerTreeView ); // gets parented to the layer view
@@ -1109,6 +1109,7 @@ bool QgsLayerTreeModel::dropMimeData( const QMimeData *data, Qt::DropAction acti
QgsReadWriteContext context;
QString errorMessage;
QgsLayerDefinition::loadLayerDefinition( layerDefinitionDoc, QgsProject::instance(), QgsLayerTree::toGroup( nodeParent ), errorMessage, context );
emit messageEmitted( tr( "New layers added from another QGIS instance" ) );
}
else
{
@@ -286,6 +286,9 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
*/
static int scaleIconSize( int standardSize );

signals:
void messageEmitted( const QString &message );

protected slots:
void nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
@@ -22,6 +22,8 @@
#include "qgslayertreeutils.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgsmaplayer.h"
#include "qgsmessagebar.h"

#include "qgsgui.h"

#include <QMenu>
@@ -77,6 +79,9 @@ void QgsLayerTreeView::setModel( QAbstractItemModel *model )
connect( model, &QAbstractItemModel::rowsInserted, this, &QgsLayerTreeView::modelRowsInserted );
connect( model, &QAbstractItemModel::rowsRemoved, this, &QgsLayerTreeView::modelRowsRemoved );

if ( mMessageBar )
connect( layerTreeModel(), &QgsLayerTreeModel::messageEmitted, [ = ]( const QString & message ) {mMessageBar->pushMessage( message );} );

QTreeView::setModel( model );

connect( layerTreeModel()->rootGroup(), &QgsLayerTreeNode::expandedChanged, this, &QgsLayerTreeView::onExpandedChanged );
@@ -490,6 +495,17 @@ void QgsLayerTreeView::collapseAllNodes()
collapseAll();
}

void QgsLayerTreeView::setMessageBar( QgsMessageBar *messageBar )
{
if ( mMessageBar == messageBar )
return;

mMessageBar = messageBar;

if ( mMessageBar )
connect( layerTreeModel(), &QgsLayerTreeModel::messageEmitted, [ = ]( const QString & message ) {mMessageBar->pushMessage( message );} );
}

void QgsLayerTreeView::mouseReleaseEvent( QMouseEvent *event )
{
// we need to keep last mouse position in order to know whether to emit an indicator's clicked() signal
@@ -29,6 +29,8 @@ class QgsLayerTreeViewDefaultActions;
class QgsLayerTreeViewIndicator;
class QgsLayerTreeViewMenuProvider;
class QgsMapLayer;
class QgsMessageBar;


/**
* \ingroup gui
@@ -206,6 +208,12 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
*/
void setLayerMarkWidth( int width ) { mLayerMarkWidth = width; }

/**
* Set the message bar to display messages from the layer tree
* \since QGIS 3.14
*/
void setMessageBar( QgsMessageBar *messageBar );

signals:
//! Emitted when a current layer is changed
void currentLayerChanged( QgsMapLayer *layer );
@@ -253,6 +261,9 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
//! Width of contextual menu mark for layer nodes
int mLayerMarkWidth;

private:
QgsMessageBar *mMessageBar = nullptr;

// friend so it can access viewOptions() method and mLastReleaseMousePos without making them public
friend class QgsLayerTreeViewItemDelegate;
};

0 comments on commit 01f7662

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