Skip to content
Permalink
Browse files
Add support for widgets in layer styling dock which operate on
QgsLayerTreeGroups instead of map layers
  • Loading branch information
nyalldawson committed Nov 25, 2021
1 parent bcde12d commit 027928a7f79d9119b584c661ddb4f6d496c83756
@@ -66,6 +66,24 @@ to the appropriate message bar.
Returns the message bar associated with the widget.

.. seealso:: :py:func:`setMessageBar`
%End

void setLayerTreeGroup( QgsLayerTreeGroup *group );
%Docstring
Sets the layer tree ``group`` associated with the widget.

.. seealso:: :py:func:`layerTreeGroup`

.. versionadded:: 3.24
%End

QgsLayerTreeGroup *layerTreeGroup() const;
%Docstring
Returns the layer tree group associated with the widget.

.. seealso:: :py:func:`setLayerTreeGroup`

.. versionadded:: 3.24
%End

};
@@ -126,6 +126,15 @@ Set support flag for style dock
Check if the layer is supported for this widget.

:return: ``True`` if this layer is supported for this widget
%End

virtual bool supportsLayerTreeGroup( QgsLayerTreeGroup *group ) const;
%Docstring
Check if a layer tree group is supported for this widget.

:return: ``True`` if the group is supported for this widget

.. versionadded:: 3.24
%End

virtual ParentPage parentPage() const;
@@ -15019,6 +15019,21 @@ void QgisApp::legendLayerSelectionChanged()
{
const QList<QgsLayerTreeLayer *> selectedLayers = mLayerTreeView ? mLayerTreeView->selectedLayerNodes() : QList<QgsLayerTreeLayer *>();

if ( selectedLayers.empty() && mLayerTreeView )
{
// check if a group node alone is selected
const QList<QgsLayerTreeNode *> selectedNodes = mLayerTreeView->selectedNodes();
if ( selectedNodes.size() == 1 && QgsLayerTree::isGroup( selectedNodes.at( 0 ) ) )
{
QgsLayerTreeGroup *groupNode = QgsLayerTree::toGroup( selectedNodes.at( 0 ) );
mMapStyleWidget->setEnabled( true );
if ( mMapStylingDock->isVisible() )
{
mMapStyleWidget->setLayerTreeGroup( groupNode );
}
}
}

mActionDuplicateLayer->setEnabled( !selectedLayers.isEmpty() );
mActionSetLayerScaleVisibility->setEnabled( !selectedLayers.isEmpty() );
mActionSetLayerCRS->setEnabled( !selectedLayers.isEmpty() );
@@ -175,6 +175,7 @@ void QgsLayerStylingWidget::setLayer( QgsMapLayer *layer )
}

mCurrentLayer = layer;
mContext.setLayerTreeGroup( nullptr );

mUndoWidget->setUndoStack( layer->undoStackStyles() );

@@ -303,10 +304,10 @@ void QgsLayerStylingWidget::setLayer( QgsMapLayer *layer )

void QgsLayerStylingWidget::apply()
{
if ( !mCurrentLayer )
return;

disconnect( mCurrentLayer, &QgsMapLayer::styleChanged, this, &QgsLayerStylingWidget::updateCurrentWidgetLayer );
if ( mCurrentLayer )
{
disconnect( mCurrentLayer, &QgsMapLayer::styleChanged, this, &QgsLayerStylingWidget::updateCurrentWidgetLayer );
}

QString undoName = QStringLiteral( "Style Change" );

@@ -357,14 +358,19 @@ void QgsLayerStylingWidget::apply()
triggerRepaint = widget->shouldTriggerLayerRepaint();
}

pushUndoItem( undoName, triggerRepaint );
if ( mCurrentLayer )
pushUndoItem( undoName, triggerRepaint );

if ( styleWasChanged )
if ( mCurrentLayer && styleWasChanged )
{
emit styleChanged( mCurrentLayer );
QgsProject::instance()->setDirty( true );
}
connect( mCurrentLayer, &QgsMapLayer::styleChanged, this, &QgsLayerStylingWidget::updateCurrentWidgetLayer );

if ( mCurrentLayer )
{
connect( mCurrentLayer, &QgsMapLayer::styleChanged, this, &QgsLayerStylingWidget::updateCurrentWidgetLayer );
}
}

void QgsLayerStylingWidget::autoApply()
@@ -389,12 +395,13 @@ void QgsLayerStylingWidget::redo()

void QgsLayerStylingWidget::updateCurrentWidgetLayer()
{
if ( !mCurrentLayer )
if ( !mCurrentLayer && !mContext.layerTreeGroup() )
return; // non-spatial are ignored in setLayer()

mBlockAutoApply = true;

whileBlocking( mLayerCombo )->setLayer( mCurrentLayer );
if ( mCurrentLayer )
whileBlocking( mLayerCombo )->setLayer( mCurrentLayer );

int row = mOptionsListWidget->currentIndex().row();

@@ -453,11 +460,11 @@ void QgsLayerStylingWidget::updateCurrentWidgetLayer()
}

