QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 <QDialogButtonBox>
20 #include <QMessageBox>
21 
23  : QgsPanelWidget( parent )
24 {
25  setupUi( this );
26  setPanelTitle( tr( "Legend Patch Shape" ) );
27 
28  mStyleItemsListWidget->setStyle( QgsStyle::defaultStyle() );
29  mStyleItemsListWidget->setEntityType( QgsStyle::LegendPatchShapeEntity );
30  mStyleItemsListWidget->setSymbolType( shape.symbolType() );
31 
32  setShape( shape );
33 
34  connect( mPreserveRatioCheckBox, &QCheckBox::toggled, this, &QgsLegendPatchShapeWidget::changed );
35  connect( mShapeEdit, &QPlainTextEdit::textChanged, this, &QgsLegendPatchShapeWidget::changed );
36 
37  connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::selectionChanged, this, &QgsLegendPatchShapeWidget::setShapeFromStyle );
38  connect( mStyleItemsListWidget, &QgsStyleItemsListWidget::saveEntity, this, &QgsLegendPatchShapeWidget::saveShape );
39 }
40 
42 {
43  QgsLegendPatchShape res( mType, QgsGeometry::fromWkt( mShapeEdit->toPlainText() ), mPreserveRatioCheckBox->isChecked() );
44  return res;
45 }
46 
48 {
49  if ( shape.geometry().asWkt() == mShapeEdit->toPlainText() && shape.preserveAspectRatio() == mPreserveRatioCheckBox->isChecked() && shape.symbolType() == mType )
50  return;
51 
52  mType = shape.symbolType();
53  whileBlocking( mShapeEdit )->setPlainText( shape.geometry().asWkt() );
54  whileBlocking( mPreserveRatioCheckBox )->setChecked( shape.preserveAspectRatio() );
55  emit changed();
56 }
57 
58 void QgsLegendPatchShapeWidget::setShapeFromStyle( const QString &name, QgsStyle::StyleEntity )
59 {
60  if ( !QgsStyle::defaultStyle()->legendPatchShapeNames().contains( name ) )
61  return;
62 
64  setShape( newShape );
65 }
66 
67 void QgsLegendPatchShapeWidget::saveShape()
68 {
70  if ( !style )
71  return;
72 
74  saveDlg.setDefaultTags( mStyleItemsListWidget->currentTagFilter() );
75  if ( !saveDlg.exec() )
76  return;
77 
78  if ( saveDlg.name().isEmpty() )
79  return;
80 
81  // check if there is no shape with same name
82  if ( style->legendPatchShapeNames().contains( saveDlg.name() ) )
83  {
84  const int res = QMessageBox::warning( this, tr( "Save Legend Patch Shape" ),
85  tr( "A legend patch shape with the name '%1' already exists. Overwrite?" )
86  .arg( saveDlg.name() ),
87  QMessageBox::Yes | QMessageBox::No );
88  if ( res != QMessageBox::Yes )
89  {
90  return;
91  }
92  style->removeEntityByName( QgsStyle::LegendPatchShapeEntity, saveDlg.name() );
93  }
94 
95  const QStringList symbolTags = saveDlg.tags().split( ',' );
96 
97  const QgsLegendPatchShape newShape = shape();
98  style->addLegendPatchShape( saveDlg.name(), newShape );
99  style->saveLegendPatchShape( saveDlg.name(), newShape, saveDlg.isFavorite(), symbolTags );
100 }
101 
102 //
103 // QgsLegendPatchShapeDialog
104 //
105 
107  : QDialog( parent )
108 {
109  QVBoxLayout *vLayout = new QVBoxLayout();
110  mWidget = new QgsLegendPatchShapeWidget( nullptr, shape );
111  vLayout->addWidget( mWidget );
112  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
113 
114  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal );
115  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
116  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
117  vLayout->addWidget( mButtonBox );
118  setLayout( vLayout );
119  setWindowTitle( tr( "Legend Patch Shape" ) );
120 }
121 
122 QDialogButtonBox *QgsLegendPatchShapeDialog::buttonBox() const
123 {
124  return mButtonBox;
125 }
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.
void selectionChanged(const QString &name, QgsStyle::StyleEntity type)
Emitted when the selected item is changed in the widget.
void saveEntity()
Emitted when the user has opted to save a new entity to the style database, by clicking the "Save" bu...
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:2206
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:131
bool removeEntityByName(StyleEntity type, const QString &name)
Removes the entry of the specified type with matching name from the database.
Definition: qgsstyle.cpp:1504
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:363
bool saveLegendPatchShape(const QString &name, const QgsLegendPatchShape &shape, bool favorite, const QStringList &tags)
Adds a legend patch shape to the database.
Definition: qgsstyle.cpp:1109
QgsLegendPatchShape legendPatchShape(const QString &name) const
Returns the legend patch shape with the specified name.
Definition: qgsstyle.cpp:2147
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:1185