Skip to content

Commit bfd69a0

Browse files
jef-nalexbruy
authored andcommitted
use native file dialogs wherever possible (fixes #3763)
1 parent 9565677 commit bfd69a0

File tree

6 files changed

+190
-385
lines changed

6 files changed

+190
-385
lines changed

src/app/composer/qgscomposer.cpp

Lines changed: 40 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -529,35 +529,26 @@ class QgsPaintEngineHack : public QPaintEngine
529529
void QgsComposer::on_mActionExportAsPDF_triggered()
530530
{
531531
QSettings myQSettings; // where we keep last used filter in persistent state
532-
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
533-
QFileInfo file( myLastUsedFile );
534-
QFileDialog *myQFileDialog = new QFileDialog( this, tr( "Choose a file name to save the map as" ),
535-
file.path(), tr( "PDF Format" ) + " (*.pdf *PDF)" );
536-
myQFileDialog->selectFile( file.fileName() );
537-
myQFileDialog->setFileMode( QFileDialog::AnyFile );
538-
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
539-
540-
int result = myQFileDialog->exec();
541-
raise();
542-
if ( result != QDialog::Accepted ) return;
532+
QString lastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
533+
QFileInfo file( lastUsedFile );
543534

544-
QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
545-
if ( myOutputFileNameQString == "" )
546-
{
547-
return;
548-
}
535+
QString outputFileName = QFileDialog::getSaveFileName(
536+
this,
537+
tr( "Choose a file name to save the map as" ),
538+
file.path(),
539+
tr( "PDF Format" ) + " (*.pdf *.PDF)" );
549540

550-
if ( !myOutputFileNameQString.endsWith( ".pdf", Qt::CaseInsensitive ) )
541+
if ( !outputFileName.endsWith( ".pdf", Qt::CaseInsensitive ) )
551542
{
552-
myOutputFileNameQString.append( ".pdf" );
543+
outputFileName += ".pdf";
553544
}
554545

555-
myQSettings.setValue( "/UI/lastSaveAsPdfFile", myOutputFileNameQString );
546+
myQSettings.setValue( "/UI/lastSaveAsPdfFile", outputFileName );
556547

557548
QPrinter printer;
558549

559550
printer.setOutputFormat( QPrinter::PdfFormat );
560-
printer.setOutputFileName( myOutputFileNameQString );
551+
printer.setOutputFileName( outputFileName );
561552
printer.setPaperSize( QSizeF( mComposition->paperWidth(), mComposition->paperHeight() ), QPrinter::Millimeter );
562553

563554
QPaintEngine *engine = printer.paintEngine();
@@ -690,93 +681,16 @@ void QgsComposer::on_mActionExportAsImage_triggered()
690681
return;
691682
}
692683

693-
// Get file and format (stolen from qgisapp.cpp but modified significantely)
694-
695-
//create a map to hold the QImageIO names and the filter names
696-
//the QImageIO name must be passed to the mapcanvas saveas image function
697-
typedef QMap<QString, QString> FilterMap;
698-
FilterMap myFilterMap;
699-
700-
//find out the last used filter
701-
QSettings myQSettings; // where we keep last used filter in persistent state
702-
QString myLastUsedFormat = myQSettings.value( "/UI/lastSaveAsImageFormat", "png" ).toString();
703-
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsImageFile", "qgis.png" ).toString();
704-
QFileInfo file( myLastUsedFile );
705-
706-
// get a list of supported output image types
707-
int myCounterInt = 0;
708-
QString myFilters;
709-
QString myLastUsedFilter;
710-
for ( ; myCounterInt < QImageWriter::supportedImageFormats().count(); myCounterInt++ )
711-
{
712-
QString myFormat = QString( QImageWriter::supportedImageFormats().at( myCounterInt ) );
713-
QString myFilter = tr( "%1 format (*.%2 *.%3)" )
714-
.arg( myFormat ).arg( myFormat.toLower() ).arg( myFormat.toUpper() );
715-
716-
if ( myCounterInt > 0 ) myFilters += ";;";
717-
myFilters += myFilter;
718-
myFilterMap[myFilter] = myFormat;
719-
if ( myFormat == myLastUsedFormat )
720-
{
721-
myLastUsedFilter = myFilter;
722-
}
723-
}
724-
#ifdef QGISDEBUG
725-
QgsDebugMsg( "Available Filters Map: " );
726-
FilterMap::Iterator myIterator;
727-
for ( myIterator = myFilterMap.begin(); myIterator != myFilterMap.end(); ++myIterator )
728-
{
729-
QgsDebugMsg( QString( "%1 : %2" ).arg( myIterator.key() ).arg( myIterator.value() ) );
730-
}
731-
#endif
732-
733-
//create a file dialog using the the filter list generated above
734-
std::auto_ptr < QFileDialog > myQFileDialog(
735-
new QFileDialog(
736-
this,
737-
tr( "Choose a file name to save the map image as" ),
738-
file.path(),
739-
myFilters
740-
)
741-
);
742-
743-
myQFileDialog->setFileMode( QFileDialog::AnyFile );
744-
745-
// set the filter to the last one used
746-
myQFileDialog->selectFilter( myLastUsedFilter );
684+
QPair<QString, QString> fileNExt = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
747685

748-
// set the 'Open' button to something that makes more sense
749-
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
686+
QgsDebugMsg( QString( "Selected filter: %1" ).arg( fileNExt.first ) );
687+
QgsDebugMsg( QString( "Image type: %1" ).arg( fileNExt.second ) );
750688

751-
//prompt the user for a file name
752-
QString myOutputFileNameQString;
753-
754-
int result = myQFileDialog->exec();
755-
//raise();
756-
757-
if ( result != QDialog::Accepted )
689+
if ( fileNExt.first.isEmpty() )
758690
{
759691
return;
760692
}
761693

762-
myOutputFileNameQString = myQFileDialog->selectedFiles().last();
763-
QgsDebugMsg( myOutputFileNameQString );
764-
QString myFilterString = myQFileDialog->selectedFilter();
765-
QgsDebugMsg( QString( "Selected filter: %1" ).arg( myFilterString ) );
766-
QgsDebugMsg( QString( "Image type: %1" ).arg( myFilterMap[myFilterString] ) );
767-
768-
// Add the file type suffix to the fileName if required
769-
if ( !myOutputFileNameQString.endsWith( myFilterMap[myFilterString] ) )
770-
{
771-
myOutputFileNameQString += "." + myFilterMap[myFilterString];
772-
}
773-
774-
myQSettings.setValue( "/UI/lastSaveAsImageFormat", myFilterMap[myFilterString] );
775-
myQSettings.setValue( "/UI/lastSaveAsImageFile", myOutputFileNameQString );
776-
777-
if ( myOutputFileNameQString == "" )
778-
return;
779-
780694
QImage image( QSize( width, height ), QImage::Format_ARGB32 );
781695
if ( image.isNull() )
782696
{
@@ -800,7 +714,7 @@ void QgsComposer::on_mActionExportAsImage_triggered()
800714
p.end();
801715
mComposition->setPlotStyle( QgsComposition::Preview );
802716
mView->setPaintingEnabled( true );
803-
image.save( myOutputFileNameQString, myFilterMap[myFilterString].toLocal8Bit().data() );
717+
image.save( fileNExt.first, fileNExt.second.toLocal8Bit().constData() );
804718
}
805719

806720

@@ -811,10 +725,10 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
811725
showWMSPrintingWarning();
812726
}
813727

