QGIS API Documentation  2.99.0-Master (716ff6c)
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 
29  const QString &caption, const QString &directory,
30  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  QgsSettings settings;
49  enc = settings.value( QStringLiteral( "UI/encoding" ), "System" ).toString();
50  }
51 
52  // The specified decoding is added if not existing alread, 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  QgsDebugMsg( QString( "Set encoding " + encoding() + " as default." ) );
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 }
QString encoding() const
Returns a string describing the chosen encoding.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:54
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void addCancelAll()
Adds a &#39;Cancel All&#39; 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())
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
bool cancelAll()
Returns true if the user clicked &#39;Cancel All&#39;.
static QStringList availableEncodings()
Returns a list of available encodings.