Navigation Menu

Skip to content

Commit

Permalink
Add a super-basic way to pick legend patch shapes
Browse files Browse the repository at this point in the history
With the only choices so far being the default or oval for fills,
and default or "zig zag" for lines
  • Loading branch information
nyalldawson committed Apr 17, 2020
1 parent 9e548a0 commit f27a490
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
81 changes: 81 additions & 0 deletions src/gui/layout/qgslayoutlegendwidget.cpp
Expand Up @@ -40,6 +40,7 @@
#include "qgsunittypes.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsexpressioncontextutils.h"
#include "qgscircle.h"

#include <QMenu>
#include <QMessageBox>
Expand Down Expand Up @@ -1377,10 +1378,50 @@ QgsLayoutLegendNodeWidget::QgsLayoutLegendNodeWidget( QgsLayoutItemLegend *legen
else
{
currentLabel = QgsLayerTree::toGroup( mNode )->name();

}

if ( mLayer && mLayer->layer() && mLayer->layer()->type() == QgsMapLayerType::VectorLayer )
{
switch ( qobject_cast< QgsVectorLayer * >( mLayer->layer() )->geometryType() )
{
case QgsWkbTypes::PolygonGeometry:
mPatchShapeCombo->addItem( tr( "Default" ) );
mPatchShapeCombo->addItem( tr( "Oval" ) );
if ( ( dynamic_cast< QgsSymbolLegendNode * >( mLegendNode ) && !dynamic_cast< QgsSymbolLegendNode * >( mLegendNode )->patchShape().isNull() )
|| ( !mLegendNode && !mLayer->patchShape().isNull() ) )
{
mPatchShapeCombo->addItem( tr( "Custom" ) );
mPatchShapeCombo->setCurrentIndex( 2 );
}
break;

case QgsWkbTypes::LineGeometry:
mPatchShapeCombo->addItem( tr( "Default" ) );
mPatchShapeCombo->addItem( tr( "Zig-zag" ) );
if ( ( dynamic_cast< QgsSymbolLegendNode * >( mLegendNode ) && !dynamic_cast< QgsSymbolLegendNode * >( mLegendNode )->patchShape().isNull() )
|| ( !mLegendNode && !mLayer->patchShape().isNull() ) )
{
mPatchShapeCombo->addItem( tr( "Custom" ) );
mPatchShapeCombo->setCurrentIndex( 2 );
}
break;

default:
mPatchShapeLabel->hide();
mPatchShapeCombo->hide();
break;
}
}
else
{
mPatchShapeLabel->hide();
mPatchShapeCombo->hide();
}

mLabelEdit->setText( currentLabel );
connect( mLabelEdit, &QLineEdit::textChanged, this, &QgsLayoutLegendNodeWidget::labelChanged );
connect( mPatchShapeCombo, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &QgsLayoutLegendNodeWidget::patchChanged );

}

Expand Down Expand Up @@ -1410,3 +1451,43 @@ void QgsLayoutLegendNodeWidget::labelChanged( const QString &label )
mLegend->updateFilterByMap();
mLegend->endCommand();
}

void QgsLayoutLegendNodeWidget::patchChanged( int )
{
mLegend->beginCommand( tr( "Edit Legend Item" ) );

QgsLegendPatchShape shape;
if ( mPatchShapeCombo->currentText() == tr( "Default" ) )
{
// use null shape to use default
}
else if ( mPatchShapeCombo->currentText() == tr( "Oval" ) )
{
QgsGeometry g = QgsGeometry::fromWkt( QStringLiteral( "CurvePolygon(CircularString (0 5, 5 0, 0 -5, -5 0, 0 5))" ) );
g.convertToStraightSegment();
shape = QgsLegendPatchShape( QgsSymbol::Fill, g, false );
}
else if ( mPatchShapeCombo->currentText() == tr( "Zig-zag" ) )
{
QgsGeometry g = QgsGeometry::fromWkt( QStringLiteral( "LineString(0 0, 1 1, 2 0, 3 1)" ) );
shape = QgsLegendPatchShape( QgsSymbol::Line, g, false );
}

if ( mLegendNode )
{
QgsMapLayerLegendUtils::setLegendNodePatchShape( mLayer, mOriginalLegendNodeIndex, shape );
mLegend->model()->refreshLayerLegend( mLayer );
}
else if ( mLayer )
{
mLayer->setPatchShape( shape );

// force update of label of the legend node with embedded icon (a bit clumsy i know)
if ( QgsLayerTreeModelLegendNode *embeddedNode = mLegend->model()->legendNodeEmbeddedInParent( mLayer ) )
embeddedNode->setUserLabel( QString() );
}

mLegend->adjustBoxSize();
mLegend->updateFilterByMap();
mLegend->endCommand();
}
1 change: 1 addition & 0 deletions src/gui/layout/qgslayoutlegendwidget.h
Expand Up @@ -184,6 +184,7 @@ class GUI_EXPORT QgsLayoutLegendNodeWidget: public QgsPanelWidget, private Ui::Q
private slots:

void labelChanged( const QString &label );
void patchChanged( int index );

private:

Expand Down
30 changes: 23 additions & 7 deletions src/ui/layout/qgslayoutlegendnodewidgetbase.ui
Expand Up @@ -26,23 +26,39 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Label</string>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="mLabelEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mPatchShapeLabel">
<property name="text">
<string>Patch shape</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="mPatchShapeCombo"/>
</item>
</layout>
</widget>
<customwidgets>
Expand Down

0 comments on commit f27a490

Please sign in to comment.