diff --git a/python/gui/qgsencodingfiledialog.sip b/python/gui/qgsencodingfiledialog.sip index a502b553753..20dc9824bb8 100644 --- a/python/gui/qgsencodingfiledialog.sip +++ b/python/gui/qgsencodingfiledialog.sip @@ -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 * * * diff --git a/src/gui/qgsencodingfiledialog.cpp b/src/gui/qgsencodingfiledialog.cpp index 9f08b16581a..a4999d67fd7 100644 --- a/src/gui/qgsencodingfiledialog.cpp +++ b/src/gui/qgsencodingfiledialog.cpp @@ -24,6 +24,7 @@ #include #include #include +#include QgsEncodingFileDialog::QgsEncodingFileDialog( QWidget *parent, const QString &caption, const QString &directory, @@ -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 ); +} diff --git a/src/gui/qgsencodingfiledialog.h b/src/gui/qgsencodingfiledialog.h index 96c67826943..0234af6e4ba 100644 --- a/src/gui/qgsencodingfiledialog.h +++ b/src/gui/qgsencodingfiledialog.h @@ -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 diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index 4bd048750d5..e57e4383ec5 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -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) diff --git a/tests/src/python/test_qgsencodingselectiondialog.py b/tests/src/python/test_qgsencodingselectiondialog.py new file mode 100644 index 00000000000..7f9f29f461a --- /dev/null +++ b/tests/src/python/test_qgsencodingselectiondialog.py @@ -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()