Skip to content

Commit f749a5e

Browse files
committed
use native file dialogs wherever possible (fixes #3763)
1 parent f294c16 commit f749a5e

File tree

7 files changed

+187
-400
lines changed

7 files changed

+187
-400
lines changed

src/app/composer/qgscomposer.cpp

+39-136
Original file line numberDiff line numberDiff line change
@@ -534,37 +534,27 @@ class QgsPaintEngineHack : public QPaintEngine
534534
void QgsComposer::on_mActionExportAsPDF_triggered()
535535
{
536536
QSettings myQSettings; // where we keep last used filter in persistent state
537-
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
538-
QFileInfo file( myLastUsedFile );
539-
QFileDialog *myQFileDialog = new QFileDialog( this, tr( "Choose a file name to save the map as" ),
540-
file.path(), tr( "PDF Format" ) + " (*.pdf *PDF)" );
541-
myQFileDialog->selectFile( file.fileName() );
542-
myQFileDialog->setFileMode( QFileDialog::AnyFile );
543-
myQFileDialog->setConfirmOverwrite( true );
544-
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
545-
546-
int result = myQFileDialog->exec();
547-
raise();
548-
if ( result != QDialog::Accepted )
537+
QString lastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
538+
QFileInfo file( lastUsedFile );
539+
540+
QString outputFileName = QFileDialog::getSaveFileName(
541+
this,
542+
tr( "Choose a file name to save the map as" ),
543+
file.path(),
544+
tr( "PDF Format" ) + " (*.pdf *.PDF)" );
545+
if ( outputFileName.isEmpty() )
549546
return;
550547

551-
QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
552-
if ( myOutputFileNameQString == "" )
553-
{
554-
return;
555-
}
556-
557-
if ( !myOutputFileNameQString.endsWith( ".pdf", Qt::CaseInsensitive ) )
548+
if ( !outputFileName.endsWith( ".pdf", Qt::CaseInsensitive ) )
558549
{
559-
myOutputFileNameQString.append( ".pdf" );
550+
outputFileName += ".pdf";
560551
}
561552

562-
myQSettings.setValue( "/UI/lastSaveAsPdfFile", myOutputFileNameQString );
553+
myQSettings.setValue( "/UI/lastSaveAsPdfFile", outputFileName );
563554

564555
QPrinter printer;
565-
566556
printer.setOutputFormat( QPrinter::PdfFormat );
567-
printer.setOutputFileName( myOutputFileNameQString );
557+
printer.setOutputFileName( outputFileName );
568558
printer.setPaperSize( QSizeF( mComposition->paperWidth(), mComposition->paperHeight() ), QPrinter::Millimeter );
569559

570560
QPaintEngine *engine = printer.paintEngine();
@@ -697,93 +687,12 @@ void QgsComposer::on_mActionExportAsImage_triggered()
697687
return;
698688
}
699689

700-
// Get file and format (stolen from qgisapp.cpp but modified significantely)
701-
702-
//create a map to hold the QImageIO names and the filter names
703-
//the QImageIO name must be passed to the mapcanvas saveas image function
704-
typedef QMap<QString, QString> FilterMap;
705-
FilterMap myFilterMap;
706-
707-
//find out the last used filter
708-
QSettings myQSettings; // where we keep last used filter in persistent state
709-
QString myLastUsedFormat = myQSettings.value( "/UI/lastSaveAsImageFormat", "png" ).toString();
710-
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsImageFile", "qgis.png" ).toString();
711-
QFileInfo file( myLastUsedFile );
712-
713-
// get a list of supported output image types
714-
int myCounterInt = 0;
715-
QString myFilters;
716-
QString myLastUsedFilter;
717-
for ( ; myCounterInt < QImageWriter::supportedImageFormats().count(); myCounterInt++ )
718-
{
719-
QString myFormat = QString( QImageWriter::supportedImageFormats().at( myCounterInt ) );
720-
QString myFilter = tr( "%1 format (*.%2 *.%3)" )
721-
.arg( myFormat ).arg( myFormat.toLower() ).arg( myFormat.toUpper() );
722-
723-
if ( myCounterInt > 0 )
724-
myFilters += ";;";
725-
myFilters += myFilter;
726-
myFilterMap[myFilter] = myFormat;
727-
if ( myFormat == myLastUsedFormat )
728-
{
729-
myLastUsedFilter = myFilter;
730-
}
731-
}
732-
#ifdef QGISDEBUG
733-
QgsDebugMsg( "Available Filters Map: " );
734-
FilterMap::Iterator myIterator;
735-
for ( myIterator = myFilterMap.begin(); myIterator != myFilterMap.end(); ++myIterator )
736-
{
737-
QgsDebugMsg( QString( "%1 : %2" ).arg( myIterator.key() ).arg( myIterator.value() ) );
738-
}
739-
#endif
740-
741-
//create a file dialog using the the filter list generated above
742-
std::auto_ptr < QFileDialog > myQFileDialog(
743-
new QFileDialog(
744-
this,
745-
tr( "Choose a file name to save the map image as" ),
746-
file.path(),
747-
myFilters
748-
)
749-
);
750-
751-
myQFileDialog->setFileMode( QFileDialog::AnyFile );
752-
myQFileDialog->setConfirmOverwrite( true );
753-
754-
// set the filter to the last one used
755-
myQFileDialog->selectFilter( myLastUsedFilter );
756-
757-
// set the 'Open' button to something that makes more sense
758-
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
759-
760-
//prompt the user for a file name
761-
QString myOutputFileNameQString;
762-
763-
int result = myQFileDialog->exec();
764-
//raise();
765-
766-
if ( result != QDialog::Accepted )
767-
{
768-
return;
769-
}
770-
771-
myOutputFileNameQString = myQFileDialog->selectedFiles().last();
772-
QgsDebugMsg( myOutputFileNameQString );
773-
QString myFilterString = myQFileDialog->selectedFilter();
774-
QgsDebugMsg( QString( "Selected filter: %1" ).arg( myFilterString ) );
775-
QgsDebugMsg( QString( "Image type: %1" ).arg( myFilterMap[myFilterString] ) );
776-
777-
// Add the file type suffix to the fileName if required
778-
if ( !myOutputFileNameQString.endsWith( myFilterMap[myFilterString] ) )
779-
{
780-
myOutputFileNameQString += "." + myFilterMap[myFilterString];
781-
}
690+
QPair<QString, QString> fileNExt = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
782691

783-
myQSettings.setValue( "/UI/lastSaveAsImageFormat", myFilterMap[myFilterString] );
784-
myQSettings.setValue( "/UI/lastSaveAsImageFile", myOutputFileNameQString );
692+
QgsDebugMsg( QString( "Selected filter: %1" ).arg( fileNExt.first ) );
693+
QgsDebugMsg( QString( "Image type: %1" ).arg( fileNExt.second ) );
785694

786-
if ( myOutputFileNameQString == "" )
695+
if ( fileNExt.first.isEmpty() )
787696
return;
788697

789698
QImage image( QSize( width, height ), QImage::Format_ARGB32 );
@@ -809,7 +718,7 @@ void QgsComposer::on_mActionExportAsImage_triggered()
809718
p.end();
810719
mComposition->setPlotStyle( QgsComposition::Preview );
811720
mView->setPaintingEnabled( true );
812-
image.save( myOutputFileNameQString, myFilterMap[myFilterString].toLocal8Bit().data() );
721+
image.save( fileNExt.first, fileNExt.second.toLocal8Bit().constData() );
813722
}
814723

