QGIS API Documentation 3.99.0-Master (357b655ed83)
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#include <QString>
30
31#include "moc_qgslayoutimageexportoptionsdialog.cpp"
32
33using namespace Qt::StringLiterals;
34
35QgsLayoutImageExportOptionsDialog::QgsLayoutImageExportOptionsDialog( QWidget *parent, const QString &fileExtension, Qt::WindowFlags flags )
36 : QDialog( parent, flags )
37 , mFileExtension( fileExtension )
38{
39 setupUi( this );
40 connect( mWidthSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsLayoutImageExportOptionsDialog::mWidthSpinBox_valueChanged );
41 connect( mHeightSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsLayoutImageExportOptionsDialog::mHeightSpinBox_valueChanged );
42 connect( mResolutionSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsLayoutImageExportOptionsDialog::mResolutionSpinBox_valueChanged );
43
44 connect( mClipToContentGroupBox, &QGroupBox::toggled, this, &QgsLayoutImageExportOptionsDialog::clipToContentsToggled );
45 connect( mHelpButtonBox, &QDialogButtonBox::helpRequested, this, &QgsLayoutImageExportOptionsDialog::showHelp );
46
47 const bool showQuality = shouldShowQuality();
48 mQualitySpinBox->setVisible( showQuality );
49 mQualitySlider->setVisible( showQuality );
50 mQualityLabel->setVisible( showQuality );
51 mQualityLabel->setText( tr( "%1 quality", "Image format" ).arg( mFileExtension.toUpper() ) );
52
53 connect( mQualitySpinBox, qOverload<int>( &QSpinBox::valueChanged ), mQualitySlider, &QSlider::setValue );
54 connect( mQualitySlider, &QSlider::valueChanged, mQualitySpinBox, &QSpinBox::setValue );
55
57}
58
60{
61 mResolutionSpinBox->setValue( resolution );
62
63 if ( mImageSize.isValid() )
64 {
65 mWidthSpinBox->blockSignals( true );
66 mHeightSpinBox->blockSignals( true );
67 if ( mClipToContentGroupBox->isChecked() )
68 {
69 mWidthSpinBox->setValue( 0 );
70 mHeightSpinBox->setValue( 0 );
71 }
72 else
73 {
74 mWidthSpinBox->setValue( mImageSize.width() * resolution / 25.4 );
75 mHeightSpinBox->setValue( mImageSize.height() * resolution / 25.4 );
76 }
77 mWidthSpinBox->blockSignals( false );
78 mHeightSpinBox->blockSignals( false );
79 }
80}
81
83{
84 return mResolutionSpinBox->value();
85}
86
88{
89 mImageSize = size;
90 mWidthSpinBox->blockSignals( true );
91 mHeightSpinBox->blockSignals( true );
92 mWidthSpinBox->setValue( size.width() * mResolutionSpinBox->value() / 25.4 );
93 mHeightSpinBox->setValue( size.height() * mResolutionSpinBox->value() / 25.4 );
94 mWidthSpinBox->blockSignals( false );
95 mHeightSpinBox->blockSignals( false );
96}
97
99{
100 return mWidthSpinBox->value();
101}
102
104{
105 return mHeightSpinBox->value();
106}
107
109{
110 mClipToContentGroupBox->setChecked( crop );
111}
112
114{
115 return mClipToContentGroupBox->isChecked();
116}
117
119{
120 mGenerateWorldFile->setChecked( generate );
121}
122
124{
125 return mGenerateWorldFile->isChecked();
126}
127
129{
130 mAntialiasingCheckBox->setChecked( antialias );
131}
132
134{
135 return mAntialiasingCheckBox->isChecked();
136}
137
138void QgsLayoutImageExportOptionsDialog::getCropMargins( int &topMargin, int &rightMargin, int &bottomMargin, int &leftMargin ) const
139{
140 topMargin = mTopMarginSpinBox->value();
141 rightMargin = mRightMarginSpinBox->value();
142 bottomMargin = mBottomMarginSpinBox->value();
143 leftMargin = mLeftMarginSpinBox->value();
144}
145
146void QgsLayoutImageExportOptionsDialog::setCropMargins( int topMargin, int rightMargin, int bottomMargin, int leftMargin )
147{
148 mTopMarginSpinBox->setValue( topMargin );
149 mRightMarginSpinBox->setValue( rightMargin );
150 mBottomMarginSpinBox->setValue( bottomMargin );
151 mLeftMarginSpinBox->setValue( leftMargin );
152}
153
155{
156 return mOpenAfterExportingCheckBox->isChecked();
157}
158
160{
161 mOpenAfterExportingCheckBox->setChecked( enabled );
162}
163
165{
166 if ( !shouldShowQuality() )
167 {
168 return -1;
169 }
170 return mQualitySpinBox->value();
171}
172
174{
175 mQualitySpinBox->setValue( quality );
176}
177
178void QgsLayoutImageExportOptionsDialog::mWidthSpinBox_valueChanged( int value )
179{
180 mHeightSpinBox->blockSignals( true );
181 mResolutionSpinBox->blockSignals( true );
182 mHeightSpinBox->setValue( mImageSize.height() * value / mImageSize.width() );
183 mResolutionSpinBox->setValue( value * 25.4 / mImageSize.width() );
184 mHeightSpinBox->blockSignals( false );
185 mResolutionSpinBox->blockSignals( false );
186}
187
188void QgsLayoutImageExportOptionsDialog::mHeightSpinBox_valueChanged( int value )
189{
190 mWidthSpinBox->blockSignals( true );
191 mResolutionSpinBox->blockSignals( true );
192 mWidthSpinBox->setValue( mImageSize.width() * value / mImageSize.height() );
193 mResolutionSpinBox->setValue( value * 25.4 / mImageSize.height() );
194 mWidthSpinBox->blockSignals( false );
195 mResolutionSpinBox->blockSignals( false );
196}
197
198void QgsLayoutImageExportOptionsDialog::mResolutionSpinBox_valueChanged( int value )
199{
200 mWidthSpinBox->blockSignals( true );
201 mHeightSpinBox->blockSignals( true );
202 if ( mClipToContentGroupBox->isChecked() )
203 {
204 mWidthSpinBox->setValue( 0 );
205 mHeightSpinBox->setValue( 0 );
206 }
207 else
208 {
209 mWidthSpinBox->setValue( mImageSize.width() * value / 25.4 );
210 mHeightSpinBox->setValue( mImageSize.height() * value / 25.4 );
211 }
212 mWidthSpinBox->blockSignals( false );
213 mHeightSpinBox->blockSignals( false );
214}
215
216void QgsLayoutImageExportOptionsDialog::clipToContentsToggled( bool state )
217{
218 mWidthSpinBox->setEnabled( !state );
219 mHeightSpinBox->setEnabled( !state );
220
221 if ( state )
222 {
223 whileBlocking( mWidthSpinBox )->setValue( 0 );
224 whileBlocking( mHeightSpinBox )->setValue( 0 );
225 }
226 else
227 {
228 whileBlocking( mWidthSpinBox )->setValue( mImageSize.width() * mResolutionSpinBox->value() / 25.4 );
229 whileBlocking( mHeightSpinBox )->setValue( mImageSize.height() * mResolutionSpinBox->value() / 25.4 );
230 }
231}
232
233void QgsLayoutImageExportOptionsDialog::showHelp()
234{
235 QgsHelp::openHelp( u"print_composer/create_output.html"_s );
236}
237
238bool QgsLayoutImageExportOptionsDialog::shouldShowQuality() const
239{
240 const QStringList validExtensions = { "jpeg", "jpg" };
241 for ( const QString &ext : validExtensions )
242 {
243 if ( mFileExtension.toLower() == ext )
244 {
245 return true;
246 }
247 }
248 return false;
249}
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
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:6839