Skip to content
Permalink
Browse files

[FEATURE] attribute editing extension

- allow resetting of fields to NULL by button
- use user defined date formats in forms, identify results and attribute table
- add support for date type in postgres provider
  • Loading branch information
jef-n committed Mar 13, 2013
1 parent 93e0259 commit 2c4ac0807b7a94500d22258dde45a66f2b285e09
@@ -6,7 +6,7 @@ class QgsFieldValidator : QValidator
%End

public:
QgsFieldValidator( QObject *parent, const QgsField &field );
QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat = "yyyy-MM-dd" );
~QgsFieldValidator();

virtual State validate( QString &, int & ) const;
@@ -79,6 +79,11 @@ QgsVectorLayer::ValueRelationData QgsAttributeTypeDialog::valueRelationData()
return mValueRelationData;
}

QString QgsAttributeTypeDialog::dateFormat()
{
return mDateFormat;
}

QMap<QString, QVariant> &QgsAttributeTypeDialog::valueMap()
{
return mValueMap;
@@ -89,7 +94,7 @@ bool QgsAttributeTypeDialog::fieldEditable()
return isFieldEditableCheckBox->isChecked();
}

void QgsAttributeTypeDialog::setFieldEditable(bool editable)
void QgsAttributeTypeDialog::setFieldEditable( bool editable )
{
isFieldEditableCheckBox->setChecked( editable );
}
@@ -338,6 +343,11 @@ void QgsAttributeTypeDialog::setValueRelation( QgsVectorLayer::ValueRelationData
mValueRelationData = valueRelation;
}

void QgsAttributeTypeDialog::setDateFormat( QString dateFormat )
{
mDateFormat = dateFormat;
}

void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editType )
{
mIndex = index;
@@ -446,6 +456,7 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
maximumDoubleSpinBox->setValue( mRangeData.mMax.toDouble() );
stepDoubleSpinBox->setValue( mRangeData.mStep.toDouble() );
}

if ( editType == QgsVectorLayer::EditRange )
{
rangeWidget->setCurrentIndex( 0 );
@@ -475,6 +486,10 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
valueRelationFilterExpression->setText( mValueRelationData.mFilterExpression );
break;

case QgsVectorLayer::Calendar:
leDateFormat->setText( mDateFormat );
break;

case QgsVectorLayer::LineEdit:
case QgsVectorLayer::UniqueValues:
case QgsVectorLayer::Classification:
@@ -484,13 +499,11 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
case QgsVectorLayer::Immutable:
case QgsVectorLayer::Hidden:
case QgsVectorLayer::TextEdit:
case QgsVectorLayer::Calendar:
case QgsVectorLayer::UuidGenerator:
break;
}
}


void QgsAttributeTypeDialog::setPage( int index )
{
selectionListWidget->setCurrentRow( index );
@@ -639,6 +652,7 @@ void QgsAttributeTypeDialog::accept()
break;
case 11:
mEditType = QgsVectorLayer::Calendar;
mDateFormat = leDateFormat->text();
break;
case 12:
mEditType = QgsVectorLayer::ValueRelation;
@@ -82,6 +82,12 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
*/
void setValueRelation( QgsVectorLayer::ValueRelationData valueRelationData );

/**
* Setter to date format
* @param dateFormat date format
*/
void setDateFormat( QString dateFormat );

/**
* Setter for checkbox for editable state of field
* @param bool editable
@@ -111,6 +117,11 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
*/
QgsVectorLayer::ValueRelationData valueRelationData();

/**
* Getter for date format
*/
QString dateFormat();

/**
* Getter for checkbox for editable state of field
*/
@@ -181,6 +192,7 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
QgsVectorLayer::RangeData mRangeData;
QgsVectorLayer::ValueRelationData mValueRelationData;
QgsVectorLayer::EditType mEditType;
QString mDateFormat;
};

#endif
@@ -487,6 +487,8 @@ void QgsFieldsProperties::attributeTypeDialog()
QPair<QString, QString> checkStates = mCheckedStates.value( index, mLayer->checkedState( index ) );
attributeTypeDialog.setCheckedState( checkStates.first, checkStates.second );

attributeTypeDialog.setDateFormat( mLayer->dateFormat( index ) );

attributeTypeDialog.setIndex( index, mEditTypeMap.value( index, mLayer->editType( index ) ) );

attributeTypeDialog.setFieldEditable( mLayer->fieldEditable( index ) );
@@ -517,6 +519,9 @@ void QgsFieldsProperties::attributeTypeDialog()
case QgsVectorLayer::ValueRelation:
mValueRelationData.insert( index, attributeTypeDialog.valueRelationData() );
break;
case QgsVectorLayer::Calendar:
mDateFormat.insert( index, attributeTypeDialog.dateFormat() );
break;
case QgsVectorLayer::LineEdit:
case QgsVectorLayer::TextEdit:
case QgsVectorLayer::UniqueValues:
@@ -526,7 +531,6 @@ void QgsFieldsProperties::attributeTypeDialog()
case QgsVectorLayer::Enumeration:
case QgsVectorLayer::Immutable:
case QgsVectorLayer::Hidden:
case QgsVectorLayer::Calendar:
case QgsVectorLayer::UuidGenerator:
break;
}
@@ -820,7 +824,7 @@ void QgsFieldsProperties::apply()
QgsVectorLayer::EditType editType = editTypeFromButtonText( pb->text() );
mLayer->setEditType( idx, editType );

