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