QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
73  QgsStyleSaveDialog saveDlg( this, QgsStyle::LegendPatchShapeEntity );
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  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  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  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal );
113  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
114  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
115  vLayout->addWidget( mButtonBox );
116  setLayout( vLayout );
117  setWindowTitle( tr( "Legend Patch Shape" ) );
118 }
119 
120 QDialogButtonBox *QgsLegendPatchShapeDialog::buttonBox() const
121 {
122  return mButtonBox;
123 }
QgsStyleItemsListWidget::saveEntity
void saveEntity()
Emitted when the user has opted to save a new entity to the style database, by clicking the "Save" bu...
QgsLegendPatchShape::geometry
QgsGeometry geometry() const
Returns the geometry for the patch shape.
Definition: qgslegendpatchshape.cpp:37
QgsStyle::removeEntityByName
bool removeEntityByName(StyleEntity type, const QString &name)
Removes the entry of the specified type with matching name from the database.
Definition: qgsstyle.cpp:1483
QgsLegendPatchShapeWidget::setShape
void setShape(const QgsLegendPatchShape &shape)
Sets the shape to show in the widget.
Definition: qgslegendpatchshapewidget.cpp:47
QgsLegendPatchShapeDialog::shape
QgsLegendPatchShape shape() const
Returns the legend patch shape defined by the dialog.
Definition: qgslegendpatchshapewidget.h:93
QgsStyle::LegendPatchShapeEntity
@ LegendPatchShapeEntity
Legend patch shape (since QGIS 3.14)
Definition: qgsstyle.h:186
qgsstylesavedialog.h
QgsLegendPatchShape::symbolType
QgsSymbol::SymbolType symbolType() const
Returns the symbol type associated with this patch.
Definition: qgslegendpatchshape.cpp:207
QgsLegendPatchShape
Represents a patch shape for use in map legends.
Definition: qgslegendpatchshape.h:31
QgsStyle::defaultStyle
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:127
QgsLegendPatchShape::preserveAspectRatio
bool preserveAspectRatio() const
Returns true if the patch shape should preserve its aspect ratio when it is resized to fit a desired ...
Definition: qgslegendpatchshape.cpp:47
QgsPanelWidget
Base class for any widget that can be shown as a inline panel.
Definition: qgspanelwidget.h:30
QgsStyle::addLegendPatchShape
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:357
QgsLegendPatchShapeWidget
Widget for configuring a custom legend patch shape.
Definition: qgslegendpatchshapewidget.h:33
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:262
QgsStyleItemsListWidget::selectionChanged
void selectionChanged(const QString &name, QgsStyle::StyleEntity type)
Emitted when the selected item is changed in the widget.
QgsLegendPatchShapeDialog::buttonBox
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
Definition: qgslegendpatchshapewidget.cpp:120
QgsLegendPatchShapeWidget::shape
QgsLegendPatchShape shape() const
Returns the legend patch shape as currently defined by the widget.
Definition: qgslegendpatchshapewidget.cpp:41
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
QgsGeometry::fromWkt
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
Definition: qgsgeometry.cpp:154
QgsLegendPatchShapeWidget::changed
void changed()
Emitted whenever the patch shape defined by the widget is changed.
QgsGeometry::asWkt
QString asWkt(int precision=17) const
Exports the geometry to WKT.
Definition: qgsgeometry.cpp:1288
QgsStyle::legendPatchShape
QgsLegendPatchShape legendPatchShape(const QString &name) const
Returns the legend patch shape with the specified name.
Definition: qgsstyle.cpp:2126
QgsStyle
Definition: qgsstyle.h:160
QgsLegendPatchShapeDialog::QgsLegendPatchShapeDialog
QgsLegendPatchShapeDialog(const QgsLegendPatchShape &shape, QWidget *parent=nullptr)
Constructor for QgsLegendPatchShapeDialog, initially showing the specified shape.
Definition: qgslegendpatchshapewidget.cpp:106
QgsStyle::saveLegendPatchShape
bool saveLegendPatchShape(const QString &name, const QgsLegendPatchShape &shape, bool favorite, const QStringList &tags)
Adds a legend patch shape to the database.
Definition: qgsstyle.cpp:1097
QgsStyle::legendPatchShapeNames
QStringList legendPatchShapeNames() const
Returns a list of names of legend patch shapes in the style.
Definition: qgsstyle.cpp:2185
QgsLegendPatchShapeWidget::QgsLegendPatchShapeWidget
QgsLegendPatchShapeWidget(QWidget *parent=nullptr, const QgsLegendPatchShape &shape=QgsLegendPatchShape())
Constructor for QgsLegendPatchShapeWidget, with the specified parent widget.
Definition: qgslegendpatchshapewidget.cpp:22
QgsStyle::StyleEntity
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:179
qgslegendpatchshapewidget.h