QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 #include "qgis.h"
19 #include "qgssettings.h"
20 #include "qgsgui.h"
21 #include "qgshelp.h"
23 #include "qgsproject.h"
24 #include "qgsmapthemecollection.h"
26 #include "qgslayertree.h"
27 
28 #include <QCheckBox>
29 #include <QPushButton>
30 #include <QMenu>
31 
32 QgsLayoutPdfExportOptionsDialog::QgsLayoutPdfExportOptionsDialog( QWidget *parent, bool allowGeoPdfExport, const QString &geoPdfReason, const QStringList &geoPdfLayerOrder, Qt::WindowFlags flags )
33  : QDialog( parent, flags )
34 {
35  setupUi( this );
36 
37  mGeoPdfStructureTreeMenu = new QMenu( this );
38 
39  mTextRenderFormatComboBox->addItem( tr( "Always Export Text as Paths (Recommended)" ), QgsRenderContext::TextFormatAlwaysOutlines );
40  mTextRenderFormatComboBox->addItem( tr( "Always Export Text as Text Objects" ), QgsRenderContext::TextFormatAlwaysText );
41 
42  mGeopdfAvailable = allowGeoPdfExport && QgsAbstractGeoPdfExporter::geoPDFCreationAvailable();
43  mGeoPDFGroupBox->setEnabled( mGeopdfAvailable );
44  mGeoPDFGroupBox->setChecked( false );
45  if ( !mGeopdfAvailable )
46  {
47  mGeoPDFOptionsStackedWidget->setCurrentIndex( 0 );
48  mGeoPdfUnavailableReason->setText( geoPdfReason.isEmpty() ? QgsAbstractGeoPdfExporter::geoPDFAvailabilityExplanation() : geoPdfReason );
49  // avoid showing reason in disabled text color - we want it to stand out
50  QPalette p = mGeoPdfUnavailableReason->palette();
51  p.setColor( QPalette::Disabled, QPalette::WindowText, QPalette::WindowText );
52  mGeoPdfUnavailableReason->setPalette( p );
53  mGeoPDFOptionsStackedWidget->removeWidget( mGeoPDFOptionsStackedWidget->widget( 1 ) );
54  }
55  else
56  {
57  mGeoPDFOptionsStackedWidget->setCurrentIndex( 1 );
58  mGeoPdfFormatComboBox->addItem( tr( "ISO 32000 Extension (recommended)" ) );
59  mGeoPdfFormatComboBox->addItem( tr( "OGC Best Practice" ) );
60  }
61 
62  const QStringList themes = QgsProject::instance()->mapThemeCollection()->mapThemes();
63  for ( const QString &theme : themes )
64  {
65  QListWidgetItem *item = new QListWidgetItem( theme );
66  item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
67  item->setCheckState( Qt::Unchecked );
68  mThemesList->addItem( item );
69  }
70 
71  QList< QgsMapLayer * > order = QgsProject::instance()->layerTreeRoot()->layerOrder();
72  for ( auto it = geoPdfLayerOrder.rbegin(); it != geoPdfLayerOrder.rend(); ++it )
73  {
74  for ( int i = 0; i < order.size(); ++i )
75  {
76  if ( order.at( i )->id() == *it )
77  {
78  order.move( i, 0 );
79  break;
80  }
81  }
82  }
83  mGeoPdfStructureModel = new QgsGeoPdfLayerTreeModel( order, this );
84  mGeoPdfStructureProxyModel = new QgsGeoPdfLayerFilteredTreeModel( mGeoPdfStructureModel, this );
85  mGeoPdfStructureTree->setModel( mGeoPdfStructureProxyModel );
86  mGeoPdfStructureTree->resizeColumnToContents( 0 );
87  mGeoPdfStructureTree->header()->show();
88  mGeoPdfStructureTree->setSelectionMode( QAbstractItemView::SingleSelection );
89  mGeoPdfStructureTree->setSelectionBehavior( QAbstractItemView::SelectRows );
90 
91  mGeoPdfStructureTree->setDragEnabled( true );
92  mGeoPdfStructureTree->setAcceptDrops( true );
93  mGeoPdfStructureTree->setDragDropMode( QAbstractItemView::InternalMove );
94  mGeoPdfStructureTree->setDefaultDropAction( Qt::MoveAction );
95 
96  mGeoPdfStructureTree->setContextMenuPolicy( Qt::CustomContextMenu );
97  connect( mGeoPdfStructureTree, &QTreeView::customContextMenuRequested, this, [ = ]( const QPoint & point )
98  {
99  const QModelIndex index = mGeoPdfStructureTree->indexAt( point );
100  if ( index.isValid() )
101  showContextMenuForGeoPdfStructure( point, mGeoPdfStructureProxyModel->mapToSource( index ) );
102  } );
103 
104  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutPdfExportOptionsDialog::showHelp );
106 }
107 
109 {
110  mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( format ) );
111 }
112 
114 {
115  return static_cast< QgsRenderContext::TextRenderFormat >( mTextRenderFormatComboBox->currentData().toInt() );
116 }
117 
119 {
120  mForceVectorCheckBox->setChecked( force );
121 }
122 
124 {
125  return mForceVectorCheckBox->isChecked();
126 }
127 
129 {
130  mAppendGeoreferenceCheckbox->setEnabled( enabled );
131 }
132 
134 {
135  mAppendGeoreferenceCheckbox->setChecked( enabled );
136 }
137 
139 {
140  return mAppendGeoreferenceCheckbox->isChecked();;
141 }
142 
144 {
145  mIncludeMetadataCheckbox->setChecked( enabled );
146 }
147 
149 {
150  return mIncludeMetadataCheckbox->isChecked();
151 }
152 
154 {
155  mDisableRasterTilingCheckBox->setChecked( disabled );
156 }
157 
159 {
160  return mDisableRasterTilingCheckBox->isChecked();
161 }
162 
164 {
165  mSimplifyGeometriesCheckbox->setChecked( enabled );
166 }
167 
169 {
170  return mSimplifyGeometriesCheckbox->isChecked();
171 }
172 
174 {
175  if ( !mGeopdfAvailable )
176  return;
177 
178  mGeoPDFGroupBox->setChecked( enabled );
179 }
180 
182 {
183  if ( !mGeopdfAvailable )
184  return false;
185 
186  return mGeoPDFGroupBox->isChecked();
187 }
188 
190 {
191  if ( !mGeopdfAvailable )
192  return;
193 
194  if ( enabled )
195  mGeoPdfFormatComboBox->setCurrentIndex( 1 );
196  else
197  mGeoPdfFormatComboBox->setCurrentIndex( 0 );
198 }
199 
201 {
202  if ( !mGeopdfAvailable )
203  return false;
204 
205  return mGeoPdfFormatComboBox->currentIndex() == 1;
206 }
207 
208 
209 void QgsLayoutPdfExportOptionsDialog::setExportThemes( const QStringList &themes )
210 {
211  if ( !mGeopdfAvailable )
212  return;
213 
214  mIncludeMapThemesCheck->setChecked( !themes.isEmpty() );
215  for ( int i = 0; i < mThemesList->count(); ++i )
216  {
217  QListWidgetItem *item = mThemesList->item( i );
218  item->setCheckState( themes.contains( item->text() ) ? Qt::Checked : Qt::Unchecked );
219  }
220 }
221 
223 {
224  QStringList res;
225  if ( !mGeopdfAvailable )
226  return res;
227 
228  if ( !mIncludeMapThemesCheck || !mIncludeMapThemesCheck->isChecked() )
229  return res;
230 
231  res.reserve( mThemesList->count() );
232  for ( int i = 0; i < mThemesList->count(); ++i )
233  {
234  QListWidgetItem *item = mThemesList->item( i );
235  if ( item->checkState() == Qt::Checked )
236  res << item->text();
237  }
238  return res;
239 }
240 
242 {
243  QStringList order;
244  for ( int row = 0; row < mGeoPdfStructureProxyModel->rowCount(); ++row )
245  {
246  order << mGeoPdfStructureProxyModel->data( mGeoPdfStructureProxyModel->index( row, 0 ), QgsGeoPdfLayerTreeModel::LayerIdRole ).toString();
247  }
248  return order;
249 }
250 
251 void QgsLayoutPdfExportOptionsDialog::showHelp()
252 {
253  QgsHelp::openHelp( QStringLiteral( "print_composer/create_output.html" ) );
254 }
255 
256 void QgsLayoutPdfExportOptionsDialog::showContextMenuForGeoPdfStructure( QPoint point, const QModelIndex &index )
257 {
258  mGeoPdfStructureTreeMenu->clear();
259 
260  switch ( index.column() )
261  {
264  {
265  QAction *selectAll = new QAction( tr( "Select All" ), mGeoPdfStructureTreeMenu );
266  mGeoPdfStructureTreeMenu->addAction( selectAll );
267  connect( selectAll, &QAction::triggered, this, [ = ]
268  {
269  mGeoPdfStructureModel->checkAll( true, QModelIndex(), index.column() );
270  } );
271  QAction *deselectAll = new QAction( tr( "Deselect All" ), mGeoPdfStructureTreeMenu );
272  mGeoPdfStructureTreeMenu->addAction( deselectAll );
273  connect( deselectAll, &QAction::triggered, this, [ = ]
274  {
275  mGeoPdfStructureModel->checkAll( false, QModelIndex(), index.column() );
276  } );
277  break;
278  }
279 
280  default:
281  break;
282  }
283 
284  if ( !mGeoPdfStructureTreeMenu->actions().empty() )
285  {
286  mGeoPdfStructureTreeMenu->exec( mGeoPdfStructureTree->mapToGlobal( point ) );
287  }
288 }
qgslayoutpdfexportoptionsdialog.h
QgsLayoutPdfExportOptionsDialog::setForceVector
void setForceVector(bool force)
Set whether to force vector output.
Definition: qgslayoutpdfexportoptionsdialog.cpp:118
QgsMapThemeCollection::mapThemes
QStringList mapThemes
Definition: qgsmapthemecollection.h:48
QgsLayoutPdfExportOptionsDialog::metadataEnabled
bool metadataEnabled() const
Returns whether metadata is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:148
qgsmapthemecollection.h
qgsgui.h
QgsGeoPdfLayerTreeModel
Definition: qgsgeopdflayertreemodel.h:39
QgsLayoutPdfExportOptionsDialog::useOgcBestPracticeFormat
bool useOgcBestPracticeFormat() const
Returns whether use of OGC best-practice format is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:200
QgsLayoutPdfExportOptionsDialog::setTextRenderFormat
void setTextRenderFormat(QgsRenderContext::TextRenderFormat format)
Sets the text render format.
Definition: qgslayoutpdfexportoptionsdialog.cpp:108
QgsLayoutPdfExportOptionsDialog::exportThemes
QStringList exportThemes() const
Returns the list of export themes.
Definition: qgslayoutpdfexportoptionsdialog.cpp:222
qgis.h
QgsRenderContext::TextRenderFormat
TextRenderFormat
Options for rendering text.
Definition: qgsrendercontext.h:90
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:458
QgsMapLayerModel::LayerIdRole
@ LayerIdRole
Stores the map layer ID.
Definition: qgsmaplayermodel.h:50
QgsGeoPdfLayerTreeModel::IncludeVectorAttributes
@ IncludeVectorAttributes
Vector attribute.
Definition: qgsgeopdflayertreemodel.h:51
QgsLayoutPdfExportOptionsDialog::setMetadataEnabled
void setMetadataEnabled(bool enabled)
Sets whether to enable metadata.
Definition: qgslayoutpdfexportoptionsdialog.cpp:143
QgsLayoutPdfExportOptionsDialog::QgsLayoutPdfExportOptionsDialog
QgsLayoutPdfExportOptionsDialog(QWidget *parent=nullptr, bool allowGeoPdfExport=true, const QString &geoPdfReason=QString(), const QStringList &geoPdfLayerOrder=QStringList(), Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsLayoutPdfExportOptionsDialog.
Definition: qgslayoutpdfexportoptionsdialog.cpp:32
QgsLayoutPdfExportOptionsDialog::rasterTilingDisabled
bool rasterTilingDisabled() const
Returns whether raster tiling is disabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:158
QgsLayoutPdfExportOptionsDialog::enableGeoreferencingOptions
void enableGeoreferencingOptions(bool enabled)
Sets whether to enable georeferencing options.
Definition: qgslayoutpdfexportoptionsdialog.cpp:128
QgsGui::enableAutoGeometryRestore
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:133
QgsProject::layerTreeRoot
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
Definition: qgsproject.cpp:3016
qgsgeopdflayertreemodel.h
QgsGeoPdfLayerTreeModel::checkAll
void checkAll(bool checked, const QModelIndex &parent=QModelIndex(), int column=IncludeVectorAttributes)
Checks (or unchecks) all rows and children from the specified parent index.
Definition: qgsgeopdflayertreemodel.cpp:227
QgsLayoutPdfExportOptionsDialog::exportGeoPdf
bool exportGeoPdf() const
Returns whether Geo-PDF export is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:181
QgsLayoutPdfExportOptionsDialog::setRasterTilingDisabled
void setRasterTilingDisabled(bool disabled)
Sets whether to disable raster tiling.
Definition: qgslayoutpdfexportoptionsdialog.cpp:153
QgsLayerTree::layerOrder
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
Definition: qgslayertree.cpp:75
QgsRenderContext::TextFormatAlwaysText
@ TextFormatAlwaysText
Always render text as text objects.
Definition: qgsrendercontext.h:127
QgsAbstractGeoPdfExporter::geoPDFCreationAvailable
static bool geoPDFCreationAvailable()
Returns true if the current QGIS build is capable of GeoPDF support.
Definition: qgsabstractgeopdfexporter.cpp:36
QgsLayoutPdfExportOptionsDialog::geoPdfLayerOrder
QStringList geoPdfLayerOrder() const
Returns a list of map layer IDs in the desired order they should appear in a generated GeoPDF file.
Definition: qgslayoutpdfexportoptionsdialog.cpp:241
QgsLayoutPdfExportOptionsDialog::setGeoreferencingEnabled
void setGeoreferencingEnabled(bool enabled)
Sets whether to enable georeferencing.
Definition: qgslayoutpdfexportoptionsdialog.cpp:133
QgsAbstractGeoPdfExporter::geoPDFAvailabilityExplanation
static QString geoPDFAvailabilityExplanation()
Returns a user-friendly, translated string explaining why GeoPDF export support is not available on t...
Definition: qgsabstractgeopdfexporter.cpp:61
QgsProject::mapThemeCollection
QgsMapThemeCollection mapThemeCollection
Definition: qgsproject.h:101
qgslayertree.h
QgsLayoutPdfExportOptionsDialog::geometriesSimplified
bool geometriesSimplified() const
Returns whether geometry simplification is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:168
QgsGeoPdfLayerTreeModel::InitiallyVisible
@ InitiallyVisible
Initial visiblity state.
Definition: qgsgeopdflayertreemodel.h:50
QgsLayoutPdfExportOptionsDialog::textRenderFormat
QgsRenderContext::TextRenderFormat textRenderFormat() const
Returns the current text render format.
Definition: qgslayoutpdfexportoptionsdialog.cpp:113
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
qgsabstractgeopdfexporter.h
qgssettings.h
QgsLayoutPdfExportOptionsDialog::georeferencingEnabled
bool georeferencingEnabled() const
Returns whether georeferencing is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:138
QgsLayoutPdfExportOptionsDialog::forceVector
bool forceVector() const
Returns whether vector output is being forced.
Definition: qgslayoutpdfexportoptionsdialog.cpp:123
QgsRenderContext::TextFormatAlwaysOutlines
@ TextFormatAlwaysOutlines
Always render text using path objects (AKA outlines/curves).
Definition: qgsrendercontext.h:111
QgsLayoutPdfExportOptionsDialog::setGeometriesSimplified
void setGeometriesSimplified(bool enabled)
Sets whether to simplify geometries.
Definition: qgslayoutpdfexportoptionsdialog.cpp:163
qgshelp.h
qgsproject.h
QgsLayoutPdfExportOptionsDialog::setUseOgcBestPracticeFormat
void setUseOgcBestPracticeFormat(bool enabled)
Sets whether to use OGC best-practice format.
Definition: qgslayoutpdfexportoptionsdialog.cpp:189
QgsLayoutPdfExportOptionsDialog::setExportThemes
void setExportThemes(const QStringList &themes)
Sets the list of export themes.
Definition: qgslayoutpdfexportoptionsdialog.cpp:209
QgsLayoutPdfExportOptionsDialog::setExportGeoPdf
void setExportGeoPdf(bool enabled)
Sets whether to export a Geo-PDF.
Definition: qgslayoutpdfexportoptionsdialog.cpp:173