mLayer->setFieldEditable( idx, mFieldEditables.value( idx, true ));
mLayer->setFieldEditable( idx, mFieldEditables.value( idx, true ) );

switch ( editType )
{
@@ -856,6 +860,13 @@ void QgsFieldsProperties::apply()
}
break;

case QgsVectorLayer::Calendar:
if ( mDateFormat.contains( idx ) )
{
mLayer->dateFormat( idx ) = mDateFormat[idx];
}
break;

case QgsVectorLayer::LineEdit:
case QgsVectorLayer::UniqueValues:
case QgsVectorLayer::UniqueValuesEditable:
@@ -865,7 +876,6 @@ void QgsFieldsProperties::apply()
case QgsVectorLayer::Immutable:
case QgsVectorLayer::Hidden:
case QgsVectorLayer::TextEdit:
case QgsVectorLayer::Calendar:
case QgsVectorLayer::UuidGenerator:
break;
}
@@ -130,6 +130,7 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
QMap<int, QPair<QString, QString> > mCheckedStates;
QMap<int, QgsVectorLayer::EditType> mEditTypeMap;
QMap<int, QPushButton*> mButtonMap;
QMap<int, QString> mDateFormat;

enum attrColumns
{
@@ -361,6 +361,11 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
value = vlayer->valueMap( i ).key( value.toString(), QString( "(%1)" ).arg( value.toString() ) );
break;

case QgsVectorLayer::Calendar:
if ( value.canConvert( QVariant::Date ) )
value = value.toDate().toString( vlayer->dateFormat( i ) );
break;

default:
break;
}
@@ -2678,13 +2678,16 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
}
break;

case Calendar:
mDateFormats[ name ] = editTypeElement.attribute( "dateFormat" );
break;

case Classification:
case FileName:
case Immutable:
case Hidden:
case LineEdit:
case TextEdit:
case Calendar:
case Enumeration:
case UniqueValues:
case UniqueValuesEditable:
@@ -2988,14 +2991,17 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
}
break;

case Calendar:
editTypeElement.setAttribute( "dateFormat", mDateFormats[ it.key()] );
break;

case LineEdit:
case UniqueValues:
case UniqueValuesEditable:
case Classification:
case FileName:
case Hidden:
case TextEdit:
case Calendar:
case Enumeration:
case Immutable:
case UuidGenerator:
@@ -3898,6 +3904,18 @@ QgsVectorLayer::RangeData &QgsVectorLayer::range( int idx )
return mRanges[fieldName];
}

QString &QgsVectorLayer::dateFormat( int idx )
{
const QgsFields &fields = pendingFields();

QString fieldName = fields[idx].name();

if ( !mDateFormats.contains( fieldName ) )
mDateFormats[fieldName] = "yyyy-MM-dd";

return mDateFormats[fieldName];
}

bool QgsVectorLayer::fieldEditable( int idx )
{
const QgsFields &fields = pendingFields();
@@ -767,6 +767,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
**/
ValueRelationData &valueRelation( int idx );

/**access date format
* @note added in 1.9
*/
QString &dateFormat( int idx );

/**is edit widget editable
* @note added in 1.9
**/
@@ -1074,6 +1079,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
QMap< QString, RangeData > mRanges;
QMap< QString, QPair<QString, QString> > mCheckedStates;
QMap< QString, ValueRelationData > mValueRelations;
QMap< QString, QString> mDateFormats;

/** Defines the default layout to use for the attribute editor (Drag and drop, UI File, Generated) */
EditorLayout mEditorLayout;
@@ -643,11 +643,20 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
}
}

if ( role == Qt::DisplayRole && mValueMaps.contains( fieldId ) )
if ( role == Qt::DisplayRole )
{
return mValueMaps[ fieldId ]->key( val.toString(), QString( "(%1)" ).arg( val.toString() ) );
if ( mValueMaps.contains( fieldId ) )
{
return mValueMaps[ fieldId ]->key( val.toString(), QString( "(%1)" ).arg( val.toString() ) );
}

if ( mLayer->editType( fieldId ) == QgsVectorLayer::Calendar && val.canConvert( QVariant::Date ) )
{
return val.toDate().toString( mLayer->dateFormat( fieldId ) );
}
}


return val.toString();
}

0 comments on commit 2c4ac08

Please sign in to comment.
You can’t perform that action at this time.