Skip to content

Commit

Permalink
Add support for widgets in layer styling dock which operate on
Browse files Browse the repository at this point in the history
QgsLayerTreeGroups instead of map layers
  • Loading branch information
nyalldawson committed Nov 25, 2021
1 parent bcde12d commit 027928a
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 11 deletions.
18 changes: 18 additions & 0 deletions python/gui/auto_generated/qgsmaplayerconfigwidget.sip.in
Expand Up @@ -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

};
Expand Down
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -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() );
Expand Down
62 changes: 51 additions & 11 deletions src/app/qgslayerstylingwidget.cpp
Expand Up @@ -175,6 +175,7 @@ void QgsLayerStylingWidget::setLayer( QgsMapLayer *layer )
}

mCurrentLayer = layer;
mContext.setLayerTreeGroup( nullptr );

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

Expand Down Expand Up @@ -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" );

Expand Down Expand Up @@ -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()
Expand All @@ -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();

Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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() ) )
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgslayerstylingwidget.h
Expand Up @@ -49,6 +49,7 @@ class QgsMessageBar;
class QgsVectorTileBasicRendererWidget;
class QgsVectorTileBasicLabelingWidget;
class QgsAnnotationLayer;
class QgsLayerTreeGroup;

class APP_EXPORT QgsLayerStyleManagerWidgetFactory : public QgsMapLayerConfigWidgetFactory
{
Expand Down Expand Up @@ -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.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/gui/qgsmaplayerconfigwidget.cpp
Expand Up @@ -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 )
Expand Down
19 changes: 19 additions & 0 deletions src/gui/qgsmaplayerconfigwidget.h
Expand Up @@ -17,8 +17,10 @@

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

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

class QgsMapCanvas;
Expand Down Expand Up @@ -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;

};

Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsmaplayerconfigwidgetfactory.cpp
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsmaplayerconfigwidgetfactory.h
Expand Up @@ -23,6 +23,7 @@
class QgsMapLayer;
class QgsMapLayerConfigWidget;
class QgsMapCanvas;
class QgsLayerTreeGroup;

/**
* \ingroup gui
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 027928a

Please sign in to comment.