QGIS API Documentation  2.99.0-Master (8ec3eaf)
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 
21 #include <QSettings>
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  QSettings 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, SIGNAL( accepted() ), this, SLOT( saveUsedEncoding() ) );
70 
71 
72 }
73 
75 {
76 
77 }
78 
80 {
81  return mEncodingComboBox->currentText();
82 }
83 
85 {
86  QSettings settings;
87  settings.setValue( QStringLiteral( "/UI/encoding" ), encoding() );
88  QgsDebugMsg( QString( "Set encoding " + encoding() + " as default." ) );
89 }
90 
92 {
93  if ( ! mCancelAllButton )
94  {
95  mCancelAllButton = new QPushButton( tr( "Cancel &All" ), nullptr );
96  layout()->addWidget( mCancelAllButton ); // Ownership transfered, no need to delete later on
97  connect( mCancelAllButton, SIGNAL( clicked() ), this, SLOT( pbnCancelAll_clicked() ) );
98  }
99 }
100 
102 {
103  return mCancelAll;
104 }
105 
107 {
108  mCancelAll = true;
109  // Now, continue as the user clicked the cancel button
110  reject();
111 }
QString encoding() const
Returns a string describing the chosen encoding.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
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())
bool cancelAll()
Returns true if the user clicked &#39;Cancel All&#39;.
static QStringList availableEncodings()
Returns a list of available encodings.