QGIS API Documentation 3.99.0-Master (752b475928d)
Loading...
Searching...
No Matches
qgslayoutimageexportoptionsdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutimageexportoptionsdialog.cpp
3 -------------------------------------
4 begin : December 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgis.h"
21#include "qgsgui.h"
22#include "qgshelp.h"
23#include "qgssettings.h"
24
25#include <QCheckBox>
26#include <QPushButton>
27#include <QSlider>
28#include <QSpinBox>
29
30#include "moc_qgslayoutimageexportoptionsdialog.cpp"
31
32QgsLayoutImageExportOptionsDialog::QgsLayoutImageExportOptionsDialog( QWidget *parent, const QString &fileExtension, Qt::WindowFlags flags )
33 : QDialog( parent, flags )
34 , mFileExtension( fileExtension )
35{
36 setupUi( this );
37 connect( mWidthSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsLayoutImageExportOptionsDialog::mWidthSpinBox_valueChanged );
38 connect( mHeightSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsLayoutImageExportOptionsDialog::mHeightSpinBox_valueChanged );
39 connect( mResolutionSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsLayoutImageExportOptionsDialog::mResolutionSpinBox_valueChanged );
40
41 connect( mClipToContentGroupBox, &QGroupBox::toggled, this, &QgsLayoutImageExportOptionsDialog::clipToContentsToggled );
42 connect( mHelpButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutImageExportOptionsDialog::showHelp );
43
44 const bool showQuality = shouldShowQuality();
45 mQualitySpinBox->setVisible( showQuality );
46 mQualitySlider->setVisible( showQuality );
47 mQualityLabel->setVisible( showQuality );
48 mQualityLabel->setText( tr( "%1 quality", "Image format" ).arg( mFileExtension.toUpper() ) );
49
50 connect( mQualitySpinBox, qOverload<int>( &QSpinBox::valueChanged ), mQualitySlider, &QSlider::setValue );
51 connect( mQualitySlider, &QSlider::valueChanged, mQualitySpinBox, &QSpinBox::setValue );
52
54}
55
57{
58 mResolutionSpinBox->setValue( resolution );
59
60 if ( mImageSize.isValid() )
61 {
62 mWidthSpinBox->blockSignals( true );
63 mHeightSpinBox->blockSignals( true );
64 if ( mClipToContentGroupBox->isChecked() )
65 {
66 mWidthSpinBox->setValue( 0 );
67 mHeightSpinBox->setValue( 0 );
68 }
69 else
70 {
71 mWidthSpinBox->setValue( mImageSize.width() * resolution / 25.4 );
72 mHeightSpinBox->setValue( mImageSize.height() * resolution / 25.4 );
73 }
74 mWidthSpinBox->blockSignals( false );
75 mHeightSpinBox->blockSignals( false );
76 }
77}
78
80{
81 return mResolutionSpinBox->value();
82}
83
85{
86 mImageSize = size;
87 mWidthSpinBox->blockSignals( true );
88 mHeightSpinBox->blockSignals( true );
89 mWidthSpinBox->setValue( size.width() * mResolutionSpinBox->value() / 25.4 );
90 mHeightSpinBox->setValue( size.height() * mResolutionSpinBox->value() / 25.4 );
91 mWidthSpinBox->blockSignals( false );
92 mHeightSpinBox->blockSignals( false );
93}
94
96{
97 return mWidthSpinBox->value();
98}
99
101{
102 return mHeightSpinBox->value();
103}
104
106{
107 mClipToContentGroupBox->setChecked( crop );
108}
109
111{
112 return mClipToContentGroupBox->isChecked();
113}
114
116{
117 mGenerateWorldFile->setChecked( generate );
118}
119
121{
122 return mGenerateWorldFile->isChecked();
123}
124
126{
127 mAntialiasingCheckBox->setChecked( antialias );
128}
129
131{
132 return mAntialiasingCheckBox->isChecked();
133}
134
135void QgsLayoutImageExportOptionsDialog::getCropMargins( int &topMargin, int &rightMargin, int &bottomMargin, int &leftMargin ) const
136{
137 topMargin = mTopMarginSpinBox->value();
138 rightMargin = mRightMarginSpinBox->value();
139 bottomMargin = mBottomMarginSpinBox->value();
140 leftMargin = mLeftMarginSpinBox->value();
141}
142
143void QgsLayoutImageExportOptionsDialog::setCropMargins( int topMargin, int rightMargin, int bottomMargin, int leftMargin )
144{
145 mTopMarginSpinBox->setValue( topMargin );
146 mRightMarginSpinBox->setValue( rightMargin );
147 mBottomMarginSpinBox->setValue( bottomMargin );
148 mLeftMarginSpinBox->setValue( leftMargin );
149}
150
152{
153 return mOpenAfterExportingCheckBox->isChecked();
154}
155
157{
158 mOpenAfterExportingCheckBox->setChecked( enabled );
159}
160
162{
163 if ( !shouldShowQuality() )
164 {
165 return -1;
166 }
167 return mQualitySpinBox->value();
168}
169
171{
172 mQualitySpinBox->setValue( quality );
173}
174
175void QgsLayoutImageExportOptionsDialog::mWidthSpinBox_valueChanged( int value )
176{
177 mHeightSpinBox->blockSignals( true );
178 mResolutionSpinBox->blockSignals( true );
179 mHeightSpinBox->setValue( mImageSize.height() * value / mImageSize.width() );
180 mResolutionSpinBox->setValue( value * 25.4 / mImageSize.width() );
181 mHeightSpinBox->blockSignals( false );
182 mResolutionSpinBox->blockSignals( false );
183}
184
185void QgsLayoutImageExportOptionsDialog::mHeightSpinBox_valueChanged( int value )
186{
187 mWidthSpinBox->blockSignals( true );
188 mResolutionSpinBox->blockSignals( true );
189 mWidthSpinBox->setValue( mImageSize.width() * value / mImageSize.height() );
190 mResolutionSpinBox->setValue( value * 25.4 / mImageSize.height() );
191 mWidthSpinBox->blockSignals( false );
192 mResolutionSpinBox->blockSignals( false );
193}
194
195void QgsLayoutImageExportOptionsDialog::mResolutionSpinBox_valueChanged( int value )
196{
197 mWidthSpinBox->blockSignals( true );
198 mHeightSpinBox->blockSignals( true );
199 if ( mClipToContentGroupBox->isChecked() )
200 {
201 mWidthSpinBox->setValue( 0 );
202 mHeightSpinBox->setValue( 0 );
203 }
204 else
205 {
206 mWidthSpinBox->setValue( mImageSize.width() * value / 25.4 );
207 mHeightSpinBox->setValue( mImageSize.height() * value / 25.4 );
208 }
209 mWidthSpinBox->blockSignals( false );
210 mHeightSpinBox->blockSignals( false );
211}
212
213void QgsLayoutImageExportOptionsDialog::clipToContentsToggled( bool state )
214{
215 mWidthSpinBox->setEnabled( !state );
216 mHeightSpinBox->setEnabled( !state );
217
218 if ( state )
219 {
220 whileBlocking( mWidthSpinBox )->setValue( 0 );
221 whileBlocking( mHeightSpinBox )->setValue( 0 );
222 }
223 else
224 {
225 whileBlocking( mWidthSpinBox )->setValue( mImageSize.width() * mResolutionSpinBox->value() / 25.4 );
226 whileBlocking( mHeightSpinBox )->setValue( mImageSize.height() * mResolutionSpinBox->value() / 25.4 );
227 }
228}
229
230void QgsLayoutImageExportOptionsDialog::showHelp()
231{
232 QgsHelp::openHelp( QStringLiteral( "print_composer/create_output.html" ) );
233}
234
235bool QgsLayoutImageExportOptionsDialog::shouldShowQuality() const
236{
237 const QStringList validExtensions = { "jpeg", "jpg" };
238 for ( const QString &ext : validExtensions )
239 {
240 if ( mFileExtension.toLower() == ext )
241 {
242 return true;
243 }
244 }
245 return false;
246}
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:221
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
bool antialiasing() const
Returns whether antialiasing should be used in the export.
void setQuality(int quality)
Sets the image quality (for JPEG).
void setCropToContents(bool crop)
Sets whether the crop to contents option should be checked in the dialog.
void setImageSize(QSizeF size)
Sets the target image size.
void setAntialiasing(bool antialias)
Sets whether antialiasing should be used in the export.
int quality() const
Returns the image quality.
double resolution() const
Returns the selected resolution from the dialog.
QgsLayoutImageExportOptionsDialog(QWidget *parent=nullptr, const QString &fileExtension=QString(), Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsLayoutImageExportOptionsDialog.
int imageWidth() const
Returns the user-set image width in pixels.
bool cropToContents() const
Returns whether the crop to contents option is checked in the dialog.
void setOpenAfterExporting(bool enabled)
Sets whether to open the pdf after exporting it.
bool generateWorldFile() const
Returns whether the generate world file option is checked in the dialog.
void setCropMargins(int topMargin, int rightMargin, int bottomMargin, int leftMargin)
Sets the current crop to contents margin values, in pixels.
void setResolution(double resolution)
Sets the initial resolution displayed in the dialog.
void getCropMargins(int &topMargin, int &rightMargin, int &bottomMargin, int &leftMargin) const
Fetches the current crop to contents margin values, in pixels.
int imageHeight() const
Returns the user-set image height in pixels.
void setGenerateWorldFile(bool generate)
Sets whether the generate world file option should be checked.
bool openAfterExporting() const
Returns whether the pdf should be opened after exporting it.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511