Skip to content

Commit

Permalink
File encoding selector made consistent and persistent between differe…
Browse files Browse the repository at this point in the history
…nt dialogs.

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11182 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
homann committed Jul 27, 2009
1 parent c04aef6 commit ea9b4fa
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/app/legend/qgslegendlayerfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void QgsLegendLayerFile::saveAsShapefileGeneral( bool saveOnlySelection )
tr( "Save layer as..." ),
dirName,
filter,
QString( "UTF-8" ) );
QString( "" ) );
openFileDialog->setAcceptMode( QFileDialog::AcceptSave );

// allow for selection of more than one file
Expand Down
19 changes: 17 additions & 2 deletions src/app/ogr/qgsopenvectorlayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ QgsOpenVectorLayerDialog::QgsOpenVectorLayerDialog( QWidget* parent, Qt::WFlags
radioSrcFile->setChecked( true );
mDataSourceType = "file";
//set encoding
cmbEncodings->setItemText( cmbEncodings->currentIndex(), QString( QTextCodec::codecForLocale()->name() ) );
// cmbEncodings->setItemText( cmbEncodings->currentIndex(), QString( QTextCodec::codecForLocale()->name() ) );
QSettings settings;
QString enc = settings.value( "/UI/encoding", QString("System") ).toString();

// The specified decoding is added if not existing alread, and then set current.
// This should select it.
int encindex = cmbEncodings->findText( enc );
if ( encindex < 0 )
{
cmbEncodings->insertItem( 0, enc );
encindex = 0;
}
cmbEncodings->setCurrentIndex( encindex );

//add database drivers
mVectorFileFilter = QgsProviderRegistry::instance()->fileVectorFilters();
Expand Down Expand Up @@ -379,11 +391,11 @@ void QgsOpenVectorLayerDialog::openFilesRememberingFilter( QString const &filter
//********************auto connected slots *****************/
void QgsOpenVectorLayerDialog::on_buttonBox_accepted()
{
QSettings settings;
QgsDebugMsg( "dialog button accepted" );
if ( radioSrcDatabase->isChecked() )
{
mDataSources.clear();
QSettings settings;
QString baseKey = "/" + cmbDatabaseTypes->currentText() + "/connections/";
baseKey += cmbConnections->currentText();
QString host = settings.value( baseKey + "/host" ).toString();
Expand Down Expand Up @@ -414,6 +426,9 @@ void QgsOpenVectorLayerDialog::on_buttonBox_accepted()
protocolURI->text()
) );
}
// Save the used encoding
settings.setValue( "/UI/encoding", encoding() );

accept();
}

Expand Down
10 changes: 2 additions & 8 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2300,12 +2300,10 @@ static void openFilesRememberingFilter_( QString const &filterName,

QString lastUsedDir = settings.value( "/UI/" + filterName + "Dir", "." ).toString();

QString lastUsedEncoding = settings.value( "/UI/encoding" ).toString();

QgsDebugMsg( "Opening file dialog with filters: " + filters );

QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( 0,
title, lastUsedDir, filters, lastUsedEncoding );
title, lastUsedDir, filters, QString ("" ) );

// allow for selection of more than one file
openFileDialog->setFileMode( QFileDialog::ExistingFiles );
Expand All @@ -2330,7 +2328,6 @@ static void openFilesRememberingFilter_( QString const &filterName,

settings.setValue( "/UI/" + filterName, openFileDialog->selectedFilter() );
settings.setValue( "/UI/" + filterName + "Dir", myPath );
settings.setValue( "/UI/encoding", openFileDialog->encoding() );
}

delete openFileDialog;
Expand Down Expand Up @@ -3153,12 +3150,10 @@ void QgisApp::newVectorLayer()
QString lastUsedDir = settings.value( "/UI/lastVectorFileFilterDir",
"." ).toString();

QString lastUsedEncoding = settings.value( "/UI/encoding" ).toString();

QgsDebugMsg( "Saving vector file dialog without filters: " );

QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( this,
tr( "Save As" ), lastUsedDir, "", lastUsedEncoding );
tr( "Save As" ), lastUsedDir, "", QString( "" ) );

// allow for selection of more than one file
openFileDialog->setFileMode( QFileDialog::AnyFile );
Expand Down Expand Up @@ -3188,7 +3183,6 @@ void QgisApp::newVectorLayer()
settings.setValue( "/UI/lastVectorFileFilter", openFileDialog->selectedFilter() );

