22
22
#include " qgsproviderregistry.h"
23
23
#include " qgsvectordataprovider.h"
24
24
#include " qgsvectorlayer.h"
25
+ #include " qgsfield.h"
26
+ #include " qgsfields.h"
25
27
#include " qgssettings.h"
26
28
#include " qgsmemoryproviderutils.h"
27
29
#include " qgsgui.h"
@@ -41,8 +43,9 @@ QgsVectorLayer *QgsNewMemoryLayerDialog::runAndCreateLayer( QWidget *parent, con
41
43
}
42
44
43
45
QgsWkbTypes::Type geometrytype = dialog.selectedType ();
46
+ QgsFields fields = dialog.fields ();
44
47
QString name = dialog.layerName ().isEmpty () ? tr ( " New scratch layer" ) : dialog.layerName ();
45
- QgsVectorLayer *newLayer = QgsMemoryProviderUtils::createMemoryLayer ( name, QgsFields () , geometrytype, dialog.crs () );
48
+ QgsVectorLayer *newLayer = QgsMemoryProviderUtils::createMemoryLayer ( name, fields , geometrytype, dialog.crs () );
46
49
return newLayer;
47
50
}
48
51
@@ -52,22 +55,42 @@ QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFla
52
55
setupUi ( this );
53
56
QgsGui::enableAutoGeometryRestore ( this );
54
57
58
+ mNameLineEdit ->setText ( tr ( " New scratch layer" ) );
59
+
55
60
mGeometryTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconTableLayer.svg" ) ), tr ( " No geometry" ), QgsWkbTypes::NoGeometry );
56
61
mGeometryTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconPointLayer.svg" ) ), tr ( " Point" ), QgsWkbTypes::Point );
57
62
mGeometryTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconLineLayer.svg" ) ), tr ( " LineString / CompoundCurve" ), QgsWkbTypes::LineString );
58
63
mGeometryTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconPolygonLayer.svg" ) ), tr ( " Polygon / CurvePolygon" ), QgsWkbTypes::Polygon );
59
64
mGeometryTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconPointLayer.svg" ) ), tr ( " MultiPoint" ), QgsWkbTypes::MultiPoint );
60
65
mGeometryTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconLineLayer.svg" ) ), tr ( " MultiLineString / MultiCurve" ), QgsWkbTypes::MultiLineString );
61
66
mGeometryTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconPolygonLayer.svg" ) ), tr ( " MultiPolygon / MultiSurface" ), QgsWkbTypes::MultiPolygon );
67
+ mGeometryTypeBox ->setCurrentIndex ( -1 );
62
68
63
69
mGeometryWithZCheckBox ->setEnabled ( false );
64
70
mGeometryWithMCheckBox ->setEnabled ( false );
71
+ mCrsSelector ->setEnabled ( false );
65
72
66
- mNameLineEdit ->setText ( tr ( " New scratch layer" ) );
73
+ mTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldText.svg" ) ), tr ( " Text" ), " string" );
74
+ mTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldInteger.svg" ) ), tr ( " Whole number" ), " integer" );
75
+ mTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldFloat.svg" ) ), tr ( " Decimal number" ), " double" );
76
+ mTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldBool.svg" ) ), tr ( " Boolean" ), " bool" );
77
+ mTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldDate.svg" ) ), tr ( " Date" ), " date" );
78
+ mTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldTime.svg" ) ), tr ( " Time" ), " time" );
79
+ mTypeBox ->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldDateTime.svg" ) ), tr ( " Date & time" ), " datetime" );
80
+
81
+ mWidth ->setValidator ( new QIntValidator ( 1 , 255 , this ) );
82
+ mPrecision ->setValidator ( new QIntValidator ( 0 , 15 , this ) );
83
+
84
+ mOkButton = mButtonBox ->button ( QDialogButtonBox::Ok );
85
+ mOkButton ->setEnabled ( false );
67
86
68
87
connect ( mGeometryTypeBox , static_cast <void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this , &QgsNewMemoryLayerDialog::geometryTypeChanged );
88
+ connect ( mFieldNameEdit , &QLineEdit::textChanged, this , &QgsNewMemoryLayerDialog::fieldNameChanged );
89
+ connect ( mAttributeView , &QTreeWidget::itemSelectionChanged, this , &QgsNewMemoryLayerDialog::selectionChanged );
90
+ connect ( mAddAttributeButton , &QToolButton::clicked, this , &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
91
+ connect ( mRemoveAttributeButton , &QToolButton::clicked, this , &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
69
92
connect ( mButtonBox , &QDialogButtonBox::helpRequested, this , &QgsNewMemoryLayerDialog::showHelp );
70
- geometryTypeChanged ( mGeometryTypeBox ->currentIndex () );
93
+ // geometryTypeChanged( mGeometryTypeBox->currentIndex() );
71
94
}
72
95
73
96
QgsWkbTypes::Type QgsNewMemoryLayerDialog::selectedType () const
@@ -96,6 +119,9 @@ void QgsNewMemoryLayerDialog::geometryTypeChanged( int )
96
119
mGeometryWithZCheckBox ->setEnabled ( isSpatial );
97
120
mGeometryWithMCheckBox ->setEnabled ( isSpatial );
98
121
mCrsSelector ->setEnabled ( isSpatial );
122
+
123
+ bool ok = ( !mNameLineEdit ->text ().isEmpty () && mGeometryTypeBox ->currentIndex () != -1 );
124
+ mOkButton ->setEnabled ( ok );
99
125
}
100
126
101
127
void QgsNewMemoryLayerDialog::setCrs ( const QgsCoordinateReferenceSystem &crs )
@@ -113,6 +139,70 @@ QString QgsNewMemoryLayerDialog::layerName() const
113
139
return mNameLineEdit ->text ();
114
140
}
115
141
142
+ void QgsNewMemoryLayerDialog::fieldNameChanged ( const QString &name )
143
+ {
144
+ mAddAttributeButton ->setDisabled ( name.isEmpty () || ! mAttributeView ->findItems ( name, Qt::MatchExactly ).isEmpty () );
145
+ }
146
+
147
+ void QgsNewMemoryLayerDialog::selectionChanged ()
148
+ {
149
+ mRemoveAttributeButton ->setDisabled ( mAttributeView ->selectedItems ().isEmpty () );
150
+ }
151
+
152
+ QgsFields QgsNewMemoryLayerDialog::fields () const
153
+ {
154
+ QgsFields fields = QgsFields ();
155
+
156
+ QTreeWidgetItemIterator it ( mAttributeView );
157
+ while ( *it )
158
+ {
159
+ QString name ( ( *it )->text ( 0 ) );
160
+ QString typeName ( ( *it )->text ( 1 ) );
161
+ int width = ( *it )->text ( 2 ).toInt ();
162
+ int precision = ( *it )->text ( 3 ).toInt ();
163
+ QVariant::Type fieldType = QVariant::Invalid;
164
+ if ( typeName == QLatin1String ( " string" ) )
165
+ fieldType = QVariant::String;
166
+ else if ( typeName == QLatin1String ( " integer" ) )
167
+ fieldType = QVariant::Int;
168
+ else if ( typeName == QLatin1String ( " double" ) )
169
+ fieldType = QVariant::Double;
170
+ else if ( typeName == QLatin1String ( " bool" ) )
171
+ fieldType = QVariant::Bool;
172
+ else if ( typeName == QLatin1String ( " date" ) )
173
+ fieldType = QVariant::Date;
174
+ else if ( typeName == QLatin1String ( " time" ) )
175
+ fieldType = QVariant::Time;
176
+ else if ( typeName == QLatin1String ( " datetime" ) )
177
+ fieldType = QVariant::DateTime;
178
+
179
+ QgsField field = QgsField ( name, fieldType, typeName, width, precision );
180
+ fields.append ( field );
181
+ ++it;
182
+ }
183
+
184
+ return fields;
185
+ }
186
+
187
+ void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked ()
188
+ {
189
+ if ( !mFieldNameEdit ->text ().isEmpty () )
190
+ {
191
+ QString fieldName = mFieldNameEdit ->text ();
192
+ QString fieldType = mTypeBox ->currentData ( Qt::UserRole ).toString ();
193
+ QString width = mWidth ->text ();
194
+ QString precision = mPrecision ->text ();
195
+ mAttributeView ->addTopLevelItem ( new QTreeWidgetItem ( QStringList () << fieldName << fieldType << width << precision ) );
196
+
197
+ mFieldNameEdit ->clear ();
198
+ }
199
+ }
200
+
201
+ void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked ()
202
+ {
203
+ delete mAttributeView ->currentItem ();
204
+ }
205
+
116
206
void QgsNewMemoryLayerDialog::showHelp ()
117
207
{
118
208
QgsHelp::openHelp ( QStringLiteral ( " managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
0 commit comments