Skip to content

Commit

Permalink
save last used dirs and last used filter in GPSTools plugin (fix #1209).
Browse files Browse the repository at this point in the history
Also remember dialog size and position and restore them (addresses #206)
  • Loading branch information
alexbruy committed Jan 12, 2012
1 parent 9b25791 commit 1c3117e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 65 deletions.
25 changes: 5 additions & 20 deletions src/plugins/gps_importer/qgsgpsplugin.cpp
Expand Up @@ -79,7 +79,6 @@ QgsGPSPlugin::~QgsGPSPlugin()
delete iter2->second;
}


/*
* Initialize the GUI interface for the plugin
*/
Expand Down Expand Up @@ -156,13 +155,14 @@ void QgsGPSPlugin::run()
myPluginGui->show();
}


void QgsGPSPlugin::createGPX()
{
QSettings settings;
QString dir = settings.value( "/Plugin-GPS/gpxdirectory", "." ).toString();
QString fileName =
QFileDialog::getSaveFileName( mQGisInterface->mainWindow(),
tr( "Save new GPX file as..." ),
".",
dir,
tr( "GPS eXchange file (*.gpx)" ) );
if ( !fileName.isEmpty() )
{
Expand All @@ -180,6 +180,8 @@ void QgsGPSPlugin::createGPX()
"directory." ) );
return;
}
settings.setValue( "/Plugin-GPS/gpxdirectory", fileInfo.absolutePath() );

ofs << "<gpx></gpx>" << std::endl;

emit drawVectorLayer( fileName + "?type=track",
Expand All @@ -191,7 +193,6 @@ void QgsGPSPlugin::createGPX()
}
}


