QGIS API Documentation 3.99.0-Master (357b655ed83)
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
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
18#include "qgslogger.h"
19#include "qgsproject.h"
20#include "qgssettings.h"
22
23#include <QComboBox>
24#include <QDialogButtonBox>
25#include <QLabel>
26#include <QLayout>
27#include <QPushButton>
28#include <QString>
29#include <QTextCodec>
30
31#include "moc_qgsencodingfiledialog.cpp"
32
33using namespace Qt::StringLiterals;
34
35QgsEncodingFileDialog::QgsEncodingFileDialog( QWidget *parent, const QString &caption, const QString &directory, const QString &filter, const QString &encoding )
36 : QFileDialog( parent, caption, directory, filter )
37{
38 mCancelAll = false;
39 mCancelAllButton = nullptr;
40 mEncodingComboBox = new QComboBox( this );
41 QLabel *l = new QLabel( tr( "Encoding:" ), this );
42
43 setOption( QFileDialog::DontUseNativeDialog );
44 layout()->addWidget( l );
45 layout()->addWidget( mEncodingComboBox );
46
47 mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
48
49 // Use default encoding if none supplied
50 QString enc = encoding;
51 if ( encoding.isEmpty() )
52 {
53 const QgsSettings settings;
54 enc = settings.value( u"UI/encoding"_s, "System" ).toString();
55 }
56
57 // The specified decoding is added if not existing already, and then set current.
58 // This should select it.
59 int encindex = mEncodingComboBox->findText( enc );
60 if ( encindex < 0 )
61 {
62 mEncodingComboBox->insertItem( 0, enc );
63 encindex = 0;
64 }
65 mEncodingComboBox->setCurrentIndex( encindex );
66
67 // if this dialog is being invoked from QgisApp::findFiles_(), then we
68 // need to force selection of the first filter since that corresponds to
69 // the file name we're looking for; even if we're not here from
70 // findFiles_(), it won't hurt to force selection of the first file filter
71 selectNameFilter( nameFilters().at( 0 ) );
72
73 // Connect our slot to get a signal when the user is done with the file dialog
74 connect( this, &QDialog::accepted, this, &QgsEncodingFileDialog::saveUsedEncoding );
75}
76
78{
79 return mEncodingComboBox->currentText();
80}
81
83{
84 QgsSettings settings;
85 settings.setValue( u"UI/encoding"_s, encoding() );
86 QgsDebugMsgLevel( u"Set encoding %1 as default."_s.arg( encoding() ), 2 );
87}
88
90{
91 if ( !mCancelAllButton )
92 {
93 mCancelAllButton = new QPushButton( tr( "Cancel &All" ), nullptr );
94 layout()->addWidget( mCancelAllButton ); // Ownership transferred, no need to delete later on
95 connect( mCancelAllButton, &QAbstractButton::clicked, this, &QgsEncodingFileDialog::pbnCancelAll_clicked );
96 }
97}
98
100{
101 return mCancelAll;
102}
103
105{
106 mCancelAll = true;
107 // Now, continue as the user clicked the cancel button
108 reject();
109}
110
111QgsEncodingSelectionDialog::QgsEncodingSelectionDialog( QWidget *parent, const QString &caption, const QString &encoding, Qt::WindowFlags flags )
112 : QDialog( parent, flags )
113{
114 QString c = caption;
115 if ( c.isEmpty() )
116 c = tr( "Encoding" );
117
118 setWindowTitle( tr( "Select Encoding" ) );
119
120 QVBoxLayout *layout = new QVBoxLayout();
121 layout->setContentsMargins( 6, 6, 6, 6 );
122
123 mEncodingComboBox = new QComboBox( this );
124 QLabel *l = new QLabel( c, this );
125
126 QHBoxLayout *hLayout = new QHBoxLayout();
127 hLayout->addWidget( l );
128 hLayout->addWidget( mEncodingComboBox, 1 );
129 layout->addLayout( hLayout );
130
131 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
132 buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
133 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
134 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
135 layout->addWidget( buttonBox );
136 setLayout( layout );
137
138 mEncodingComboBox->addItem( tr( "System" ) );
139 mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
140
141 // Use default encoding if none supplied
142 QString enc = encoding;
143 if ( encoding.isEmpty() )
144 {
145 const QgsSettings settings;
146 enc = settings.value( u"UI/encoding"_s, "System" ).toString();
147 }
148
149 setEncoding( enc );
150}
151
153{
154 return mEncodingComboBox->currentText();
155}
156
158{
159 // The specified decoding is added if not existing already, and then set current.
160 // This should select it.
161
162 int encindex = mEncodingComboBox->findText( encoding );
163 if ( encindex < 0 )
164 {
165 mEncodingComboBox->insertItem( 0, encoding );
166 encindex = 0;
167 }
168 mEncodingComboBox->setCurrentIndex( encindex );
169}
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.
bool cancelAll() const
Returns true if the user clicked 'Cancel All'.
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.
Stores settings for use within QGIS.
Definition qgssettings.h:68
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:63