814-
QString myQSettingsLabel = "/UI/displaySVGWarning";
815-
QSettings myQSettings;
728+
QString settingsLabel = "/UI/displaySVGWarning";
729+
QSettings settings;
816730

817-
bool displaySVGWarning = myQSettings.value( myQSettingsLabel, true ).toBool();
731+
bool displaySVGWarning = settings.value( settingsLabel, true ).toBool();
818732

819733
if ( displaySVGWarning )
820734
{
@@ -823,7 +737,7 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
823737
m->setCheckBoxText( tr( "Don't show this message again" ) );
824738
m->setCheckBoxState( Qt::Unchecked );
825739
m->setCheckBoxVisible( true );
826-
m->setCheckBoxQSettingsLabel( myQSettingsLabel );
740+
m->setCheckBoxQSettingsLabel( settingsLabel );
827741
m->setMessageAsHtml( tr( "<p>The SVG export function in Qgis has several "
828742
"problems due to bugs and deficiencies in the " )
829743
+ tr( "Qt4 svg code. In particular, there are problems "
@@ -836,37 +750,35 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
836750
"</p>" ) );
837751
m->exec();
838752
}
839-
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsSvgFile", "qgis.svg" ).toString();
840-
QFileInfo file( myLastUsedFile );
841-
QFileDialog *myQFileDialog = new QFileDialog( this, tr( "Choose a file name to save the map as" ),
842-
file.path(), tr( "SVG Format" ) + " (*.svg *SVG)" );
843-
myQFileDialog->selectFile( file.fileName() );
844-
myQFileDialog->setFileMode( QFileDialog::AnyFile );
845-
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
846753

