[ui] Allow toggling of vector tile labels in the layer tree right-click menu and style panel

This commit is contained in:
Mathieu Pellerin 2023-06-27 11:22:42 +07:00
parent 1f1d1f981c
commit 951d7e3307
4 changed files with 189 additions and 118 deletions

View File

@ -199,13 +199,16 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QAction *showFeatureCount = actions->actionShowFeatureCount( menu );
menu->addAction( showFeatureCount );
showFeatureCount->setEnabled( vlayer->isValid() );
}
const QString iconName = vlayer && vlayer->labeling() && vlayer->labeling()->type() == QLatin1String( "rule-based" )
if ( vlayer || vectorTileLayer )
{
const QString iconName = vectorTileLayer || ( vlayer && vlayer->labeling() && vlayer->labeling()->type() == QLatin1String( "rule-based" ) )
? QStringLiteral( "labelingRuleBased.svg" )
: QStringLiteral( "labelingSingle.svg" );
QAction *actionShowLabels = new QAction( QgsApplication::getThemeIcon( iconName ), tr( "Show &Labels" ), menu );
actionShowLabels->setCheckable( true );
actionShowLabels->setChecked( vlayer->labelsEnabled() );
actionShowLabels->setChecked( vectorTileLayer ? vectorTileLayer->labelsEnabled() : vlayer->labelsEnabled() );
connect( actionShowLabels, &QAction::toggled, this, &QgsAppLayerTreeViewMenuProvider::toggleLabels );
menu->addAction( actionShowLabels );
}
@ -1335,22 +1338,30 @@ void QgsAppLayerTreeViewMenuProvider::toggleLabels( bool enabled )
const QList<QgsLayerTreeLayer *> selectedLayerNodes = mView->selectedLayerNodes();
for ( QgsLayerTreeLayer *l : selectedLayerNodes )
{
QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( l->layer() );
if ( !vlayer || !vlayer->isSpatial() )
continue;
if ( QgsVectorLayer *vlayer = qobject_cast< QgsVectorLayer * >( l->layer() ) )
{
if ( !vlayer->isSpatial() )
continue;
if ( enabled && !vlayer->labeling() )
{
// no labeling setup - create default labeling for layer
const QgsPalLayerSettings settings = QgsAbstractVectorLayerLabeling::defaultSettingsForLayer( vlayer );
vlayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vlayer->setLabelsEnabled( true );
if ( enabled && !vlayer->labeling() )
{
// no labeling setup - create default labeling for layer
const QgsPalLayerSettings settings = QgsAbstractVectorLayerLabeling::defaultSettingsForLayer( vlayer );
vlayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vlayer->setLabelsEnabled( true );
}
else
{
vlayer->setLabelsEnabled( enabled );
}
vlayer->emitStyleChanged();
vlayer->triggerRepaint();
}
else
else if ( QgsVectorTileLayer *vectorTilelayer = qobject_cast< QgsVectorTileLayer * >( l->layer() ) )
{
vlayer->setLabelsEnabled( enabled );
vectorTilelayer->setLabelsEnabled( enabled );
vectorTilelayer->emitStyleChanged();
vectorTilelayer->triggerRepaint();
}
vlayer->emitStyleChanged();
vlayer->triggerRepaint();
}
}

View File

