/
qgsvectorlayersaveasdialog.cpp
192 lines (164 loc) · 6.06 KB
/
qgsvectorlayersaveasdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/***************************************************************************
qgsvectorlayersaveasdialog.h
Dialog to select destination, type and crs for ogr layers
-------------------
begin : Mon Mar 22 2010
copyright : (C) 2010 by Juergen E. Fischer
email : jef at norbit dot de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsvectorlayersaveasdialog.h"
#include "qgsgenericprojectionselector.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorfilewriter.h"
#include "qgscoordinatereferencesystem.h"
#include <QSettings>
#include <QFileDialog>
#include <QTextCodec>
QgsVectorLayerSaveAsDialog::QgsVectorLayerSaveAsDialog( long srsid, QWidget* parent, Qt::WFlags fl )
: QDialog( parent, fl )
, mCRS( srsid )
{
setupUi( this );
QSettings settings;
restoreGeometry( settings.value( "/Windows/VectorLayerSaveAs/geometry" ).toByteArray() );
QMap<QString, QString> map = QgsVectorFileWriter::ogrDriverList();
mFormatComboBox->blockSignals( true );
for ( QMap< QString, QString>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
{
mFormatComboBox->addItem( it.key(), it.value() );
}
QString format = settings.value( "/UI/lastVectorFormat", "ESRI Shapefile" ).toString();
mFormatComboBox->setCurrentIndex( mFormatComboBox->findData( format ) );
mFormatComboBox->blockSignals( false );
mEncodingComboBox->addItems( QgsVectorDataProvider::availableEncodings() );
QString enc = settings.value( "/UI/encoding", "System" ).toString();
int idx = mEncodingComboBox->findText( enc );
if ( idx < 0 )
{
mEncodingComboBox->insertItem( 0, enc );
idx = 0;
}
mCRSSelection->clear();
mCRSSelection->addItems( QStringList() << tr( "Layer CRS" ) << tr( "Project CRS" ) << tr( "Selected CRS" ) );
QgsCoordinateReferenceSystem srs( srsid, QgsCoordinateReferenceSystem::InternalCrsId );
leCRS->setText( srs.description() );
mEncodingComboBox->setCurrentIndex( idx );
on_mFormatComboBox_currentIndexChanged( mFormatComboBox->currentIndex() );
}
QgsVectorLayerSaveAsDialog::~QgsVectorLayerSaveAsDialog()
{
QSettings settings;
settings.setValue( "/Windows/VectorLayerSaveAs/geometry", saveGeometry() );
}
void QgsVectorLayerSaveAsDialog::accept()
{
QSettings settings;
settings.setValue( "/UI/lastVectorFileFilterDir", QFileInfo( filename() ).absolutePath() );
settings.setValue( "/UI/lastVectorFormat", format() );
settings.setValue( "/UI/encoding", encoding() );
QDialog::accept();
}
void QgsVectorLayerSaveAsDialog::on_mCRSSelection_currentIndexChanged( int idx )
{
leCRS->setEnabled( idx == 2 );
}
void QgsVectorLayerSaveAsDialog::on_mFormatComboBox_currentIndexChanged( int idx )
{
Q_UNUSED( idx );
browseFilename->setEnabled( true );
leFilename->setEnabled( true );
if ( format() == "KML" )
{
mEncodingComboBox->setCurrentIndex( mEncodingComboBox->findText( "UTF-8" ) );
mEncodingComboBox->setDisabled( true );
mSkipAttributeCreation->setEnabled( true );
}
else if ( format() == "DXF" )
{
mSkipAttributeCreation->setChecked( true );
mSkipAttributeCreation->setDisabled( true );
}
else
{
mEncodingComboBox->setEnabled( true );
mSkipAttributeCreation->setEnabled( true );
}
}
void QgsVectorLayerSaveAsDialog::on_browseFilename_clicked()
{
QSettings settings;
QString dirName = leFilename->text().isEmpty() ? settings.value( "/UI/lastVectorFileFilterDir", "." ).toString() : leFilename->text();
QString filterString = QgsVectorFileWriter::filterForDriver( format() );
QString outputFile = QFileDialog::getSaveFileName( 0, tr( "Save layer as..." ), dirName, filterString );
if ( !outputFile.isNull() )
{
leFilename->setText( outputFile );
}
}
void QgsVectorLayerSaveAsDialog::on_browseCRS_clicked()
{
QgsGenericProjectionSelector * mySelector = new QgsGenericProjectionSelector();
if ( mCRS >= 0 )
mySelector->setSelectedCrsId( mCRS );
mySelector->setMessage( tr( "Select the coordinate reference system for the vector file. "
"The data points will be transformed from the layer coordinate reference system." ) );
if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem srs( mySelector->selectedCrsId(), QgsCoordinateReferenceSystem::InternalCrsId );
mCRS = srs.srsid();
leCRS->setText( srs.description() );
mCRSSelection->setCurrentIndex( 2 );
}
delete mySelector;
}
QString QgsVectorLayerSaveAsDialog::filename() const
{
return leFilename->text();
}
QString QgsVectorLayerSaveAsDialog::encoding() const
{
return mEncodingComboBox->currentText();
}
QString QgsVectorLayerSaveAsDialog::format() const
{
return mFormatComboBox->itemData( mFormatComboBox->currentIndex() ).toString();
}
long QgsVectorLayerSaveAsDialog::crs() const
{
if ( mCRSSelection->currentIndex() == 0 )
{
return -1; // Layer CRS
}
else if ( mCRSSelection->currentIndex() == 1 )
{
return -2; // Project CRS
}
else
{
return mCRS;
}
}
QStringList QgsVectorLayerSaveAsDialog::datasourceOptions() const
{
return mOgrDatasourceOptions->toPlainText().split( "\n" );
}
QStringList QgsVectorLayerSaveAsDialog::layerOptions() const
{
return mOgrLayerOptions->toPlainText().split( "\n" );
}
bool QgsVectorLayerSaveAsDialog::skipAttributeCreation() const
{
return mSkipAttributeCreation->isChecked();
}
bool QgsVectorLayerSaveAsDialog::addToCanvas() const
{
return mAddToCanvas->isChecked();
}