QGIS API Documentation 4.1.0-Master (376402f9aeb)
Loading...
Searching...
No Matches
qgslayoutpdfexportoptionsdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutpdfexportoptionsdialog.cpp
3 -------------------------------------
4 begin : August 2019
5 copyright : (C) 2019 by 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 "qgis.h"
22#include "qgsgui.h"
23#include "qgshelp.h"
24#include "qgslayertree.h"
26#include "qgsproject.h"
27#include "qgssettings.h"
28
29#include <QCheckBox>
30#include <QMenu>
31#include <QPushButton>
32#include <QString>
33
34#include "moc_qgslayoutpdfexportoptionsdialog.cpp"
35
36using namespace Qt::StringLiterals;
37
39 QWidget *parent, bool allowGeospatialPdfExport, const QString &geospatialPdfReason, const QStringList &geospatialPdfLayerOrder, Qt::WindowFlags flags
40)
41 : QDialog( parent, flags )
42{
43 setupUi( this );
44
45 mGeospatialPdfStructureTreeMenu = new QMenu( this );
46
47 mTextRenderFormatComboBox->addItem( tr( "Always Export Text as Paths (Recommended)" ), static_cast<int>( Qgis::TextRenderFormat::AlwaysOutlines ) );
48 mTextRenderFormatComboBox->addItem( tr( "Always Export Text as Text Objects" ), static_cast<int>( Qgis::TextRenderFormat::AlwaysText ) );
49 mTextRenderFormatComboBox->addItem( tr( "Prefer Exporting Text as Text Objects" ), static_cast<int>( Qgis::TextRenderFormat::PreferText ) );
50
51 mGeospatialPdfAvailable = allowGeospatialPdfExport && QgsAbstractGeospatialPdfExporter::geospatialPDFCreationAvailable();
52 mGeospatialPDFGroupBox->setEnabled( mGeospatialPdfAvailable );
53 mGeospatialPDFGroupBox->setChecked( false );
54 if ( !mGeospatialPdfAvailable )
55 {
56 mGeospatialPDFOptionsStackedWidget->setCurrentIndex( 0 );
57 mGeospatialPdfUnavailableReason->setText( geospatialPdfReason.isEmpty() ? QgsAbstractGeospatialPdfExporter::geospatialPDFAvailabilityExplanation() : geospatialPdfReason );
58 // avoid showing reason in disabled text color - we want it to stand out
59 QPalette p = mGeospatialPdfUnavailableReason->palette();
60 p.setColor( QPalette::Disabled, QPalette::WindowText, QPalette::WindowText );
61 mGeospatialPdfUnavailableReason->setPalette( p );
62 mGeospatialPDFOptionsStackedWidget->removeWidget( mGeospatialPDFOptionsStackedWidget->widget( 1 ) );
63 }
64 else
65 {
66 mGeospatialPDFOptionsStackedWidget->setCurrentIndex( 1 );
67 }
68
69 mGeospatialPDFCustomConfigRadioButton->setChecked( true );
70 mGeospatialPDFCustomConfigFrame->setEnabled( true );
71 connect( mGeospatialPDFLayerTreeConfigRadioButton, &QRadioButton::toggled, this, &QgsLayoutPdfExportOptionsDialog::toggleLayerTreeConfig );
72
73 mComboImageCompression->addItem( tr( "Lossy (JPEG)" ), false );
74 mComboImageCompression->addItem( tr( "Lossless" ), true );
75
76 const QStringList themes = QgsProject::instance()->mapThemeCollection()->mapThemes();
77 for ( const QString &theme : themes )
78 {
79 QListWidgetItem *item = new QListWidgetItem( theme );
80 item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
81 item->setCheckState( Qt::Unchecked );
82 mThemesList->addItem( item );
83 }
84
85 QList<QgsMapLayer *> order = QgsProject::instance()->layerTreeRoot()->layerOrder();
86 for ( auto it = geospatialPdfLayerOrder.rbegin(); it != geospatialPdfLayerOrder.rend(); ++it )
87 {
88 for ( int i = 0; i < order.size(); ++i )
89 {
90 if ( order.at( i )->id() == *it )
91 {
92 order.move( i, 0 );
93 break;
94 }
95 }
96 }
97 mGeospatialPdfStructureModel = new QgsGeospatialPdfLayerTreeModel( order, this );
98 mGeospatialPdfStructureProxyModel = new QgsGeospatialPdfLayerFilteredTreeModel( mGeospatialPdfStructureModel, this );
99 mGeospatialPdfStructureTree->setModel( mGeospatialPdfStructureProxyModel );
100 mGeospatialPdfStructureTree->resizeColumnToContents( 0 );
101 mGeospatialPdfStructureTree->header()->show();
102 mGeospatialPdfStructureTree->setSelectionMode( QAbstractItemView::SingleSelection );
103 mGeospatialPdfStructureTree->setSelectionBehavior( QAbstractItemView::SelectRows );
104
105 mGeospatialPdfStructureTree->setDragEnabled( true );
106 mGeospatialPdfStructureTree->setAcceptDrops( true );
107 mGeospatialPdfStructureTree->setDragDropMode( QAbstractItemView::InternalMove );
108 mGeospatialPdfStructureTree->setDefaultDropAction( Qt::MoveAction );
109
110 mGeospatialPdfStructureTree->setContextMenuPolicy( Qt::CustomContextMenu );
111 connect( mGeospatialPdfStructureTree, &QTreeView::customContextMenuRequested, this, [this]( const QPoint &point ) {
112 const QModelIndex index = mGeospatialPdfStructureTree->indexAt( point );
113 if ( index.isValid() )
114 showContextMenuForGeospatialPdfStructure( point, mGeospatialPdfStructureProxyModel->mapToSource( index ) );
115 } );
116
117 connect( mHelpButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutPdfExportOptionsDialog::showHelp );
119}
120
122{
123 mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( static_cast<int>( format ) ) );
124}
125
127{
128 return static_cast<Qgis::TextRenderFormat>( mTextRenderFormatComboBox->currentData().toInt() );
129}
130
132{
133 mForceVectorCheckBox->setChecked( force );
134}
135
137{
138 return mForceVectorCheckBox->isChecked();
139}
140
142{
143 mAppendGeoreferenceCheckbox->setEnabled( enabled );
144}
145
147{
148 mAppendGeoreferenceCheckbox->setChecked( enabled );
149}
150
152{
153 return mAppendGeoreferenceCheckbox->isChecked();
154}
155
157{
158 mIncludeMetadataCheckbox->setChecked( enabled );
159}
160
162{
163 return mIncludeMetadataCheckbox->isChecked();
164}
165
167{
168 mDisableRasterTilingCheckBox->setChecked( disabled );
169}
170
172{
173 return mDisableRasterTilingCheckBox->isChecked();
174}
175
177{
178 mSimplifyGeometriesCheckbox->setChecked( enabled );
179}
180
182{
183 return mSimplifyGeometriesCheckbox->isChecked();
184}
185
187{
188 mComboImageCompression->setCurrentIndex( mComboImageCompression->findData( enabled ) );
189}
190
192{
193 return mComboImageCompression->currentData().toBool();
194}
195
197{
198 if ( !mGeospatialPdfAvailable )
199 return;
200
201 mGeospatialPDFGroupBox->setChecked( enabled );
202}
203
205{
206 if ( !mGeospatialPdfAvailable )
207 return false;
208
209 return mGeospatialPDFGroupBox->isChecked();
210}
211
213{
214 if ( !mGeospatialPdfAvailable )
215 return;
216
217 mGeospatialPDFLayerTreeConfigRadioButton->setChecked( enabled );
218 mGeospatialPDFCustomConfigFrame->setEnabled( !enabled );
219}
220
222{
223 if ( !mGeospatialPdfAvailable )
224 return false;
225
226 return mGeospatialPDFLayerTreeConfigRadioButton->isChecked();
227}
228
230{
231 setUseLayerTreeConfig( false );
232 mGeospatialPDFLayerTreeConfigRadioButton->setEnabled( false );
233 mGeospatialPDFLayerTreeConfigRadioButton->setToolTip(
234 u"Unavailable: All map items in the layout are currently following either map themes or locked layers, which is not compatible with the QGIS layer tree configuration."_s
235 );
236}
237
239{
240 if ( !mGeospatialPdfAvailable )
241 return;
242
243 mIncludeMapThemesCheck->setChecked( !themes.isEmpty() );
244 for ( int i = 0; i < mThemesList->count(); ++i )
245 {
246 QListWidgetItem *item = mThemesList->item( i );
247 item->setCheckState( themes.contains( item->text() ) ? Qt::Checked : Qt::Unchecked );
248 }
249}
250
252{
253 QStringList res;
254 if ( !mGeospatialPdfAvailable )
255 return res;
256
257 if ( !mIncludeMapThemesCheck || !mIncludeMapThemesCheck->isChecked() )
258 return res;
259
260 res.reserve( mThemesList->count() );
261 for ( int i = 0; i < mThemesList->count(); ++i )
262 {
263 QListWidgetItem *item = mThemesList->item( i );
264 if ( item->checkState() == Qt::Checked )
265 res << item->text();
266 }
267 return res;
268}
269
271{
272 QStringList order;
273 for ( int row = 0; row < mGeospatialPdfStructureProxyModel->rowCount(); ++row )
274 {
275 order << mGeospatialPdfStructureProxyModel->data( mGeospatialPdfStructureProxyModel->index( row, 0 ), static_cast<int>( QgsMapLayerModel::CustomRole::LayerId ) ).toString();
276 }
277 return order;
278}
279
281{
282 // we don't explicitly expose a "group order" widget in the dialog -- rather
283 // we use the ordering of the layers, and build the group ordering based
284 // on grouped layers which appear first
285 QStringList groupOrder;
286 for ( int row = 0; row < mGeospatialPdfStructureProxyModel->rowCount(); ++row )
287 {
288 const QString group = mGeospatialPdfStructureProxyModel->data( mGeospatialPdfStructureProxyModel->index( row, QgsGeospatialPdfLayerTreeModel::GroupColumn ), Qt::DisplayRole ).toString().trimmed();
289 if ( !group.isEmpty() && !groupOrder.contains( group ) )
290 groupOrder << group;
291 }
292 return groupOrder;
293}
294
296{
297 mOpenAfterExportingCheckBox->setChecked( enabled );
298}
299
301{
302 return mOpenAfterExportingCheckBox->isChecked();
303}
304
305void QgsLayoutPdfExportOptionsDialog::showHelp()
306{
307 QgsHelp::openHelp( u"print_composer/create_output.html"_s );
308}
309
310void QgsLayoutPdfExportOptionsDialog::showContextMenuForGeospatialPdfStructure( QPoint point, const QModelIndex &index )
311{
312 mGeospatialPdfStructureTreeMenu->clear();
313
314 switch ( index.column() )
315 {
318 {
319 QAction *selectAll = new QAction( tr( "Select All" ), mGeospatialPdfStructureTreeMenu );
320 mGeospatialPdfStructureTreeMenu->addAction( selectAll );
321 connect( selectAll, &QAction::triggered, this, [this, index] { mGeospatialPdfStructureModel->checkAll( true, QModelIndex(), index.column() ); } );
322 QAction *deselectAll = new QAction( tr( "Deselect All" ), mGeospatialPdfStructureTreeMenu );
323 mGeospatialPdfStructureTreeMenu->addAction( deselectAll );
324 connect( deselectAll, &QAction::triggered, this, [this, index] { mGeospatialPdfStructureModel->checkAll( false, QModelIndex(), index.column() ); } );
325 break;
326 }
327
328 default:
329 break;
330 }
331
332 if ( !mGeospatialPdfStructureTreeMenu->actions().empty() )
333 {
334 mGeospatialPdfStructureTreeMenu->exec( mGeospatialPdfStructureTree->mapToGlobal( point ) );
335 }
336}
337
338void QgsLayoutPdfExportOptionsDialog::toggleLayerTreeConfig()
339{
340 mGeospatialPDFCustomConfigFrame->setEnabled( !mGeospatialPDFLayerTreeConfigRadioButton->isChecked() );
341}
TextRenderFormat
Options for rendering text.
Definition qgis.h:2990
@ PreferText
Render text as text objects, unless doing so results in rendering artifacts or poor quality rendering...
Definition qgis.h:2997
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
Definition qgis.h:2991
@ AlwaysText
Always render text as text objects. While this mode preserves text objects as text for post-processin...
Definition qgis.h:2994
static QString geospatialPDFAvailabilityExplanation()
Returns a user-friendly, translated string explaining why Geospatial PDF export support is not availa...
static bool geospatialPDFCreationAvailable()
Returns true if the current QGIS build is capable of Geospatial PDF support.
Layer tree model for Geo-PDF layers.
@ InitiallyVisible
Initial visibility state.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:224
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
void setExportGeospatialPdf(bool enabled)
Sets whether to export a Geospatial PDF.
void setOpenAfterExporting(bool enabled)
Sets whether to open the pdf after exporting it.
bool useLayerTreeConfig() const
Returns whether to use QGIS layer tree config to export a Geospatial PDF.
QStringList exportThemes() const
Returns the list of export themes.
bool geometriesSimplified() const
Returns whether geometry simplification is enabled.
bool losslessImageExport() const
Returns whether lossless image compression is enabled.
void setLosslessImageExport(bool enabled)
Sets whether to use lossless image compression.
void setUseLayerTreeConfig(bool enabled)
Sets whether to use QGIS layer tree config to export a Geospatial PDF.
bool exportGeospatialPdf() const
Returns whether Geospatial PDF export is enabled.
bool openAfterExporting() const
Returns whether the pdf should be opened after exporting it.
bool georeferencingEnabled() const
Returns whether georeferencing is enabled.
void setRasterTilingDisabled(bool disabled)
Sets whether to disable raster tiling.
bool forceVector() const
Returns whether vector output is being forced.
void setGeometriesSimplified(bool enabled)
Sets whether to simplify geometries.
bool metadataEnabled() const
Returns whether metadata is enabled.
void setGeoreferencingEnabled(bool enabled)
Sets whether to enable georeferencing.
void setMetadataEnabled(bool enabled)
Sets whether to enable metadata.
void enableGeoreferencingOptions(bool enabled)
Sets whether to enable georeferencing options.
void disableUseLayerTreeConfig()
Disables the option to follow QGIS layer tree configuration from the GUI.
QStringList geospatialPdfGroupOrder() const
Returns a list of groups in the desired order they should appear in a generated Geospatial PDF file.
QgsLayoutPdfExportOptionsDialog(QWidget *parent=nullptr, bool allowGeospatialPdfExport=true, const QString &geospatialPdfReason=QString(), const QStringList &geospatialPdfLayerOrder=QStringList(), Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsLayoutPdfExportOptionsDialog.
QStringList geospatialPdfLayerOrder() const
Returns a list of map layer IDs in the desired order they should appear in a generated Geospatial PDF...
void setTextRenderFormat(Qgis::TextRenderFormat format)
Sets the text render format.
Qgis::TextRenderFormat textRenderFormat() const
Returns the current text render format.
bool rasterTilingDisabled() const
Returns whether raster tiling is disabled.
void setForceVector(bool force)
Set whether to force vector output.
void setExportThemes(const QStringList &themes)
Sets the list of export themes.
@ LayerId
Stores the map layer ID.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsMapThemeCollection * mapThemeCollection
Definition qgsproject.h:123
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.