815724

@@ -820,10 +729,10 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
820729
showWMSPrintingWarning();
821730
}
822731

823-
QString myQSettingsLabel = "/UI/displaySVGWarning";
824-
QSettings myQSettings;
732+
QString settingsLabel = "/UI/displaySVGWarning";
733+
QSettings settings;
825734

826-
bool displaySVGWarning = myQSettings.value( myQSettingsLabel, true ).toBool();
735+
bool displaySVGWarning = settings.value( settingsLabel, true ).toBool();
827736

828737
if ( displaySVGWarning )
829738
{
@@ -832,7 +741,7 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
832741
m->setCheckBoxText( tr( "Don't show this message again" ) );
833742
m->setCheckBoxState( Qt::Unchecked );
834743
m->setCheckBoxVisible( true );
835-
m->setCheckBoxQSettingsLabel( myQSettingsLabel );
744+
m->setCheckBoxQSettingsLabel( settingsLabel );
836745
m->setMessageAsHtml( tr( "<p>The SVG export function in Qgis has several "
837746
"problems due to bugs and deficiencies in the " )
838747
+ tr( "Qt4 svg code. In particular, there are problems "
@@ -845,39 +754,31 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
845754
"</p>" ) );
846755
m->exec();
847756
}
848-
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsSvgFile", "qgis.svg" ).toString();
849-
QFileInfo file( myLastUsedFile );
850-
QFileDialog *myQFileDialog = new QFileDialog( this, tr( "Choose a file name to save the map as" ),
851-
file.path(), tr( "SVG Format" ) + " (*.svg *SVG)" );
852-
myQFileDialog->selectFile( file.fileName() );
853-
myQFileDialog->setFileMode( QFileDialog::AnyFile );
854-
myQFileDialog->setConfirmOverwrite( true );
855-
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
856757

857-
int result = myQFileDialog->exec();
858-
raise();
859-
if ( result != QDialog::Accepted )
860-
return;
758+
QString lastUsedFile = settings.value( "/UI/lastSaveAsSvgFile", "qgis.svg" ).toString();
759+
QFileInfo file( lastUsedFile );
861760

862-
QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
863-
if ( myOutputFileNameQString == "" )
864-
{
761+
QString outputFileName = QFileDialog::getSaveFileName(
762+
this,
763+
tr( "Choose a file name to save the map as" ),
764+
file.path(),
765+
tr( "SVG Format" ) + " (*.svg *.SVG)" );
766+
if ( outputFileName.isEmpty() )
865767
return;
866-
}
867768

868-
if ( !myOutputFileNameQString.endsWith( ".svg", Qt::CaseInsensitive ) )
769+
if ( !outputFileName.endsWith( ".svg", Qt::CaseInsensitive ) )
869770
{
870-
myOutputFileNameQString.append( ".svg" );
771+
outputFileName += ".svg";
871772
}
872773

873-
myQSettings.setValue( "/UI/lastSaveAsSvgFile", myOutputFileNameQString );
774+
settings.setValue( "/UI/lastSaveAsSvgFile", outputFileName );
874775
mComposition->setPlotStyle( QgsComposition::Print );
875776

876777
QSvgGenerator generator;
877778
#if QT_VERSION >= 0x040500
878779
generator.setTitle( QgsProject::instance()->title() );
879780
#endif
880-
generator.setFileName( myOutputFileNameQString );
781+
generator.setFileName( outputFileName );
881782
//width in pixel
882783
int width = ( int )( mComposition->paperWidth() * mComposition->printResolution() / 25.4 );
883784
//height in pixel
@@ -976,11 +877,13 @@ void QgsComposer::on_mActionSaveAsTemplate_triggered()
976877
//show file dialog
977878
QSettings settings;
978879
QString lastSaveDir = settings.value( "UI/lastComposerTemplateDir", "" ).toString();
979-
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)" );
980885
if ( saveFileName.isEmpty() )
981-
{
982886
return;
983-
}
984887

985888
QFileInfo saveFileInfo( saveFileName );
986889
//check if suffix has been added

src/app/qgisapp.cpp