@ -15,9 +15,10 @@
#include "qgsvectortilebasiclabelingwidget.h"
#include "qgis.h"
#include "qgsapplication.h"
#include "qgsvectortilebasiclabeling.h"
#include "qgsvectortilelayer.h"
#include "qgslabelinggui.h"
#include "qgsmapcanvas.h"
#include "qgsvectortileutils.h"
@ -332,6 +333,10 @@ QgsVectorTileBasicLabelingWidget::QgsVectorTileBasicLabelingWidget( QgsVectorTil
connect( btnEditRule, &QPushButton::clicked, this, &QgsVectorTileBasicLabelingWidget::editStyle );
connect( btnRemoveRule, &QAbstractButton::clicked, this, &QgsVectorTileBasicLabelingWidget::removeStyle );
mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingNone.svg" ) ), tr( "No Labels" ) );
mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingRuleBased.svg" ) ), tr( "Rule-based Labeling" ) );
connect( mLabelModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsVectorTileBasicLabelingWidget::labelModeChanged );
connect( viewStyles, &QAbstractItemView::doubleClicked, this, &QgsVectorTileBasicLabelingWidget::editStyleAtIndex );
if ( mMapCanvas )
@ -374,16 +379,25 @@ QgsVectorTileBasicLabelingWidget::QgsVectorTileBasicLabelingWidget( QgsVectorTil
void QgsVectorTileBasicLabelingWidget::setLayer( QgsVectorTileLayer *layer )
{
if ( mVTLayer )
{
disconnect( mVTLayer );
}
mVTLayer = layer;
connect( mVTLayer, &QgsMapLayer::styleChanged, this, [ = ]() { setLayer( mVTLayer ); } );
if ( layer && layer->labeling() && layer->labeling()->type() == QLatin1String( "basic" ) )
{
mLabeling.reset( static_cast<QgsVectorTileBasicLabeling *>( layer->labeling()->clone() ) );
whileBlocking( mLabelModeComboBox )->setCurrentIndex( layer->labelsEnabled() ? 1 : 0 );
}
else
{
mLabeling.reset( new QgsVectorTileBasicLabeling() );
whileBlocking( mLabelModeComboBox )->setCurrentIndex( 1 );
}
mOptionsStackedWidget->setCurrentIndex( mLabelModeComboBox->currentIndex() );
mModel = new QgsVectorTileBasicLabelingListModel( mLabeling.get(), viewStyles );
mProxyModel = new QgsVectorTileBasicLabelingProxyModel( mModel, viewStyles );
@ -411,6 +425,13 @@ QgsVectorTileBasicLabelingWidget::~QgsVectorTileBasicLabelingWidget() = default;
void QgsVectorTileBasicLabelingWidget::apply()
{
mVTLayer->setLabeling( mLabeling->clone() );
mVTLayer->setLabelsEnabled( mLabelModeComboBox->currentIndex() == 1 );
}
void QgsVectorTileBasicLabelingWidget::labelModeChanged()
{
mOptionsStackedWidget->setCurrentIndex( mLabelModeComboBox->currentIndex() );
emit widgetChanged();
}
void QgsVectorTileBasicLabelingWidget::addStyle( Qgis::GeometryType geomType )

View File

@ -59,6 +59,7 @@ class GUI_EXPORT QgsVectorTileBasicLabelingWidget : public QgsMapLayerConfigWidg
void editStyleAtIndex( const QModelIndex &index );
void removeStyle();
void labelModeChanged();
void updateLabelingFromWidget();
private:

View File

@ -12,115 +12,152 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QgsFilterLineEdit" name="mFilterLineEdit"/>
</item>
<item>
<widget class="QCheckBox" name="mCheckVisibleOnly">
<property name="toolTip">
<string>Hides any rules which are invisible because they fall outside the current map canvas zoom level</string>
</property>
<property name="text">
<string>Visible rules only</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTreeView" name="viewStyles">
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="editTriggers">
<set>QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="rootIsDecorated">
<bool>false</bool>
<widget class="QComboBox" name="mLabelModeComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="btnAddRule">
<property name="toolTip">
<string>Add rule</string>
<item>
<widget class="QStackedWidget" name="mOptionsStackedWidget">
<property name="lineWidth">
<number>0</number>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="mNoLabelPage"/>
<widget class="QWidget" name="mRuleBasedLabelPage">
<layout class="QVBoxLayout" name="verticalLayout_1">
<property name="leftMargin">
<number>0</number>
</property>
<property name="text">
<string/>
<property name="topMargin">
<number>0</number>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyAdd.svg</normaloff>:/images/themes/default/symbologyAdd.svg</iconset>
<property name="rightMargin">
<number>0</number>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
<property name="bottomMargin">
<number>0</number>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRemoveRule">
<property name="toolTip">
<string>Remove selected rules</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyRemove.svg</normaloff>:/images/themes/default/symbologyRemove.svg</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnEditRule">
<property name="toolTip">
<string>Edit current rule</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyEdit.svg</normaloff>:/images/themes/default/symbologyEdit.svg</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="mLabelCurrentZoom">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QgsFilterLineEdit" name="mFilterLineEdit"/>
</item>
<item>
<widget class="QCheckBox" name="mCheckVisibleOnly">
<property name="toolTip">
<string>Hides any rules which are invisible because they fall outside the current map canvas zoom level</string>
</property>
<property name="text">
<string>Visible rules only</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTreeView" name="viewStyles">
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="editTriggers">
<set>QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="rootIsDecorated">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QToolButton" name="btnAddRule">
<property name="toolTip">
<string>Add rule</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyAdd.svg</normaloff>:/images/themes/default/symbologyAdd.svg</iconset>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRemoveRule">
<property name="toolTip">
<string>Remove selected rules</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyRemove.svg</normaloff>:/images/themes/default/symbologyRemove.svg</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnEditRule">
<property name="toolTip">
<string>Edit current rule</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyEdit.svg</normaloff>:/images/themes/default/symbologyEdit.svg</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="mLabelCurrentZoom">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
@ -138,6 +175,7 @@
<tabstop>btnAddRule</tabstop>
<tabstop>btnRemoveRule</tabstop>
<tabstop>btnEditRule</tabstop>
<tabstop>mCheckLabelsEnabled</tabstop>
</tabstops>
<resources>
<include location="../../images/images.qrc"/>