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 authored and alexbruy committed Jan 29, 2012
1 parent 9565677 commit bfd69a0
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 385 deletions.
163 changes: 40 additions & 123 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -529,35 +529,26 @@ 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->setAcceptMode( QFileDialog::AcceptSave );

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

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

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 @@ -690,93 +681,16 @@ 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 );

// set the filter to the last one used
myQFileDialog->selectFilter( myLastUsedFilter );
QPair<QString, QString> fileNExt = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );

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

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

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

if ( result != QDialog::Accepted )
if ( fileNExt.first.isEmpty() )
{
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];
}

myQSettings.setValue( "/UI/lastSaveAsImageFormat", myFilterMap[myFilterString] );
myQSettings.setValue( "/UI/lastSaveAsImageFile", myOutputFileNameQString );

if ( myOutputFileNameQString == "" )
return;

QImage image( QSize( width, height ), QImage::Format_ARGB32 );
if ( image.isNull() )
{
Expand All @@ -800,7 +714,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 @@ -811,10 +725,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 @@ -823,7 +737,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 @@ -836,37 +750,35 @@ 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->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 outputFileName = QFileDialog::getSaveFileName(
this,
tr( "Choose a file name to save the map as" ),
file.path(),
tr( "SVG Format" ) + " (*.svg *.SVG)" );

QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
if ( myOutputFileNameQString == "" )
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 @@ -965,7 +877,12 @@ 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;
Expand Down
61 changes: 18 additions & 43 deletions src/app/qgisapp.cpp
Expand Up @@ -2616,7 +2616,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 @@ -2755,31 +2755,21 @@ 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)" ) );

saveFileDialog->setFileMode( QFileDialog::AnyFile );
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
saveFileDialog->setConfirmOverwrite( true );
saveFileDialog->selectFile( QgsProject::instance()->title() );
if ( path.isEmpty() )
return true;

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 @@ -2819,33 +2809,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

0 comments on commit bfd69a0

Please sign in to comment.