Skip to content

Commit

Permalink
Add a new dialog for selecting file encoding, QgsEncodingSelectionDialog
Browse files Browse the repository at this point in the history
Can be used to prompt users for a file encoding choice
  • Loading branch information
nyalldawson committed Nov 21, 2017
1 parent e05cca2 commit f660d78
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
40 changes: 40 additions & 0 deletions python/gui/qgsencodingfiledialog.sip
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,46 @@ Returns true if the user clicked 'Cancel All'

};

class QgsEncodingSelectionDialog: QDialog
{
%Docstring
A dialog which presents the user with a choice of file encodings.
.. versionadded:: 3.0
*
%End

%TypeHeaderCode
#include "qgsencodingfiledialog.h"
%End
public:

QgsEncodingSelectionDialog( QWidget *parent /TransferThis/ = 0,
const QString &caption = QString(), const QString &encoding = QString(),
Qt::WindowFlags flags = Qt::WindowFlags() );
%Docstring
Constructor for QgsEncodingSelectionDialog.

If ``caption`` is set, it will be used as the caption within the dialog.

The ``encoding`` argument can be used to specify the encoding initially selected in the dialog.
%End

QString encoding() const;
%Docstring
Returns the encoding selected within the dialog.
.. seealso:: setEncoding()
:rtype: str
%End

void setEncoding( const QString &encoding );
%Docstring
Sets the ``encoding`` selected within the dialog.
see encoding()
%End

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
61 changes: 61 additions & 0 deletions src/gui/qgsencodingfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QLabel>
#include <QLayout>
#include <QTextCodec>
#include <QDialogButtonBox>

QgsEncodingFileDialog::QgsEncodingFileDialog( QWidget *parent,
const QString &caption, const QString &directory,
Expand Down Expand Up @@ -102,3 +103,63 @@ void QgsEncodingFileDialog::pbnCancelAll_clicked()
// Now, continue as the user clicked the cancel button
reject();
}

QgsEncodingSelectionDialog::QgsEncodingSelectionDialog( QWidget *parent, const QString &caption, const QString &encoding, Qt::WindowFlags flags )
: QDialog( parent, flags )
{
QString c = caption;
if ( c.isEmpty() )
c = tr( "Encoding" );

setWindowTitle( tr( "Select Encoding" ) );

QVBoxLayout *layout = new QVBoxLayout();
layout->setMargin( 6 );

mEncodingComboBox = new QComboBox( this );
QLabel *l = new QLabel( c, this );

QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget( l );
hLayout->addWidget( mEncodingComboBox, 1 );
layout->addLayout( hLayout );

QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
Qt::Horizontal, this );
buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
layout->addWidget( buttonBox );
setLayout( layout );

mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );

// Use default encoding if none supplied
QString enc = encoding;
if ( encoding.isEmpty() )
{
QgsSettings settings;
enc = settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString();
}

setEncoding( enc );
}

QString QgsEncodingSelectionDialog::encoding() const
{
return mEncodingComboBox->currentText();
}

void QgsEncodingSelectionDialog::setEncoding( const QString &encoding )
{
// The specified decoding is added if not existing alread, and then set current.
// This should select it.

int encindex = mEncodingComboBox->findText( encoding );
if ( encindex < 0 )
{
mEncodingComboBox->insertItem( 0, encoding );
encindex = 0;
}
mEncodingComboBox->setCurrentIndex( encindex );
}
41 changes: 41 additions & 0 deletions src/gui/qgsencodingfiledialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,45 @@ class GUI_EXPORT QgsEncodingFileDialog: public QFileDialog
bool mCancelAll;
};

/**
* \ingroup gui
* A dialog which presents the user with a choice of file encodings.
* \since QGIS 3.0
**/
class GUI_EXPORT QgsEncodingSelectionDialog: public QDialog
{
Q_OBJECT

public:

/**
* Constructor for QgsEncodingSelectionDialog.
*
* If \a caption is set, it will be used as the caption within the dialog.
*
* The \a encoding argument can be used to specify the encoding initially selected in the dialog.
*/
QgsEncodingSelectionDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr,
const QString &caption = QString(), const QString &encoding = QString(),
Qt::WindowFlags flags = Qt::WindowFlags() );

/**
* Returns the encoding selected within the dialog.
* \see setEncoding()
*/
QString encoding() const;

/**
* Sets the \a encoding selected within the dialog.
* see encoding()
*/
void setEncoding( const QString &encoding );

private:

QComboBox *mEncodingComboBox = nullptr;

};


#endif
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ADD_PYTHON_TEST(PyQgsDelimitedTextProvider test_qgsdelimitedtextprovider.py)
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
ADD_PYTHON_TEST(PyQgsEllipsoidUtils test_qgsellipsoidutils.py)
ADD_PYTHON_TEST(PyQgsEncodingSelectionDialog test_qgsencodingselectiondialog.py)
ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
ADD_PYTHON_TEST(PyQgsExpressionBuilderWidget test_qgsexpressionbuilderwidget.py)
ADD_PYTHON_TEST(PyQgsExpressionLineEdit test_qgsexpressionlineedit.py)
Expand Down
38 changes: 38 additions & 0 deletions tests/src/python/test_qgsencodingselectiondialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsEncodingSelectionDialog
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '21/11/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.gui import QgsEncodingSelectionDialog

from qgis.testing import start_app, unittest

start_app()


class TestQgsEncodingSelectionDialog(unittest.TestCase):

def testGettersSetters(self):
""" test dialog getters/setters """
dlg = qgis.gui.QgsEncodingSelectionDialog(encoding='UTF-16')
self.assertEqual(dlg.encoding(), 'UTF-16')
dlg.setEncoding('UTF-8')
self.assertEqual(dlg.encoding(), 'UTF-8')
# custom encoding option
dlg.setEncoding('trisolarian')
self.assertEqual(dlg.encoding(), 'trisolarian')


if __name__ == '__main__':
unittest.main()

0 comments on commit f660d78

Please sign in to comment.