24 #include <QFileDialog>
25 #include <QMessageBox>
31 QWidget* parent, Qt::WindowFlags f ) :
33 , mRasterLayer( rasterLayer ), mDataProvider( sourceProvider )
34 , mCurrentExtent( currentExtent ), mLayerCrs( layerCrs )
35 , mCurrentCrs( currentCrs )
36 , mResolutionState( OriginalResolution )
44 mNoDataTableWidget->setColumnCount( 2 );
45 mNoDataTableWidget->setHorizontalHeaderItem( 0,
new QTableWidgetItem(
tr(
"From" ) ) );
46 mNoDataTableWidget->setHorizontalHeaderItem( 1,
new QTableWidgetItem(
tr(
"To" ) ) );
48 on_mRawModeRadioButton_toggled(
true );
52 toggleResolutionSize();
55 QStringList myFormats;
57 foreach ( QString myFormat, myFormats )
59 mFormatComboBox->addItem( myFormat );
67 setOriginalResolution();
68 int xSize = mDataProvider->
xSize();
69 int ySize = mDataProvider->
ySize();
70 mMaximumSizeXLineEdit->setText( QString::number( xSize ) );
71 mMaximumSizeYLineEdit->setText( QString::number( ySize ) );
75 mTileModeCheckBox->setChecked(
true );
76 mMaximumSizeXLineEdit->setText( QString::number( 2000 ) );
77 mMaximumSizeYLineEdit->setText( QString::number( 2000 ) );
81 mCreateOptionsWidget->setProvider( mDataProvider->
name() );
82 if ( mDataProvider->
name() ==
"gdal" )
84 mCreateOptionsWidget->setFormat( myFormats[0] );
86 mCreateOptionsWidget->setRasterLayer( mRasterLayer );
87 mCreateOptionsWidget->update();
95 mPyramidsOptionsWidget->createOptionsWidget()->setRasterLayer( mRasterLayer );
100 mPyramidsUseExistingCheckBox->setEnabled(
false );
101 mPyramidsUseExistingCheckBox->setVisible(
false );
103 populatePyramidsLevels();
104 connect( mPyramidsOptionsWidget, SIGNAL( overviewListChanged() ),
105 this, SLOT( populatePyramidsLevels() ) );
109 mPyramidsGroupBox->setEnabled(
false );
114 mCreateOptionsGroupBox->setSaveCheckedState(
true );
119 mTilesGroupBox->hide();
121 mCrsSelector->setLayerCrs( mLayerCrs );
122 mCrsSelector->setCrs( mCurrentCrs );
124 this, SLOT( crsChanged() ) );
126 QPushButton* okButton = mButtonBox->button( QDialogButtonBox::Ok );
129 okButton->setEnabled(
false );
132 mExtentGroupBox->setOutputCrs(
outputCrs() );
133 mExtentGroupBox->setOriginalExtent( mDataProvider->
extent(), mLayerCrs );
134 mExtentGroupBox->setCurrentExtent( mCurrentExtent, mCurrentCrs );
135 mExtentGroupBox->setOutputExtentFromOriginal();
136 connect( mExtentGroupBox, SIGNAL( extentChanged(
QgsRectangle ) ),
this, SLOT( extentChanged() ) );
138 recalcResolutionSize();
141 void QgsRasterLayerSaveAsDialog::setValidators()
143 mXResolutionLineEdit->setValidator(
new QDoubleValidator(
this ) );
144 mYResolutionLineEdit->setValidator(
new QDoubleValidator(
this ) );
145 mColumnsLineEdit->setValidator(
new QIntValidator(
this ) );
146 mRowsLineEdit->setValidator(
new QIntValidator(
this ) );
147 mMaximumSizeXLineEdit->setValidator(
new QIntValidator(
this ) );
148 mMaximumSizeYLineEdit->setValidator(
new QIntValidator(
this ) );
155 void QgsRasterLayerSaveAsDialog::on_mBrowseButton_clicked()
160 QString dirName = mSaveAsLineEdit->text().isEmpty() ? settings.value(
"/UI/lastRasterFileDir",
"." ).toString() : mSaveAsLineEdit->text();
162 if ( mTileModeCheckBox->isChecked() )
167 fileName = QFileDialog::getExistingDirectory(
this,
tr(
"Select output directory" ), dirName );
170 if ( fileName.isEmpty() )
break;
173 QDir dir( fileName );
174 QString baseName = QFileInfo( fileName ).baseName();
176 filters << QString(
"%1.*" ).arg( baseName );
177 QStringList files = dir.entryList( filters );
178 if ( !files.isEmpty() )
180 QMessageBox::StandardButton button = QMessageBox::warning(
this,
tr(
"Warning" ),
181 tr(
"The directory %1 contains files which will be overwritten: %2" ).arg( dir.absolutePath() ).arg( files.join(
", " ) ),
182 QMessageBox::Ok | QMessageBox::Cancel );
184 if ( button == QMessageBox::Ok )
201 fileName = QFileDialog::getSaveFileName(
this,
tr(
"Select output file" ), dirName,
tr(
"GeoTIFF" ) +
" (*.tif *.tiff *.TIF *.TIFF)" );
204 if ( !fileName.isEmpty() )
206 mSaveAsLineEdit->setText( fileName );
210 void QgsRasterLayerSaveAsDialog::on_mSaveAsLineEdit_textChanged(
const QString& text )
212 QPushButton* okButton = mButtonBox->button( QDialogButtonBox::Ok );
218 okButton->setEnabled( QFileInfo( text ).absoluteDir().exists() );
222 void QgsRasterLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged(
const QString & text )
225 if ( mDataProvider && mDataProvider->
name() ==
"gdal" )
227 mCreateOptionsWidget->setFormat( text );
228 mCreateOptionsWidget->update();
234 return mColumnsLineEdit->text().toInt();
239 return mRowsLineEdit->text().toInt();
244 return mXResolutionLineEdit->text().toDouble();
249 return mYResolutionLineEdit->text().toDouble();
254 return mMaximumSizeXLineEdit->text().toInt();
259 return mMaximumSizeYLineEdit->text().toInt();
264 return mTileModeCheckBox->isChecked();
269 return mSaveAsLineEdit->text();
274 return mFormatComboBox->currentText();
279 return mCreateOptionsGroupBox->isChecked() ? mCreateOptionsWidget->options() : QStringList();
284 return mExtentGroupBox->outputExtent();
289 mFormatLabel->hide();
290 mFormatComboBox->hide();
295 mSaveAsLabel->hide();
296 mSaveAsLineEdit->hide();
297 mBrowseButton->hide();
298 QPushButton* okButton = mButtonBox->button( QDialogButtonBox::Ok );
301 okButton->setEnabled(
true );
305 void QgsRasterLayerSaveAsDialog::toggleResolutionSize()
309 bool on = mResolutionRadioButton->isChecked();
310 mXResolutionLineEdit->setEnabled( on );
311 mYResolutionLineEdit->setEnabled( on );
312 mOriginalResolutionPushButton->setEnabled( on && hasResolution );
313 mColumnsLineEdit->setEnabled( !on );
314 mRowsLineEdit->setEnabled( !on );
315 mOriginalSizePushButton->setEnabled( !on && hasResolution );
318 void QgsRasterLayerSaveAsDialog::setOriginalResolution()
330 xRes = yRes = mDataProvider->
extent().
width() / 100;
332 setResolution( xRes, yRes, mLayerCrs );
350 QgsRectangle srcExtent( srsCenter.
x() - xRes / 2, srsCenter.
y() - yRes / 2, srsCenter.
x() + xRes / 2, srsCenter.
y() + yRes / 2 );
353 xRes = extent.
width();
354 yRes = extent.height();
356 mXResolutionLineEdit->setText( QString::number( xRes ) );
357 mYResolutionLineEdit->setText( QString::number( yRes ) );
360 void QgsRasterLayerSaveAsDialog::recalcSize()
366 mColumnsLineEdit->setText( QString::number( xSize ) );
367 mRowsLineEdit->setText( QString::number( ySize ) );
368 updateResolutionStateMsg();
371 void QgsRasterLayerSaveAsDialog::setOriginalSize()
373 mColumnsLineEdit->setText( QString::number( mDataProvider->
xSize() ) );
374 mRowsLineEdit->setText( QString::number( mDataProvider->
ySize() ) );
378 void QgsRasterLayerSaveAsDialog::recalcResolution()
384 mXResolutionLineEdit->setText( QString::number( xRes ) );
385 mYResolutionLineEdit->setText( QString::number( yRes ) );
386 updateResolutionStateMsg();
389 void QgsRasterLayerSaveAsDialog::recalcResolutionSize()
392 if ( mResolutionRadioButton->isChecked() )
403 void QgsRasterLayerSaveAsDialog::updateResolutionStateMsg()
406 switch ( mResolutionState )
412 msg =
tr(
"user defined" );
417 msg =
tr(
"Resolution (current: %1)" ).arg( msg );
418 mResolutionGroupBox->setTitle( msg );
421 void QgsRasterLayerSaveAsDialog::extentChanged()
424 if ( mSizeRadioButton->isChecked() )
428 recalcResolutionSize();
431 void QgsRasterLayerSaveAsDialog::crsChanged()
435 mExtentGroupBox->setOutputCrs(
outputCrs() );
443 mExtentGroupBox->setOutputExtentFromOriginal();
447 mExtentGroupBox->setOutputExtentFromCurrent();
451 mExtentGroupBox->setOutputExtentFromUser( mExtentGroupBox->outputExtent(), mPreviousCrs );
455 if ( mResolutionRadioButton->isChecked() )
459 setOriginalResolution();
478 return mCrsSelector->crs();
487 void QgsRasterLayerSaveAsDialog::on_mRawModeRadioButton_toggled(
bool checked )
489 mNoDataGroupBox->setEnabled( checked && mDataProvider->
bandCount() == 1 );
492 void QgsRasterLayerSaveAsDialog::on_mAddNoDataManuallyToolButton_clicked()
494 addNoDataRow( std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN() );
497 void QgsRasterLayerSaveAsDialog::on_mLoadTransparentNoDataToolButton_clicked()
499 if ( !mRasterLayer->
renderer() )
return;
501 if ( !rasterTransparency )
return;
507 addNoDataRow( transparencyPixel.
min, transparencyPixel.
max );
508 if ( transparencyPixel.
min != transparencyPixel.
max )
510 setNoDataToEdited( mNoDataTableWidget->rowCount() - 1 );
516 void QgsRasterLayerSaveAsDialog::on_mRemoveSelectedNoDataToolButton_clicked()
518 mNoDataTableWidget->removeRow( mNoDataTableWidget->currentRow() );
521 void QgsRasterLayerSaveAsDialog::on_mRemoveAllNoDataToolButton_clicked()
523 while ( mNoDataTableWidget->rowCount() > 0 )
525 mNoDataTableWidget->removeRow( 0 );
529 void QgsRasterLayerSaveAsDialog::addNoDataRow(
double min,
double max )
531 mNoDataTableWidget->insertRow( mNoDataTableWidget->rowCount() );
532 for (
int i = 0; i < 2; i++ )
534 double value = i == 0 ? min :
max;
535 QLineEdit *lineEdit =
new QLineEdit();
536 lineEdit->setFrame(
false );
537 lineEdit->setContentsMargins( 1, 1, 1, 1 );
543 lineEdit->setValidator(
new QDoubleValidator( 0 ) );
544 if ( !qIsNaN( value ) )
550 lineEdit->setValidator(
new QIntValidator( 0 ) );
551 if ( !qIsNaN( value ) )
553 valueString = QString::number( static_cast<int>( value ) );
557 lineEdit->setText( valueString );
558 mNoDataTableWidget->setCellWidget( mNoDataTableWidget->rowCount() - 1, i, lineEdit );
560 adjustNoDataCellWidth( mNoDataTableWidget->rowCount() - 1, i );
562 connect( lineEdit, SIGNAL( textEdited(
const QString & ) ),
this, SLOT( noDataCellTextEdited(
const QString & ) ) );
564 mNoDataTableWidget->resizeColumnsToContents();
565 mNoDataTableWidget->resizeRowsToContents();
568 void QgsRasterLayerSaveAsDialog::noDataCellTextEdited(
const QString & text )
572 QLineEdit *lineEdit =
dynamic_cast<QLineEdit *
>( sender() );
573 if ( !lineEdit )
return;
576 for (
int r = 0 ; r < mNoDataTableWidget->rowCount(); r++ )
578 for (
int c = 0 ; c < mNoDataTableWidget->columnCount(); c++ )
580 if ( mNoDataTableWidget->cellWidget( r, c ) == sender() )
587 if ( row != -1 )
break;
589 QgsDebugMsg( QString(
"row = %1 column =%2" ).arg( row ).arg( column ) );
593 QLineEdit *toLineEdit =
dynamic_cast<QLineEdit *
>( mNoDataTableWidget->cellWidget( row, 1 ) );
594 if ( !toLineEdit )
return;
595 bool toChanged = mNoDataToEdited.value( row );
596 QgsDebugMsg( QString(
"toChanged = %1" ).arg( toChanged ) );
599 toLineEdit->setText( lineEdit->text() );
602 else if ( column == 1 )
604 setNoDataToEdited( row );
608 void QgsRasterLayerSaveAsDialog::on_mTileModeCheckBox_toggled(
bool toggled )
627 mTilesGroupBox->show();
631 mTilesGroupBox->hide();
635 void QgsRasterLayerSaveAsDialog::on_mPyramidsGroupBox_toggled(
bool toggled )
638 populatePyramidsLevels();
641 void QgsRasterLayerSaveAsDialog::populatePyramidsLevels()
645 if ( mPyramidsGroupBox->isChecked() )
647 QList<QgsRasterPyramid> myPyramidList;
650 if ( mPyramidsUseExistingCheckBox->isChecked() )
656 if ( ! mPyramidsOptionsWidget->overviewList().isEmpty() )
657 myPyramidList = mDataProvider->
buildPyramidList( mPyramidsOptionsWidget->overviewList() );
659 QList<QgsRasterPyramid>::iterator myRasterPyramidIterator;
660 for ( myRasterPyramidIterator = myPyramidList.begin();
661 myRasterPyramidIterator != myPyramidList.end();
662 ++myRasterPyramidIterator )
664 if ( ! mPyramidsUseExistingCheckBox->isChecked() || myRasterPyramidIterator->exists )
666 text += QString::number( myRasterPyramidIterator->xDim ) + QString(
"x" ) +
667 QString::number( myRasterPyramidIterator->yDim ) +
" ";
672 mPyramidResolutionsLineEdit->setText( text.trimmed() );
675 void QgsRasterLayerSaveAsDialog::setNoDataToEdited(
int row )
677 if ( row >= mNoDataToEdited.size() )
679 mNoDataToEdited.resize( row + 1 );
681 mNoDataToEdited[row] =
true;
684 double QgsRasterLayerSaveAsDialog::noDataCellValue(
int row,
int column )
const
686 QLineEdit *lineEdit =
dynamic_cast<QLineEdit *
>( mNoDataTableWidget->cellWidget( row, column ) );
687 if ( !lineEdit || lineEdit->text().isEmpty() )
689 std::numeric_limits<double>::quiet_NaN();
691 return lineEdit->text().toDouble();
694 void QgsRasterLayerSaveAsDialog::adjustNoDataCellWidth(
int row,
int column )
696 QLineEdit *lineEdit =
dynamic_cast<QLineEdit *
>( mNoDataTableWidget->cellWidget( row, column ) );
697 if ( !lineEdit )
return;
699 int width = qMax( lineEdit->fontMetrics().width( lineEdit->text() ) + 10, 100 );
700 width = qMax( width, mNoDataTableWidget->columnWidth( column ) );
702 lineEdit->setFixedWidth( width );
708 if ( ! mNoDataGroupBox->isChecked() )
711 for (
int r = 0 ; r < mNoDataTableWidget->rowCount(); r++ )
714 noDataList.append( noData );
722 return mPyramidsGroupBox->isChecked() ? mPyramidsOptionsWidget->overviewList() : QList<int>();
727 if ( ! mPyramidsGroupBox->isChecked() )
729 else if ( mPyramidsUseExistingCheckBox->isChecked() )
735 bool QgsRasterLayerSaveAsDialog::validate()
const
737 if ( mCreateOptionsGroupBox->isChecked() )
739 QString message = mCreateOptionsWidget->validateOptions(
true,
false );
740 if ( !message.isNull() )
743 if ( mPyramidsGroupBox->isChecked() )
745 QString message = mPyramidsOptionsWidget->createOptionsWidget()->validateOptions(
true,
false );
746 if ( !message.isNull() )