Skip to content

Commit 5fd8567

Browse files
committedFeb 24, 2014
use native file selection in new shapefile dialog (implements #9637)
1 parent 5db13ab commit 5fd8567

File tree

3 files changed

+81
-60
lines changed

3 files changed

+81
-60
lines changed
 

‎src/gui/qgsnewvectorlayerdialog.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
#include "qgslogger.h"
2222
#include "qgscoordinatereferencesystem.h"
2323
#include "qgsgenericprojectionselector.h"
24-
#include <QPushButton>
24+
#include "qgsproviderregistry.h"
25+
#include "qgsvectordataprovider.h"
26+
#include "qgsvectorfilewriter.h"
2527

28+
#include <QPushButton>
29+
#include <QComboBox>
2630
#include <QLibrary>
2731
#include <QSettings>
28-
#include "qgsencodingfiledialog.h"
29-
#include "qgsproviderregistry.h"
32+
#include <QFileDialog>
3033

3134

3235
QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl )
@@ -61,6 +64,21 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl
6164
mFileFormatLabel->setVisible( false );
6265
}
6366

67+
mFileEncoding->addItems( QgsVectorDataProvider::availableEncodings() );
68+
69+
// Use default encoding if none supplied
70+
QString enc = QSettings().value( "/UI/encoding", "System" ).toString();
71+
72+
// The specified decoding is added if not existing alread, and then set current.
73+
// This should select it.
74+
int encindex = mFileEncoding->findText( enc );
75+
if ( encindex < 0 )
76+
{
77+
mFileEncoding->insertItem( 0, enc );
78+
encindex = 0;
79+
}
80+
mFileEncoding->setCurrentIndex( encindex );
81+
6482
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
6583

6684
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << "id" << "Integer" << "10" << "" ) );
@@ -203,6 +221,11 @@ QString QgsNewVectorLayerDialog::selectedFileFormat() const
203221
return myType;
204222
}
205223

224+
QString QgsNewVectorLayerDialog::selectedFileEncoding() const
225+
{
226+
return mFileEncoding->currentText();
227+
}
228+
206229
void QgsNewVectorLayerDialog::nameChanged( QString name )
207230
{
208231
mAddAttributeButton->setDisabled( name.isEmpty() || mAttributeView->findItems( name, Qt::MatchExactly ).size() > 0 );
@@ -225,50 +248,27 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget* parent, QString* pE
225248

226249
QGis::WkbType geometrytype = geomDialog.selectedType();
227250
QString fileformat = geomDialog.selectedFileFormat();
251+
QString enc = geomDialog.selectedFileEncoding();
228252
int crsId = geomDialog.selectedCrsId();
229253
QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );
230254

231255
QList< QPair<QString, QString> > attributes;
232256
geomDialog.attributes( attributes );
233257

234-
QString enc;
235-
QString fileName;
236-
237258
QSettings settings;
238259
QString lastUsedDir = settings.value( "/UI/lastVectorFileFilterDir", "." ).toString();
239-
240-
QgsDebugMsg( "Saving vector file dialog without filters: " );
241-
242-
QgsEncodingFileDialog* openFileDialog =
243-
new QgsEncodingFileDialog( parent, tr( "Save As" ), lastUsedDir, "", QString( "" ) );
244-
245-
openFileDialog->setFileMode( QFileDialog::AnyFile );
246-
openFileDialog->setAcceptMode( QFileDialog::AcceptSave );
247-
openFileDialog->setConfirmOverwrite( true );
248-
249-
if ( settings.contains( "/UI/lastVectorFileFilter" ) )
250-
{
251-
QString lastUsedFilter = settings.value( "/UI/lastVectorFileFilter", QVariant( QString::null ) ).toString();
252-
openFileDialog->selectFilter( lastUsedFilter );
253-
}
254-
255-
if ( openFileDialog->exec() == QDialog::Rejected )
260+
QString filterString = QgsVectorFileWriter::filterForDriver( fileformat );
261+
QString fileName = QFileDialog::getSaveFileName( 0, tr( "Save layer as..." ), lastUsedDir, filterString );
262+
if ( fileName.isNull() )
256263
{
257-
delete openFileDialog;
258-
return QString();
264+
return fileName;
259265
}
260266

261-
fileName = openFileDialog->selectedFiles().first();
262-
263267
if ( fileformat == "ESRI Shapefile" && !fileName.endsWith( ".shp", Qt::CaseInsensitive ) )
264268
fileName += ".shp";
265269

266-
enc = openFileDialog->encoding();
267-
268-
settings.setValue( "/UI/lastVectorFileFilter", openFileDialog->selectedFilter() );
269-
settings.setValue( "/UI/lastVectorFileFilterDir", openFileDialog->directory().absolutePath() );
270-
271-
delete openFileDialog;
270+
settings.setValue( "/UI/lastVectorFileFilterDir", QFileInfo( fileName ).absolutePath() );
271+
settings.setValue( "/UI/encoding", enc );
272272

