QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
63  mComboImageCompression->addItem( tr( "Lossy (JPEG)" ), false );
64  mComboImageCompression->addItem( tr( "Lossless" ), true );
65 #else
66  mComboImageCompression->setDisabled( true );
67  mComboImageCompression->addItem( tr( "Lossy (JPEG)" ) );
68  mComboImageCompression->setCurrentIndex( 0 );
69  mComboImageCompression->setToolTip( tr( "Lossless image compression is available only with QGIS builds using Qt 5.13 or later" ) );
70 #endif
71 
72 
73  const QStringList themes = QgsProject::instance()->mapThemeCollection()->mapThemes();
74  for ( const QString &theme : themes )
75  {
76  QListWidgetItem *item = new QListWidgetItem( theme );
77  item->setFlags( item->flags() | Qt::ItemIsUserCheckable );
78  item->setCheckState( Qt::Unchecked );
79  mThemesList->addItem( item );
80  }
81 
82  QList< QgsMapLayer * > order = QgsProject::instance()->layerTreeRoot()->layerOrder();
83  for ( auto it = geoPdfLayerOrder.rbegin(); it != geoPdfLayerOrder.rend(); ++it )
84  {
85  for ( int i = 0; i < order.size(); ++i )
86  {
87  if ( order.at( i )->id() == *it )
88  {
89  order.move( i, 0 );
90  break;
91  }
92  }
93  }
94  mGeoPdfStructureModel = new QgsGeoPdfLayerTreeModel( order, this );
95  mGeoPdfStructureProxyModel = new QgsGeoPdfLayerFilteredTreeModel( mGeoPdfStructureModel, this );
96  mGeoPdfStructureTree->setModel( mGeoPdfStructureProxyModel );
97  mGeoPdfStructureTree->resizeColumnToContents( 0 );
98  mGeoPdfStructureTree->header()->show();
99  mGeoPdfStructureTree->setSelectionMode( QAbstractItemView::SingleSelection );
100  mGeoPdfStructureTree->setSelectionBehavior( QAbstractItemView::SelectRows );
101 
102  mGeoPdfStructureTree->setDragEnabled( true );
103  mGeoPdfStructureTree->setAcceptDrops( true );
104  mGeoPdfStructureTree->setDragDropMode( QAbstractItemView::InternalMove );
105  mGeoPdfStructureTree->setDefaultDropAction( Qt::MoveAction );
106 
107  mGeoPdfStructureTree->setContextMenuPolicy( Qt::CustomContextMenu );
108  connect( mGeoPdfStructureTree, &QTreeView::customContextMenuRequested, this, [ = ]( const QPoint & point )
109  {
110  const QModelIndex index = mGeoPdfStructureTree->indexAt( point );
111  if ( index.isValid() )
112  showContextMenuForGeoPdfStructure( point, mGeoPdfStructureProxyModel->mapToSource( index ) );
113  } );
114 
115  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutPdfExportOptionsDialog::showHelp );
117 }
118 
120 {
121  mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( format ) );
122 }
123 
125 {
126  return static_cast< QgsRenderContext::TextRenderFormat >( mTextRenderFormatComboBox->currentData().toInt() );
127 }
128 
130 {
131  mForceVectorCheckBox->setChecked( force );
132 }
133 
135 {
136  return mForceVectorCheckBox->isChecked();
137 }
138 
140 {
141  mAppendGeoreferenceCheckbox->setEnabled( enabled );
142 }
143 
145 {
146  mAppendGeoreferenceCheckbox->setChecked( enabled );
147 }
148 
150 {
151  return mAppendGeoreferenceCheckbox->isChecked();
152 }
153 
155 {
156  mIncludeMetadataCheckbox->setChecked( enabled );
157 }
158 
160 {
161  return mIncludeMetadataCheckbox->isChecked();
162 }
163 
165 {
166  mDisableRasterTilingCheckBox->setChecked( disabled );
167 }
168 
170 {
171  return mDisableRasterTilingCheckBox->isChecked();
172 }
173 
175 {
176  mSimplifyGeometriesCheckbox->setChecked( enabled );
177 }
178 
180 {
181  return mSimplifyGeometriesCheckbox->isChecked();
182 }
183 
185 {
186 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
187  mComboImageCompression->setCurrentIndex( mComboImageCompression->findData( enabled ) );
188 #else
189  Q_UNUSED( enabled )
190 #endif
191 }
192 
194 {
195 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
196  return mComboImageCompression->currentData().toBool();
197 #else
198  return false;
199 #endif
200 }
201 
203 {
204  if ( !mGeopdfAvailable )
205  return;
206 
207  mGeoPDFGroupBox->setChecked( enabled );
208 }
209 
211 {
212  if ( !mGeopdfAvailable )
213  return false;
214 
215  return mGeoPDFGroupBox->isChecked();
216 }
217 
219 {
220  if ( !mGeopdfAvailable )
221  return;
222 
223  if ( enabled )
224  mGeoPdfFormatComboBox->setCurrentIndex( 1 );
225  else
226  mGeoPdfFormatComboBox->setCurrentIndex( 0 );
227 }
228 
230 {
231  if ( !mGeopdfAvailable )
232  return false;
233 
234  return mGeoPdfFormatComboBox->currentIndex() == 1;
235 }
236 
237 
238 void QgsLayoutPdfExportOptionsDialog::setExportThemes( const QStringList &themes )
239 {
240  if ( !mGeopdfAvailable )
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 ( !mGeopdfAvailable )
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 < mGeoPdfStructureProxyModel->rowCount(); ++row )
274  {
275  order << mGeoPdfStructureProxyModel->data( mGeoPdfStructureProxyModel->index( row, 0 ), QgsGeoPdfLayerTreeModel::LayerIdRole ).toString();
276  }
277  return order;
278 }
279 
280 void QgsLayoutPdfExportOptionsDialog::showHelp()
281 {
282  QgsHelp::openHelp( QStringLiteral( "print_composer/create_output.html" ) );
283 }
284 
285 void QgsLayoutPdfExportOptionsDialog::showContextMenuForGeoPdfStructure( QPoint point, const QModelIndex &index )
286 {
287  mGeoPdfStructureTreeMenu->clear();
288 
289  switch ( index.column() )
290  {
293  {
294  QAction *selectAll = new QAction( tr( "Select All" ), mGeoPdfStructureTreeMenu );
295  mGeoPdfStructureTreeMenu->addAction( selectAll );
296  connect( selectAll, &QAction::triggered, this, [ = ]
297  {
298  mGeoPdfStructureModel->checkAll( true, QModelIndex(), index.column() );
299  } );
300  QAction *deselectAll = new QAction( tr( "Deselect All" ), mGeoPdfStructureTreeMenu );
301  mGeoPdfStructureTreeMenu->addAction( deselectAll );
302  connect( deselectAll, &QAction::triggered, this, [ = ]
303  {
304  mGeoPdfStructureModel->checkAll( false, QModelIndex(), index.column() );
305  } );
306  break;
307  }
308 
309  default:
310  break;
311  }
312 
313  if ( !mGeoPdfStructureTreeMenu->actions().empty() )
314  {
315  mGeoPdfStructureTreeMenu->exec( mGeoPdfStructureTree->mapToGlobal( point ) );
316  }
317 }
qgslayoutpdfexportoptionsdialog.h
QgsLayoutPdfExportOptionsDialog::setForceVector
void setForceVector(bool force)
Set whether to force vector output.
Definition: qgslayoutpdfexportoptionsdialog.cpp:129
QgsMapThemeCollection::mapThemes
QStringList mapThemes
Definition: qgsmapthemecollection.h:48
QgsLayoutPdfExportOptionsDialog::metadataEnabled
bool metadataEnabled() const
Returns whether metadata is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:159
qgsmapthemecollection.h
qgsgui.h
QgsGeoPdfLayerTreeModel
Layer tree model for Geo-PDF layers.
Definition: qgsgeopdflayertreemodel.h:40
QgsLayoutPdfExportOptionsDialog::useOgcBestPracticeFormat
bool useOgcBestPracticeFormat() const
Returns whether use of OGC best-practice format is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:229
QgsLayoutPdfExportOptionsDialog::setTextRenderFormat
void setTextRenderFormat(QgsRenderContext::TextRenderFormat format)
Sets the text render format.
Definition: qgslayoutpdfexportoptionsdialog.cpp:119
QgsLayoutPdfExportOptionsDialog::exportThemes
QStringList exportThemes() const
Returns the list of export themes.
Definition: qgslayoutpdfexportoptionsdialog.cpp:251
qgis.h
QgsRenderContext::TextRenderFormat
TextRenderFormat
Options for rendering text.
Definition: qgsrendercontext.h:96
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:468
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:154
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:169
QgsLayoutPdfExportOptionsDialog::enableGeoreferencingOptions
void enableGeoreferencingOptions(bool enabled)
Sets whether to enable georeferencing options.
Definition: qgslayoutpdfexportoptionsdialog.cpp:139
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:139
QgsProject::layerTreeRoot
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
Definition: qgsproject.cpp:3100
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:222
QgsLayoutPdfExportOptionsDialog::exportGeoPdf
bool exportGeoPdf() const
Returns whether Geo-PDF export is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:210
QgsLayoutPdfExportOptionsDialog::setRasterTilingDisabled
void setRasterTilingDisabled(bool disabled)
Sets whether to disable raster tiling.
Definition: qgslayoutpdfexportoptionsdialog.cpp:164
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:132
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:270
QgsLayoutPdfExportOptionsDialog::setLosslessImageExport
void setLosslessImageExport(bool enabled)
Sets whether to use lossless image compression.
Definition: qgslayoutpdfexportoptionsdialog.cpp:184
QgsLayoutPdfExportOptionsDialog::setGeoreferencingEnabled
void setGeoreferencingEnabled(bool enabled)
Sets whether to enable georeferencing.
Definition: qgslayoutpdfexportoptionsdialog.cpp:144
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:103
qgslayertree.h
QgsLayoutPdfExportOptionsDialog::geometriesSimplified
bool geometriesSimplified() const
Returns whether geometry simplification is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:179
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:124
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:149
QgsLayoutPdfExportOptionsDialog::forceVector
bool forceVector() const
Returns whether vector output is being forced.
Definition: qgslayoutpdfexportoptionsdialog.cpp:134
QgsLayoutPdfExportOptionsDialog::losslessImageExport
bool losslessImageExport() const
Returns whether lossless image compression is enabled.
Definition: qgslayoutpdfexportoptionsdialog.cpp:193
QgsRenderContext::TextFormatAlwaysOutlines
@ TextFormatAlwaysOutlines
Always render text using path objects (AKA outlines/curves).
Definition: qgsrendercontext.h:116
QgsLayoutPdfExportOptionsDialog::setGeometriesSimplified
void setGeometriesSimplified(bool enabled)
Sets whether to simplify geometries.
Definition: qgslayoutpdfexportoptionsdialog.cpp:174
qgshelp.h
qgsproject.h
QgsLayoutPdfExportOptionsDialog::setUseOgcBestPracticeFormat
void setUseOgcBestPracticeFormat(bool enabled)
Sets whether to use OGC best-practice format.
Definition: qgslayoutpdfexportoptionsdialog.cpp:218
QgsLayoutPdfExportOptionsDialog::setExportThemes
void setExportThemes(const QStringList &themes)
Sets the list of export themes.
Definition: qgslayoutpdfexportoptionsdialog.cpp:238
QgsLayoutPdfExportOptionsDialog::setExportGeoPdf
void setExportGeoPdf(bool enabled)
Sets whether to export a Geo-PDF.
Definition: qgslayoutpdfexportoptionsdialog.cpp:202