settings.setValue( "/UI/lastVectorFileFilterDir", openFileDialog->directory().absolutePath() );
settings.setValue( "/UI/encoding", openFileDialog->encoding() );

delete openFileDialog;

Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsvectordataprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/
/* $Id$ */

#include <QSettings>
#include <QTextCodec>

#include <cfloat> // for DBL_MAX
Expand All @@ -27,9 +28,10 @@
QgsVectorDataProvider::QgsVectorDataProvider( QString uri )
: QgsDataProvider( uri ),
mCacheMinMaxDirty( TRUE ),
mEncoding( QTextCodec::codecForLocale() ),
mFetchFeaturesWithoutGeom( FALSE )
{
QSettings settings;
setEncoding( settings.value( "/UI/encoding", QString("System") ).toString() );
}


Expand Down
10 changes: 9 additions & 1 deletion src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "qgsvectordataprovider.h"

#include <QFile>
#include <QSettings>
#include <QFileInfo>
#include <QTextCodec>

Expand Down Expand Up @@ -72,8 +73,15 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
mCodec = QTextCodec::codecForName( fileEncoding.toLocal8Bit().data() );
if ( !mCodec )
{
QSettings settings;
QString enc = settings.value( "/UI/encoding", QString("System") ).toString();
QgsDebugMsg( "error finding QTextCodec for " + fileEncoding );
mCodec = QTextCodec::codecForLocale();
mCodec = QTextCodec::codecForName( enc.toLocal8Bit().data() );
if ( !mCodec )
{
QgsDebugMsg( "error finding QTextCodec for " + enc );
mCodec = QTextCodec::codecForLocale();
}
}

// consider spatial reference system of the layer
Expand Down
30 changes: 27 additions & 3 deletions src/gui/qgsencodingfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
***************************************************************************/

#include "qgsencodingfiledialog.h"
#include "qgsproject.h"
#include "qgslogger.h"
#include <QSettings>
#include <QComboBox>
#include <QLabel>
#include <QLayout>
Expand Down Expand Up @@ -71,21 +74,35 @@ QgsEncodingFileDialog::QgsEncodingFileDialog( QWidget * parent,
mEncodingComboBox->addItem( "CP1258" );
mEncodingComboBox->addItem( "Apple Roman" );
mEncodingComboBox->addItem( "TIS-620" );

// Use default encoding if none supplied
QString enc = encoding;
if ( encoding.isEmpty() )
{
mEncodingComboBox->setItemText( mEncodingComboBox->currentIndex(), QString( QTextCodec::codecForLocale()->name() ) );
QSettings settings;
enc = settings.value( "/UI/encoding", QString("System") ).toString();
}
else

// The specified decoding is added if not existing alread, and then set current.
// This should select it.
int encindex = mEncodingComboBox->findText( enc );
if ( encindex < 0 )
{
mEncodingComboBox->setItemText( mEncodingComboBox->currentIndex(), encoding );
mEncodingComboBox->insertItem( 0, enc );
encindex = 0;
}
mEncodingComboBox->setCurrentIndex( encindex );

// if this dialog is being invoked from QgisApp::findFiles_(), then we
// need to force selection of the first filter since that corresponds to
// the file name we're looking for; even if we're not here from
// findFiles_(), it won't hurt to force selection of the first file filter
selectFilter( filters().at( 0 ) );

// Connect our slot to get a signal when the user is done with the file dialog
connect( this, SIGNAL( accepted() ), this, SLOT( saveUsedEncoding() ) );


}

QgsEncodingFileDialog::~QgsEncodingFileDialog()
Expand All @@ -97,3 +114,10 @@ QString QgsEncodingFileDialog::encoding() const
{
return mEncodingComboBox->currentText();
}

void QgsEncodingFileDialog::saveUsedEncoding()
{
QSettings settings;
settings.setValue( "/UI/encoding", encoding() );
QgsDebugMsg(QString( "Set encoding " + encoding() + " as default.") );
}
3 changes: 3 additions & 0 deletions src/gui/qgsencodingfiledialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class GUI_EXPORT QgsEncodingFileDialog: public QFileDialog
~QgsEncodingFileDialog();
/**Returns a string describing the choosen encoding*/
QString encoding() const;
public slots:
void saveUsedEncoding();

private:
/**Box to choose the encoding type*/
QComboBox* mEncodingComboBox;
Expand Down

0 comments on commit ea9b4fa

Please sign in to comment.