45 #include <QMessageBox> 46 #include <QInputDialog> 61 int unfilteredNodeIndex = _unfilteredLegendNodeIndex( legendNode );
63 return ( unfilteredNodeIndex >= 0 && unfilteredNodeIndex < order.count() ? order[unfilteredNodeIndex] : -1 );
70 , mMapCanvas( mapCanvas )
75 connect( mWrapCharLineEdit, &QLineEdit::textChanged,
this, &QgsLayoutLegendWidget::mWrapCharLineEdit_textChanged );
76 connect( mTitleLineEdit, &QLineEdit::textChanged,
this, &QgsLayoutLegendWidget::mTitleLineEdit_textChanged );
81 connect( mColumnCountSpinBox, static_cast <
void ( QSpinBox::* )(
int ) > ( &QSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mColumnCountSpinBox_valueChanged );
82 connect( mSplitLayerCheckBox, &QCheckBox::toggled,
this, &QgsLayoutLegendWidget::mSplitLayerCheckBox_toggled );
83 connect( mEqualColumnWidthCheckBox, &QCheckBox::toggled,
this, &QgsLayoutLegendWidget::mEqualColumnWidthCheckBox_toggled );
84 connect( mSymbolWidthSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mSymbolWidthSpinBox_valueChanged );
85 connect( mSymbolHeightSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mSymbolHeightSpinBox_valueChanged );
86 connect( mWmsLegendWidthSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mWmsLegendWidthSpinBox_valueChanged );
87 connect( mWmsLegendHeightSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mWmsLegendHeightSpinBox_valueChanged );
88 connect( mTitleSpaceBottomSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mTitleSpaceBottomSpinBox_valueChanged );
89 connect( mGroupSpaceSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mGroupSpaceSpinBox_valueChanged );
90 connect( mSpaceBelowGroupHeadingSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::spaceBelowGroupHeadingChanged );
91 connect( mLayerSpaceSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mLayerSpaceSpinBox_valueChanged );
92 connect( mSpaceBelowSubgroupHeadingSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::spaceBelowSubGroupHeadingChanged );
93 connect( mSymbolSpaceSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mSymbolSpaceSpinBox_valueChanged );
94 connect( mIconLabelSpaceSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mIconLabelSpaceSpinBox_valueChanged );
96 connect( mBoxSpaceSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mBoxSpaceSpinBox_valueChanged );
97 connect( mColumnSpaceSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mColumnSpaceSpinBox_valueChanged );
98 connect( mLineSpacingSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mLineSpacingSpinBox_valueChanged );
99 connect( mCheckBoxAutoUpdate, &QCheckBox::stateChanged,
this, &QgsLayoutLegendWidget::mCheckBoxAutoUpdate_stateChanged );
100 connect( mCheckboxResizeContents, &QCheckBox::toggled,
this, &QgsLayoutLegendWidget::mCheckboxResizeContents_toggled );
101 connect( mRasterStrokeGroupBox, &QgsCollapsibleGroupBoxBasic::toggled,
this, &QgsLayoutLegendWidget::mRasterStrokeGroupBox_toggled );
102 connect( mRasterStrokeWidthSpinBox, static_cast <
void ( QDoubleSpinBox::* )(
double ) > ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutLegendWidget::mRasterStrokeWidthSpinBox_valueChanged );
103 connect( mRasterStrokeColorButton, &
QgsColorButton::colorChanged,
this, &QgsLayoutLegendWidget::mRasterStrokeColorButton_colorChanged );
104 connect( mMoveDownToolButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mMoveDownToolButton_clicked );
105 connect( mMoveUpToolButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mMoveUpToolButton_clicked );
106 connect( mRemoveToolButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mRemoveToolButton_clicked );
107 connect( mAddToolButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mAddToolButton_clicked );
108 connect( mEditPushButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mEditPushButton_clicked );
109 connect( mCountToolButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mCountToolButton_clicked );
110 connect( mExpressionFilterButton, &QgsLegendFilterButton::toggled,
this, &QgsLayoutLegendWidget::mExpressionFilterButton_toggled );
111 connect( mLayerExpressionButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mLayerExpressionButton_clicked );
112 connect( mFilterByMapCheckBox, &QCheckBox::toggled,
this, &QgsLayoutLegendWidget::mFilterByMapCheckBox_toggled );
113 connect( mUpdateAllPushButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mUpdateAllPushButton_clicked );
114 connect( mAddGroupToolButton, &QToolButton::clicked,
this, &QgsLayoutLegendWidget::mAddGroupToolButton_clicked );
115 connect( mFilterLegendByAtlasCheckBox, &QCheckBox::toggled,
this, &QgsLayoutLegendWidget::mFilterLegendByAtlasCheckBox_toggled );
116 connect( mItemTreeView, &QgsLayerTreeView::doubleClicked,
this, &QgsLayoutLegendWidget::mItemTreeView_doubleClicked );
124 mTitleAlignCombo->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight );
125 mGroupAlignCombo->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight );
126 mSubgroupAlignCombo->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight );
127 mItemAlignCombo->setAvailableAlignments( Qt::AlignLeft | Qt::AlignHCenter | Qt::AlignRight );
129 mArrangementCombo->setAvailableAlignments( Qt::AlignLeft | Qt::AlignRight );
131 mArrangementCombo->customizeAlignmentDisplay( Qt::AlignLeft, tr(
"Symbols on Left" ),
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconArrangeSymbolsLeft.svg" ) ) );
132 mArrangementCombo->customizeAlignmentDisplay( Qt::AlignRight, tr(
"Symbols on Right" ),
QgsApplication::getThemeIcon( QStringLiteral(
"/mIconArrangeSymbolsRight.svg" ) ) );
134 mSpaceBelowGroupHeadingSpinBox->setClearValue( 0 );
135 mSpaceBelowSubgroupHeadingSpinBox->setClearValue( 0 );
156 mFontColorButton->setColorDialogTitle( tr(
"Select Font Color" ) );
157 mFontColorButton->setContext( QStringLiteral(
"composer" ) );
159 mRasterStrokeColorButton->setColorDialogTitle( tr(
"Select Stroke Color" ) );
160 mRasterStrokeColorButton->setAllowOpacity(
true );
161 mRasterStrokeColorButton->setContext( QStringLiteral(
"composer " ) );
163 mMapComboBox->setCurrentLayout( legend->
layout() );
169 mainLayout->addWidget( mItemPropertiesWidget );
171 mItemTreeView->setHeaderHidden(
true );
173 mItemTreeView->setModel( legend->
model() );
175 setLegendMapViewData();
190 connect( mItemTreeView->selectionModel(), &QItemSelectionModel::currentChanged,
191 this, &QgsLayoutLegendWidget::selectedChanged );
200 if ( mItemPropertiesWidget )
204 void QgsLayoutLegendWidget::setGuiElements()
211 blockAllSignals(
true );
212 mTitleLineEdit->setText( mLegend->title() );
213 whileBlocking( mTitleAlignCombo )->setCurrentAlignment( mLegend->titleAlignment() );
217 whileBlocking( mArrangementCombo )->setCurrentAlignment( mLegend->symbolAlignment() );
218 mFilterByMapCheckBox->setChecked( mLegend->legendFilterByMapEnabled() );
219 mColumnCountSpinBox->setValue( mLegend->columnCount() );
220 mSplitLayerCheckBox->setChecked( mLegend->splitLayer() );
221 mEqualColumnWidthCheckBox->setChecked( mLegend->equalColumnWidth() );
222 mSymbolWidthSpinBox->setValue( mLegend->symbolWidth() );
223 mSymbolHeightSpinBox->setValue( mLegend->symbolHeight() );
224 mWmsLegendWidthSpinBox->setValue( mLegend->wmsLegendWidth() );
225 mWmsLegendHeightSpinBox->setValue( mLegend->wmsLegendHeight() );
234 mBoxSpaceSpinBox->setValue( mLegend->boxSpace() );
235 mColumnSpaceSpinBox->setValue( mLegend->columnSpace() );
236 mLineSpacingSpinBox->setValue( mLegend->lineSpacing() );
238 mRasterStrokeGroupBox->setChecked( mLegend->drawRasterStroke() );
239 mRasterStrokeWidthSpinBox->setValue( mLegend->rasterStrokeWidth() );
240 mRasterStrokeColorButton->setColor( mLegend->rasterStrokeColor() );
242 mCheckBoxAutoUpdate->setChecked( mLegend->autoUpdateModel() );
244 mCheckboxResizeContents->setChecked( mLegend->resizeToContents() );
245 mFilterLegendByAtlasCheckBox->setChecked( mLegend->legendFilterOutAtlas() );
246 mWrapCharLineEdit->setText( mLegend->wrapString() );
249 mMapComboBox->setItem( map );
250 mFontColorButton->setColor( mLegend->fontColor() );
256 blockAllSignals(
false );
258 mCheckBoxAutoUpdate_stateChanged( mLegend->autoUpdateModel() ? Qt::Checked : Qt::Unchecked );
263 void QgsLayoutLegendWidget::mWrapCharLineEdit_textChanged(
const QString &text )
267 mLegend->beginCommand( tr(
"Change Legend Wrap" ) );
268 mLegend->setWrapString( text );
269 mLegend->adjustBoxSize();
271 mLegend->endCommand();
275 void QgsLayoutLegendWidget::mTitleLineEdit_textChanged(
const QString &text )
280 mLegend->setTitle( text );
281 mLegend->adjustBoxSize();
283 mLegend->endCommand();
287 void QgsLayoutLegendWidget::titleAlignmentChanged()
291 Qt::AlignmentFlag alignment =
static_cast< Qt::AlignmentFlag
>(
static_cast< int >( mTitleAlignCombo->currentAlignment() & Qt::AlignHorizontal_Mask ) );
292 mLegend->beginCommand( tr(
"Change Title Alignment" ) );
293 mLegend->setTitleAlignment( alignment );
295 mLegend->endCommand();
299 void QgsLayoutLegendWidget::groupAlignmentChanged()
303 mLegend->beginCommand( tr(
"Change Group Alignment" ) );
306 mLegend->endCommand();
310 void QgsLayoutLegendWidget::subgroupAlignmentChanged()
314 mLegend->beginCommand( tr(
"Change Subgroup Alignment" ) );
317 mLegend->endCommand();
321 void QgsLayoutLegendWidget::itemAlignmentChanged()
325 mLegend->beginCommand( tr(
"Change Item Alignment" ) );
328 mLegend->endCommand();
332 void QgsLayoutLegendWidget::arrangementChanged()
336 Qt::AlignmentFlag alignment =
static_cast< Qt::AlignmentFlag
>(
static_cast< int >( mArrangementCombo->currentAlignment() & Qt::AlignHorizontal_Mask ) );
337 mLegend->beginCommand( tr(
"Change Legend Arrangement" ) );
338 mLegend->setSymbolAlignment( alignment );
340 mLegend->endCommand();
344 void QgsLayoutLegendWidget::mColumnCountSpinBox_valueChanged(
int c )
349 mLegend->setColumnCount( c );
350 mLegend->adjustBoxSize();
352 mLegend->endCommand();
354 mSplitLayerCheckBox->setEnabled( c > 1 );
355 mEqualColumnWidthCheckBox->setEnabled( c > 1 );
358 void QgsLayoutLegendWidget::mSplitLayerCheckBox_toggled(
bool checked )
362 mLegend->beginCommand( tr(
"Split Legend Layers" ) );
363 mLegend->setSplitLayer( checked );
364 mLegend->adjustBoxSize();
366 mLegend->endCommand();
370 void QgsLayoutLegendWidget::mEqualColumnWidthCheckBox_toggled(
bool checked )
374 mLegend->beginCommand( tr(
"Legend Column Width" ) );
375 mLegend->setEqualColumnWidth( checked );
376 mLegend->adjustBoxSize();
378 mLegend->endCommand();
382 void QgsLayoutLegendWidget::mSymbolWidthSpinBox_valueChanged(
double d )
387 mLegend->setSymbolWidth( d );
388 mLegend->adjustBoxSize();
390 mLegend->endCommand();
394 void QgsLayoutLegendWidget::mSymbolHeightSpinBox_valueChanged(
double d )
399 mLegend->setSymbolHeight( d );
400 mLegend->adjustBoxSize();
402 mLegend->endCommand();
406 void QgsLayoutLegendWidget::mWmsLegendWidthSpinBox_valueChanged(
double d )
411 mLegend->setWmsLegendWidth( d );
412 mLegend->adjustBoxSize();
414 mLegend->endCommand();
418 void QgsLayoutLegendWidget::mWmsLegendHeightSpinBox_valueChanged(
double d )
423 mLegend->setWmsLegendHeight( d );
424 mLegend->adjustBoxSize();
426 mLegend->endCommand();
430 void QgsLayoutLegendWidget::mTitleSpaceBottomSpinBox_valueChanged(
double d )
436 mLegend->adjustBoxSize();
438 mLegend->endCommand();
442 void QgsLayoutLegendWidget::mGroupSpaceSpinBox_valueChanged(
double d )
448 mLegend->adjustBoxSize();
450 mLegend->endCommand();
454 void QgsLayoutLegendWidget::spaceBelowGroupHeadingChanged(
double space )
460 mLegend->adjustBoxSize();
462 mLegend->endCommand();
466 void QgsLayoutLegendWidget::mLayerSpaceSpinBox_valueChanged(
double d )
472 mLegend->adjustBoxSize();
474 mLegend->endCommand();
478 void QgsLayoutLegendWidget::mSymbolSpaceSpinBox_valueChanged(
double d )
486 mLegend->adjustBoxSize();
488 mLegend->endCommand();
492 void QgsLayoutLegendWidget::mIconLabelSpaceSpinBox_valueChanged(
double d )
498 mLegend->adjustBoxSize();
500 mLegend->endCommand();
504 void QgsLayoutLegendWidget::titleFontChanged()
510 mLegend->adjustBoxSize();
512 mLegend->endCommand();
516 void QgsLayoutLegendWidget::groupFontChanged()
522 mLegend->adjustBoxSize();
524 mLegend->endCommand();
528 void QgsLayoutLegendWidget::layerFontChanged()
534 mLegend->adjustBoxSize();
536 mLegend->endCommand();
540 void QgsLayoutLegendWidget::itemFontChanged()
546 mLegend->adjustBoxSize();
548 mLegend->endCommand();
552 void QgsLayoutLegendWidget::spaceBelowSubGroupHeadingChanged(
double space )
558 mLegend->adjustBoxSize();
560 mLegend->endCommand();
564 void QgsLayoutLegendWidget::mFontColorButton_colorChanged(
const QColor &newFontColor )
572 mLegend->setFontColor( newFontColor );
574 mLegend->endCommand();
577 void QgsLayoutLegendWidget::mBoxSpaceSpinBox_valueChanged(
double d )
582 mLegend->setBoxSpace( d );
583 mLegend->adjustBoxSize();
585 mLegend->endCommand();
589 void QgsLayoutLegendWidget::mColumnSpaceSpinBox_valueChanged(
double d )
594 mLegend->setColumnSpace( d );
595 mLegend->adjustBoxSize();
597 mLegend->endCommand();
601 void QgsLayoutLegendWidget::mLineSpacingSpinBox_valueChanged(
double d )
606 mLegend->setLineSpacing( d );
607 mLegend->adjustBoxSize();
609 mLegend->endCommand();
613 static void _moveLegendNode(
QgsLayerTreeLayer *nodeLayer,
int legendNodeIndex,
int offset )
617 if ( legendNodeIndex < 0 || legendNodeIndex >= order.count() )
619 if ( legendNodeIndex + offset < 0 || legendNodeIndex + offset >= order.count() )
622 int id = order.takeAt( legendNodeIndex );
623 order.insert( legendNodeIndex + offset,
id );
629 void QgsLayoutLegendWidget::mMoveDownToolButton_clicked()
636 QModelIndex index = mItemTreeView->selectionModel()->currentIndex();
637 QModelIndex parentIndex = index.parent();
638 if ( !index.isValid() || index.row() == mItemTreeView->model()->rowCount( parentIndex ) - 1 )
641 QgsLayerTreeNode *node = mItemTreeView->layerTreeModel()->index2node( index );
643 if ( !node && !legendNode )
646 mLegend->beginCommand( tr(
"Moved Legend Item Down" ) );
656 _moveLegendNode( legendNode->
layerNode(), _unfilteredLegendNodeIndex( legendNode ), 1 );
657 mItemTreeView->layerTreeModel()->refreshLayerLegend( legendNode->
layerNode() );
660 mItemTreeView->setCurrentIndex( mItemTreeView->layerTreeModel()->index( index.row() + 1, 0, parentIndex ) );
663 mLegend->endCommand();
666 void QgsLayoutLegendWidget::mMoveUpToolButton_clicked()
673 QModelIndex index = mItemTreeView->selectionModel()->currentIndex();
674 QModelIndex parentIndex = index.parent();
675 if ( !index.isValid() || index.row() == 0 )
678 QgsLayerTreeNode *node = mItemTreeView->layerTreeModel()->index2node( index );
680 if ( !node && !legendNode )
683 mLegend->beginCommand( tr(
"Move Legend Item Up" ) );
693 _moveLegendNode( legendNode->
layerNode(), _unfilteredLegendNodeIndex( legendNode ), -1 );
694 mItemTreeView->layerTreeModel()->refreshLayerLegend( legendNode->
layerNode() );
697 mItemTreeView->setCurrentIndex( mItemTreeView->layerTreeModel()->index( index.row() - 1, 0, parentIndex ) );
700 mLegend->endCommand();
703 void QgsLayoutLegendWidget::mCheckBoxAutoUpdate_stateChanged(
int state )
705 mLegend->beginCommand( tr(
"Change Auto Update" ) );
707 mLegend->setAutoUpdateModel( state == Qt::Checked );
709 mLegend->updateFilterByMap();
710 mLegend->endCommand();
713 QList<QWidget *> widgets;
714 widgets << mMoveDownToolButton << mMoveUpToolButton << mRemoveToolButton << mAddToolButton
715 << mEditPushButton << mCountToolButton << mUpdateAllPushButton << mAddGroupToolButton
716 << mExpressionFilterButton;
717 for ( QWidget *w : qgis::as_const( widgets ) )
718 w->setEnabled( state != Qt::Checked );
720 if ( state == Qt::Unchecked )
723 selectedChanged( QModelIndex(), QModelIndex() );
727 void QgsLayoutLegendWidget::composerMapChanged(
QgsLayoutItem *item )
743 mLegend->beginCommand( tr(
"Change Legend Map" ) );
744 mLegend->setLinkedMap( map );
745 mLegend->updateFilterByMap();
746 mLegend->endCommand();
748 setLegendMapViewData();
752 void QgsLayoutLegendWidget::mCheckboxResizeContents_toggled(
bool checked )
759 mLegend->beginCommand( tr(
"Resize Legend to Contents" ) );
760 mLegend->setResizeToContents( checked );
762 mLegend->adjustBoxSize();
763 mLegend->updateFilterByMap();
764 mLegend->endCommand();
767 void QgsLayoutLegendWidget::mRasterStrokeGroupBox_toggled(
bool state )
774 mLegend->beginCommand( tr(
"Change Legend Borders" ) );
775 mLegend->setDrawRasterStroke( state );
776 mLegend->adjustBoxSize();
778 mLegend->endCommand();
781 void QgsLayoutLegendWidget::mRasterStrokeWidthSpinBox_valueChanged(
double d )
789 mLegend->setRasterStrokeWidth( d );
790 mLegend->adjustBoxSize();
792 mLegend->endCommand();
795 void QgsLayoutLegendWidget::mRasterStrokeColorButton_colorChanged(
const QColor &newColor )
803 mLegend->setRasterStrokeColor( newColor );
805 mLegend->endCommand();
808 void QgsLayoutLegendWidget::mAddToolButton_clicked()
815 QList< QgsMapLayer * > visibleLayers;
816 if ( mLegend->linkedMap() )
818 visibleLayers = mLegend->linkedMap()->layersToRender();
820 if ( visibleLayers.isEmpty() )
823 visibleLayers = mMapCanvas->
layers();
828 if ( addDialog.exec() == QDialog::Accepted )
831 if ( !layers.empty() )
833 mLegend->beginCommand( tr(
"Add Legend Item(s)" ) );
836 mLegend->model()->rootGroup()->addLayer( layer );
838 mLegend->updateLegend();
839 mLegend->endCommand();
844 void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
851 QItemSelectionModel *selectionModel = mItemTreeView->selectionModel();
852 if ( !selectionModel )
857 mLegend->beginCommand( tr(
"Remove Legend Item" ) );
859 QList<QPersistentModelIndex> indexes;
860 const auto constSelectedIndexes = selectionModel->selectedIndexes();
861 for (
const QModelIndex &index : constSelectedIndexes )
865 QHash<QgsLayerTreeLayer *, QList<int> > nodesWithRemoval;
866 for (
const QPersistentModelIndex &index : qgis::as_const( indexes ) )
871 nodesWithRemoval[nodeLayer].append( _unfilteredLegendNodeIndex( legendNode ) );
874 for (
auto it = nodesWithRemoval.constBegin(); it != nodesWithRemoval.constEnd(); ++it )
876 QList<int> toDelete = it.value();
877 std::sort( toDelete.begin(), toDelete.end(), std::greater<int>() );
880 const auto constToDelete = toDelete;
881 for (
int i : constToDelete )
883 if ( i >= 0 && i < order.count() )
888 mItemTreeView->layerTreeModel()->refreshLayerLegend( it.key() );
892 for (
const QPersistentModelIndex &index : qgis::as_const( indexes ) )
894 if ( index.isValid() && mItemTreeView->layerTreeModel()->index2node( index ) )
895 mLegend->model()->removeRow( index.row(), index.parent() );
898 mLegend->updateLegend();
899 mLegend->endCommand();
902 void QgsLayoutLegendWidget::mEditPushButton_clicked()
909 QModelIndex idx = mItemTreeView->selectionModel()->currentIndex();
910 mItemTreeView_doubleClicked( idx );
921 QModelIndex currentIndex = mItemTreeView->currentIndex();
922 if ( !currentIndex.isValid() )
928 if (
QgsLayerTreeNode *node = mItemTreeView->layerTreeModel()->index2node( currentIndex ) )
941 mLegend->beginCommand( tr(
"Update Legend" ) );
944 for (
const QString &key : constCustomProperties )
946 if ( key.startsWith( QLatin1String(
"legend/" ) ) )
950 mItemTreeView->layerTreeModel()->refreshLayerLegend( nodeLayer );
952 mLegend->updateLegend();
953 mLegend->endCommand();
956 void QgsLayoutLegendWidget::mCountToolButton_clicked(
bool checked )
965 QModelIndex currentIndex = mItemTreeView->currentIndex();
966 if ( !currentIndex.isValid() )
975 mLegend->beginCommand( tr(
"Update Legend" ) );
976 currentNode->
setCustomProperty( QStringLiteral(
"showFeatureCount" ), checked ? 1 : 0 );
977 mLegend->updateFilterByMap();
978 mLegend->adjustBoxSize();
979 mLegend->endCommand();
982 void QgsLayoutLegendWidget::mFilterByMapCheckBox_toggled(
bool checked )
984 mLegend->beginCommand( tr(
"Update Legend" ) );
985 mLegend->setLegendFilterByMapEnabled( checked );
986 mLegend->adjustBoxSize();
988 mLegend->endCommand();
991 void QgsLayoutLegendWidget::mExpressionFilterButton_toggled(
bool checked )
999 QModelIndex currentIndex = mItemTreeView->currentIndex();
1000 if ( !currentIndex.isValid() )
1010 mExpressionFilterButton->expressionText(),
1013 mLegend->beginCommand( tr(
"Update Legend" ) );
1014 mLegend->updateFilterByMap();
1015 mLegend->adjustBoxSize();
1016 mLegend->endCommand();
1019 void QgsLayoutLegendWidget::mLayerExpressionButton_clicked()
1026 QModelIndex currentIndex = mItemTreeView->currentIndex();
1027 if ( !currentIndex.isValid() )
1040 QString currentExpression;
1042 currentExpression = QStringLiteral(
"@symbol_label" );
1046 legendContext.
appendScope( vl->createExpressionContextScope() );
1052 QStringList highlighted;
1057 if ( !legendSymbols.empty() )
1063 highlighted << QStringLiteral(
"symbol_label" ) << QStringLiteral(
"symbol_id" );
1067 highlighted << QStringLiteral(
"symbol_count" );
1077 if ( expressiondialog.exec() )
1080 mLegend->beginCommand( tr(
"Update Legend" ) );
1081 mLegend->updateLegend();
1082 mLegend->adjustBoxSize();
1083 mLegend->endCommand();
1086 void QgsLayoutLegendWidget::mUpdateAllPushButton_clicked()
1091 void QgsLayoutLegendWidget::mAddGroupToolButton_clicked()
1095 mLegend->beginCommand( tr(
"Add Legend Group" ) );
1096 mLegend->model()->rootGroup()->addGroup( tr(
"Group" ) );
1097 mLegend->updateLegend();
1098 mLegend->endCommand();
1102 void QgsLayoutLegendWidget::mFilterLegendByAtlasCheckBox_toggled(
bool toggled )
1107 mLegend->setLegendFilterOutAtlas( toggled );
1117 mLegend->beginCommand( tr(
"Update Legend" ) );
1120 mLegend->setAutoUpdateModel(
true );
1121 mLegend->setAutoUpdateModel(
false );
1122 mLegend->updateFilterByMap();
1123 mLegend->endCommand();
1129 mFilterLegendByAtlasCheckBox->setText( tr(
"Only show items inside current %1 feature" ).arg(
string ) );
1130 mFilterLegendByAtlasCheckBox->setToolTip( tr(
"Filter out legend elements that lie outside the current %1 feature." ).arg(
string ) );
1144 mItemPropertiesWidget->
setItem( mLegend );
1148 mItemTreeView->setModel( mLegend->model() );
1157 void QgsLayoutLegendWidget::blockAllSignals(
bool b )
1159 mTitleLineEdit->blockSignals( b );
1160 mTitleAlignCombo->blockSignals( b );
1161 mItemTreeView->blockSignals( b );
1162 mCheckBoxAutoUpdate->blockSignals( b );
1163 mMapComboBox->blockSignals( b );
1164 mFilterByMapCheckBox->blockSignals( b );
1165 mColumnCountSpinBox->blockSignals( b );
1166 mSplitLayerCheckBox->blockSignals( b );
1167 mEqualColumnWidthCheckBox->blockSignals( b );
1168 mSymbolWidthSpinBox->blockSignals( b );
1169 mSymbolHeightSpinBox->blockSignals( b );
1170 mGroupSpaceSpinBox->blockSignals( b );
1171 mLayerSpaceSpinBox->blockSignals( b );
1172 mSymbolSpaceSpinBox->blockSignals( b );
1173 mIconLabelSpaceSpinBox->blockSignals( b );
1174 mBoxSpaceSpinBox->blockSignals( b );
1175 mColumnSpaceSpinBox->blockSignals( b );
1176 mFontColorButton->blockSignals( b );
1177 mRasterStrokeGroupBox->blockSignals( b );
1178 mRasterStrokeColorButton->blockSignals( b );
1179 mRasterStrokeWidthSpinBox->blockSignals( b );
1180 mWmsLegendWidthSpinBox->blockSignals( b );
1181 mWmsLegendHeightSpinBox->blockSignals( b );
1182 mCheckboxResizeContents->blockSignals( b );
1183 mTitleSpaceBottomSpinBox->blockSignals( b );
1184 mFilterLegendByAtlasCheckBox->blockSignals( b );
1185 mTitleFontButton->blockSignals( b );
1186 mGroupFontButton->blockSignals( b );
1187 mLayerFontButton->blockSignals( b );
1188 mItemFontButton->blockSignals( b );
1189 mWrapCharLineEdit->blockSignals( b );
1192 void QgsLayoutLegendWidget::selectedChanged(
const QModelIndex ¤t,
const QModelIndex &previous )
1195 Q_UNUSED( previous )
1197 mLayerExpressionButton->setEnabled(
false );
1199 if ( mLegend && mLegend->autoUpdateModel() )
1210 mLayerExpressionButton->setEnabled(
true );
1214 mCountToolButton->setChecked(
false );
1215 mCountToolButton->setEnabled(
false );
1218 mExpressionFilterButton->blockSignals(
true );
1219 mExpressionFilterButton->setChecked(
false );
1220 mExpressionFilterButton->setEnabled(
false );
1221 mExpressionFilterButton->blockSignals(
false );
1232 mCountToolButton->setChecked( currentNode->
customProperty( QStringLiteral(
"showFeatureCount" ), 0 ).toInt() );
1233 mCountToolButton->setEnabled(
true );
1234 mLayerExpressionButton->setEnabled(
true );
1238 mExpressionFilterButton->blockSignals(
true );
1239 mExpressionFilterButton->setExpressionText( expr );
1240 mExpressionFilterButton->setVectorLayer( vl );
1241 mExpressionFilterButton->setEnabled(
true );
1242 mExpressionFilterButton->setChecked( exprEnabled );
1243 mExpressionFilterButton->blockSignals(
false );
1248 QAction *a = qobject_cast<QAction *>( sender() );
1249 if ( !a || !mItemTreeView->currentNode() )
1253 mLegend->updateFilterByMap();
1256 void QgsLayoutLegendWidget::setLegendMapViewData()
1258 if ( mLegend->linkedMap() )
1262 measurementConverter.
setDpi( dpi );
1265 double mapUnitsPerPixelX = mLegend->linkedMap()->extent().width() / mapWidth;
1266 double mapUnitsPerPixelY = mLegend->linkedMap()->extent().height() / mapHeight;
1267 mLegend->model()->setLegendMapViewData( ( mapUnitsPerPixelX > mapUnitsPerPixelY ? mapUnitsPerPixelX : mapUnitsPerPixelY ), dpi, mLegend->linkedMap()->scale() );
1271 void QgsLayoutLegendWidget::updateFilterLegendByAtlasButton()
1275 mFilterLegendByAtlasCheckBox->setEnabled( atlas->enabled() && mLegend->layout()->reportContext().layer() && mLegend->layout()->reportContext().layer()->geometryType() ==
QgsWkbTypes::PolygonGeometry );
1279 void QgsLayoutLegendWidget::mItemTreeView_doubleClicked(
const QModelIndex &idx )
1281 if ( !mLegend || !idx.isValid() )
1289 QString currentText;
1298 QVariant v = currentNode->
customProperty( QStringLiteral(
"legend/title-label" ) );
1300 currentText = v.toString();
1302 else if ( legendNode )
1304 currentText = legendNode->
data( Qt::EditRole ).toString();
1308 QString newText = QInputDialog::getText(
this, tr(
"Legend Item Properties" ), tr(
"Item text" ),
1309 QLineEdit::Normal, currentText, &ok );
1310 if ( !ok || newText == currentText )
1313 mLegend->beginCommand( tr(
"Edit Legend Item" ) );
1321 currentNode->
setCustomProperty( QStringLiteral(
"legend/title-label" ), newText );
1325 embeddedNode->setUserLabel( QString() );
1327 else if ( legendNode )
1329 int originalIndex = _originalLegendNodeIndex( legendNode );
1334 mLegend->adjustBoxSize();
1335 mLegend->updateFilterByMap();
1336 mLegend->endCommand();
1357 QMenu *menu =
new QMenu();
1362 menu->addSeparator();
1367 QList<QgsLegendStyle::Style> lst;
1372 action->setCheckable(
true );
1373 action->setChecked( currentStyle == style );
1374 action->setData( static_cast< int >( style ) );
static void setNodeLegendStyle(QgsLayerTreeNode *node, QgsLegendStyle::Style style)
Sets the style of a node.
Layer tree group node serves as a container for layers and further groups.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
Single variable definition for use within a QgsExpressionContextScope.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
The QgsLayerTreeView class extends QTreeView and provides some additional functionality when working ...
static void setLegendNodeOrder(QgsLayerTreeLayer *nodeLayer, const QList< int > &order)
QString labelExpression() const
Returns the expression member of the LayerTreeNode.
QList< QgsLegendSymbolItem > QgsLegendSymbolList
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
QgsLegendModel * model()
Returns the legend model.
QList< QgsMapLayer * > layers() const
Returns the list of layers shown within the map canvas.
Base class for graphical items within a QgsLayout.
int type() const override
Returns a unique graphics item type identifier.
QList< QgsLayerTreeModelLegendNode * > layerOriginalLegendNodes(QgsLayerTreeLayer *nodeLayer)
Returns original (unfiltered) list of legend nodes attached to a particular layer node...
static void setLegendNodeUserLabel(QgsLayerTreeLayer *nodeLayer, int originalIndex, const QString &newLabel)
QgsLayoutMeasurement convert(QgsLayoutMeasurement measurement, QgsUnitTypes::LayoutUnit targetUnits) const
Converts a measurement from one unit to another.
virtual QgsLayerTreeNode * clone() const =0
Create a copy of the node. Returns new instance.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
QgsLayerTreeModelLegendNode * legendNodeEmbeddedInParent(QgsLayerTreeLayer *nodeLayer) const
Returns legend node that may be embedded in parent (i.e.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
static QList< int > legendNodeOrder(QgsLayerTreeLayer *nodeLayer)
Implementation of legend node interface for displaying preview of vector symbols and their labels and...
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
void removeChildNode(QgsLayerTreeNode *node)
Remove a child node from this group.
void setDpi(const double dpi)
Sets the dots per inch (dpi) for the measurement converter.
void toggled(bool)
Emitted when atlas is enabled or disabled.
QList< QgsMapLayer *> selectedLayers() const
Returns the list of selected layers.
QStringList customProperties() const
Returns list of keys stored in custom properties.
Map canvas is a class for displaying all GIS data types on a canvas.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
The QgsLayerTreeModel class is model implementation for Qt item views framework.
static QgsLegendStyle::Style nodeLegendStyle(QgsLayerTreeNode *node, QgsLayerTreeModel *model)
Returns the style for the given node, within the specified model.
Layout graphical items for displaying a map.
QString name() const override
Returns the layer's name.
const QgsLayout * layout() const
Returns the layout the object is attached to.
#define QgsDebugMsgLevel(str, level)
A dialog to add new layers to the legend.
Legend raster stroke color.
static QgsLayerTreeModelLegendNode * index2legendNode(const QModelIndex &index)
Returns legend node for given index.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
QgsLayerTreeModel * model() const
Returns pointer to model owning this legend node.
Class used to render QgsLayout as an atlas, by iterating over the features from an associated vector ...
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file...
void setName(const QString &n) override
Sets the group's name.
Symbol icon (excluding label)
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file...
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
static QString styleLabel(Style s)
Returns a translated string representing a style component, for use in UI.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
Style
Component of legends which can be styled.
QgsLayerTreeModel * layerTreeModel() const
Gets access to the model casted to QgsLayerTreeModel.
void refreshLayerLegend(QgsLayerTreeLayer *nodeLayer)
Force a refresh of legend nodes of a layer node.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
void setVisibleLayers(const QList< QgsMapLayer *> &layers)
Sets a list of visible layers, to use for filtering within the dialog.
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
Legend raster stroke width.
Special style, item is hidden including margins around.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
static void setLegendFilterByExpression(QgsLayerTreeLayer &layer, const QString &expr, bool enabled=true)
Sets the expression filter of a legend layer.
void insertChildNode(int index, QgsLayerTreeNode *node)
Insert existing node at specified position.
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
This class provides a method of converting QgsLayoutMeasurements from one unit to another...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
bool autoUpdateModel() const
Returns whether the legend content should auto update to reflect changes in the project's layer tree...
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
void changed()
Emitted when the alignment is changed.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user...
void setLabelExpression(const QString &expression)
set the expression to evaluate
QString name() const override
Returns the group's name.
A layout item subclass for map legends.
Symbol label (excluding icon)
Interface for master layout type objects, such as print layouts and reports.
void changed()
Emitted when the object's properties change.
Represents a vector layer which manages a vector based data sets.
Legend icon symbol space.
A generic dialog for building expression strings.
static QString legendFilterByExpression(const QgsLayerTreeLayer &layer, bool *enabled=nullptr)
Returns the expression filter of a legend layer.
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file...
Layer tree node points to a map layer.
QgsLayerTreeNode * currentNode() const
Gets current node. May be nullptr.