QGIS API Documentation  3.6.0-Noosa (5873452)
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 : [email protected]
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 
16 #include "qgsencodingfiledialog.h"
17 #include "qgsproject.h"
18 #include "qgslogger.h"
19 #include "qgsvectordataprovider.h"
20 #include "qgssettings.h"
21 
22 #include <QComboBox>
23 #include <QPushButton>
24 #include <QLabel>
25 #include <QLayout>
26 #include <QTextCodec>
27 #include <QDialogButtonBox>
28 
30  const QString &caption, const QString &directory,
31  const QString &filter, const QString &encoding )
32  : QFileDialog( parent, caption, directory, filter )
33 {
34  mCancelAll = false;
35  mCancelAllButton = nullptr;
36  mEncodingComboBox = new QComboBox( this );
37  QLabel *l = new QLabel( tr( "Encoding:" ), this );
38 
39  setOption( QFileDialog::DontUseNativeDialog );
40  layout()->addWidget( l );
41  layout()->addWidget( mEncodingComboBox );
42 
43  mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
44 
45  // Use default encoding if none supplied
46  QString enc = encoding;
47  if ( encoding.isEmpty() )
48  {
49  QgsSettings settings;
50  enc = settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString();
51  }
52 
53  // The specified decoding is added if not existing alread, and then set current.
54  // This should select it.
55  int encindex = mEncodingComboBox->findText( enc );
56  if ( encindex < 0 )
57  {
58  mEncodingComboBox->insertItem( 0, enc );
59  encindex = 0;
60  }
61  mEncodingComboBox->setCurrentIndex( encindex );
62 
63  // if this dialog is being invoked from QgisApp::findFiles_(), then we
64  // need to force selection of the first filter since that corresponds to
65  // the file name we're looking for; even if we're not here from
66  // findFiles_(), it won't hurt to force selection of the first file filter
67  selectNameFilter( nameFilters().at( 0 ) );
68 
69  // Connect our slot to get a signal when the user is done with the file dialog
70  connect( this, &QDialog::accepted, this, &QgsEncodingFileDialog::saveUsedEncoding );
71 }
72 
74 {
75  return mEncodingComboBox->currentText();
76 }
77 
79 {
80  QgsSettings settings;
81  settings.setValue( QStringLiteral( "UI/encoding" ), encoding() );
82  QgsDebugMsg( QStringLiteral( "Set encoding %1 as default." ).arg( encoding() ) );
83 }
84 
86 {
87  if ( ! mCancelAllButton )
88  {
89  mCancelAllButton = new QPushButton( tr( "Cancel &All" ), nullptr );
90  layout()->addWidget( mCancelAllButton ); // Ownership transferred, no need to delete later on
91  connect( mCancelAllButton, &QAbstractButton::clicked, this, &QgsEncodingFileDialog::pbnCancelAll_clicked );
92  }
93 }
94 
96 {
97  return mCancelAll;
98 }
99 
101 {
102  mCancelAll = true;
103  // Now, continue as the user clicked the cancel button
104  reject();
105 }
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->addItem( tr( "System" ) );
136  mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
137 
138  // Use default encoding if none supplied
139  QString enc = encoding;
140  if ( encoding.isEmpty() )
141  {
142  QgsSettings settings;
143  enc = settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString();
144  }
145 
146  setEncoding( enc );
147 }
148 
150 {
151  return mEncodingComboBox->currentText();
152 }
153 
155 {
156  // The specified decoding is added if not existing alread, and then set current.
157  // This should select it.
158 
159  int encindex = mEncodingComboBox->findText( encoding );
160  if ( encindex < 0 )
161  {
162  mEncodingComboBox->insertItem( 0, encoding );
163  encindex = 0;
164  }
165  mEncodingComboBox->setCurrentIndex( encindex );
166 }
QString encoding() const
Returns a string describing the chosen encoding.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setEncoding(const QString &encoding)
Sets the encoding selected within the dialog.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsEncodingSelectionDialog(QWidget *parent=nullptr, const QString &caption=QString(), const QString &encoding=QString(), Qt::WindowFlags flags=Qt::WindowFlags())
Constructor for QgsEncodingSelectionDialog.
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
void addCancelAll()
Adds a &#39;Cancel All&#39; button for the user to click.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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()
Returns true if the user clicked &#39;Cancel All&#39;.
QString encoding() const
Returns the encoding selected within the dialog.
static QStringList availableEncodings()
Returns a list of available encodings.