QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  if ( name.isEmpty() )
63  return;
64 
65  QgsStyle *style = nullptr;
66 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
67  style = QgsProject::instance()->styleSettings()->styleAtPath( stylePath );
68 #else
69  ( void )stylePath;
70 #endif
71 
72  if ( !style )
73  style = QgsStyle::defaultStyle();
74 
75  if ( !style->legendPatchShapeNames().contains( name ) )
76  return;
77 
78  const QgsLegendPatchShape newShape = style->legendPatchShape( name );
79  setShape( newShape );
80 }
81 
82 void QgsLegendPatchShapeWidget::saveShape()
83 {
84  QgsStyleSaveDialog saveDlg( this, QgsStyle::LegendPatchShapeEntity );
85  saveDlg.setDefaultTags( mStyleItemsListWidget->currentTagFilter() );
86  if ( !saveDlg.exec() )
87  return;
88 
89  if ( saveDlg.name().isEmpty() )
90  return;
91 
92  QgsStyle *style = saveDlg.destinationStyle();
93  if ( !style )
94  return;
95 
96  // check if there is no shape with same name
97  if ( style->legendPatchShapeNames().contains( saveDlg.name() ) )
98  {
99  const int res = QMessageBox::warning( this, tr( "Save Legend Patch Shape" ),
100  tr( "A legend patch shape with the name '%1' already exists. Overwrite?" )
101  .arg( saveDlg.name() ),
102  QMessageBox::Yes | QMessageBox::No );
103  if ( res != QMessageBox::Yes )
104  {
105  return;
106  }
107  style->removeEntityByName( QgsStyle::LegendPatchShapeEntity, saveDlg.name() );
108  }
109 
110  const QStringList symbolTags = saveDlg.tags().split( ',' );
111 
112  const QgsLegendPatchShape newShape = shape();
113  style->addLegendPatchShape( saveDlg.name(), newShape );
114  style->saveLegendPatchShape( saveDlg.name(), newShape, saveDlg.isFavorite(), symbolTags );
115 }
116 
117 //
118 // QgsLegendPatchShapeDialog
119 //
120 
122  : QDialog( parent )
123 {
124  QVBoxLayout *vLayout = new QVBoxLayout();
125  mWidget = new QgsLegendPatchShapeWidget( nullptr, shape );
126  vLayout->addWidget( mWidget );
127  connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
128 
129  mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok, Qt::Horizontal );
130  connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
131  connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
132  vLayout->addWidget( mButtonBox );
133  setLayout( vLayout );
134  setWindowTitle( tr( "Legend Patch Shape" ) );
135 }
136 
137 QDialogButtonBox *QgsLegendPatchShapeDialog::buttonBox() const
138 {
139  return mButtonBox;
140 }
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
qgsprojectstylesettings.h
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:1502
QgsGeometry::fromWkt
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QgsLegendPatchShapeWidget::setShape
void setShape(const QgsLegendPatchShape &shape)
Sets the shape to show in the widget.
Definition: qgslegendpatchshapewidget.cpp:49
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
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
QgsStyleItemsListWidget::selectionChangedWithStylePath
void selectionChangedWithStylePath(const QString &name, QgsStyle::StyleEntity type, const QString &stylePath)
Emitted when the selected item is changed in the widget.
QgsProject::styleSettings
const QgsProjectStyleSettings * styleSettings() const
Returns the project's style settings, which contains settings and properties relating to how a QgsPro...
Definition: qgsproject.cpp:3501
qgsstylesavedialog.h
QgsLegendPatchShape
Represents a patch shape for use in map legends.
Definition: qgslegendpatchshape.h:33
QgsStyle::defaultStyle
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:145
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:29
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:379
QgsLegendPatchShapeWidget
Widget for configuring a custom legend patch shape.
Definition: qgslegendpatchshapewidget.h:32
whileBlocking
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:2191
QgsPanelWidget::panelAccepted
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
QgsLegendPatchShapeDialog::buttonBox
QDialogButtonBox * buttonBox() const
Returns a reference to the dialog's button box.
Definition: qgslegendpatchshapewidget.cpp:137
QgsLegendPatchShapeWidget::shape
QgsLegendPatchShape shape() const
Returns the legend patch shape as currently defined by the widget.
Definition: qgslegendpatchshapewidget.cpp:43
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
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:1407
QgsStyle::legendPatchShape
QgsLegendPatchShape legendPatchShape(const QString &name) const
Returns the legend patch shape with the specified name.
Definition: qgsstyle.cpp:2145
QgsStyle
Definition: qgsstyle.h:159
QgsLegendPatchShape::symbolType
Qgis::SymbolType symbolType() const
Returns the symbol type associated with this patch.
Definition: qgslegendpatchshape.cpp:229
QgsLegendPatchShapeDialog::QgsLegendPatchShapeDialog
QgsLegendPatchShapeDialog(const QgsLegendPatchShape &shape, QWidget *parent=nullptr)
Constructor for QgsLegendPatchShapeDialog, initially showing the specified shape.
Definition: qgslegendpatchshapewidget.cpp:121
QgsProjectStyleSettings::styleAtPath
QgsStyle * styleAtPath(const QString &path)
Returns a reference to the style database associated with the project with matching file path.
Definition: qgsprojectstylesettings.cpp:337
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:1093
QgsStyle::legendPatchShapeNames
QStringList legendPatchShapeNames() const
Returns a list of names of legend patch shapes in the style.
Definition: qgsstyle.cpp:2204
QgsLegendPatchShapeWidget::QgsLegendPatchShapeWidget
QgsLegendPatchShapeWidget(QWidget *parent=nullptr, const QgsLegendPatchShape &shape=QgsLegendPatchShape())
Constructor for QgsLegendPatchShapeWidget, with the specified parent widget.
Definition: qgslegendpatchshapewidget.cpp:24
qgsproject.h
QgsStyle::StyleEntity
StyleEntity
Enum for Entities involved in a style.
Definition: qgsstyle.h:178
qgslegendpatchshapewidget.h