QGIS API Documentation  3.25.0-Master (10b47c2603)
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 
60 void QgsLegendPatchShapeWidget::setShapeFromStyle( const QString &name, QgsStyle::StyleEntity, const QString &stylePath )
61 {
62  QgsStyle *style = nullptr;
63 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
64  style = QgsProject::instance()->styleSettings()->styleAtPath( stylePath );
65 #else
66  ( void )stylePath;
67 #endif
68 
69  if ( !style )
70  style = QgsStyle::defaultStyle();
71 
72  if ( !style->legendPatchShapeNames().contains( name ) )
73  return;
74 
75  const QgsLegendPatchShape newShape = style->legendPatchShape( name );
76  setShape( newShape );
77 }
78 
79 void QgsLegendPatchShapeWidget::saveShape()
80 {
82  saveDlg.setDefaultTags( mStyleItemsListWidget->currentTagFilter() );
83  if ( !saveDlg.exec() )
84  return;
85 
86  if ( saveDlg.name().isEmpty() )
87  return;
88 
89  QgsStyle *style = saveDlg.destinationStyle();
90  if ( !style )
91  return;
92 
93  // check if there is no shape with same name
94  if ( style->legendPatchShapeNames().contains( saveDlg.name() ) )
95  {
96  const int res = QMessageBox::warning( this, tr( "Save Legend Patch Shape" ),
97  tr( "A legend patch shape with the name '%1' already exists. Overwrite?" )
98  .arg( saveDlg.name() ),
99  QMessageBox::Yes | QMessageBox::No );
100  if ( res != QMessageBox::Yes )
101  {
102  return;
103  }
104  style->removeEntityByName( QgsStyle::LegendPatchShapeEntity, saveDlg.name() );
105  }
106 
107  const QStringList symbolTags = saveDlg.tags().split( ',' );
108 
109  const QgsLegendPatchShape newShape = shape();
110  style->addLegendPatchShape( saveDlg.name(), newShape );
111  style->saveLegendPatchShape( saveDlg.name(), newShape, saveDlg.isFavorite(), symbolTags );
112 }
113 
114 //
115 // QgsLegendPatchShapeDialog
116 //
117 
119  : QDialog( parent )
120 {
121  QVBoxLayout *vLayout = new QVBoxLayout();
122  mWidget = new QgsLegendPatchShapeWidget( nullptr, shape );
123  vLayout->addWidget( mWidget );
124  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
125 
126  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal );
127  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
128  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
129  vLayout->addWidget( mButtonBox );
130  setLayout( vLayout );
131  setWindowTitle( tr( "Legend Patch Shape" ) );
132 }
133 
134 QDialogButtonBox *QgsLegendPatchShapeDialog::buttonBox() const
135 {
136  return mButtonBox;
137 }
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:479
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:2001