Skip to content

Commit de0ac14

Browse files
committed
[ui] use qgsfilewidget in the save raster as dialog
1 parent 09e830b commit de0ac14

5 files changed

+99
-104
lines changed

python/gui/qgsfilewidget.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ returns the filters used for QDialog.getOpenFileName
100100

101101
void setConfirmOverwrite( bool confirmOverwrite );
102102
%Docstring
103-
Sets whether a confirmation to overwrite an existing file will appear
103+
Sets whether a confirmation to overwrite an existing file will appear.
104+
By default, a confirmation will appear.
104105
\param confirmOverwrite If set to true, an overwrite confirmation will be shown
105106
%End
106107

src/gui/qgsfilewidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ class GUI_EXPORT QgsFileWidget : public QWidget
122122
void setFilter( const QString &filter );
123123

124124
/**
125-
* Sets whether a confirmation to overwrite an existing file will appear
125+
* Sets whether a confirmation to overwrite an existing file will appear.
126+
* By default, a confirmation will appear.
126127
* \param confirmOverwrite If set to true, an overwrite confirmation will be shown
127128
*/
128129
void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }

src/gui/qgsrasterlayersaveasdialog.cpp

Lines changed: 86 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ QgsRasterLayerSaveAsDialog::QgsRasterLayerSaveAsDialog( QgsRasterLayer *rasterLa
4444
{
4545
setupUi( this );
4646
connect( mRawModeRadioButton, &QRadioButton::toggled, this, &QgsRasterLayerSaveAsDialog::mRawModeRadioButton_toggled );
47-
connect( mBrowseButton, &QPushButton::clicked, this, &QgsRasterLayerSaveAsDialog::mBrowseButton_clicked );
48-
connect( mSaveAsLineEdit, &QLineEdit::textChanged, this, &QgsRasterLayerSaveAsDialog::mSaveAsLineEdit_textChanged );
4947
connect( mFormatComboBox, static_cast<void ( QComboBox::* )( const QString & )>( &QComboBox::currentIndexChanged ), this, &QgsRasterLayerSaveAsDialog::mFormatComboBox_currentIndexChanged );
5048
connect( mResolutionRadioButton, &QRadioButton::toggled, this, &QgsRasterLayerSaveAsDialog::mResolutionRadioButton_toggled );
5149
connect( mOriginalResolutionPushButton, &QPushButton::clicked, this, &QgsRasterLayerSaveAsDialog::mOriginalResolutionPushButton_clicked );
@@ -161,6 +159,59 @@ QgsRasterLayerSaveAsDialog::QgsRasterLayerSaveAsDialog( QgsRasterLayer *rasterLa
161159

162160
QgsSettings settings;
163161
restoreGeometry( settings.value( QStringLiteral( "Windows/RasterLayerSaveAs/geometry" ) ).toByteArray() );
162+
163+
if ( mTileModeCheckBox->isChecked() )
164+
{
165+
mFilename->setStorageMode( QgsFileWidget::GetDirectory );
166+
mFilename->setDialogTitle( tr( "Select output directory" ) );
167+
}
168+
else
169+
{
170+
mFilename->setStorageMode( QgsFileWidget::SaveFile );
171+
mFilename->setDialogTitle( tr( "Select output file" ) );
172+
}
173+
mFilename->setDefaultRoot( settings.value( QStringLiteral( "UI/lastRasterFileDir" ), QDir::homePath() ).toString() );
174+
connect( mFilename, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
175+
{
176+
QgsSettings settings;
177+
QFileInfo tmplFileInfo( filePath );
178+
settings.setValue( QStringLiteral( "UI/lastRasterFileDir" ), tmplFileInfo.absolutePath() );
179+
180+
if ( mTileModeCheckBox->isChecked() )
181+
{
182+
QString fileName = filePath;
183+
Q_FOREVER
184+
{
185+
// TODO: would not it be better to select .vrt file instead of directory?
186+
//fileName = QFileDialog::getSaveFileName( this, tr( "Select output file" ), QString(), tr( "VRT" ) + " (*.vrt *.VRT)" );
187+
if ( fileName.isEmpty() )
188+
break; // canceled
189+
190+
// Check if directory is empty
191+
QDir dir( fileName );
192+
QString baseName = QFileInfo( fileName ).baseName();
193+
QStringList filters;
194+
filters << QStringLiteral( "%1.*" ).arg( baseName );
195+
QStringList files = dir.entryList( filters );
196+
if ( files.isEmpty() )
197+
break;
198+
199+
if ( QMessageBox::warning( this, tr( "Warning" ),
200+
tr( "The directory %1 contains files which will be overwritten: %2" ).arg( dir.absolutePath(), files.join( QStringLiteral( ", " ) ) ),
201+
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Ok )
202+
break;
203+
204+
fileName = QFileDialog::getExistingDirectory( this, tr( "Select output directory" ), tmplFileInfo.absolutePath() );
205+
}
206+
}
207+
208+
QPushButton *okButton = mButtonBox->button( QDialogButtonBox::Ok );
209+
if ( !okButton )
210+
{
211+
return;
212+
}
213+
okButton->setEnabled( tmplFileInfo.absoluteDir().exists() );
214+
} );
164215
}
165216

166217
QgsRasterLayerSaveAsDialog::~QgsRasterLayerSaveAsDialog()
@@ -241,84 +292,6 @@ void QgsRasterLayerSaveAsDialog::setValidators()
241292
mMaximumSizeYLineEdit->setValidator( new QIntValidator( this ) );
242293
}
243294

244-
void QgsRasterLayerSaveAsDialog::mBrowseButton_clicked()
245-
{
246-
QString fileName;
247-
248-
QgsSettings settings;
249-
QString dirName = mSaveAsLineEdit->text().isEmpty() ? settings.value( QStringLiteral( "UI/lastRasterFileDir" ), QDir::homePath() ).toString() : mSaveAsLineEdit->text();
250-
251-
if ( mTileModeCheckBox->isChecked() )
252-
{
253-
Q_FOREVER
254-
{
255-
// TODO: would not it be better to select .vrt file instead of directory?
256-
fileName = QFileDialog::getExistingDirectory( this, tr( "Select output directory" ), dirName );
257-
//fileName = QFileDialog::getSaveFileName( this, tr( "Select output file" ), QString(), tr( "VRT" ) + " (*.vrt *.VRT)" );
258-
259-
if ( fileName.isEmpty() )
260-
break; // canceled
261-
262-
// Check if directory is empty
263-
QDir dir( fileName );
264-
QString baseName = QFileInfo( fileName ).baseName();
265-
QStringList filters;
266-
filters << QStringLiteral( "%1.*" ).arg( baseName );
267-
QStringList files = dir.entryList( filters );
268-
if ( files.isEmpty() )
269-
break;
270-
271-
if ( QMessageBox::warning( this, tr( "Warning" ),
272-
tr( "The directory %1 contains files which will be overwritten: %2" ).arg( dir.absolutePath(), files.join( QStringLiteral( ", " ) ) ),
273-
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Ok )
274-
break;
275-
276-
fileName.clear();
277-
}
278-
}
279-
else
280-
{
281-
QStringList extensions = QgsRasterFileWriter::extensionsForFormat( outputFormat() );
282-
QString filter;
283-
QString defaultExt;
284-
if ( extensions.empty() )
285-
filter = tr( "All files (*.*)" );
286-
else
287-
{
288-
filter = QStringLiteral( "%1 (*.%2);;%3" ).arg( mFormatComboBox->currentText(),
289-
extensions.join( QStringLiteral( " *." ) ),
290-
tr( "All files (*.*)" ) );
291-
defaultExt = extensions.at( 0 );
292-
}
293-
294-
fileName = QFileDialog::getSaveFileName( this, tr( "Select output file" ), dirName, filter );
295-
296-
// ensure the user never omits the extension from the file name
297-
QFileInfo fi( fileName );
298-
if ( !fileName.isEmpty() && fi.suffix().isEmpty() )
299-
{
300-
fileName += '.' + defaultExt;
301-
}
302-
}
303-
304-
if ( !fileName.isEmpty() )
305-
{
306-
mSaveAsLineEdit->setText( fileName );
307-
}
308-
}
309-
310-
void QgsRasterLayerSaveAsDialog::mSaveAsLineEdit_textChanged( const QString &text )
311-
{
312-
QPushButton *okButton = mButtonBox->button( QDialogButtonBox::Ok );
313-
if ( !okButton )
314-
{
315-
return;
316-
}
317-
318-
okButton->setEnabled( QFileInfo( text ).absoluteDir().exists() );
319-
}
320-
321-
322295
void QgsRasterLayerSaveAsDialog::mFormatComboBox_currentIndexChanged( const QString & )
323296
{
324297
//gdal-specific
@@ -327,6 +300,18 @@ void QgsRasterLayerSaveAsDialog::mFormatComboBox_currentIndexChanged( const QStr
327300
mCreateOptionsWidget->setFormat( outputFormat() );
328301
mCreateOptionsWidget->update();
329302
}
303+
304+
QStringList extensions = QgsRasterFileWriter::extensionsForFormat( outputFormat() );
305+
QString filter;
306+
if ( extensions.empty() )
307+
filter = tr( "All files (*.*)" );
308+
else
309+
{
310+
filter = QStringLiteral( "%1 (*.%2);;%3" ).arg( mFormatComboBox->currentText(),
311+
extensions.join( QStringLiteral( " *." ) ),
312+
tr( "All files (*.*)" ) );
313+
}
314+
mFilename->setFilter( filter );
330315
}
331316

332317
int QgsRasterLayerSaveAsDialog::nColumns() const
@@ -371,7 +356,22 @@ bool QgsRasterLayerSaveAsDialog::addToCanvas() const
371356

372357
QString QgsRasterLayerSaveAsDialog::outputFileName() const
373358
{
374-
return mSaveAsLineEdit->text();
359+
QStringList extensions = QgsRasterFileWriter::extensionsForFormat( outputFormat() );
360+
QString defaultExt;
361+
if ( !extensions.empty() )
362+
{
363+
defaultExt = extensions.at( 0 );
364+
}
365+
366+
// ensure the user never omits the extension from the file name
367+
QString fileName = mFilename->filePath();
368+
QFileInfo fi( fileName );
369+
if ( !fileName.isEmpty() && fi.suffix().isEmpty() )
370+
{
371+
fileName += '.' + defaultExt;
372+
}
373+
374+
return fileName;
375375
}
376376

377377
QString QgsRasterLayerSaveAsDialog::outputFormat() const
@@ -398,8 +398,7 @@ void QgsRasterLayerSaveAsDialog::hideFormat()
398398
void QgsRasterLayerSaveAsDialog::hideOutput()
399399
{
400400
mSaveAsLabel->hide();
401-
mSaveAsLineEdit->hide();
402-
mBrowseButton->hide();
401+
mFilename->hide();
403402
QPushButton *okButton = mButtonBox->button( QDialogButtonBox::Ok );
404403
if ( okButton )
405404
{
@@ -710,10 +709,14 @@ void QgsRasterLayerSaveAsDialog::mTileModeCheckBox_toggled( bool toggled )
710709

711710
// Show / hide tile options
712711
mTilesGroupBox->show();
712+
mFilename->setStorageMode( QgsFileWidget::GetDirectory );
713+
mFilename->setDialogTitle( tr( "Select output directory" ) );
713714
}
714715
else
715716
{
716717
mTilesGroupBox->hide();
718+
mFilename->setStorageMode( QgsFileWidget::SaveFile );
719+
mFilename->setDialogTitle( tr( "Select output file" ) );
717720
}
718721
}
719722

src/gui/qgsrasterlayersaveasdialog.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ class GUI_EXPORT QgsRasterLayerSaveAsDialog: public QDialog, private Ui::QgsRast
9090

9191
private slots:
9292
void mRawModeRadioButton_toggled( bool );
93-
void mBrowseButton_clicked();
94-
void mSaveAsLineEdit_textChanged( const QString &text );
9593
void mFormatComboBox_currentIndexChanged( const QString &text );
9694
void mResolutionRadioButton_toggled( bool ) { toggleResolutionSize(); }
9795
void mOriginalResolutionPushButton_clicked() { setOriginalResolution(); }

src/ui/qgsrasterlayersaveasdialogbase.ui

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,15 @@ datasets with maximum width and height specified below.</string>
107107
<string>Save as</string>
108108
</property>
109109
<property name="buddy">
110-
<cstring>mSaveAsLineEdit</cstring>
110+
<cstring>mFilename</cstring>
111111
</property>
112112
</widget>
113113
</item>
114114
<item>
115-
<widget class="QLineEdit" name="mSaveAsLineEdit">
115+
<widget class="QgsFileWidget" name="mFilename">
116116
<property name="enabled">
117117
<bool>true</bool>
118118
</property>
119-
<property name="text">
120-
<string/>
121-
</property>
122-
</widget>
123-
</item>
124-
<item>
125-
<widget class="QPushButton" name="mBrowseButton">
126-
<property name="enabled">
127-
<bool>true</bool>
128-
</property>
129-
<property name="text">
130-
<string>Browse...</string>
131-
</property>
132119
</widget>
133120
</item>
134121
</layout>
@@ -717,14 +704,19 @@ datasets with maximum width and height specified below.</string>
717704
<header>qgsrasterpyramidsoptionswidget.h</header>
718705
<container>1</container>
719706
</customwidget>
707+
<customwidget>
708+
<class>QgsFileWidget</class>
709+
<extends>QWidget</extends>
710+
<header>qgsfilewidget.h</header>
711+
<container>1</container>
712+
</customwidget>
720713
</customwidgets>
721714
<tabstops>
722715
<tabstop>mRawModeRadioButton</tabstop>
723716
<tabstop>mRenderedModeRadioButton</tabstop>
724717
<tabstop>mFormatComboBox</tabstop>
725718
<tabstop>mTileModeCheckBox</tabstop>
726-
<tabstop>mSaveAsLineEdit</tabstop>
727-
<tabstop>mBrowseButton</tabstop>
719+
<tabstop>mFilename</tabstop>
728720
<tabstop>mCrsSelector</tabstop>
729721
<tabstop>mAddToCanvas</tabstop>
730722
<tabstop>mScrollArea</tabstop>

0 commit comments

Comments
 (0)