Skip to content

Commit 74630bb

Browse files
author
mhugent
committed
Use native file dialogs (via static methods from QFileDialog)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11795 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent e984868 commit 74630bb

File tree

3 files changed

+73
-211
lines changed

3 files changed

+73
-211
lines changed

src/app/ogr/qgsopenvectorlayerdialog.cpp

+8-22
Original file line numberDiff line numberDiff line change
@@ -348,42 +348,28 @@ void QgsOpenVectorLayerDialog::openFilesRememberingFilter( QString const &filter
348348
QVariant( QString::null ) ).toString();
349349

350350
QString lastUsedDir = settings.value( "/UI/" + filterName + "Dir", "." ).toString();
351-
352-
//QString lastUsedEncoding = settings.value( "/UI/encoding" ).toString();
353-
354351
QgsDebugMsg( "Opening file dialog with filters: " + filters );
355352

356-
QFileDialog* openFileDialog = new QFileDialog( 0,
357-
title, lastUsedDir, filters );
358-
359-
// allow for selection of more than one file
360-
openFileDialog->setFileMode( QFileDialog::ExistingFiles );
361-
362-
if ( haveLastUsedFilter ) // set the filter to the last one used
353+
if ( haveLastUsedFilter )
363354
{
364-
openFileDialog->selectFilter( lastUsedFilter );
355+
selectedFiles = QFileDialog::getOpenFileNames( 0, title, lastUsedDir, filters, &lastUsedFilter );
356+
}
357+
else
358+
{
359+
selectedFiles = QFileDialog::getOpenFileNames( 0, title, lastUsedDir, filters );
365360
}
366361

367-
if ( openFileDialog->exec() == QDialog::Accepted )
362+
if ( !selectedFiles.isEmpty() )
368363
{
369-
selectedFiles = openFileDialog->selectedFiles();
370-
//enc = openFileDialog->encoding();
371-
// Fix by Tim - getting the dirPath from the dialog
372-
// directly truncates the last node in the dir path.
373-
// This is a workaround for that
374364
QString myFirstFileName = selectedFiles.first();
375365
QFileInfo myFI( myFirstFileName );
376366
QString myPath = myFI.path();
377367

378368
QgsDebugMsg( "Writing last used dir: " + myPath );
379369

380-
settings.setValue( "/UI/" + filterName, openFileDialog->selectedFilter() );
370+
settings.setValue( "/UI/" + filterName, lastUsedFilter );
381371
settings.setValue( "/UI/" + filterName + "Dir", myPath );
382-
//settings.setValue( "/UI/encoding", openFileDialog->encoding() );
383372
}
384-
385-
delete openFileDialog;
386-
387373
} // openFilesRememberingFilter_
388374

389375

src/app/qgisapp.cpp