847-
int result = myQFileDialog->exec();
848-
raise();
849-
if ( result != QDialog::Accepted ) return;
754+
QString lastUsedFile = settings.value( "/UI/lastSaveAsSvgFile", "qgis.svg" ).toString();
755+
QFileInfo file( lastUsedFile );
756+
757+
QString outputFileName = QFileDialog::getSaveFileName(
758+
this,
759+
tr( "Choose a file name to save the map as" ),
760+
file.path(),
761+
tr( "SVG Format" ) + " (*.svg *.SVG)" );
850762

851-
QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
852-
if ( myOutputFileNameQString == "" )
763+
if ( outputFileName.isEmpty() )
853764
{
854765
return;
855766
}
856767

857-
if ( !myOutputFileNameQString.endsWith( ".svg", Qt::CaseInsensitive ) )
768+
769+
if ( !outputFileName.endsWith( ".svg", Qt::CaseInsensitive ) )
858770
{
859-
myOutputFileNameQString.append( ".svg" );
771+
outputFileName += ".svg" ;
860772
}
861773

862-
myQSettings.setValue( "/UI/lastSaveAsSvgFile", myOutputFileNameQString );
774+
settings.setValue( "/UI/lastSaveAsSvgFile", outputFileName );
863775
mComposition->setPlotStyle( QgsComposition::Print );
864776