// The last widget is always the undo stack.
if ( row == mOptionsListWidget->count() - 1 )
if ( mCurrentLayer && row == mOptionsListWidget->count() - 1 )
{
mWidgetStack->setMainPanel( mUndoWidget );
}
else
else if ( mCurrentLayer )
{
switch ( mCurrentLayer->type() )
{
@@ -715,6 +722,39 @@ void QgsLayerStylingWidget::setAnnotationItem( QgsAnnotationLayer *layer, const
}
}

void QgsLayerStylingWidget::setLayerTreeGroup( QgsLayerTreeGroup *group )
{
mOptionsListWidget->blockSignals( true );
mOptionsListWidget->clear();
mUserPages.clear();

for ( const QgsMapLayerConfigWidgetFactory *factory : std::as_const( mPageFactories ) )
{
if ( factory->supportsStyleDock() && factory->supportsLayerTreeGroup( group ) )
{
QListWidgetItem *item = new QListWidgetItem( factory->icon(), QString() );
item->setToolTip( factory->title() );
mOptionsListWidget->addItem( item );
int row = mOptionsListWidget->row( item );
mUserPages[row] = factory;
}
}

mContext.setLayerTreeGroup( group );
setLayer( nullptr );

mOptionsListWidget->setCurrentRow( 0 );
mOptionsListWidget->blockSignals( false );
updateCurrentWidgetLayer();

mStackedWidget->setCurrentIndex( 1 );

if ( QgsMapLayerConfigWidget *configWidget = qobject_cast< QgsMapLayerConfigWidget * >( mWidgetStack->mainPanel() ) )
{
configWidget->setMapLayerConfigWidgetContext( mContext );
}
}

void QgsLayerStylingWidget::focusDefaultWidget()
{
if ( QgsMapLayerConfigWidget *configWidget = qobject_cast< QgsMapLayerConfigWidget * >( mWidgetStack->mainPanel() ) )
@@ -49,6 +49,7 @@ class QgsMessageBar;
class QgsVectorTileBasicRendererWidget;
class QgsVectorTileBasicLabelingWidget;
class QgsAnnotationLayer;
class QgsLayerTreeGroup;

class APP_EXPORT QgsLayerStyleManagerWidgetFactory : public QgsMapLayerConfigWidgetFactory
{
@@ -135,6 +136,11 @@ class APP_EXPORT QgsLayerStylingWidget : public QWidget, private Ui::QgsLayerSty
*/
void setAnnotationItem( QgsAnnotationLayer *layer, const QString &itemId );

/**
* Sets a layer tree group to show in the widget.
*/
void setLayerTreeGroup( QgsLayerTreeGroup *group );

/**
* Focuses the default widget for the current page.
*/
@@ -14,6 +14,25 @@
***************************************************************************/
#include "qgsmaplayerconfigwidget.h"
#include "qgspanelwidget.h"
#include "qgslayertreegroup.h"

//
// QgsMapLayerConfigWidgetContext
//

void QgsMapLayerConfigWidgetContext::setLayerTreeGroup( QgsLayerTreeGroup *group )
{
mLayerTreeGroup = group;
}

QgsLayerTreeGroup *QgsMapLayerConfigWidgetContext::layerTreeGroup() const
{
return mLayerTreeGroup;
}

//
// QgsMapLayerConfigWidget
//

QgsMapLayerConfigWidget::QgsMapLayerConfigWidget( QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent )
: QgsPanelWidget( parent )
@@ -17,8 +17,10 @@

#include <QWidget>
#include <QIcon>
#include <QPointer>

#include "qgspanelwidget.h"
#include "qgslayertreegroup.h"
#include "qgis_gui.h"

class QgsMapCanvas;
@@ -78,11 +80,28 @@ class GUI_EXPORT QgsMapLayerConfigWidgetContext
*/
QgsMessageBar *messageBar() const { return mMessageBar; }

/**
* Sets the layer tree \a group associated with the widget.
*
* \see layerTreeGroup()
* \since QGIS 3.24
*/
void setLayerTreeGroup( QgsLayerTreeGroup *group );

/**
* Returns the layer tree group associated with the widget.
*
* \see setLayerTreeGroup()
* \since QGIS 3.24
*/
QgsLayerTreeGroup *layerTreeGroup() const;

private:

QString mAnnotationId;
QgsMapCanvas *mMapCanvas = nullptr;
QgsMessageBar *mMessageBar = nullptr;
QPointer< QgsLayerTreeGroup > mLayerTreeGroup = nullptr;

};

@@ -32,6 +32,11 @@ bool QgsMapLayerConfigWidgetFactory::supportsLayer( QgsMapLayer *layer ) const
return true;
}

bool QgsMapLayerConfigWidgetFactory::supportsLayerTreeGroup( QgsLayerTreeGroup * ) const
{
return false;
}

QgsMapLayerConfigWidgetFactory::ParentPage QgsMapLayerConfigWidgetFactory::parentPage() const
{
return ParentPage::NoParent;
@@ -23,6 +23,7 @@
class QgsMapLayer;
class QgsMapLayerConfigWidget;
class QgsMapCanvas;
class QgsLayerTreeGroup;

/**
* \ingroup gui
@@ -127,6 +128,13 @@ class GUI_EXPORT QgsMapLayerConfigWidgetFactory
*/
virtual bool supportsLayer( QgsMapLayer *layer ) const;

/**
* \brief Check if a layer tree group is supported for this widget.
* \returns TRUE if the group is supported for this widget
* \since QGIS 3.24
*/
virtual bool supportsLayerTreeGroup( QgsLayerTreeGroup *group ) const;

/**
* Returns the associated parent page, for factories which create sub-components of a standard page.
*

0 comments on commit 027928a

Please sign in to comment.