Skip to content

Commit f660d78

Browse files
committedNov 21, 2017
Add a new dialog for selecting file encoding, QgsEncodingSelectionDialog
Can be used to prompt users for a file encoding choice
1 parent e05cca2 commit f660d78

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed
 

‎python/gui/qgsencodingfiledialog.sip

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ Returns true if the user clicked 'Cancel All'
4949

5050
};
5151

52+
class QgsEncodingSelectionDialog: QDialog
53+
{
54+
%Docstring
55+
A dialog which presents the user with a choice of file encodings.
56+
.. versionadded:: 3.0
57+
*
58+
%End
59+
60+
%TypeHeaderCode
61+
#include "qgsencodingfiledialog.h"
62+
%End
63+
public:
64+
65+
QgsEncodingSelectionDialog( QWidget *parent /TransferThis/ = 0,
66+
const QString &caption = QString(), const QString &encoding = QString(),
67+
Qt::WindowFlags flags = Qt::WindowFlags() );
68+
%Docstring
69+
Constructor for QgsEncodingSelectionDialog.
70+
71+
If ``caption`` is set, it will be used as the caption within the dialog.
72+
73+
The ``encoding`` argument can be used to specify the encoding initially selected in the dialog.
74+
%End
75+
76+
QString encoding() const;
77+
%Docstring
78+
Returns the encoding selected within the dialog.
79+
.. seealso:: setEncoding()
80+
:rtype: str
81+
%End
82+
83+
void setEncoding( const QString &encoding );
84+
%Docstring
85+
Sets the ``encoding`` selected within the dialog.
86+
see encoding()
87+
%End
88+
89+
};
90+
91+
5292
/************************************************************************
5393
* This file has been generated automatically from *
5494
* *

‎src/gui/qgsencodingfiledialog.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QLabel>
2525
#include <QLayout>
2626
#include <QTextCodec>
27+
#include <QDialogButtonBox>
2728

2829
QgsEncodingFileDialog::QgsEncodingFileDialog( QWidget *parent,
2930
const QString &caption, const QString &directory,
@@ -102,3 +103,63 @@ void QgsEncodingFileDialog::pbnCancelAll_clicked()
102103
// Now, continue as the user clicked the cancel button
103104
reject();
104105
}
106+
107+
QgsEncodingSelectionDialog::QgsEncodingSelectionDialog( QWidget *parent, const QString &caption, const QString &encoding, Qt::WindowFlags flags )
108+
: QDialog( parent, flags )
109+
{
110+
QString c = caption;
111+
if ( c.isEmpty() )
112+
c = tr( "Encoding" );
113+
114+
setWindowTitle( tr( "Select Encoding" ) );
115+
116+
QVBoxLayout *layout = new QVBoxLayout();
117+
layout->setMargin( 6 );
118+
119+
mEncodingComboBox = new QComboBox( this );
120+
QLabel *l = new QLabel( c, this );
121+
122+
QHBoxLayout *hLayout = new QHBoxLayout();
123+
hLayout->addWidget( l );
124+
hLayout->addWidget( mEncodingComboBox, 1 );
125+
layout->addLayout( hLayout );
126+
127+
QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
128+
Qt::Horizontal, this );
129+
buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
130+
connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
131+
connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
132+
layout->addWidget( buttonBox );
133+
setLayout( layout );
134+
135+
mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
136+
137+
// Use default encoding if none supplied
138+
QString enc = encoding;
139+
if ( encoding.isEmpty() )
140+
{
141+
QgsSettings settings;
142+
enc = settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString();
143+
}
144+
145+
setEncoding( enc );
146+
}
147+
148+
QString QgsEncodingSelectionDialog::encoding() const
149+
{
150+
return mEncodingComboBox->currentText();
151+
}
152+
153+
void QgsEncodingSelectionDialog::setEncoding( const QString &encoding )
154+
{
155+
// The specified decoding is added if not existing alread, and then set current.
156+
// This should select it.
157+
158+
int encindex = mEncodingComboBox->findText( encoding );
159+
if ( encindex < 0 )
160+
{
161+
mEncodingComboBox->insertItem( 0, encoding );
162+
encindex = 0;
163+
}
164+
mEncodingComboBox->setCurrentIndex( encindex );
165+
}

‎src/gui/qgsencodingfiledialog.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,45 @@ class GUI_EXPORT QgsEncodingFileDialog: public QFileDialog
6161
bool mCancelAll;
6262
};
6363

64+
/**
65+
* \ingroup gui
66+
* A dialog which presents the user with a choice of file encodings.
67+
* \since QGIS 3.0
68+
**/
69+
class GUI_EXPORT QgsEncodingSelectionDialog: public QDialog
70+
{
71+
Q_OBJECT
72+
73+
public:
74+
75+
/**
76+
* Constructor for QgsEncodingSelectionDialog.
77+
*
78+
* If \a caption is set, it will be used as the caption within the dialog.
79+
*
80+
* The \a encoding argument can be used to specify the encoding initially selected in the dialog.
81+
*/
82+
QgsEncodingSelectionDialog( QWidget *parent SIP_TRANSFERTHIS = nullptr,
83+
const QString &caption = QString(), const QString &encoding = QString(),
84+
Qt::WindowFlags flags = Qt::WindowFlags() );
85+
86+
/**
87+
* Returns the encoding selected within the dialog.
88+
* \see setEncoding()
89+
*/
90+
QString encoding() const;
91+
92+
/**
93+
* Sets the \a encoding selected within the dialog.
94+
* see encoding()
95+
*/
96+
void setEncoding( const QString &encoding );
97+
98+
private:
99+
100+
QComboBox *mEncodingComboBox = nullptr;
101+
102+
};
103+
104+
64105
#endif

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ ADD_PYTHON_TEST(PyQgsDelimitedTextProvider test_qgsdelimitedtextprovider.py)
4848
ADD_PYTHON_TEST(PyQgsDistanceArea test_qgsdistancearea.py)
4949
ADD_PYTHON_TEST(PyQgsEditWidgets test_qgseditwidgets.py)
5050
ADD_PYTHON_TEST(PyQgsEllipsoidUtils test_qgsellipsoidutils.py)
51+
ADD_PYTHON_TEST(PyQgsEncodingSelectionDialog test_qgsencodingselectiondialog.py)
5152
ADD_PYTHON_TEST(PyQgsExpression test_qgsexpression.py)
5253
ADD_PYTHON_TEST(PyQgsExpressionBuilderWidget test_qgsexpressionbuilderwidget.py)
5354
ADD_PYTHON_TEST(PyQgsExpressionLineEdit test_qgsexpressionlineedit.py)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsEncodingSelectionDialog
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '21/11/2017'
11+
__copyright__ = 'Copyright 2017, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.gui import QgsEncodingSelectionDialog
18+
19+
from qgis.testing import start_app, unittest
20+
21+
start_app()
22+
23+
24+
class TestQgsEncodingSelectionDialog(unittest.TestCase):
25+
26+
def testGettersSetters(self):
27+
""" test dialog getters/setters """
28+
dlg = qgis.gui.QgsEncodingSelectionDialog(encoding='UTF-16')
29+
self.assertEqual(dlg.encoding(), 'UTF-16')
30+
dlg.setEncoding('UTF-8')
31+
self.assertEqual(dlg.encoding(), 'UTF-8')
32+
# custom encoding option
33+
dlg.setEncoding('trisolarian')
34+
self.assertEqual(dlg.encoding(), 'trisolarian')
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.