/
qgsnewvectorlayerdialog.cpp
313 lines (268 loc) · 10.3 KB
/
qgsnewvectorlayerdialog.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/***************************************************************************
qgsnewvectorlayerdialog.cpp - description
-------------------
begin : October 2004
copyright : (C) 2004 by Marco Hugentobler
email : marco.hugentobler@autoform.ch
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsnewvectorlayerdialog.h"
#include "qgsapplication.h"
#include "qgis.h"
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsgenericprojectionselector.h"
#include "qgsproviderregistry.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorfilewriter.h"
#include <QPushButton>
#include <QComboBox>
#include <QLibrary>
#include <QSettings>
#include <QFileDialog>
QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setupUi( this );
QSettings settings;
restoreGeometry( settings.value( "/Windows/NewVectorLayer/geometry" ).toByteArray() );
mAddAttributeButton->setIcon( QgsApplication::getThemeIcon( "/mActionNewAttribute.png" ) );
mRemoveAttributeButton->setIcon( QgsApplication::getThemeIcon( "/mActionDeleteAttribute.png" ) );
mTypeBox->addItem( tr( "Text data" ), "String" );
mTypeBox->addItem( tr( "Whole number" ), "Integer" );
mTypeBox->addItem( tr( "Decimal number" ), "Real" );
mTypeBox->addItem( tr( "Date" ), "Date" );
mWidth->setValidator( new QIntValidator( 1, 255, this ) );
mPrecision->setValidator( new QIntValidator( 0, 15, this ) );
mPointRadioButton->setChecked( true );
mFileFormatComboBox->addItem( tr( "ESRI Shapefile" ), "ESRI Shapefile" );
#if 0
// Disabled until provider properly supports editing the created file formats
mFileFormatComboBox->addItem( tr( "Comma Separated Value" ), "Comma Separated Value" );
mFileFormatComboBox->addItem( tr( "GML" ), "GML" );
mFileFormatComboBox->addItem( tr( "Mapinfo File" ), "Mapinfo File" );
#endif
if ( mFileFormatComboBox->count() == 1 )
{
mFileFormatComboBox->setVisible( false );
mFileFormatLabel->setVisible( false );
}
mFileEncoding->addItems( QgsVectorDataProvider::availableEncodings() );
// Use default encoding if none supplied
QString enc = QSettings().value( "/UI/encoding", "System" ).toString();
// The specified decoding is added if not existing alread, and then set current.
// This should select it.
int encindex = mFileEncoding->findText( enc );
if ( encindex < 0 )
{
mFileEncoding->insertItem( 0, enc );
encindex = 0;
}
mFileEncoding->setCurrentIndex( encindex );
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << "id" << "Integer" << "10" << "" ) );
QgsCoordinateReferenceSystem srs;
srs.createFromOgcWmsCrs( settings.value( "/Projections/layerDefaultCrs", GEO_EPSG_CRS_AUTHID ).toString() );
srs.validate();
mCrsId = srs.srsid();
leSpatialRefSys->setText( srs.authid() + " - " + srs.description() );
connect( mNameEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged( QString ) ) );
connect( mAttributeView, SIGNAL( itemSelectionChanged() ), this, SLOT( selectionChanged() ) );
mAddAttributeButton->setEnabled( false );
mRemoveAttributeButton->setEnabled( false );
}
QgsNewVectorLayerDialog::~QgsNewVectorLayerDialog()
{
QSettings settings;
settings.setValue( "/Windows/NewVectorLayer/geometry", saveGeometry() );
}
void QgsNewVectorLayerDialog::on_mTypeBox_currentIndexChanged( int index )
{
// FIXME: sync with providers/ogr/qgsogrprovider.cpp
switch ( index )
{
case 0: // Text data
if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
mWidth->setText( "80" );
mPrecision->setEnabled( false );
mWidth->setValidator( new QIntValidator( 1, 255, this ) );
break;
case 1: // Whole number
if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
mWidth->setText( "10" );
mPrecision->setEnabled( false );
mWidth->setValidator( new QIntValidator( 1, 10, this ) );
break;
case 2: // Decimal number
if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 20 )
mWidth->setText( "20" );
mPrecision->setEnabled( true );
mWidth->setValidator( new QIntValidator( 1, 20, this ) );
break;
default:
QgsDebugMsg( "unexpected index" );
break;
}
}
QGis::WkbType QgsNewVectorLayerDialog::selectedType() const
{
if ( mPointRadioButton->isChecked() )
{
return QGis::WKBPoint;
}
else if ( mLineRadioButton->isChecked() )
{
return QGis::WKBLineString;
}
else if ( mPolygonRadioButton->isChecked() )
{
return QGis::WKBPolygon;
}
return QGis::WKBUnknown;
}
int QgsNewVectorLayerDialog::selectedCrsId() const
{
return mCrsId;
}
void QgsNewVectorLayerDialog::on_mAddAttributeButton_clicked()
{
QString myName = mNameEdit->text();
QString myWidth = mWidth->text();
QString myPrecision = mPrecision->isEnabled() ? mPrecision->text() : "";
//use userrole to avoid translated type string
QString myType = mTypeBox->itemData( mTypeBox->currentIndex(), Qt::UserRole ).toString();
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
if ( mAttributeView->topLevelItemCount() > 0 )
{
mOkButton->setEnabled( true );
}
mNameEdit->clear();
}
void QgsNewVectorLayerDialog::on_mRemoveAttributeButton_clicked()
{
delete mAttributeView->currentItem();
if ( mAttributeView->topLevelItemCount() == 0 )
{
mOkButton->setEnabled( false );
}
}
void QgsNewVectorLayerDialog::on_pbnChangeSpatialRefSys_clicked()
{
QgsGenericProjectionSelector *mySelector = new QgsGenericProjectionSelector( this );
mySelector->setMessage();
mySelector->setSelectedCrsId( mCrsId );
if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem srs;
srs.createFromOgcWmsCrs( mySelector->selectedAuthId() );
mCrsId = srs.srsid();
leSpatialRefSys->setText( srs.authid() + " - " + srs.description() );
}
else
{
QApplication::restoreOverrideCursor();
}
delete mySelector;
}
void QgsNewVectorLayerDialog::attributes( QList< QPair<QString, QString> >& at ) const
{
QTreeWidgetItemIterator it( mAttributeView );
while ( *it )
{
QTreeWidgetItem *item = *it;
QString type = QString( "%1;%2;%3" ).arg( item->text( 1 ) ).arg( item->text( 2 ) ).arg( item->text( 3 ) );
at.push_back( qMakePair( item->text( 0 ), type ) );
QgsDebugMsg( QString( "appending %1//%2" ).arg( item->text( 0 ) ).arg( type ) );
++it;
}
}
QString QgsNewVectorLayerDialog::selectedFileFormat() const
{
//use userrole to avoid translated type string
QString myType = mFileFormatComboBox->itemData( mFileFormatComboBox->currentIndex(), Qt::UserRole ).toString();
return myType;
}
QString QgsNewVectorLayerDialog::selectedFileEncoding() const
{
return mFileEncoding->currentText();
}
void QgsNewVectorLayerDialog::nameChanged( QString name )
{
mAddAttributeButton->setDisabled( name.isEmpty() || mAttributeView->findItems( name, Qt::MatchExactly ).size() > 0 );
}
void QgsNewVectorLayerDialog::selectionChanged()
{
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().size() == 0 );
}
// this is static
QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget* parent, QString* pEnc )
{
QgsNewVectorLayerDialog geomDialog( parent );
if ( geomDialog.exec() == QDialog::Rejected )
{
return "";
}
QGis::WkbType geometrytype = geomDialog.selectedType();
QString fileformat = geomDialog.selectedFileFormat();
QString enc = geomDialog.selectedFileEncoding();
int crsId = geomDialog.selectedCrsId();
QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );
QList< QPair<QString, QString> > attributes;
geomDialog.attributes( attributes );
QSettings settings;
QString lastUsedDir = settings.value( "/UI/lastVectorFileFilterDir", "." ).toString();
QString filterString = QgsVectorFileWriter::filterForDriver( fileformat );
QString fileName = QFileDialog::getSaveFileName( 0, tr( "Save layer as..." ), lastUsedDir, filterString );
if ( fileName.isNull() )
{
return "";
}
if ( fileformat == "ESRI Shapefile" && !fileName.endsWith( ".shp", Qt::CaseInsensitive ) )
fileName += ".shp";
settings.setValue( "/UI/lastVectorFileFilterDir", QFileInfo( fileName ).absolutePath() );
settings.setValue( "/UI/encoding", enc );
//try to create the new layer with OGRProvider instead of QgsVectorFileWriter
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
QString ogrlib = pReg->library( "ogr" );
// load the data provider
QLibrary* myLib = new QLibrary( ogrlib );
bool loaded = myLib->load();
if ( loaded )
{
QgsDebugMsg( "ogr provider loaded" );
typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType,
const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( myLib->resolve( "createEmptyDataSource" ) );
if ( createEmptyDataSource )
{
if ( geometrytype != QGis::WKBUnknown )
{
QgsCoordinateReferenceSystem srs( crsId, QgsCoordinateReferenceSystem::InternalCrsId );
if ( !createEmptyDataSource( fileName, fileformat, enc, geometrytype, attributes, &srs ) )
{
return QString::null;
}
}
else
{
QgsDebugMsg( "geometry type not recognised" );
return QString::null;
}
}
else
{
QgsDebugMsg( "Resolving newEmptyDataSource(...) failed" );
return QString::null;
}
}
if ( pEnc )
*pEnc = enc;
return fileName;
}