-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Legend: optional text on top of symbols for vector layers
In some cases it is useful to add extra information to the symbols in the legend. This work allows definition of additional labels in vector layer properties > Legend tab.
- Loading branch information
Showing
17 changed files
with
570 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/*************************************************************************** | ||
qgsvectorlayerlegendwidget.cpp | ||
--------------------- | ||
Date : April 2018 | ||
Copyright : (C) 2018 by Martin Dobias | ||
Email : wonder dot sk at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsvectorlayerlegendwidget.h" | ||
|
||
#include <QBoxLayout> | ||
#include <QStandardItemModel> | ||
#include <QTreeView> | ||
|
||
#include "qgsmaplayerlegend.h" | ||
#include "qgsrenderer.h" | ||
#include "qgssymbollayerutils.h" | ||
#include "qgstextformatwidget.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
|
||
QgsVectorLayerLegendWidget::QgsVectorLayerLegendWidget( QWidget *parent ) | ||
: QWidget( parent ) | ||
{ | ||
mLegendTreeView = new QTreeView; | ||
mLegendTreeView->setRootIsDecorated( false ); | ||
|
||
mTextOnSymbolFormatButton = new QPushButton( tr( "Set Text Format..." ) ); | ||
connect( mTextOnSymbolFormatButton, &QPushButton::clicked, this, &QgsVectorLayerLegendWidget::openTextFormatWidget ); | ||
|
||
mTextOnSymbolGroupBox = new QgsCollapsibleGroupBox; | ||
|
||
QVBoxLayout *groupLayout = new QVBoxLayout; | ||
groupLayout->addWidget( mLegendTreeView ); | ||
groupLayout->addWidget( mTextOnSymbolFormatButton ); | ||
|
||
mTextOnSymbolGroupBox->setTitle( tr( "Text on Symbols" ) ); | ||
mTextOnSymbolGroupBox->setCheckable( true ); | ||
mTextOnSymbolGroupBox->setLayout( groupLayout ); | ||
mTextOnSymbolGroupBox->setCollapsed( true ); | ||
|
||
QVBoxLayout *layout = new QVBoxLayout; | ||
layout->addWidget( mTextOnSymbolGroupBox ); | ||
setLayout( layout ); | ||
} | ||
|
||
|
||
void QgsVectorLayerLegendWidget::setLayer( QgsVectorLayer *layer ) | ||
{ | ||
mLayer = layer; | ||
|
||
QgsDefaultVectorLayerLegend *legend = qobject_cast<QgsDefaultVectorLayerLegend *>( layer->legend() ); | ||
if ( !legend ) | ||
return; | ||
|
||
mTextOnSymbolGroupBox->setChecked( legend->textOnSymbolEnabled() ); | ||
mTextOnSymbolTextFormat = legend->textOnSymbolTextFormat(); | ||
QHash<QString, QString> content = legend->textOnSymbolContent(); | ||
|
||
QStandardItemModel *model = new QStandardItemModel; | ||
model->setColumnCount( 2 ); | ||
model->setHorizontalHeaderLabels( QStringList() << tr( "Symbol" ) << tr( "Text" ) ); | ||
|
||
const QgsLegendSymbolList lst = layer->renderer()->legendSymbolItems(); | ||
for ( const QgsLegendSymbolItem &symbolItem : lst ) | ||
{ | ||
if ( !symbolItem.symbol() ) | ||
continue; | ||
|
||
QgsRenderContext context; | ||
QSize iconSize( 16, 16 ); | ||
QIcon icon = QgsSymbolLayerUtils::symbolPreviewPixmap( symbolItem.symbol(), iconSize, 0, &context ); | ||
|
||
QStandardItem *item1 = new QStandardItem( icon, symbolItem.label() ); | ||
item1->setEditable( false ); | ||
QStandardItem *item2 = new QStandardItem; | ||
if ( symbolItem.ruleKey().isEmpty() ) | ||
{ | ||
item1->setEnabled( false ); | ||
item2->setEnabled( true ); | ||
} | ||
else | ||
{ | ||
item1->setData( symbolItem.ruleKey() ); | ||
if ( content.contains( symbolItem.ruleKey() ) ) | ||
item2->setText( content.value( symbolItem.ruleKey() ) ); | ||
} | ||
model->appendRow( QList<QStandardItem *>() << item1 << item2 ); | ||
} | ||
mLegendTreeView->setModel( model ); | ||
mLegendTreeView->resizeColumnToContents( 0 ); | ||
} | ||
|
||
|
||
void QgsVectorLayerLegendWidget::applyToLayer() | ||
{ | ||
QgsDefaultVectorLayerLegend *legend = new QgsDefaultVectorLayerLegend( mLayer ); | ||
legend->setTextOnSymbolEnabled( mTextOnSymbolGroupBox->isChecked() ); | ||
legend->setTextOnSymbolTextFormat( mTextOnSymbolTextFormat ); | ||
|
||
QHash<QString, QString> content; | ||
if ( QStandardItemModel *model = qobject_cast<QStandardItemModel *>( mLegendTreeView->model() ) ) | ||
{ | ||
for ( int i = 0; i < model->rowCount(); ++i ) | ||
{ | ||
QString ruleKey = model->item( i, 0 )->data().toString(); | ||
QString label = model->item( i, 1 )->text(); | ||
if ( !label.isEmpty() ) | ||
content[ruleKey] = label; | ||
} | ||
} | ||
legend->setTextOnSymbolContent( content ); | ||
|
||
mLayer->setLegend( legend ); | ||
} | ||
|
||
|
||
void QgsVectorLayerLegendWidget::openTextFormatWidget() | ||
{ | ||
QgsTextFormatWidget *textOnSymbolFormatWidget = new QgsTextFormatWidget( mTextOnSymbolTextFormat ); | ||
QDialogButtonBox *dialogButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel ); | ||
QVBoxLayout *layout = new QVBoxLayout; | ||
layout->addWidget( textOnSymbolFormatWidget ); | ||
layout->addWidget( dialogButtonBox ); | ||
QDialog dlg; | ||
connect( dialogButtonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept ); | ||
connect( dialogButtonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject ); | ||
dlg.setLayout( layout ); | ||
if ( !dlg.exec() ) | ||
return; | ||
|
||
mTextOnSymbolTextFormat = textOnSymbolFormatWidget->format(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/*************************************************************************** | ||
qgsvectorlayerlegendwidget.h | ||
--------------------- | ||
Date : April 2018 | ||
Copyright : (C) 2018 by Martin Dobias | ||
Email : wonder dot sk at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSVECTORLAYERLEGENDWIDGET_H | ||
#define QGSVECTORLAYERLEGENDWIDGET_H | ||
|
||
#include <QWidget> | ||
|
||
#include "qgstextrenderer.h" | ||
|
||
class QLabel; | ||
class QPushButton; | ||
class QTreeView; | ||
|
||
class QgsCollapsibleGroupBox; | ||
class QgsVectorLayer; | ||
|
||
/** | ||
* A widget for configuration of options specific to vector layer's legend. | ||
*/ | ||
class QgsVectorLayerLegendWidget : public QWidget | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit QgsVectorLayerLegendWidget( QWidget *parent = nullptr ); | ||
|
||
//! Initialize widget with a map layer | ||
void setLayer( QgsVectorLayer *layer ); | ||
|
||
//! Store changes made in the widget to the layer | ||
void applyToLayer(); | ||
|
||
private slots: | ||
void openTextFormatWidget(); | ||
|
||
private: | ||
QTreeView *mLegendTreeView = nullptr; | ||
QPushButton *mTextOnSymbolFormatButton = nullptr; | ||
QgsCollapsibleGroupBox *mTextOnSymbolGroupBox = nullptr; | ||
QLabel *mTextOnSymbolLabel = nullptr; | ||
|
||
QgsVectorLayer *mLayer = nullptr; | ||
QgsTextFormat mTextOnSymbolTextFormat; | ||
}; | ||
|
||
#endif // QGSVECTORLAYERLEGENDWIDGET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.