From 100d702bd041bf6a75eddf66bf68a528f8a4fd20 Mon Sep 17 00:00:00 2001 From: "Juergen E. Fischer" Date: Thu, 29 Dec 2011 01:34:29 +0100 Subject: [PATCH] [FEATURE] add uuid generator widget --- python/core/qgsvectorlayer.sip | 1 + src/app/qgsattributetypedialog.cpp | 10 ++- src/app/qgsvectorlayerproperties.cpp | 3 + src/core/qgsvectorlayer.cpp | 79 ++++++++++++------- src/core/qgsvectorlayer.h | 1 + src/gui/qgsattributeeditor.cpp | 16 +++- .../postgres/qgspostgresprovider.cpp | 1 + src/ui/qgsattributetypeedit.ui | 33 +++++++- 8 files changed, 113 insertions(+), 31 deletions(-) diff --git a/python/core/qgsvectorlayer.sip b/python/core/qgsvectorlayer.sip index 70abd0982d6..65f876030ad 100644 --- a/python/core/qgsvectorlayer.sip +++ b/python/core/qgsvectorlayer.sip @@ -68,6 +68,7 @@ public: Calendar, /* calendar widget @added in 1.5 */ DialRange, /* dial range @added in 1.5 */ ValueRelation, /* value map from an table @added in 1.8 */ + UuidGenerator, /* uuid generator - readonly and automatically intialized @added in 1.9 */ }; struct RangeData { diff --git a/src/app/qgsattributetypedialog.cpp b/src/app/qgsattributetypedialog.cpp index cf302d4665d..9eb5dc1dc52 100644 --- a/src/app/qgsattributetypedialog.cpp +++ b/src/app/qgsattributetypedialog.cpp @@ -279,6 +279,10 @@ void QgsAttributeTypeDialog::setPageForEditType( QgsVectorLayer::EditType editTy case QgsVectorLayer::ValueRelation: setPage( 12 ); break; + + case QgsVectorLayer::UuidGenerator: + setPage( 13 ); + break; } } @@ -419,7 +423,7 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT break; case QgsVectorLayer::UniqueValuesEditable: - editableUniqueValues->setChecked( editType == QgsVectorLayer::UniqueValuesEditable ); + editableUniqueValues->setChecked( true ); break; case QgsVectorLayer::ValueRelation: @@ -440,6 +444,7 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT case QgsVectorLayer::Hidden: case QgsVectorLayer::TextEdit: case QgsVectorLayer::Calendar: + case QgsVectorLayer::UuidGenerator: break; } } @@ -600,6 +605,9 @@ void QgsAttributeTypeDialog::accept() mValueRelationData.mAllowNull = valueRelationAllowNull->isChecked(); mValueRelationData.mOrderByValue = valueRelationOrderByValue->isChecked(); break; + case 13: + mEditType = QgsVectorLayer::UuidGenerator; + break; } QDialog::accept(); diff --git a/src/app/qgsvectorlayerproperties.cpp b/src/app/qgsvectorlayerproperties.cpp index a52f424cddf..17aa502a825 100644 --- a/src/app/qgsvectorlayerproperties.cpp +++ b/src/app/qgsvectorlayerproperties.cpp @@ -310,6 +310,7 @@ void QgsVectorLayerProperties::attributeTypeDialog( ) case QgsVectorLayer::Immutable: case QgsVectorLayer::Hidden: case QgsVectorLayer::Calendar: + case QgsVectorLayer::UuidGenerator: break; } @@ -583,6 +584,7 @@ void QgsVectorLayerProperties::setupEditTypes() editTypeMap.insert( QgsVectorLayer::TextEdit, tr( "Text edit" ) ); editTypeMap.insert( QgsVectorLayer::Calendar, tr( "Calendar" ) ); editTypeMap.insert( QgsVectorLayer::ValueRelation, tr( "Value relation" ) ); + editTypeMap.insert( QgsVectorLayer::UuidGenerator, tr( "UUID generator" ) ); } QString QgsVectorLayerProperties::editTypeButtonText( QgsVectorLayer::EditType type ) @@ -690,6 +692,7 @@ void QgsVectorLayerProperties::apply() case QgsVectorLayer::Hidden: case QgsVectorLayer::TextEdit: case QgsVectorLayer::Calendar: + case QgsVectorLayer::UuidGenerator: break; } } diff --git a/src/core/qgsvectorlayer.cpp b/src/core/qgsvectorlayer.cpp index f85bb05b1cb..fc57ed87479 100644 --- a/src/core/qgsvectorlayer.cpp +++ b/src/core/qgsvectorlayer.cpp @@ -3048,37 +3048,61 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage EditType editType = ( EditType ) editTypeElement.attribute( "type" ).toInt(); mEditTypes.insert( name, editType ); - if ( editType == ValueMap && editTypeNode.hasChildNodes() ) + switch ( editType ) { - mValueMaps.insert( name, QMap() ); + case ValueMap: + if ( editTypeNode.hasChildNodes() ) + { + mValueMaps.insert( name, QMap() ); - QDomNodeList valueMapNodes = editTypeNode.childNodes(); - for ( int j = 0; j < valueMapNodes.size(); j++ ) + QDomNodeList valueMapNodes = editTypeNode.childNodes(); + for ( int j = 0; j < valueMapNodes.size(); j++ ) + { + QDomElement value = valueMapNodes.at( j ).toElement(); + mValueMaps[ name ].insert( value.attribute( "key" ), value.attribute( "value" ) ); + } + } + break; + + case EditRange: + case SliderRange: + case DialRange: { - QDomElement value = valueMapNodes.at( j ).toElement(); - mValueMaps[ name ].insert( value.attribute( "key" ), value.attribute( "value" ) ); - } - } - else if ( editType == EditRange || editType == SliderRange ) - { - QVariant min = editTypeElement.attribute( "min" ); - QVariant max = editTypeElement.attribute( "max" ); - QVariant step = editTypeElement.attribute( "step" ); + QVariant min = editTypeElement.attribute( "min" ); + QVariant max = editTypeElement.attribute( "max" ); + QVariant step = editTypeElement.attribute( "step" ); - mRanges[ name ] = RangeData( min, max, step ); - } - else if ( editType == CheckBox ) - { - mCheckedStates[ name ] = QPair( editTypeElement.attribute( "checked" ), editTypeElement.attribute( "unchecked" ) ); - } - else if ( editType == ValueRelation ) - { - QString id = editTypeElement.attribute( "layer" ); - QString key = editTypeElement.attribute( "key" ); - QString value = editTypeElement.attribute( "value" ); - bool allowNull = editTypeElement.attribute( "allowNull" ) == "true"; - bool orderByValue = editTypeElement.attribute( "orderByValue" ) == "true"; - mValueRelations[ name ] = ValueRelationData( id, key, value, allowNull, orderByValue ); + mRanges[ name ] = RangeData( min, max, step ); + } + break; + + case CheckBox: + mCheckedStates[ name ] = QPair( editTypeElement.attribute( "checked" ), editTypeElement.attribute( "unchecked" ) ); + break; + + case ValueRelation: + { + QString id = editTypeElement.attribute( "layer" ); + QString key = editTypeElement.attribute( "key" ); + QString value = editTypeElement.attribute( "value" ); + bool allowNull = editTypeElement.attribute( "allowNull" ) == "true"; + bool orderByValue = editTypeElement.attribute( "orderByValue" ) == "true"; + mValueRelations[ name ] = ValueRelationData( id, key, value, allowNull, orderByValue ); + } + break; + + case Classification: + case FileName: + case Immutable: + case Hidden: + case LineEdit: + case TextEdit: + case Calendar: + case Enumeration: + case UniqueValues: + case UniqueValuesEditable: + case UuidGenerator: + break; } } } @@ -3297,6 +3321,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString& case Calendar: case Enumeration: case Immutable: + case UuidGenerator: break; } diff --git a/src/core/qgsvectorlayer.h b/src/core/qgsvectorlayer.h index 837ddba42e8..cf01cb68ee3 100644 --- a/src/core/qgsvectorlayer.h +++ b/src/core/qgsvectorlayer.h @@ -105,6 +105,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer Calendar, /* calendar widget @added in 1.5 */ DialRange, /* dial range @added in 1.5 */ ValueRelation, /* value map from an table @added in 1.8 */ + UuidGenerator, /* uuid generator - readonly and automatically intialized @added in 1.9 */ }; struct RangeData diff --git a/src/gui/qgsattributeeditor.cpp b/src/gui/qgsattributeeditor.cpp index 85d3dbb2cab..85a82863716 100644 --- a/src/gui/qgsattributeeditor.cpp +++ b/src/gui/qgsattributeeditor.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -40,6 +41,7 @@ #include #include #include +#include void QgsAttributeEditor::selectFileName() { @@ -268,7 +270,6 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed } break; - case QgsVectorLayer::DialRange: case QgsVectorLayer::SliderRange: case QgsVectorLayer::EditRange: @@ -365,6 +366,7 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed case QgsVectorLayer::LineEdit: case QgsVectorLayer::TextEdit: + case QgsVectorLayer::UuidGenerator: case QgsVectorLayer::UniqueValuesEditable: case QgsVectorLayer::Immutable: { @@ -404,6 +406,11 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed le->setCompleter( c ); } + if ( editType == QgsVectorLayer::UuidGenerator ) + { + le->setReadOnly( true ); + } + le->setValidator( new QgsFieldValidator( le, field ) ); myWidget = le; } @@ -735,6 +742,7 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx, case QgsVectorLayer::LineEdit: case QgsVectorLayer::UniqueValuesEditable: case QgsVectorLayer::Immutable: + case QgsVectorLayer::UuidGenerator: default: { QLineEdit *le = qobject_cast( editor ); @@ -745,12 +753,18 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx, QString text; if ( value.isNull() ) + { if ( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong ) text = ""; + else if ( editType == QgsVectorLayer::UuidGenerator ) + text = QUuid::createUuid().toString(); else text = nullValue; + } else + { text = value.toString(); + } if ( le ) le->setText( text ); diff --git a/src/providers/postgres/qgspostgresprovider.cpp b/src/providers/postgres/qgspostgresprovider.cpp index 690208e0354..f8321b8e61b 100644 --- a/src/providers/postgres/qgspostgresprovider.cpp +++ b/src/providers/postgres/qgspostgresprovider.cpp @@ -1736,6 +1736,7 @@ bool QgsPostgresProvider::loadFields() fieldTypeName == "geometry" || fieldTypeName == "money" || fieldTypeName == "ltree" || + fieldTypeName == "uuid" || fieldTypeName.startsWith( "time" ) || fieldTypeName.startsWith( "date" ) ) { diff --git a/src/ui/qgsattributetypeedit.ui b/src/ui/qgsattributetypeedit.ui index 69536474350..b7ba4d02313 100644 --- a/src/ui/qgsattributetypeedit.ui +++ b/src/ui/qgsattributetypeedit.ui @@ -81,6 +81,11 @@ Value relation + + + UUID generator + + @@ -92,7 +97,7 @@ - 12 + 0 @@ -596,7 +601,7 @@ - + @@ -679,6 +684,30 @@ + + + + + + Read-only field that generates a UUID if empty. + + + + + + + Qt::Vertical + + + + 20 + 294 + + + + + +