void QgsGPSPlugin::drawVectorLayer( QString thePathNameQString,
QString theBaseNameQString,
QString theProviderQString )
Expand All @@ -214,7 +215,6 @@ void QgsGPSPlugin::unload()
void QgsGPSPlugin::loadGPXFile( QString fileName, bool loadWaypoints, bool loadRoutes,
bool loadTracks )
{

//check if input file is readable
QFileInfo fileInfo( fileName );
if ( !fileInfo.isReadable() )
Expand All @@ -225,10 +225,6 @@ void QgsGPSPlugin::loadGPXFile( QString fileName, bool loadWaypoints, bool loadR
return;
}

// remember the directory
QSettings settings;
settings.setValue( "/Plugin-GPS/gpxdirectory", fileInfo.path() );

// add the requested layers
if ( loadTracks )
emit drawVectorLayer( fileName + "?type=track",
Expand All @@ -243,13 +239,11 @@ void QgsGPSPlugin::loadGPXFile( QString fileName, bool loadWaypoints, bool loadR
emit closeGui();
}


void QgsGPSPlugin::importGPSFile( QString inputFileName, QgsBabelFormat* importer,
bool importWaypoints, bool importRoutes,
bool importTracks, QString outputFileName,
QString layerName )
{

// what features does the user want to import?
QString typeArg;
if ( importWaypoints )
Expand Down Expand Up @@ -312,15 +306,12 @@ void QgsGPSPlugin::importGPSFile( QString inputFileName, QgsBabelFormat* importe
emit closeGui();
}


void QgsGPSPlugin::convertGPSFile( QString inputFileName,
int convertType,
QString outputFileName,
QString layerName )
{

// what features does the user want to import?

QStringList convertStrings;

switch ( convertType )
Expand Down Expand Up @@ -399,7 +390,6 @@ void QgsGPSPlugin::downloadFromGPS( QString device, QString port,
bool downloadTracks, QString outputFileName,
QString layerName )
{

// what does the user want to download?
QString typeArg, features;
if ( downloadWaypoints )
Expand Down Expand Up @@ -430,7 +420,6 @@ void QgsGPSPlugin::downloadFromGPS( QString device, QString port,
return;
}


QgsDebugMsg( QString( "Download command: " ) + babelArgs.join( "|" ) );

QProcess babelProcess;
Expand Down Expand Up @@ -481,11 +470,9 @@ void QgsGPSPlugin::downloadFromGPS( QString device, QString port,
emit closeGui();
}


void QgsGPSPlugin::uploadToGPS( QgsVectorLayer* gpxLayer, QString device,
QString port )
{

const QString& source( gpxLayer->dataProvider()->dataSourceUri() );

// what kind of data does the user want to upload?
Expand Down Expand Up @@ -562,10 +549,8 @@ void QgsGPSPlugin::uploadToGPS( QgsVectorLayer* gpxLayer, QString device,
emit closeGui();
}


void QgsGPSPlugin::setupBabel()
{

// where is gpsbabel?
QSettings settings;
mBabelPath = settings.value( "/Plugin-GPS/gpsbabelpath", "" ).toString();
Expand Down
130 changes: 85 additions & 45 deletions src/plugins/gps_importer/qgsgpsplugingui.cpp
Expand Up @@ -35,6 +35,10 @@ QgsGPSPluginGui::QgsGPSPluginGui( const BabelMap& importers,
, mDevices( devices )
{
setupUi( this );

// restore size, position and active tab
restoreState();

populatePortComboBoxes();
populateULLayerComboBox();
populateIMPBabelFormats();
Expand Down Expand Up @@ -80,17 +84,17 @@ QgsGPSPluginGui::~QgsGPSPluginGui()

void QgsGPSPluginGui::on_buttonBox_accepted()
{
saveState();

// what should we do?
switch ( tabWidget->currentIndex() )
{
// add a GPX layer?
// add a GPX layer?
case 0:
emit loadGPXFile( leGPXFile->text(), cbGPXWaypoints->isChecked(),
cbGPXRoutes->isChecked(), cbGPXTracks->isChecked() );
break;

// or import other file?
// or import other file?
case 1:
{
const QString& typeString( cmbIMPFeature->currentText() );
Expand All @@ -109,7 +113,7 @@ void QgsGPSPluginGui::on_buttonBox_accepted()
int featureType = cmbDLFeatureType->currentIndex();

QString fileName = leDLOutput->text();
if ( fileName.right( 4 ) != ".gpx" )
if ( !fileName.toLower().endsWith( ".gpx" ) )
{
fileName += ".gpx";
}
Expand Down Expand Up @@ -140,24 +144,32 @@ void QgsGPSPluginGui::on_buttonBox_accepted()
break;
}
}

// The slots that are called above will emit closeGui() when successful.
// If not successful, the user will get another shot without starting from scratch
// accept();
}


void QgsGPSPluginGui::on_pbnDLOutput_clicked()
{
QSettings settings;
QString dir = settings.value( "/Plugin-GPS/gpxdirectory", "." ).toString();
QString myFileNameQString =
QFileDialog::getSaveFileName( this, //parent dialog
QFileDialog::getSaveFileName( this,
tr( "Choose a file name to save under" ),
".", //initial dir
dir,
tr( "GPS eXchange format (*.gpx)" ) );
if ( !myFileNameQString.isEmpty() )
{
if ( !myFileNameQString.toLower().endsWith( ".gpx" ) )
{
myFileNameQString += ".gpx";
}
leDLOutput->setText( myFileNameQString );
settings.setValue( "/Plugin-GPS/gpxdirectory", QFileInfo( myFileNameQString ).absolutePath() );
}
}


void QgsGPSPluginGui::enableRelevantControls()
{
// load GPX
Expand Down Expand Up @@ -188,7 +200,6 @@ void QgsGPSPluginGui::enableRelevantControls()
// import other file
else if ( tabWidget->currentIndex() == 1 )
{

if (( leIMPInput->text() == "" ) || ( leIMPOutput->text() == "" ) ||
( leIMPLayer->text() == "" ) )
pbnOK->setEnabled( false );
Expand Down Expand Up @@ -218,7 +229,6 @@ void QgsGPSPluginGui::enableRelevantControls()
// convert between waypoint/routes
else if ( tabWidget->currentIndex() == 4 )
{

if (( leCONVInput->text() == "" ) || ( leCONVOutput->text() == "" ) ||
( leCONVLayer->text() == "" ) )
pbnOK->setEnabled( false );
Expand All @@ -227,45 +237,47 @@ void QgsGPSPluginGui::enableRelevantControls()
}
}


void QgsGPSPluginGui::on_buttonBox_rejected()
{
saveState();
reject();
}


void QgsGPSPluginGui::on_pbnGPXSelectFile_clicked()
{
QgsLogger::debug( " GPS File Importer::pbnGPXSelectFile_clicked() " );
QString myFileTypeQString;
QString myFilterString = tr( "GPS eXchange format (*.gpx)" );
QSettings settings;
QString dir = settings.value( "/Plugin-GPS/gpxdirectory" ).toString();
if ( dir.isEmpty() )
dir = ".";
QString dir = settings.value( "/Plugin-GPS/gpxdirectory", "." ).toString();
QString myFileNameQString = QFileDialog::getOpenFileName(
this, //parent dialog
tr( "Select GPX file" ), //caption
dir, //initial dir
myFilterString, //filters to select
&myFileTypeQString ); //the pointer to store selected filter
QgsLogger::debug( "Selected filetype filter is : " + myFileTypeQString );
this,
tr( "Select GPX file" ),
dir,
tr( "GPS eXchange format (*.gpx)" ) );
if ( !myFileNameQString.isEmpty() )
{
leGPXFile->setText( myFileNameQString );
settings.setValue( "/Plugin-GPS/gpxdirectory", QFileInfo( myFileNameQString ).absolutePath() );
}
}


void QgsGPSPluginGui::on_pbnIMPInput_clicked()
{
QString myFileType;
QSettings settings;
QString dir = settings.value( "/Plugin-GPS/importdirectory", "." ).toString();
QString tf = mBabelFilter.split( ";;" ).first();
QString myFileType = settings.value( "/Plugin-GPS/lastImportFilter", tf ).toString();
QString myFileName = QFileDialog::getOpenFileName(
this, //parent dialog
tr( "Select file and format to import" ), //caption
".", //initial dir
this,
tr( "Select file and format to import" ),
dir,
mBabelFilter,
&myFileType ); //the pointer to store selected filter
&myFileType );
if ( !myFileName.isEmpty() )
{
// save directory and file type
settings.setValue( "/Plugin-GPS/importdirectory", QFileInfo( myFileName ).absolutePath() );
settings.setValue( "/Plugin-GPS/lastImportFilter", myFileType );

mImpFormat = myFileType.left( myFileType.length() - 6 );
std::map<QString, QgsBabelFormat*>::const_iterator iter;
iter = mImporters.find( mImpFormat );
Expand All @@ -289,16 +301,24 @@ void QgsGPSPluginGui::on_pbnIMPInput_clicked()
}
}


void QgsGPSPluginGui::on_pbnIMPOutput_clicked()
{
QSettings settings;
QString dir = settings.value( "/Plugin-GPS/gpxdirectory", "." ).toString();
QString myFileNameQString =
QFileDialog::getSaveFileName( this, //parent dialog
QFileDialog::getSaveFileName( this,
tr( "Choose a file name to save under" ),
".", //initial dir
dir,
tr( "GPS eXchange format (*.gpx)" ) );
if ( !myFileNameQString.isEmpty() )
{
if ( !myFileNameQString.toLower().endsWith( ".gpx" ) )
{
myFileNameQString += ".gpx";
}
leIMPOutput->setText( myFileNameQString );
settings.setValue( "/Plugin-GPS/gpxdirectory", QFileInfo( myFileNameQString ).absolutePath() );
}
}

void QgsGPSPluginGui::on_pbnRefresh_clicked()
Expand Down Expand Up @@ -343,7 +363,6 @@ void QgsGPSPluginGui::populateULLayerComboBox()
cmbULLayer->addItem( mGPXLayers[i]->name() );
}


void QgsGPSPluginGui::populateIMPBabelFormats()
{
mBabelFilter = "";
Expand Down Expand Up @@ -375,31 +394,38 @@ void QgsGPSPluginGui::populateIMPBabelFormats()

void QgsGPSPluginGui::on_pbnCONVInput_clicked()
{
QString myFileTypeQString;
QString myFilterString = tr( "GPS eXchange format (*.gpx)" );
QSettings settings;
QString dir = settings.value( "/Plugin-GPS/gpxdirectory" ).toString();
if ( dir.isEmpty() )
dir = ".";
QString dir = settings.value( "/Plugin-GPS/gpxdirectory", "." ).toString();
QString myFileNameQString = QFileDialog::getOpenFileName(
this, //parent dialog
tr( "Select GPX file" ), //caption
dir, //initial dir
myFilterString, //filters to select
&myFileTypeQString ); //the pointer to store selected filter
this,
tr( "Select GPX file" ),
dir,
tr( "GPS eXchange format (*.gpx)" ) );
if ( !myFileNameQString.isEmpty() )
{
leCONVInput->setText( myFileNameQString );
settings.setValue( "/Plugin-GPS/gpxdirectory", QFileInfo( myFileNameQString ).absolutePath() );
}
}

void QgsGPSPluginGui::on_pbnCONVOutput_clicked()
{
QSettings settings;
QString dir = settings.value( "/Plugin-GPS/gpxdirectory", "." ).toString();
QString myFileNameQString =
QFileDialog::getSaveFileName( this, //parent dialog
QFileDialog::getSaveFileName( this,
tr( "Choose a file name to save under" ),
".", //initial dir
dir,
tr( "GPS eXchange format (*.gpx)" ) );
if ( !myFileNameQString.isEmpty() )
{
if ( !myFileNameQString.toLower().endsWith( ".gpx" ) )
{
myFileNameQString += ".gpx";
}
leCONVOutput->setText( myFileNameQString );
settings.setValue( "/Plugin-GPS/gpxdirectory", QFileInfo( myFileNameQString ).absolutePath() );
}
}

void QgsGPSPluginGui::openDeviceEditor()
Expand All @@ -413,3 +439,17 @@ void QgsGPSPluginGui::devicesUpdated()
{
populateIMPBabelFormats();
}

void QgsGPSPluginGui::saveState()
{
QSettings settings;
settings.setValue( "/Plugin-GPS/geometry", saveGeometry() );
settings.setValue( "/Plugin-GPS/lastTab", tabWidget->currentIndex() );
}

void QgsGPSPluginGui::restoreState()
{
QSettings settings;
restoreGeometry( settings.value( "/Plugin-GPS/geometry" ).toByteArray() );
tabWidget->setCurrentIndex( settings.value( "/Plugin-GPS/lastTab", 4 ).toInt() );
}

0 comments on commit 1c3117e

Please sign in to comment.