31 #include <QPushButton>
34 #include <QFileDialog>
39 dialog.
setCrs( defaultCrs );
40 if ( dialog.exec() == QDialog::Rejected )
47 QString name = dialog.
layerName().isEmpty() ? tr(
"New scratch layer" ) : dialog.
layerName();
53 : QDialog( parent, fl )
58 mNameLineEdit->setText( tr(
"New scratch layer" ) );
67 mGeometryTypeBox->setCurrentIndex( -1 );
69 mGeometryWithZCheckBox->setEnabled(
false );
70 mGeometryWithMCheckBox->setEnabled(
false );
71 mCrsSelector->setEnabled(
false );
81 mTypeBox_currentIndexChanged( 1 );
83 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
84 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
86 mAddAttributeButton->setEnabled(
false );
87 mRemoveAttributeButton->setEnabled(
false );
89 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
90 mOkButton->setEnabled(
false );
92 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
93 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
94 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
95 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
96 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
97 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
98 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
105 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
109 if ( mGeometryWithZCheckBox->isChecked() )
111 if ( mGeometryWithMCheckBox->isChecked() )
118 void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
121 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
124 mGeometryWithZCheckBox->setEnabled( isSpatial );
125 mGeometryWithMCheckBox->setEnabled( isSpatial );
126 mCrsSelector->setEnabled( isSpatial );
128 bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
129 mOkButton->setEnabled( ok );
132 void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int index )
137 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
138 mWidth->setText( QStringLiteral(
"255" ) );
140 mPrecision->setEnabled(
false );
141 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
144 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
145 mWidth->setText( QStringLiteral(
"10" ) );
147 mPrecision->setEnabled(
false );
148 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
151 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
152 mWidth->setText( QStringLiteral(
"30" ) );
153 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
154 mPrecision->setText( QStringLiteral(
"6" ) );
155 mPrecision->setEnabled(
true );
156 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
160 mWidth->setEnabled(
false );
162 mPrecision->setEnabled(
false );
166 mWidth->setEnabled(
false );
168 mPrecision->setEnabled(
false );
172 mWidth->setEnabled(
false );
174 mPrecision->setEnabled(
false );
178 mWidth->setEnabled(
false );
180 mPrecision->setEnabled(
false );
184 mWidth->setEnabled(
false );
186 mPrecision->setEnabled(
false );
190 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
197 mCrsSelector->setCrs(
crs );
202 return mCrsSelector->crs();
207 return mNameLineEdit->text();
210 void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
212 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
215 void QgsNewMemoryLayerDialog::selectionChanged()
217 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
224 QTreeWidgetItemIterator it( mAttributeView );
227 QString name( ( *it )->text( 0 ) );
228 QString
typeName( ( *it )->text( 1 ) );
229 int width = ( *it )->text( 2 ).toInt();
230 int precision = ( *it )->text( 3 ).toInt();
231 QVariant::Type fieldType = QVariant::Invalid;
232 if (
typeName == QLatin1String(
"string" ) )
233 fieldType = QVariant::String;
234 else if (
typeName == QLatin1String(
"integer" ) )
235 fieldType = QVariant::Int;
236 else if (
typeName == QLatin1String(
"double" ) )
237 fieldType = QVariant::Double;
238 else if (
typeName == QLatin1String(
"bool" ) )
239 fieldType = QVariant::Bool;
240 else if (
typeName == QLatin1String(
"date" ) )
241 fieldType = QVariant::Date;
242 else if (
typeName == QLatin1String(
"time" ) )
243 fieldType = QVariant::Time;
244 else if (
typeName == QLatin1String(
"datetime" ) )
245 fieldType = QVariant::DateTime;
255 void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
257 if ( !mFieldNameEdit->text().isEmpty() )
259 QString fieldName = mFieldNameEdit->text();
260 QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
261 QString width = mWidth->text();
263 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
265 mFieldNameEdit->clear();
269 void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
271 delete mAttributeView->currentItem();
274 void QgsNewMemoryLayerDialog::showHelp()
276 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );