Skip to content

Commit

Permalink
[FEATURE] add uuid generator widget
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Dec 30, 2011
1 parent d7b1499 commit 100d702
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 30 deletions.
1 change: 1 addition & 0 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion src/app/qgsattributetypedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ void QgsAttributeTypeDialog::setPageForEditType( QgsVectorLayer::EditType editTy
case QgsVectorLayer::ValueRelation:
setPage( 12 );
break;

case QgsVectorLayer::UuidGenerator:
setPage( 13 );
break;
}
}

Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -600,6 +605,9 @@ void QgsAttributeTypeDialog::accept()
mValueRelationData.mAllowNull = valueRelationAllowNull->isChecked();
mValueRelationData.mOrderByValue = valueRelationOrderByValue->isChecked();
break;
case 13:
mEditType = QgsVectorLayer::UuidGenerator;
break;
}

QDialog::accept();
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ void QgsVectorLayerProperties::attributeTypeDialog( )
case QgsVectorLayer::Immutable:
case QgsVectorLayer::Hidden:
case QgsVectorLayer::Calendar:
case QgsVectorLayer::UuidGenerator:
break;
}

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -690,6 +692,7 @@ void QgsVectorLayerProperties::apply()
case QgsVectorLayer::Hidden:
case QgsVectorLayer::TextEdit:
case QgsVectorLayer::Calendar:
case QgsVectorLayer::UuidGenerator:
break;
}
}
Expand Down
77 changes: 51 additions & 26 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QString, QVariant>() );
case ValueMap:
if ( editTypeNode.hasChildNodes() )
{
mValueMaps.insert( name, QMap<QString, QVariant>() );

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;

QDomNodeList valueMapNodes = editTypeNode.childNodes();
for ( int j = 0; j < valueMapNodes.size(); j++ )
case EditRange:
case SliderRange:
case DialRange:
{
QDomElement value = valueMapNodes.at( j ).toElement();
mValueMaps[ name ].insert( value.attribute( "key" ), value.attribute( "value" ) );
QVariant min = editTypeElement.attribute( "min" );
QVariant max = editTypeElement.attribute( "max" );
QVariant step = editTypeElement.attribute( "step" );

mRanges[ name ] = RangeData( min, max, step );
}
}
else if ( editType == EditRange || editType == SliderRange )
{
QVariant min = editTypeElement.attribute( "min" );
QVariant max = editTypeElement.attribute( "max" );
QVariant step = editTypeElement.attribute( "step" );
break;

mRanges[ name ] = RangeData( min, max, step );
}
else if ( editType == CheckBox )
{
mCheckedStates[ name ] = QPair<QString, QString>( 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 );
case CheckBox:
mCheckedStates[ name ] = QPair<QString, QString>( 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;
}
}
}
Expand Down Expand Up @@ -3297,6 +3321,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
case Calendar:
case Enumeration:
case Immutable:
case UuidGenerator:
break;
}

Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/gui/qgsattributeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <qgslonglongvalidator.h>
#include <qgsfieldvalidator.h>
#include <qgsmaplayerregistry.h>
#include <qgslogger.h>

#include <QPushButton>
#include <QLineEdit>
Expand All @@ -40,6 +41,7 @@
#include <QDialogButtonBox>
#include <QSettings>
#include <QDir>
#include <QUuid>

void QgsAttributeEditor::selectFileName()
{
Expand Down Expand Up @@ -268,7 +270,6 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
}
break;


case QgsVectorLayer::DialRange:
case QgsVectorLayer::SliderRange:
case QgsVectorLayer::EditRange:
Expand Down Expand Up @@ -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:
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<QLineEdit *>( editor );
Expand All @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,7 @@ bool QgsPostgresProvider::loadFields()
fieldTypeName == "geometry" ||
fieldTypeName == "money" ||
fieldTypeName == "ltree" ||
fieldTypeName == "uuid" ||
fieldTypeName.startsWith( "time" ) ||
fieldTypeName.startsWith( "date" ) )
{
Expand Down
33 changes: 31 additions & 2 deletions src/ui/qgsattributetypeedit.ui
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<string>Value relation</string>
</property>
</item>
<item>
<property name="text">
<string>UUID generator</string>
</property>
</item>
</widget>
</item>
<item>
Expand All @@ -92,7 +97,7 @@
</sizepolicy>
</property>
<property name="currentIndex">
<number>12</number>
<number>0</number>
</property>
<widget class="QWidget" name="lineEditPage">
<layout class="QVBoxLayout" name="verticalLayout_1">
Expand Down Expand Up @@ -596,7 +601,7 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="page">
<widget class="QWidget" name="valueRelationPage">
<layout class="QGridLayout" name="gridLayout_3">
<item row="1" column="0">
<widget class="QLabel" name="label_5">
Expand Down Expand Up @@ -679,6 +684,30 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="uuidGenPage">
<layout class="QVBoxLayout" name="verticalLayout_14">
<item>
<widget class="QLabel" name="label_9">
<property name="text">
<string>Read-only field that generates a UUID if empty.</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_11">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>294</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
</item>
<item>
Expand Down

0 comments on commit 100d702

Please sign in to comment.