QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
19#include "qgsproject.h"
21#include "qgsstylesavedialog.h"
22
23#include <QDialogButtonBox>
24#include <QMessageBox>
25
26#include "moc_qgslegendpatchshapewidget.cpp"
27
29 : QgsPanelWidget( parent )
30{
31 setupUi( this );
32 setPanelTitle( tr( "Legend Patch Shape" ) );
33
34 mStyleItemsListWidget->setStyle( QgsStyle::defaultStyle() );
35 mStyleItemsListWidget->setEntityType( QgsStyle::LegendPatchShapeEntity );
36 mStyleItemsListWidget->setSymbolType( shape.symbolType() );
37
38 setShape( shape );
39
40 connect( mPreserveRatioCheckBox, &QCheckBox::toggled, this, &QgsLegendPatchShapeWidget::changed );
41 connect( mShapeEdit, &QPlainTextEdit::textChanged, this, &QgsLegendPatchShapeWidget::changed );
42
43 connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::selectionChangedWithStylePath, this, &QgsLegendPatchShapeWidget::setShapeFromStyle );
44 connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::saveEntity, this, &QgsLegendPatchShapeWidget::saveShape );
45}
46
48{
49 QgsLegendPatchShape res( mType, QgsGeometry::fromWkt( mShapeEdit->toPlainText() ), mPreserveRatioCheckBox->isChecked() );
50 return res;
51}
52
54{
55 if ( shape.geometry().asWkt() == mShapeEdit->toPlainText() && shape.preserveAspectRatio() == mPreserveRatioCheckBox->isChecked() && shape.symbolType() == mType )
56 return;
57
58 mType = shape.symbolType();
59 whileBlocking( mShapeEdit )->setPlainText( shape.geometry().asWkt() );
60 whileBlocking( mPreserveRatioCheckBox )->setChecked( shape.preserveAspectRatio() );
61 emit changed();
62}
63
64void QgsLegendPatchShapeWidget::setShapeFromStyle( const QString &name, QgsStyle::StyleEntity, const QString &stylePath )
65{
66 if ( name.isEmpty() )
67 return;
68
69 QgsStyle *style = QgsProject::instance()->styleSettings()->styleAtPath( stylePath );
70
71 if ( !style )
72 style = QgsStyle::defaultStyle();
73
74 if ( !style->legendPatchShapeNames().contains( name ) )
75 return;
76
77 const QgsLegendPatchShape newShape = style->legendPatchShape( name );
78 setShape( newShape );
79}
80
81void QgsLegendPatchShapeWidget::saveShape()
82{
83 QgsStyleSaveDialog saveDlg( this, QgsStyle::LegendPatchShapeEntity );
84 saveDlg.setDefaultTags( mStyleItemsListWidget->currentTagFilter() );
85 if ( !saveDlg.exec() )
86 return;
87
88 if ( saveDlg.name().isEmpty() )
89 return;
90
91 QgsStyle *style = saveDlg.destinationStyle();
92 if ( !style )
93 return;
94
95 // check if there is no shape with same name
96 if ( style->legendPatchShapeNames().contains( saveDlg.name() ) )
97 {
98 const int res = QMessageBox::warning( this, tr( "Save Legend Patch Shape" ), tr( "A legend patch shape with the name '%1' already exists. Overwrite?" ).arg( saveDlg.name() ), 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.
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.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
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 database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:88
QStringList tags() const
Returns a list of all tags in the style database.
StyleEntity
Enum for Entities involved in a style.
Definition qgsstyle.h:204
@ LegendPatchShapeEntity
Legend patch shape.
Definition qgsstyle.h:211
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:417
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:147
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:6511