Skip to content

Commit

Permalink
allow defining fields directly in the new scratch layer dialog (fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed May 9, 2020
1 parent 61f1566 commit 65dfb22
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 64 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsnewmemorylayerdialog.sip.in
Expand Up @@ -56,6 +56,13 @@ Returns the selected CRS for the new layer.
QString layerName() const;
%Docstring
Returns the layer name
%End

QgsFields fields() const;
%Docstring
Returns attributes for the new layer.

.. versionadded:: 3.14
%End

};
Expand Down
86 changes: 84 additions & 2 deletions src/gui/qgsnewmemorylayerdialog.cpp
Expand Up @@ -22,6 +22,8 @@
#include "qgsproviderregistry.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsfield.h"
#include "qgsfields.h"
#include "qgssettings.h"
#include "qgsmemoryproviderutils.h"
#include "qgsgui.h"
Expand All @@ -41,8 +43,9 @@ QgsVectorLayer *QgsNewMemoryLayerDialog::runAndCreateLayer( QWidget *parent, con
}

QgsWkbTypes::Type geometrytype = dialog.selectedType();
QgsFields fields = dialog.fields();
QString name = dialog.layerName().isEmpty() ? tr( "New scratch layer" ) : dialog.layerName();
QgsVectorLayer *newLayer = QgsMemoryProviderUtils::createMemoryLayer( name, QgsFields(), geometrytype, dialog.crs() );
QgsVectorLayer *newLayer = QgsMemoryProviderUtils::createMemoryLayer( name, fields, geometrytype, dialog.crs() );
return newLayer;
}

Expand All @@ -52,6 +55,8 @@ QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFla
setupUi( this );
QgsGui::enableAutoGeometryRestore( this );

mNameLineEdit->setText( tr( "New scratch layer" ) );

mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconTableLayer.svg" ) ), tr( "No geometry" ), QgsWkbTypes::NoGeometry );
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPointLayer.svg" ) ), tr( "Point" ), QgsWkbTypes::Point );
mGeometryTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconLineLayer.svg" ) ), tr( "LineString / CompoundCurve" ), QgsWkbTypes::LineString );
Expand All @@ -63,9 +68,22 @@ QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFla
mGeometryWithZCheckBox->setEnabled( false );
mGeometryWithMCheckBox->setEnabled( false );

mNameLineEdit->setText( tr( "New scratch layer" ) );
mTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldText.svg" ) ), tr( "Text" ), "string" );
mTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldInteger.svg" ) ), tr( "Whole number" ), "integer" );
mTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldFloat.svg" ) ), tr( "Decimal number" ), "double" );
mTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldBool.svg" ) ), tr( "Boolean" ), "bool" );
mTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDate.svg" ) ), tr( "Date" ), "date" );
mTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldTime.svg" ) ), tr( "Time" ), "time" );
mTypeBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFieldDateTime.svg" ) ), tr( "Date & time" ), "datetime" );

mWidth->setValidator( new QIntValidator( 1, 255, this ) );
mPrecision->setValidator( new QIntValidator( 0, 15, this ) );

connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewMemoryLayerDialog::geometryTypeChanged );
connect( mFieldNameEdit, &QLineEdit::textChanged, this, &QgsNewMemoryLayerDialog::fieldNameChanged );
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewMemoryLayerDialog::selectionChanged );
connect( mAddAttributeButton, &QToolButton::clicked, this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsNewMemoryLayerDialog::showHelp );
geometryTypeChanged( mGeometryTypeBox->currentIndex() );
}
Expand Down Expand Up @@ -113,6 +131,70 @@ QString QgsNewMemoryLayerDialog::layerName() const
return mNameLineEdit->text();
}

void QgsNewMemoryLayerDialog::fieldNameChanged( const QString &name )
{
mAddAttributeButton->setDisabled( name.isEmpty() || ! mAttributeView->findItems( name, Qt::MatchExactly ).isEmpty() );
}

void QgsNewMemoryLayerDialog::selectionChanged()
{
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
}

QgsFields QgsNewMemoryLayerDialog::fields() const
{
QgsFields fields = QgsFields();

QTreeWidgetItemIterator it( mAttributeView );
while ( *it )
{
QString name( ( *it )->text( 0 ) );
QString typeName( ( *it )->text( 1 ) );
int width = ( *it )->text( 2 ).toInt();
int precision = ( *it )->text( 3 ).toInt();
QVariant::Type fieldType = QVariant::Invalid;
if ( typeName == QLatin1String( "string" ) )
fieldType = QVariant::String;
else if ( typeName == QLatin1String( "integer" ) )
fieldType = QVariant::Int;
else if ( typeName == QLatin1String( "double" ) )
fieldType = QVariant::Double;
else if ( typeName == QLatin1String( "bool" ) )
fieldType = QVariant::Bool;
else if ( typeName == QLatin1String( "date" ) )
fieldType = QVariant::Date;
else if ( typeName == QLatin1String( "time" ) )
fieldType = QVariant::Time;
else if ( typeName == QLatin1String( "datetime" ) )
fieldType = QVariant::DateTime;

QgsField field = QgsField( name, fieldType, typeName, width, precision );
fields.append( field );
++it;
}

return fields;
}

void QgsNewMemoryLayerDialog::mAddAttributeButton_clicked()
{
if ( !mFieldNameEdit->text().isEmpty() )
{
QString fieldName = mFieldNameEdit->text();
QString fieldType = mTypeBox->currentData( Qt::UserRole ).toString();
QString width = mWidth->text();
QString precision = mPrecision->text();
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << fieldName << fieldType << width << precision ) );

mFieldNameEdit->clear();
}
}

void QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked()
{
delete mAttributeView->currentItem();
}

void QgsNewMemoryLayerDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
Expand Down
11 changes: 11 additions & 0 deletions src/gui/qgsnewmemorylayerdialog.h
Expand Up @@ -23,6 +23,7 @@
#include "qgshelp.h"
#include "qgis_gui.h"

class QgsFields;
class QgsVectorLayer;

/**
Expand Down Expand Up @@ -67,13 +68,23 @@ class GUI_EXPORT QgsNewMemoryLayerDialog: public QDialog, private Ui::QgsNewMemo
//! Returns the layer name
QString layerName() const;

/**
* Returns attributes for the new layer.
* \since QGIS 3.14
*/
QgsFields fields() const;

private:

QString mCrsId;

private slots:

void geometryTypeChanged( int index );
void fieldNameChanged( const QString & );
void mAddAttributeButton_clicked();
void mRemoveAttributeButton_clicked();
void selectionChanged();
void showHelp();
};

Expand Down

0 comments on commit 65dfb22

Please sign in to comment.