273273
//try to create the new layer with OGRProvider instead of QgsVectorFileWriter
274274
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();

‎src/gui/qgsnewvectorlayerdialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
4040
void attributes( QList< QPair<QString, QString> >& at ) const;
4141
/**Returns the file format for storage*/
4242
QString selectedFileFormat() const;
43+
/**Returns the file format for storage*/
44+
QString selectedFileEncoding() const;
4345
/**Returns the selected crs id*/
4446
int selectedCrsId() const;
4547

‎src/ui/qgsnewvectorlayerdialogbase.ui

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,7 @@
1717
<bool>true</bool>
1818
</property>
1919
<layout class="QGridLayout">
20-
<item row="0" column="0">
21-
<widget class="QLabel" name="mFileFormatLabel">
22-
<property name="enabled">
23-
<bool>true</bool>
24-
</property>
25-
<property name="text">
26-
<string>File format</string>
27-
</property>
28-
<property name="buddy">
29-
<cstring>mFileFormatComboBox</cstring>
30-
</property>
31-
</widget>
32-
</item>
33-
<item row="0" column="1" colspan="2">
34-
<widget class="QComboBox" name="mFileFormatComboBox">
35-
<property name="enabled">
36-
<bool>true</bool>
37-
</property>
38-
</widget>
39-
</item>
40-
<item row="1" column="0" colspan="3">
20+
<item row="2" column="0" colspan="3">
4121
<widget class="QGroupBox" name="buttonGroup1">
4222
<property name="title">
4323
<string>Type</string>
@@ -67,7 +47,7 @@
6747
</layout>
6848
</widget>
6949
</item>
70-
<item row="4" column="0" colspan="3">
50+
<item row="9" column="0" colspan="3">
7151
<widget class="QGroupBox" name="groupBox">
7252
<property name="title">
7353
<string>New attribute</string>
@@ -154,7 +134,7 @@
154134
</layout>
155135
</widget>
156136
</item>
157-
<item row="6" column="0" colspan="3">
137+
<item row="11" column="0" colspan="3">
158138
<widget class="QGroupBox" name="groupBox_2">
159139
<property name="title">
160140
<string>Attributes list</string>
@@ -238,7 +218,7 @@
238218
</layout>
239219
</widget>
240220
</item>
241-
<item row="8" column="0" colspan="3">
221+
<item row="13" column="0" colspan="3">
242222
<widget class="QDialogButtonBox" name="buttonBox">
243223
<property name="orientation">
244224
<enum>Qt::Horizontal</enum>
@@ -248,7 +228,7 @@
248228
</property>
249229
</widget>
250230
</item>
251-
<item row="3" column="2">
231+
<item row="8" column="2">
252232
<widget class="QPushButton" name="pbnChangeSpatialRefSys">
253233
<property name="toolTip">
254234
<string>Specify the coordinate reference system of the layer's geometry.</string>
@@ -261,18 +241,57 @@
261241
</property>
262242
</widget>
263243
</item>
264-
<item row="3" column="0" colspan="2">
244+
<item row="8" column="0" colspan="2">
265245
<widget class="QLineEdit" name="leSpatialRefSys">
266246
<property name="readOnly">
267247
<bool>true</bool>
268248
</property>
269249
</widget>
270250
</item>
251+
<item row="5" column="0">
252+
<widget class="QLabel" name="mFileFormatLabel">
253+
<property name="enabled">
254+
<bool>true</bool>
255+
</property>
256+
<property name="text">
257+
<string>File format</string>
258+
</property>
259+
<property name="buddy">
260+
<cstring>mFileFormatComboBox</cstring>
261+
</property>
262+
</widget>
263+
</item>
264+
<item row="4" column="0">
265+
<widget class="QLabel" name="mFileFormatLabel_2">
266+
<property name="enabled">
267+
<bool>true</bool>
268+
</property>
269+
<property name="text">
270+
<string>File encoding</string>
271+
</property>
272+
<property name="buddy">
273+
<cstring>mFileFormatComboBox</cstring>
274+
</property>
275+
</widget>
276+
</item>
277+
<item row="4" column="1" colspan="2">
278+
<widget class="QComboBox" name="mFileFormatComboBox">
279+
<property name="enabled">
280+
<bool>true</bool>
281+
</property>
282+
</widget>
283+
</item>
284+
<item row="5" column="1" colspan="2">
285+
<widget class="QComboBox" name="mFileEncoding">
286+
<property name="enabled">
287+
<bool>true</bool>
288+
</property>
289+
</widget>
290+
</item>
271291
</layout>
272292
</widget>
273293
<layoutdefault spacing="6" margin="11"/>
274294
<tabstops>
275-
<tabstop>mFileFormatComboBox</tabstop>
276295
<tabstop>mPointRadioButton</tabstop>
277296
<tabstop>mLineRadioButton</tabstop>
278297
<tabstop>mPolygonRadioButton</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.