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" ) );
71 mGeometryTypeBox->setCurrentIndex( -1 );
73 mGeometryWithZCheckBox->setEnabled(
false );
74 mGeometryWithMCheckBox->setEnabled(
false );
75 mCrsSelector->setEnabled(
false );
85 mTypeBox_currentIndexChanged( 1 );
87 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
88 mPrecision->setValidator(
new QIntValidator( 0, 30,
this ) );
90 mAddAttributeButton->setEnabled(
false );
91 mRemoveAttributeButton->setEnabled(
false );
93 mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
94 mOkButton->setEnabled(
false );
96 connect( mGeometryTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
97 connect( mFieldNameEdit, &QLineEdit::textChanged,
this, &QgsNewMemoryLayerDialog::fieldNameChanged );
98 connect( mTypeBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged );
99 connect( mAttributeView, &QTreeWidget::itemSelectionChanged,
this, &QgsNewMemoryLayerDialog::selectionChanged );
100 connect( mAddAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
101 connect( mRemoveAttributeButton, &QToolButton::clicked,
this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
102 connect( mButtonBox, &QDialogButtonBox::helpRequested,
this, &QgsNewMemoryLayerDialog::showHelp );
109 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
113 if ( mGeometryWithZCheckBox->isChecked() )
115 if ( mGeometryWithMCheckBox->isChecked() )
122 void QgsNewMemoryLayerDialog::geometryTypeChanged(
int )
125 ( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
128 mGeometryWithZCheckBox->setEnabled( isSpatial );
129 mGeometryWithMCheckBox->setEnabled( isSpatial );
130 mCrsSelector->setEnabled( isSpatial );
132 bool ok = ( !mNameLineEdit->text().isEmpty() && mGeometryTypeBox->currentIndex() != -1 );
133 mOkButton->setEnabled( ok );
136 void QgsNewMemoryLayerDialog::mTypeBox_currentIndexChanged(
int index )
141 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 255 )
142 mWidth->setText( QStringLiteral(
"255" ) );
144 mPrecision->setEnabled(
false );
145 mWidth->setValidator(
new QIntValidator( 1, 255,
this ) );
148 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 10 )
149 mWidth->setText( QStringLiteral(
"10" ) );
151 mPrecision->setEnabled(
false );
152 mWidth->setValidator(
new QIntValidator( 1, 10,
this ) );
155 if ( mWidth->text().toInt() < 1 || mWidth->text().toInt() > 30 )
156 mWidth->setText( QStringLiteral(
"30" ) );
157 if ( mPrecision->text().toInt() < 1 || mPrecision->text().toInt() > 30 )
158 mPrecision->setText( QStringLiteral(
"6" ) );
159 mPrecision->setEnabled(
true );
160 mWidth->setValidator(
new QIntValidator( 1, 20,
this ) );
164 mWidth->setEnabled(
false );
166 mPrecision->setEnabled(
false );
170 mWidth->setEnabled(
false );
172 mPrecision->setEnabled(
false );
176 mWidth->setEnabled(
false );
178 mPrecision->setEnabled(
false );
182 mWidth->setEnabled(
false );
184 mPrecision->setEnabled(
false );
188 mWidth->setEnabled(
false );
190 mPrecision->setEnabled(
false );
194 QgsDebugMsg( QStringLiteral(
"unexpected index" ) );
201 mCrsSelector->setCrs(
crs );
206 return mCrsSelector->crs();
211 return mNameLineEdit->text();
214 void QgsNewMemoryLayerDialog::fieldNameChanged(
const QString &name )
216 mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
219 void QgsNewMemoryLayerDialog::selectionChanged()
221 mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
228 QTreeWidgetItemIterator it( mAttributeView );
231 QString name( ( *it )->text( 0 ) );
232 QString
typeName( ( *it )->text( 1 ) );
233 int width = ( *it )->text( 2 ).toInt();
234 int precision = ( *it )->text( 3 ).toInt();
235 QVariant::Type fieldType = QVariant::Invalid;
236 if (
typeName == QLatin1String(
"string" ) )
237 fieldType = QVariant::String;
238 else if (
typeName == QLatin1String(
"integer" ) )
239 fieldType = QVariant::Int;
240 else if (
typeName == QLatin1String(
"double" ) )
241 fieldType = QVariant::Double;
242 else if (
typeName == QLatin1String(
"bool" ) )
243 fieldType = QVariant::Bool;
244 else if (
typeName == QLatin1String(
"date" ) )
245 fieldType = QVariant::Date;
246 else if (
typeName == QLatin1String(
"time" ) )
247 fieldType = QVariant::Time;
248 else if (
typeName == QLatin1String(
"datetime" ) )
249 fieldType = QVariant::DateTime;
259 void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
261 if ( !mFieldNameEdit->text().isEmpty() )
263 QString fieldName = mFieldNameEdit->text();
264 QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
265 QString width = mWidth->text();
267 mAttributeView->addTopLevelItem(
new QTreeWidgetItem( QStringList() << fieldName << fieldType << width <<
precision ) );
269 mFieldNameEdit->clear();
273 void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
275 delete mAttributeView->currentItem();
278 void QgsNewMemoryLayerDialog::showHelp()
280 QgsHelp::openHelp( QStringLiteral(
"managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );