Skip to content

Commit

Permalink
use native file dialogs wherever possible (fixes #3763)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jan 27, 2012
1 parent f294c16 commit f749a5e
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 400 deletions.
175 changes: 39 additions & 136 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -534,37 +534,27 @@ class QgsPaintEngineHack : public QPaintEngine
void QgsComposer::on_mActionExportAsPDF_triggered()
{
QSettings myQSettings; // where we keep last used filter in persistent state
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
QFileInfo file( myLastUsedFile );
QFileDialog *myQFileDialog = new QFileDialog( this, tr( "Choose a file name to save the map as" ),
file.path(), tr( "PDF Format" ) + " (*.pdf *PDF)" );
myQFileDialog->selectFile( file.fileName() );
myQFileDialog->setFileMode( QFileDialog::AnyFile );
myQFileDialog->setConfirmOverwrite( true );
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );

int result = myQFileDialog->exec();
raise();
if ( result != QDialog::Accepted )
QString lastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
QFileInfo file( lastUsedFile );

QString outputFileName = QFileDialog::getSaveFileName(
this,
tr( "Choose a file name to save the map as" ),
file.path(),
tr( "PDF Format" ) + " (*.pdf *.PDF)" );
if ( outputFileName.isEmpty() )
return;

QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
if ( myOutputFileNameQString == "" )
{
return;
}

if ( !myOutputFileNameQString.endsWith( ".pdf", Qt::CaseInsensitive ) )
if ( !outputFileName.endsWith( ".pdf", Qt::CaseInsensitive ) )
{
myOutputFileNameQString.append( ".pdf" );
outputFileName += ".pdf";
}

myQSettings.setValue( "/UI/lastSaveAsPdfFile", myOutputFileNameQString );
myQSettings.setValue( "/UI/lastSaveAsPdfFile", outputFileName );

QPrinter printer;

printer.setOutputFormat( QPrinter::PdfFormat );
printer.setOutputFileName( myOutputFileNameQString );
printer.setOutputFileName( outputFileName );
printer.setPaperSize( QSizeF( mComposition->paperWidth(), mComposition->paperHeight() ), QPrinter::Millimeter );

QPaintEngine *engine = printer.paintEngine();
Expand Down Expand Up @@ -697,93 +687,12 @@ void QgsComposer::on_mActionExportAsImage_triggered()
return;
}

// Get file and format (stolen from qgisapp.cpp but modified significantely)

//create a map to hold the QImageIO names and the filter names
//the QImageIO name must be passed to the mapcanvas saveas image function
typedef QMap<QString, QString> FilterMap;
FilterMap myFilterMap;

//find out the last used filter
QSettings myQSettings; // where we keep last used filter in persistent state
QString myLastUsedFormat = myQSettings.value( "/UI/lastSaveAsImageFormat", "png" ).toString();
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsImageFile", "qgis.png" ).toString();
QFileInfo file( myLastUsedFile );

// get a list of supported output image types
int myCounterInt = 0;
QString myFilters;
QString myLastUsedFilter;
for ( ; myCounterInt < QImageWriter::supportedImageFormats().count(); myCounterInt++ )
{
QString myFormat = QString( QImageWriter::supportedImageFormats().at( myCounterInt ) );
QString myFilter = tr( "%1 format (*.%2 *.%3)" )
.arg( myFormat ).arg( myFormat.toLower() ).arg( myFormat.toUpper() );

if ( myCounterInt > 0 )
myFilters += ";;";
myFilters += myFilter;
myFilterMap[myFilter] = myFormat;
if ( myFormat == myLastUsedFormat )
{
myLastUsedFilter = myFilter;
}
}
#ifdef QGISDEBUG
QgsDebugMsg( "Available Filters Map: " );
FilterMap::Iterator myIterator;
for ( myIterator = myFilterMap.begin(); myIterator != myFilterMap.end(); ++myIterator )
{
QgsDebugMsg( QString( "%1 : %2" ).arg( myIterator.key() ).arg( myIterator.value() ) );
}
#endif

//create a file dialog using the the filter list generated above
std::auto_ptr < QFileDialog > myQFileDialog(
new QFileDialog(
this,
tr( "Choose a file name to save the map image as" ),
file.path(),
myFilters
)
);

myQFileDialog->setFileMode( QFileDialog::AnyFile );
myQFileDialog->setConfirmOverwrite( true );

// set the filter to the last one used
myQFileDialog->selectFilter( myLastUsedFilter );

// set the 'Open' button to something that makes more sense
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );

//prompt the user for a file name
QString myOutputFileNameQString;

int result = myQFileDialog->exec();
//raise();

if ( result != QDialog::Accepted )
{
return;
}

myOutputFileNameQString = myQFileDialog->selectedFiles().last();
QgsDebugMsg( myOutputFileNameQString );
QString myFilterString = myQFileDialog->selectedFilter();
QgsDebugMsg( QString( "Selected filter: %1" ).arg( myFilterString ) );
QgsDebugMsg( QString( "Image type: %1" ).arg( myFilterMap[myFilterString] ) );

// Add the file type suffix to the fileName if required
if ( !myOutputFileNameQString.endsWith( myFilterMap[myFilterString] ) )
{
myOutputFileNameQString += "." + myFilterMap[myFilterString];
}
QPair<QString, QString> fileNExt = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );

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

if ( myOutputFileNameQString == "" )
if ( fileNExt.first.isEmpty() )
return;

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


Expand All @@ -820,10 +729,10 @@ void QgsComposer::on_mActionExportAsSVG_triggered()
showWMSPrintingWarning();
}

QString myQSettingsLabel = "/UI/displaySVGWarning";
QSettings myQSettings;
QString settingsLabel = "/UI/displaySVGWarning";
QSettings settings;

bool displaySVGWarning = myQSettings.value( myQSettingsLabel, true ).toBool();
bool displaySVGWarning = settings.value( settingsLabel, true ).toBool();

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

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

QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
if ( myOutputFileNameQString == "" )
{
QString outputFileName = QFileDialog::getSaveFileName(
this,
tr( "Choose a file name to save the map as" ),
file.path(),
tr( "SVG Format" ) + " (*.svg *.SVG)" );
if ( outputFileName.isEmpty() )
return;
}

if ( !myOutputFileNameQString.endsWith( ".svg", Qt::CaseInsensitive ) )
if ( !outputFileName.endsWith( ".svg", Qt::CaseInsensitive ) )
{
myOutputFileNameQString.append( ".svg" );
outputFileName += ".svg";
}

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

QSvgGenerator generator;
#if QT_VERSION >= 0x040500
generator.setTitle( QgsProject::instance()->title() );
#endif
generator.setFileName( myOutputFileNameQString );
generator.setFileName( outputFileName );
//width in pixel
int width = ( int )( mComposition->paperWidth() * mComposition->printResolution() / 25.4 );
//height in pixel
Expand Down Expand Up @@ -976,11 +877,13 @@ void QgsComposer::on_mActionSaveAsTemplate_triggered()
//show file dialog
QSettings settings;
QString lastSaveDir = settings.value( "UI/lastComposerTemplateDir", "" ).toString();
QString saveFileName = QFileDialog::getSaveFileName( 0, tr( "save template" ), lastSaveDir, "*.qpt" );
QString saveFileName = QFileDialog::getSaveFileName(
this,
tr( "Save template" ),
lastSaveDir,
tr( "Composer templates" ) + " (*.qpt *.QPT)" );
if ( saveFileName.isEmpty() )
{
return;
}

QFileInfo saveFileInfo( saveFileName );
//check if suffix has been added
Expand Down
62 changes: 18 additions & 44 deletions src/app/qgisapp.cpp
Expand Up @@ -2701,7 +2701,7 @@ void QgisApp::fileOpen()
// Retrieve last used project dir from persistent settings
QSettings settings;
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
QString fullPath = QFileDialog::getOpenFileName( this, tr( "Choose a QGIS project file to open" ), lastUsedDir, tr( "QGis files (*.qgs)" ) );
QString fullPath = QFileDialog::getOpenFileName( this, tr( "Choose a QGIS project file to open" ), lastUsedDir, tr( "QGis files (*.qgs *.QGS)" ) );
if ( fullPath.isNull() )
{
return;
Expand Down Expand Up @@ -2840,31 +2840,20 @@ bool QgisApp::fileSave()
QSettings settings;
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();

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

saveFileDialog->setFileMode( QFileDialog::AnyFile );
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
saveFileDialog->setConfirmOverwrite( true );
saveFileDialog->selectFile( QgsProject::instance()->title() );

if ( saveFileDialog->exec() == QDialog::Accepted )
{
fullPath.setFile( saveFileDialog->selectedFiles().first() );
}
else
{
// if they didn't select anything, just return
// delete saveFileDialog; auto_ptr auto destroys
return false;
}
QFileInfo fullPath( path );

// make sure we have the .qgs extension in the file name
if ( "qgs" != fullPath.suffix() )
if ( "qgs" != fullPath.suffix().toLower() )
{
QString newFilePath = fullPath.filePath() + ".qgs";
fullPath.setFile( newFilePath );
fullPath.setFile( fullPath.filePath() + ".qgs" );
}


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

std::auto_ptr<QFileDialog> saveFileDialog( new QFileDialog( this,
tr( "Choose a file name to save the QGIS project file as" ),
lastUsedDir, tr( "QGis files (*.qgs)" ) ) );
saveFileDialog->setFileMode( QFileDialog::AnyFile );
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
saveFileDialog->setConfirmOverwrite( true );
saveFileDialog->selectFile( QgsProject::instance()->title() );

QFileInfo fullPath;
if ( saveFileDialog->exec() == QDialog::Accepted )
{
//saveFilePath = saveFileDialog->selectedFiles().first();
fullPath.setFile( saveFileDialog->selectedFiles().first() );
}
else
{
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)" ) );
if ( path.isEmpty() )
return;
}

QString myPath = fullPath.path();
settings.setValue( "/UI/lastProjectDir", myPath );
QFileInfo fullPath( path );

settings.setValue( "/UI/lastProjectDir", fullPath.path() );

// make sure the .qgs extension is included in the path name. if not, add it...
if ( "qgs" != fullPath.suffix() )
if ( "qgs" != fullPath.suffix().toLower() )
{
myPath = fullPath.filePath() + ".qgs";
fullPath.setFile( myPath );
fullPath.setFile( fullPath.filePath() + ".qgs" );
}

QgsProject::instance()->setFileName( fullPath.filePath() );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsembedlayerdialog.cpp
Expand Up @@ -62,7 +62,7 @@ void QgsEmbedLayerDialog::on_mBrowseFileToolButton_clicked()
mProjectFileLineEdit->blockSignals( true );

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

0 comments on commit f749a5e

Please sign in to comment.