QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 "moc_qgslegendpatchshapewidget.cpp"
19#include "qgsstylesavedialog.h"
20#include "qgsproject.h"
22#include <QDialogButtonBox>
23#include <QMessageBox>
24
26 : QgsPanelWidget( parent )
27{
28 setupUi( this );
29 setPanelTitle( tr( "Legend Patch Shape" ) );
30
31 mStyleItemsListWidget->setStyle( QgsStyle::defaultStyle() );
32 mStyleItemsListWidget->setEntityType( QgsStyle::LegendPatchShapeEntity );
33 mStyleItemsListWidget->setSymbolType( shape.symbolType() );
34
35 setShape( shape );
36
37 connect( mPreserveRatioCheckBox, &QCheckBox::toggled, this, &QgsLegendPatchShapeWidget::changed );
38 connect( mShapeEdit, &QPlainTextEdit::textChanged, this, &QgsLegendPatchShapeWidget::changed );
39
40 connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::selectionChangedWithStylePath, this, &QgsLegendPatchShapeWidget::setShapeFromStyle );
41 connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::saveEntity, this, &QgsLegendPatchShapeWidget::saveShape );
42}
43
45{
46 QgsLegendPatchShape res( mType, QgsGeometry::fromWkt( mShapeEdit->toPlainText() ), mPreserveRatioCheckBox->isChecked() );
47 return res;
48}
49
51{
52 if ( shape.geometry().asWkt() == mShapeEdit->toPlainText() && shape.preserveAspectRatio() == mPreserveRatioCheckBox->isChecked() && shape.symbolType() == mType )
53 return;
54
55 mType = shape.symbolType();
56 whileBlocking( mShapeEdit )->setPlainText( shape.geometry().asWkt() );
57 whileBlocking( mPreserveRatioCheckBox )->setChecked( shape.preserveAspectRatio() );
58 emit changed();
59}
60
61void QgsLegendPatchShapeWidget::setShapeFromStyle( const QString &name, QgsStyle::StyleEntity, const QString &stylePath )
62{
63 if ( name.isEmpty() )
64 return;
65
66 QgsStyle *style = QgsProject::instance()->styleSettings()->styleAtPath( stylePath );
67
68 if ( !style )
69 style = QgsStyle::defaultStyle();
70
71 if ( !style->legendPatchShapeNames().contains( name ) )
72 return;
73
74 const QgsLegendPatchShape newShape = style->legendPatchShape( name );
75 setShape( newShape );
76}
77
78void QgsLegendPatchShapeWidget::saveShape()
79{
81 saveDlg.setDefaultTags( mStyleItemsListWidget->currentTagFilter() );
82 if ( !saveDlg.exec() )
83 return;
84
85 if ( saveDlg.name().isEmpty() )
86 return;
87
88 QgsStyle *style = saveDlg.destinationStyle();
89 if ( !style )
90 return;
91
92 // check if there is no shape with same name
93 if ( style->legendPatchShapeNames().contains( saveDlg.name() ) )
94 {
95 const int res = QMessageBox::warning( this, tr( "Save Legend Patch Shape" ),
96 tr( "A legend patch shape with the name '%1' already exists. Overwrite?" )
97 .arg( saveDlg.name() ),
98 QMessageBox::Yes | QMessageBox::No );
99 if ( res != QMessageBox::Yes )
100 {
101 return;
102 }
104 }
105
106 const QStringList symbolTags = saveDlg.tags().split( ',' );
107
108 const QgsLegendPatchShape newShape = shape();
109 style->addLegendPatchShape( saveDlg.name(), newShape );
110 style->saveLegendPatchShape( saveDlg.name(), newShape, saveDlg.isFavorite(), symbolTags );
111}
112
113//
114// QgsLegendPatchShapeDialog
115//
116
118 : QDialog( parent )
119{
120 QVBoxLayout *vLayout = new QVBoxLayout();
121 mWidget = new QgsLegendPatchShapeWidget( nullptr, shape );
122 vLayout->addWidget( mWidget );
123 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
124
125 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal );
126 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
127 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
128 vLayout->addWidget( mButtonBox );
129 setLayout( vLayout );
130 setWindowTitle( tr( "Legend Patch Shape" ) );
131}
132
134{
135 return mButtonBox;
136}
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
Q_INVOKABLE 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.
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:203
@ LegendPatchShapeEntity
Legend patch shape.
Definition qgsstyle.h:210
QStringList legendPatchShapeNames() const
Returns a list of names of legend patch shapes in the style.
bool removeEntityByName(StyleEntity type, const QString &name)
Removes the entry of the specified type with matching name from the database.
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:412
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:146
bool saveLegendPatchShape(const QString &name, const QgsLegendPatchShape &shape, bool favorite, const QStringList &tags)
Adds a legend patch shape to the database.
QgsLegendPatchShape legendPatchShape(const QString &name) const
Returns the legend patch shape with the specified name.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5821