865777
QSvgGenerator generator;
866778
#if QT_VERSION >= 0x040500
867779
generator.setTitle( QgsProject::instance()->title() );
868780
#endif
869-
generator.setFileName( myOutputFileNameQString );
781+
generator.setFileName( outputFileName );
870782
//width in pixel
871783
int width = ( int )( mComposition->paperWidth() * mComposition->printResolution() / 25.4 );
872784
//height in pixel
@@ -965,7 +877,12 @@ void QgsComposer::on_mActionSaveAsTemplate_triggered()
965877
//show file dialog
966878
QSettings settings;
967879
QString lastSaveDir = settings.value( "UI/lastComposerTemplateDir", "" ).toString();
968-
QString saveFileName = QFileDialog::getSaveFileName( 0, tr( "save template" ), lastSaveDir, "*.qpt" );
880+
QString saveFileName = QFileDialog::getSaveFileName(
881+
this,
882+
tr( "Save template" ),
883+
lastSaveDir,
884+
tr( "Composer templates" ) + " (*.qpt *.QPT)" );
885+
969886
if ( saveFileName.isEmpty() )
970887
{
971888
return;

src/app/qgisapp.cpp

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ void QgisApp::fileOpen()
26162616
// Retrieve last used project dir from persistent settings
26172617
QSettings settings;
26182618
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
2619-
QString fullPath = QFileDialog::getOpenFileName( this, tr( "Choose a QGIS project file to open" ), lastUsedDir, tr( "QGis files (*.qgs)" ) );
2619+
QString fullPath = QFileDialog::getOpenFileName( this, tr( "Choose a QGIS project file to open" ), lastUsedDir, tr( "QGis files (*.qgs *.QGS)" ) );
26202620
if ( fullPath.isNull() )
26212621
{
26222622
return;
@@ -2755,31 +2755,21 @@ bool QgisApp::fileSave()
27552755
QSettings settings;
27562756
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
27572757

2758-
std::auto_ptr<QFileDialog> saveFileDialog( new QFileDialog( this,
2759-
tr( "Choose a QGIS project file" ),
2760-
lastUsedDir, tr( "QGis files (*.qgs)" ) ) );
2758+
QString path = QFileDialog::getSaveFileName(
2759+
this,
2760+
tr( "Choose a QGIS project file" ),
2761+
lastUsedDir + "/" + QgsProject::instance()->title(),
2762+
tr( "QGis files (*.qgs *.QGS)" ) );
27612763

2762-
saveFileDialog->setFileMode( QFileDialog::AnyFile );
2763-
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
2764-
saveFileDialog->setConfirmOverwrite( true );
2765-
saveFileDialog->selectFile( QgsProject::instance()->title() );
2764+
if ( path.isEmpty() )
2765+
return true;
27662766

2767-
if ( saveFileDialog->exec() == QDialog::Accepted )
2768-
{
2769-
fullPath.setFile( saveFileDialog->selectedFiles().first() );
2770-
}
2771-
else
2772-
{
2773-
// if they didn't select anything, just return
2774-
// delete saveFileDialog; auto_ptr auto destroys
2775-
return false;
2776-
}
2767+
QFileInfo fullPath( path );
27772768

27782769
// make sure we have the .qgs extension in the file name
2779-
if ( "qgs" != fullPath.suffix() )
2770+
if ( "qgs" != fullPath.suffix().toLower() )
27802771
{
2781-
QString newFilePath = fullPath.filePath() + ".qgs";
2782-
fullPath.setFile( newFilePath );
2772+
fullPath.setFile( fullPath.filePath() + ".qgs" );
27832773
}
27842774

27852775

@@ -2819,33 +2809,18 @@ void QgisApp::fileSaveAs()
28192809
QSettings settings;
28202810
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
28212811

2822-
std::auto_ptr<QFileDialog> saveFileDialog( new QFileDialog( this,
2823-
tr( "Choose a file name to save the QGIS project file as" ),
2824-
lastUsedDir, tr( "QGis files (*.qgs)" ) ) );
2825-
saveFileDialog->setFileMode( QFileDialog::AnyFile );
2826-
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
2827-
saveFileDialog->setConfirmOverwrite( true );
2828-
saveFileDialog->selectFile( QgsProject::instance()->title() );
2829-
2830-
QFileInfo fullPath;
2831-
if ( saveFileDialog->exec() == QDialog::Accepted )
2832-
{
2833-
//saveFilePath = saveFileDialog->selectedFiles().first();
2834-
fullPath.setFile( saveFileDialog->selectedFiles().first() );
2835-
}
2836-
else
2837-
{
2812+
QString path = QFileDialog::getSaveFileName( this, tr( "Choose a file name to save the QGIS project file as" ), lastUsedDir + "/" + QgsProject::instance()->title(), tr( "QGis files (*.qgs *.QGS)" ) );
2813+
if ( path.isEmpty() )
28382814
return;
2839-
}
28402815

2841-
QString myPath = fullPath.path();
2842-
settings.setValue( "/UI/lastProjectDir", myPath );
2816+
QFileInfo fullPath( path );
2817+
settings.setValue( "/UI/lastProjectDir", fullPath.path() );
2818+
28432819

28442820
// make sure the .qgs extension is included in the path name. if not, add it...
2845-
if ( "qgs" != fullPath.suffix() )
2821+
if ( "qgs" != fullPath.suffix().toLower() )
28462822
{
2847-
myPath = fullPath.filePath() + ".qgs";
2848-
fullPath.setFile( myPath );
2823+
fullPath.setFile( fullPath.filePath() + ".qgs" );
28492824
}
28502825

28512826
QgsProject::instance()->setFileName( fullPath.filePath() );

0 commit comments

Comments
 (0)