QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsencodingfiledialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsencodingfiledialog.cpp - File dialog which queries the encoding type
3 --------------------------------------
4 Date : 16-Feb-2005
5 Copyright : (C) 2005 by Marco Hugentobler
6 email : marco.hugentobler@autoform.ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsencodingfiledialog.cpp"
18#include "qgsproject.h"
19#include "qgslogger.h"
21#include "qgssettings.h"
22
23#include <QComboBox>
24#include <QPushButton>
25#include <QLabel>
26#include <QLayout>
27#include <QTextCodec>
28#include <QDialogButtonBox>
29
31 const QString &caption, const QString &directory,
32 const QString &filter, const QString &encoding )
33 : QFileDialog( parent, caption, directory, filter )
34{
35 mCancelAll = false;
36 mCancelAllButton = nullptr;
37 mEncodingComboBox = new QComboBox( this );
38 QLabel *l = new QLabel( tr( "Encoding:" ), this );
39
40 setOption( QFileDialog::DontUseNativeDialog );
41 layout()->addWidget( l );
42 layout()->addWidget( mEncodingComboBox );
43
44 mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
45
46 // Use default encoding if none supplied
47 QString enc = encoding;
48 if ( encoding.isEmpty() )
49 {
50 const QgsSettings settings;
51 enc = settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString();
52 }
53
54 // The specified decoding is added if not existing already, and then set current.
55 // This should select it.
56 int encindex = mEncodingComboBox->findText( enc );
57 if ( encindex < 0 )
58 {
59 mEncodingComboBox->insertItem( 0, enc );
60 encindex = 0;
61 }
62 mEncodingComboBox->setCurrentIndex( encindex );
63
64 // if this dialog is being invoked from QgisApp::findFiles_(), then we
65 // need to force selection of the first filter since that corresponds to
66 // the file name we're looking for; even if we're not here from
67 // findFiles_(), it won't hurt to force selection of the first file filter
68 selectNameFilter( nameFilters().at( 0 ) );
69
70 // Connect our slot to get a signal when the user is done with the file dialog
71 connect( this, &QDialog::accepted, this, &QgsEncodingFileDialog::saveUsedEncoding );
72}
73
75{
76 return mEncodingComboBox->currentText();
77}
78
80{
81 QgsSettings settings;
82 settings.setValue( QStringLiteral( "UI/encoding" ), encoding() );
83 QgsDebugMsgLevel( QStringLiteral( "Set encoding %1 as default." ).arg( encoding() ), 2 );
84}
85
87{
88 if ( ! mCancelAllButton )
89 {
90 mCancelAllButton = new QPushButton( tr( "Cancel &All" ), nullptr );
91 layout()->addWidget( mCancelAllButton ); // Ownership transferred, no need to delete later on
92 connect( mCancelAllButton, &QAbstractButton::clicked, this, &QgsEncodingFileDialog::pbnCancelAll_clicked );
93 }
94}
95
97{
98 return mCancelAll;
99}
100
102{
103 mCancelAll = true;
104 // Now, continue as the user clicked the cancel button
105 reject();
106}
107
108QgsEncodingSelectionDialog::QgsEncodingSelectionDialog( QWidget *parent, const QString &caption, const QString &encoding, Qt::WindowFlags flags )
109 : QDialog( parent, flags )
110{
111 QString c = caption;
112 if ( c.isEmpty() )
113 c = tr( "Encoding" );
114
115 setWindowTitle( tr( "Select Encoding" ) );
116
117 QVBoxLayout *layout = new QVBoxLayout();
118 layout->setContentsMargins( 6, 6, 6, 6 );
119
120 mEncodingComboBox = new QComboBox( this );
121 QLabel *l = new QLabel( c, this );
122
123 QHBoxLayout *hLayout = new QHBoxLayout();
124 hLayout->addWidget( l );
125 hLayout->addWidget( mEncodingComboBox, 1 );
126 layout->addLayout( hLayout );
127
128 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
129 Qt::Horizontal, this );
130 buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
131 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
132 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
133 layout->addWidget( buttonBox );
134 setLayout( layout );
135
136 mEncodingComboBox->addItem( tr( "System" ) );
137 mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
138
139 // Use default encoding if none supplied
140 QString enc = encoding;
141 if ( encoding.isEmpty() )
142 {
143 const QgsSettings settings;
144 enc = settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString();
145 }
146
147 setEncoding( enc );
148}
149
151{
152 return mEncodingComboBox->currentText();
153}
154
155void QgsEncodingSelectionDialog::setEncoding( const QString &encoding )
156{
157 // The specified decoding is added if not existing already, and then set current.
158 // This should select it.
159
160 int encindex = mEncodingComboBox->findText( encoding );
161 if ( encindex < 0 )
162 {
163 mEncodingComboBox->insertItem( 0, encoding );
164 encindex = 0;
165 }
166 mEncodingComboBox->setCurrentIndex( encindex );
167}
bool cancelAll()
Returns true if the user clicked 'Cancel All'.
QString encoding() const
Returns a string describing the chosen encoding.
void addCancelAll()
Adds a 'Cancel All' button for the user to click.
QgsEncodingFileDialog(QWidget *parent=nullptr, const QString &caption=QString(), const QString &directory=QString(), const QString &filter=QString(), const QString &encoding=QString())
Constructor for QgsEncodingFileDialog.
QString encoding() const
Returns the encoding selected within the dialog.
QgsEncodingSelectionDialog(QWidget *parent=nullptr, const QString &caption=QString(), const QString &encoding=QString(), Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsEncodingSelectionDialog.
void setEncoding(const QString &encoding)
Sets the encoding selected within the dialog.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QStringList availableEncodings()
Returns a list of available encodings.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39