QGIS API Documentation 3.27.0-Master (1d7a28cfd2)
qgslegendpatchshapewidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslegendpatchshapewidget.cpp
3 -----------------------------
4 Date : April 2020
5 Copyright : (C) 2020 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18#include "qgsstylesavedialog.h"
19#include "qgsproject.h"
21#include <QDialogButtonBox>
22#include <QMessageBox>
23
25 : QgsPanelWidget( parent )
26{
27 setupUi( this );
28 setPanelTitle( tr( "Legend Patch Shape" ) );
29
30 mStyleItemsListWidget->setStyle( QgsStyle::defaultStyle() );
31 mStyleItemsListWidget->setEntityType( QgsStyle::LegendPatchShapeEntity );
32 mStyleItemsListWidget->setSymbolType( shape.symbolType() );
33
34 setShape( shape );
35
36 connect( mPreserveRatioCheckBox, &QCheckBox::toggled, this, &QgsLegendPatchShapeWidget::changed );
37 connect( mShapeEdit, &QPlainTextEdit::textChanged, this, &QgsLegendPatchShapeWidget::changed );
38
39 connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::selectionChangedWithStylePath, this, &QgsLegendPatchShapeWidget::setShapeFromStyle );
40 connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::saveEntity, this, &QgsLegendPatchShapeWidget::saveShape );
41}
42
44{
45 QgsLegendPatchShape res( mType, QgsGeometry::fromWkt( mShapeEdit->toPlainText() ), mPreserveRatioCheckBox->isChecked() );
46 return res;
47}
48
50{
51 if ( shape.geometry().asWkt() == mShapeEdit->toPlainText() && shape.preserveAspectRatio() == mPreserveRatioCheckBox->isChecked() && shape.symbolType() == mType )
52 return;
53
54 mType = shape.symbolType();
55 whileBlocking( mShapeEdit )->setPlainText( shape.geometry().asWkt() );
56 whileBlocking( mPreserveRatioCheckBox )->setChecked( shape.preserveAspectRatio() );
57 emit changed();
58}
59
60void QgsLegendPatchShapeWidget::setShapeFromStyle( const QString &name, QgsStyle::StyleEntity, const QString &stylePath )
61{
62 if ( name.isEmpty() )
63 return;
64
65 QgsStyle *style = QgsProject::instance()->styleSettings()->styleAtPath( stylePath );
66
67 if ( !style )
68 style = QgsStyle::defaultStyle();
69
70 if ( !style->legendPatchShapeNames().contains( name ) )
71 return;
72
73 const QgsLegendPatchShape newShape = style->legendPatchShape( name );
74 setShape( newShape );
75}
76
77void QgsLegendPatchShapeWidget::saveShape()
78{
80 saveDlg.setDefaultTags( mStyleItemsListWidget->currentTagFilter() );
81 if ( !saveDlg.exec() )
82 return;
83
84 if ( saveDlg.name().isEmpty() )
85 return;
86
87 QgsStyle *style = saveDlg.destinationStyle();
88 if ( !style )
89 return;
90
91 // check if there is no shape with same name
92 if ( style->legendPatchShapeNames().contains( saveDlg.name() ) )
93 {
94 const int res = QMessageBox::warning( this, tr( "Save Legend Patch Shape" ),
95 tr( "A legend patch shape with the name '%1' already exists. Overwrite?" )
96 .arg( saveDlg.name() ),
97 QMessageBox::Yes | QMessageBox::No );
98 if ( res != QMessageBox::Yes )
99 {
100 return;
101 }
103 }
104
105 const QStringList symbolTags = saveDlg.tags().split( ',' );
106
107 const QgsLegendPatchShape newShape = shape();
108 style->addLegendPatchShape( saveDlg.name(), newShape );
109 style->saveLegendPatchShape( saveDlg.name(), newShape, saveDlg.isFavorite(), symbolTags );
110}
111
112//
113// QgsLegendPatchShapeDialog
114//
115
117 : QDialog( parent )
118{
119 QVBoxLayout *vLayout = new QVBoxLayout();
120 mWidget = new QgsLegendPatchShapeWidget( nullptr, shape );
121 vLayout->addWidget( mWidget );
122 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
123
124 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal );
125 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
126 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
127 vLayout->addWidget( mButtonBox );
128 setLayout( vLayout );
129 setWindowTitle( tr( "Legend Patch Shape" ) );
130}
131
133{
134 return mButtonBox;
135}
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QString asWkt(int precision=17) const
Exports the geometry to WKT.
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
QgsLegendPatchShape shape() const
Returns the legend patch shape defined by the dialog.
QgsLegendPatchShapeDialog(const QgsLegendPatchShape &shape, QWidget *parent=nullptr)
Constructor for QgsLegendPatchShapeDialog, initially showing the specified shape.
Widget for configuring a custom legend patch shape.
QgsLegendPatchShape shape() const
Returns the legend patch shape as currently defined by the widget.
void changed()
Emitted whenever the patch shape defined by the widget is changed.
QgsLegendPatchShapeWidget(QWidget *parent=nullptr, const QgsLegendPatchShape &shape=QgsLegendPatchShape())
Constructor for QgsLegendPatchShapeWidget, with the specified parent widget.
void setShape(const QgsLegendPatchShape &shape)
Sets the shape to show in the widget.
Represents a patch shape for use in map legends.
bool preserveAspectRatio() const
Returns true if the patch shape should preserve its aspect ratio when it is resized to fit a desired ...
QgsGeometry geometry() const
Returns the geometry for the patch shape.
Qgis::SymbolType symbolType() const
Returns the symbol type associated with this patch.
Base class for any widget that can be shown as a inline panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
QgsStyle * styleAtPath(const QString &path)
Returns a reference to the style database associated with the project with matching file path.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:477
const QgsProjectStyleSettings * styleSettings() const
Returns the project's style settings, which contains settings and properties relating to how a QgsPro...
void saveEntity()
Emitted when the user has opted to save a new entity to the style database, by clicking the "Save" bu...
void selectionChangedWithStylePath(const QString &name, QgsStyle::StyleEntity type, const QString &stylePath)
Emitted when the selected item is changed in the widget.
a dialog for setting properties of a newly saved style.
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:179
@ LegendPatchShapeEntity
Legend patch shape (since QGIS 3.14)
Definition: qgsstyle.h:186
QStringList legendPatchShapeNames() const
Returns a list of names of legend patch shapes in the style.
Definition: qgsstyle.cpp:2204
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:145
bool removeEntityByName(StyleEntity type, const QString &name)
Removes the entry of the specified type with matching name from the database.
Definition: qgsstyle.cpp:1502
bool addLegendPatchShape(const QString &name, const QgsLegendPatchShape &shape, bool update=false)
Adds a legend patch shape with the specified name to the style.
Definition: qgsstyle.cpp:379
bool saveLegendPatchShape(const QString &name, const QgsLegendPatchShape &shape, bool favorite, const QStringList &tags)
Adds a legend patch shape to the database.
Definition: qgsstyle.cpp:1093
QgsLegendPatchShape legendPatchShape(const QString &name) const
Returns the legend patch shape with the specified name.
Definition: qgsstyle.cpp:2145
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2433