+23-88
Original file line numberDiff line numberDiff line change
@@ -2304,8 +2304,6 @@ static bool openFilesRememberingFilter_( QString const &filterName,
23042304
bool cancelAll = false )
23052305
{
23062306

2307-
bool retVal = false;
2308-
23092307
bool haveLastUsedFilter = false; // by default, there is no last
23102308
// used filter
23112309

@@ -2319,28 +2317,9 @@ static bool openFilesRememberingFilter_( QString const &filterName,
23192317
QString lastUsedDir = settings.value( "/UI/" + filterName + "Dir", "." ).toString();
23202318

23212319
QgsDebugMsg( "Opening file dialog with filters: " + filters );
2322-
2323-
QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( 0,
2324-
title, lastUsedDir, filters, QString( "" ) );
2325-
2326-
// allow for selection of more than one file
2327-
openFileDialog->setFileMode( QFileDialog::ExistingFiles );
2328-
2329-
if ( haveLastUsedFilter ) // set the filter to the last one used
2330-
{
2331-
openFileDialog->selectFilter( lastUsedFilter );
2332-
}
2333-
2334-
// Check if we should add a cancel all button
2335-
if ( cancelAll )
2336-
{
2337-
openFileDialog->addCancelAll();
2338-
}
2339-
2340-
if ( openFileDialog->exec() == QDialog::Accepted )
2320+
selectedFiles = QFileDialog::getOpenFileNames( 0, title, lastUsedDir, filters, &lastUsedFilter );
2321+
if ( !selectedFiles.isEmpty() )
23412322
{
2342-
selectedFiles = openFileDialog->selectedFiles();
2343-
enc = openFileDialog->encoding();
23442323
// Fix by Tim - getting the dirPath from the dialog
23452324
// directly truncates the last node in the dir path.
23462325
// This is a workaround for that
@@ -2350,17 +2329,11 @@ static bool openFilesRememberingFilter_( QString const &filterName,
23502329

23512330
QgsDebugMsg( "Writing last used dir: " + myPath );
23522331

2353-
settings.setValue( "/UI/" + filterName, openFileDialog->selectedFilter() );
2332+
settings.setValue( "/UI/" + filterName, lastUsedFilter );
23542333
settings.setValue( "/UI/" + filterName + "Dir", myPath );
2334+
return true;
23552335
}
2356-
else
2357-
{
2358-
// Cancel or cancel all
2359-
retVal = openFileDialog->cancelAll();
2360-
}
2361-
2362-
delete openFileDialog;
2363-
return retVal;
2336+
return false;
23642337
} // openFilesRememberingFilter_
23652338

23662339

@@ -3281,33 +3254,19 @@ void QgisApp::fileOpen()
32813254
// Retrieve last used project dir from persistent settings
32823255
QSettings settings;
32833256
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
3284-
3285-
QFileDialog * openFileDialog = new QFileDialog( this,
3286-
tr( "Choose a QGIS project file to open" ),
3287-
lastUsedDir, tr( "QGis files (*.qgs)" ) );
3288-
openFileDialog->setFileMode( QFileDialog::ExistingFile );
3289-
3290-
3291-
QString fullPath;
3292-
if ( openFileDialog->exec() == QDialog::Accepted )
3293-
{
3294-
// Fix by Tim - getting the dirPath from the dialog
3295-
// directly truncates the last node in the dir path.
3296-
// This is a workaround for that
3297-
fullPath = openFileDialog->selectedFiles().first();
3298-
QFileInfo myFI( fullPath );
3299-
QString myPath = myFI.path();
3300-
// Persist last used project dir
3301-
settings.setValue( "/UI/lastProjectDir", myPath );
3302-
}
3303-
else
3257+
QString fullPath = QFileDialog::getOpenFileName( this, tr( "Choose a QGIS project file to open" ), lastUsedDir, tr( "QGis files (*.qgs)" ) );
3258+
if ( fullPath.isNull() )
33043259
{
3305-
// if they didn't select anything, just return
3306-
delete openFileDialog;
33073260
return;
33083261
}
33093262

3310-
delete openFileDialog;
3263+
// Fix by Tim - getting the dirPath from the dialog
3264+
// directly truncates the last node in the dir path.
3265+
// This is a workaround for that
3266+
QFileInfo myFI( fullPath );
3267+
QString myPath = myFI.path();
3268+
// Persist last used project dir
3269+
settings.setValue( "/UI/lastProjectDir", myPath );
33113270

33123271
delete mComposer;
33133272
mComposer = new QgsComposer( this );
@@ -3563,55 +3522,31 @@ void QgisApp::fileSaveAs()
35633522
// Retrieve last used project dir from persistent settings
35643523
QSettings settings;
35653524
QString lastUsedDir = settings.value( "/UI/lastProjectDir", "." ).toString();
3566-
3567-
std::auto_ptr<QFileDialog> saveFileDialog( new QFileDialog( this,
3568-
tr( "Choose a file name to save the QGIS project file as" ),
3569-
lastUsedDir, tr( "QGis files (*.qgs)" ) ) );
3570-
3571-
saveFileDialog->setFileMode( QFileDialog::AnyFile );
3572-
3573-
saveFileDialog->setAcceptMode( QFileDialog::AcceptSave );
3574-
3575-
saveFileDialog->setConfirmOverwrite( true );
3576-
3577-
// if we don't have a file name, then obviously we need to get one; note
3578-
// that the project file name is reset to null in fileNew()
3579-
QFileInfo fullPath;
3580-
3581-
if ( saveFileDialog->exec() == QDialog::Accepted )
3525+
QString saveFilePath = QFileDialog::getSaveFileName( this, tr( "Choose a file name to save the QGIS project file as" ), lastUsedDir, tr( "QGis files (*.qgs)" ) );
3526+
if ( saveFilePath.isNull() ) //canceled
35823527
{
3583-
// Fix by Tim - getting the dirPath from the dialog
3584-
// directly truncates the last node in the dir path.
3585-
// This is a workaround for that
3586-
fullPath.setFile( saveFileDialog->selectedFiles().first() );
3587-
QString myPath = fullPath.path();
3588-
// Persist last used project dir
3589-
settings.setValue( "/UI/lastProjectDir", myPath );
3590-
}
3591-
else
3592-
{
3593-
// if they didn't select anything, just return
3594-
// delete saveFileDialog; auto_ptr auto deletes
35953528
return;
35963529
}
3530+
QFileInfo myFI( saveFilePath );
3531+
QString myPath = myFI.path();
3532+
settings.setValue( "/UI/lastProjectDir", myPath );
35973533

35983534
// make sure the .qgs extension is included in the path name. if not, add it...
3599-
if ( "qgs" != fullPath.suffix() )
3535+
if ( "qgs" != myFI.suffix() )
36003536
{
3601-
QString newFilePath = fullPath.filePath() + ".qgs";
3602-
fullPath.setFile( newFilePath );
3537+
saveFilePath = myFI.filePath() + ".qgs";
36033538
}
36043539

36053540
try
36063541
{
3607-
QgsProject::instance()->setFileName( fullPath.filePath() );
3542+
QgsProject::instance()->setFileName( saveFilePath );
36083543

36093544
if ( QgsProject::instance()->write() )
36103545
{
36113546
setTitleBarText_( *this ); // update title bar
36123547
statusBar()->showMessage( tr( "Saved project to: %1" ).arg( QgsProject::instance()->fileName() ) );
36133548
// add this to the list of recently used project files
3614-
saveRecentProjectPath( fullPath.filePath(), settings );
3549+
saveRecentProjectPath( saveFilePath, settings );
36153550
}
36163551
else
36173552
{

src/app/qgsvectorlayerproperties.cpp

+42-101
Original file line numberDiff line numberDiff line change
@@ -956,125 +956,66 @@ void QgsVectorLayerProperties::on_pbnLoadStyle_clicked()
956956
{
957957
QSettings myQSettings; // where we keep last used filter in persistant state
958958
QString myLastUsedDir = myQSettings.value( "style/lastStyleDir", "." ).toString();
959-
960-
//create a file dialog
961-
std::auto_ptr < QFileDialog > myFileDialog
962-
(
963-
new QFileDialog(
964-
this,
965-
tr( "Load layer properties from style file (.qml)" ),
966-
myLastUsedDir,
967-
tr( "QGIS Layer Style File (*.qml)" )
968-
)
969-
);
970-
myFileDialog->setFileMode( QFileDialog::AnyFile );
971-
myFileDialog->setAcceptMode( QFileDialog::AcceptOpen );
972-
973-
//prompt the user for a file name
974-
QString myFileName;
975-
if ( myFileDialog->exec() == QDialog::Accepted )
959+
QString myFileName = QFileDialog::getOpenFileName( this, tr( "Load layer properties from style file (.qml)" ), myLastUsedDir, tr( "QGIS Layer Style File (*.qml)" ) );
960+
if ( myFileName.isNull() )
976961
{
977-
QStringList myFiles = myFileDialog->selectedFiles();
978-
if ( !myFiles.isEmpty() )
979-
{
980-
myFileName = myFiles[0];
981-
}
962+
return;
982963
}
983964

984-
if ( !myFileName.isEmpty() )
965+
bool defaultLoadedFlag = false;
966+
QString myMessage = layer->loadNamedStyle( myFileName, defaultLoadedFlag );
967+
//reset if the default style was loaded ok only
968+
if ( defaultLoadedFlag )
985969
{
986-
if ( myFileDialog->selectedFilter() == tr( "QGIS Layer Style File (*.qml)" ) )
987-
{
988-
//ensure the user never omitted the extension from the file name
989-
if ( !myFileName.endsWith( ".qml", Qt::CaseInsensitive ) )
990-
{
991-
myFileName += ".qml";
992-
}
993-
bool defaultLoadedFlag = false;
994-
QString myMessage = layer->loadNamedStyle( myFileName, defaultLoadedFlag );
995-
//reset if the default style was loaded ok only
996-
if ( defaultLoadedFlag )
997-
{
998-
reset();
999-
}
1000-
else
1001-
{
1002-
//let the user know what went wrong
1003-
QMessageBox::information( this, tr( "Saved Style" ), myMessage );
1004-
}
1005-
}
1006-
else
1007-
{
1008-
QMessageBox::warning( this, tr( "QGIS" ),
1009-
tr( "Unknown style format: %1" ).arg( myFileDialog->selectedFilter() ) );
1010-
}
1011-
myQSettings.setValue( "style/lastStyleDir", myFileDialog->directory().absolutePath() );
970+
reset();
1012971
}
972+
else
973+
{
974+
//let the user know what went wrong
975+
QMessageBox::information( this, tr( "Saved Style" ), myMessage );
976+
}
977+
978+
QFileInfo myFI( myFileName );
979+
QString myPath = myFI.path();
980+
myQSettings.setValue( "style/lastStyleDir", myPath );
1013981
}
1014982

1015983

1016984
void QgsVectorLayerProperties::on_pbnSaveStyleAs_clicked()
1017985
{
1018986
QSettings myQSettings; // where we keep last used filter in persistant state
1019987
QString myLastUsedDir = myQSettings.value( "style/lastStyleDir", "." ).toString();
1020-
1021-
//create a file dialog
1022-
std::auto_ptr < QFileDialog > myFileDialog
1023-
(
1024-
new QFileDialog(
1025-
this,
1026-
tr( "Save layer properties as style file (.qml)" ),
1027-
myLastUsedDir,
1028-
tr( "QGIS Layer Style File (*.qml)" )
1029-
)
1030-
);
1031-
myFileDialog->setFileMode( QFileDialog::AnyFile );
1032-
myFileDialog->setAcceptMode( QFileDialog::AcceptSave );
1033-
1034-
//prompt the user for a file name
1035-
QString myOutputFileName;
1036-
if ( myFileDialog->exec() == QDialog::Accepted )
988+
QString myOutputFileName = QFileDialog::getSaveFileName( this, tr( "Save layer properties as style file (.qml)" ), myLastUsedDir, tr( "QGIS Layer Style File (*.qml)" ) );
989+
if ( myOutputFileName.isNull() ) //dialog canceled
1037990
{
1038-
QStringList myFiles = myFileDialog->selectedFiles();
1039-
if ( !myFiles.isEmpty() )
1040-
{
1041-
myOutputFileName = myFiles[0];
1042-
}
991+
return;
1043992
}
1044993

1045-
if ( !myOutputFileName.isEmpty() )
1046-
{
1047-
if ( myFileDialog->selectedFilter() == tr( "QGIS Layer Style File (*.qml)" ) )
1048-
{
1049-
apply(); // make sure the qml to save is uptodate
1050-
1051-
//ensure the user never ommitted the extension from the file name
1052-
if ( !myOutputFileName.endsWith( ".qml", Qt::CaseInsensitive ) )
1053-
{
1054-
myOutputFileName += ".qml";
1055-
}
994+
apply(); // make sure the qml to save is uptodate
1056995

1057-
bool defaultLoadedFlag = false;
1058-
QString myMessage = layer->saveNamedStyle( myOutputFileName, defaultLoadedFlag );
1059-
//reset if the default style was loaded ok only
1060-
if ( defaultLoadedFlag )
1061-
{
1062-
reset();
1063-
}
1064-
else
1065-
{
1066-
//let the user know what went wrong
1067-
QMessageBox::information( this, tr( "Saved Style" ), myMessage );
1068-
}
1069-
}
1070-
else
1071-
{
1072-
QMessageBox::warning( this, tr( "QGIS" ),
1073-
tr( "Unknown style format: %1" ).arg( myFileDialog->selectedFilter() ) );
1074-
}
996+
//ensure the user never ommitted the extension from the file name
997+
if ( !myOutputFileName.endsWith( ".qml", Qt::CaseInsensitive ) )
998+
{
999+
myOutputFileName += ".qml";
1000+
}
10751001

1076-
myQSettings.setValue( "style/lastStyleDir", myFileDialog->directory().absolutePath() );
1002+
bool defaultLoadedFlag = false;
1003+
QString myMessage = layer->saveNamedStyle( myOutputFileName, defaultLoadedFlag );
1004+
//reset if the default style was loaded ok only
1005+
if ( defaultLoadedFlag )
1006+
{
1007+
reset();
1008+
}
1009+
else
1010+
{
1011+
//let the user know what went wrong
1012+
QMessageBox::information( this, tr( "Saved Style" ), myMessage );
10771013
}
1014+
1015+
QFileInfo myFI( myOutputFileName );
1016+
QString myPath = myFI.path();
1017+
// Persist last used dir
1018+
myQSettings.setValue( "style/lastStyleDir", myPath );
10781019
}
10791020

10801021
void QgsVectorLayerProperties::on_tblAttributes_cellChanged( int row, int column )

0 commit comments

Comments
 (0)