+18-44
Original file line numberDiff line numberDiff line change
@@ -2701,7 +2701,7 @@ void QgisApp::fileOpen()
27012701
// Retrieve last used project dir from persistent settings
27022702
QSettings settings;
27032703
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
2704-
QString fullPath = QFileDialog::getOpenFileName( this, tr( "Choose a QGIS project file to open" ), lastUsedDir, tr( "QGis files (*.qgs)" ) );
2704+
QString fullPath = QFileDialog::getOpenFileName( this, tr( "Choose a QGIS project file to open" ), lastUsedDir, tr( "QGis files (*.qgs *.QGS)" ) );
27052705
if ( fullPath.isNull() )
27062706
{
27072707
return;
@@ -2840,31 +2840,20 @@ bool QgisApp::fileSave()
28402840
QSettings settings;
28412841
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
28422842

2843-
std::auto_ptr<QFileDialog> saveFileDialog( new QFileDialog( this,
2844-
tr( "Choose a QGIS project file" ),
2845-
lastUsedDir, tr( "QGis files (*.qgs)" ) ) );
2843+
QString path = QFileDialog::getSaveFileName(
2844+
this,
2845+
tr( "Choose a QGIS project file" ),
2846+
lastUsedDir + "/" + QgsProject::instance()->title(),
2847+
tr( "QGis files (*.qgs *.QGS)" ) );
2848+
if ( path.isEmpty() )
2849+
return true;
28462850

2847-
saveFileDialog->setFileMode( QFileDialog::AnyFile );
2848-
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
2849-
saveFileDialog->setConfirmOverwrite( true );
2850-
saveFileDialog->selectFile( QgsProject::instance()->title() );
2851-
2852-
if ( saveFileDialog->exec() == QDialog::Accepted )
2853-
{
2854-
fullPath.setFile( saveFileDialog->selectedFiles().first() );
2855-
}
2856-
else
2857-
{
2858-
// if they didn't select anything, just return
2859-
// delete saveFileDialog; auto_ptr auto destroys
2860-
return false;
2861-
}
2851+
QFileInfo fullPath( path );
28622852

28632853
// make sure we have the .qgs extension in the file name
2864-
if ( "qgs" != fullPath.suffix() )
2854+
if ( "qgs" != fullPath.suffix().toLower() )
28652855
{
2866-
QString newFilePath = fullPath.filePath() + ".qgs";
2867-
fullPath.setFile( newFilePath );
2856+
fullPath.setFile( fullPath.filePath() + ".qgs" );
28682857
}
28692858

28702859

@@ -2904,33 +2893,18 @@ void QgisApp::fileSaveAs()
29042893
QSettings settings;
29052894
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
29062895

2907-
std::auto_ptr<QFileDialog> saveFileDialog( new QFileDialog( this,
2908-
tr( "Choose a file name to save the QGIS project file as" ),
2909-
lastUsedDir, tr( "QGis files (*.qgs)" ) ) );
2910-
saveFileDialog->setFileMode( QFileDialog::AnyFile );
2911-
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
2912-
saveFileDialog->setConfirmOverwrite( true );
2913-
saveFileDialog->selectFile( QgsProject::instance()->title() );
2914-
2915-
QFileInfo fullPath;
2916-
if ( saveFileDialog->exec() == QDialog::Accepted )
2917-
{
2918-
//saveFilePath = saveFileDialog->selectedFiles().first();
2919-
fullPath.setFile( saveFileDialog->selectedFiles().first() );
2920-
}
2921-
else
2922-
{
2896+
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)" ) );
2897+
if ( path.isEmpty() )
29232898
return;
2924-
}
29252899

2926-
QString myPath = fullPath.path();
2927-
settings.setValue( "/UI/lastProjectDir", myPath );
2900+
QFileInfo fullPath( path );
2901+
2902+
settings.setValue( "/UI/lastProjectDir", fullPath.path() );
29282903

29292904
// make sure the .qgs extension is included in the path name. if not, add it...
2930-
if ( "qgs" != fullPath.suffix() )
2905+
if ( "qgs" != fullPath.suffix().toLower() )
29312906
{
2932-
myPath = fullPath.filePath() + ".qgs";
2933-
fullPath.setFile( myPath );
2907+
fullPath.setFile( fullPath.filePath() + ".qgs" );
29342908
}
29352909

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

src/app/qgsembedlayerdialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void QgsEmbedLayerDialog::on_mBrowseFileToolButton_clicked()
6262
mProjectFileLineEdit->blockSignals( true );
6363

6464
QSettings s;
65-
QString projectFile = QFileDialog::getOpenFileName( 0, tr( "Select project file" ), s.value( "/qgis/last_embedded_project_path" ).toString() , tr( "QGIS project files (*.qgs)" ) );
65+
QString projectFile = QFileDialog::getOpenFileName( 0, tr( "Select project file" ), s.value( "/qgis/last_embedded_project_path" ).toString() , tr( "QGIS project files (*.qgs *.QGS)" ) );
6666
if ( !projectFile.isEmpty() )
6767
{
6868
mProjectFileLineEdit->setText( projectFile );

0 commit comments

Comments
 (0)