Skip to content

Commit 2c4ac08

Browse files
committedMar 13, 2013
[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
1 parent 93e0259 commit 2c4ac08

16 files changed

+207
-41
lines changed
 

‎python/gui/qgsfieldvalidator.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class QgsFieldValidator : QValidator
66
%End
77

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

1212
virtual State validate( QString &, int & ) const;

‎src/app/qgsattributetypedialog.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ QgsVectorLayer::ValueRelationData QgsAttributeTypeDialog::valueRelationData()
7979
return mValueRelationData;
8080
}
8181

82+
QString QgsAttributeTypeDialog::dateFormat()
83+
{
84+
return mDateFormat;
85+
}
86+
8287
QMap<QString, QVariant> &QgsAttributeTypeDialog::valueMap()
8388
{
8489
return mValueMap;
@@ -89,7 +94,7 @@ bool QgsAttributeTypeDialog::fieldEditable()
8994
return isFieldEditableCheckBox->isChecked();
9095
}
9196

92-
void QgsAttributeTypeDialog::setFieldEditable(bool editable)
97+
void QgsAttributeTypeDialog::setFieldEditable( bool editable )
9398
{
9499
isFieldEditableCheckBox->setChecked( editable );
95100
}
@@ -338,6 +343,11 @@ void QgsAttributeTypeDialog::setValueRelation( QgsVectorLayer::ValueRelationData
338343
mValueRelationData = valueRelation;
339344
}
340345

346+
void QgsAttributeTypeDialog::setDateFormat( QString dateFormat )
347+
{
348+
mDateFormat = dateFormat;
349+
}
350+
341351
void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editType )
342352
{
343353
mIndex = index;
@@ -446,6 +456,7 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
446456
maximumDoubleSpinBox->setValue( mRangeData.mMax.toDouble() );
447457
stepDoubleSpinBox->setValue( mRangeData.mStep.toDouble() );
448458
}
459+
449460
if ( editType == QgsVectorLayer::EditRange )
450461
{
451462
rangeWidget->setCurrentIndex( 0 );
@@ -475,6 +486,10 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
475486
valueRelationFilterExpression->setText( mValueRelationData.mFilterExpression );
476487
break;
477488

489+
case QgsVectorLayer::Calendar:
490+
leDateFormat->setText( mDateFormat );
491+
break;
492+
478493
case QgsVectorLayer::LineEdit:
479494
case QgsVectorLayer::UniqueValues:
480495
case QgsVectorLayer::Classification:
@@ -484,13 +499,11 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
484499
case QgsVectorLayer::Immutable:
485500
case QgsVectorLayer::Hidden:
486501
case QgsVectorLayer::TextEdit:
487-
case QgsVectorLayer::Calendar:
488502
case QgsVectorLayer::UuidGenerator:
489503
break;
490504
}
491505
}
492506

493-
494507
void QgsAttributeTypeDialog::setPage( int index )
495508
{
496509
selectionListWidget->setCurrentRow( index );
@@ -639,6 +652,7 @@ void QgsAttributeTypeDialog::accept()
639652
break;
640653
case 11:
641654
mEditType = QgsVectorLayer::Calendar;
655+
mDateFormat = leDateFormat->text();
642656
break;
643657
case 12:
644658
mEditType = QgsVectorLayer::ValueRelation;

‎src/app/qgsattributetypedialog.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
8282
*/
8383
void setValueRelation( QgsVectorLayer::ValueRelationData valueRelationData );
8484

85+
/**
86+
* Setter to date format
87+
* @param dateFormat date format
88+
*/
89+
void setDateFormat( QString dateFormat );
90+
8591
/**
8692
* Setter for checkbox for editable state of field
8793
* @param bool editable
@@ -111,6 +117,11 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
111117
*/
112118
QgsVectorLayer::ValueRelationData valueRelationData();
113119

120+
/**
121+
* Getter for date format
122+
*/
123+
QString dateFormat();
124+
114125
/**
115126
* Getter for checkbox for editable state of field
116127
*/
@@ -181,6 +192,7 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
181192
QgsVectorLayer::RangeData mRangeData;
182193
QgsVectorLayer::ValueRelationData mValueRelationData;
183194
QgsVectorLayer::EditType mEditType;
195+
QString mDateFormat;
184196
};
185197

186198
#endif

‎src/app/qgsfieldsproperties.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ void QgsFieldsProperties::attributeTypeDialog()
487487
QPair<QString, QString> checkStates = mCheckedStates.value( index, mLayer->checkedState( index ) );
488488
attributeTypeDialog.setCheckedState( checkStates.first, checkStates.second );
489489

490+
attributeTypeDialog.setDateFormat( mLayer->dateFormat( index ) );
491+
490492
attributeTypeDialog.setIndex( index, mEditTypeMap.value( index, mLayer->editType( index ) ) );
491493

492494
attributeTypeDialog.setFieldEditable( mLayer->fieldEditable( index ) );
@@ -517,6 +519,9 @@ void QgsFieldsProperties::attributeTypeDialog()
517519
case QgsVectorLayer::ValueRelation:
518520
mValueRelationData.insert( index, attributeTypeDialog.valueRelationData() );
519521
break;
522+
case QgsVectorLayer::Calendar:
523+
mDateFormat.insert( index, attributeTypeDialog.dateFormat() );
524+
break;
520525
case QgsVectorLayer::LineEdit:
521526
case QgsVectorLayer::TextEdit:
522527
case QgsVectorLayer::UniqueValues:
@@ -526,7 +531,6 @@ void QgsFieldsProperties::attributeTypeDialog()
526531
case QgsVectorLayer::Enumeration:
527532
case QgsVectorLayer::Immutable:
528533
case QgsVectorLayer::Hidden:
529-
case QgsVectorLayer::Calendar:
530534
case QgsVectorLayer::UuidGenerator:
531535
break;
532536
}
@@ -820,7 +824,7 @@ void QgsFieldsProperties::apply()
820824
QgsVectorLayer::EditType editType = editTypeFromButtonText( pb->text() );
821825
mLayer->setEditType( idx, editType );
822826

823-
mLayer->setFieldEditable( idx, mFieldEditables.value( idx, true ));
827+
mLayer->setFieldEditable( idx, mFieldEditables.value( idx, true ) );
824828

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

863+
case QgsVectorLayer::Calendar:
864+
if ( mDateFormat.contains( idx ) )
865+
{
866+
mLayer->dateFormat( idx ) = mDateFormat[idx];
867+
}
868+
break;
869+
859870
case QgsVectorLayer::LineEdit:
860871
case QgsVectorLayer::UniqueValues:
861872
case QgsVectorLayer::UniqueValuesEditable:
@@ -865,7 +876,6 @@ void QgsFieldsProperties::apply()
865876
case QgsVectorLayer::Immutable:
866877
case QgsVectorLayer::Hidden:
867878
case QgsVectorLayer::TextEdit:
868-
case QgsVectorLayer::Calendar:
869879
case QgsVectorLayer::UuidGenerator:
870880
break;
871881
}

‎src/app/qgsfieldsproperties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPropertiesBase
130130
QMap<int, QPair<QString, QString> > mCheckedStates;
131131
QMap<int, QgsVectorLayer::EditType> mEditTypeMap;
132132
QMap<int, QPushButton*> mButtonMap;
133+
QMap<int, QString> mDateFormat;
133134

134135
enum attrColumns
135136
{

‎src/app/qgsidentifyresultsdialog.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
361361
value = vlayer->valueMap( i ).key( value.toString(), QString( "(%1)" ).arg( value.toString() ) );
362362
break;
363363

364+
case QgsVectorLayer::Calendar:
365+
if ( value.canConvert( QVariant::Date ) )
366+
value = value.toDate().toString( vlayer->dateFormat( i ) );
367+
break;
368+
364369
default:
365370
break;
366371
}

‎src/core/qgsvectorlayer.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,13 +2678,16 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
26782678
}
26792679
break;
26802680

2681+
case Calendar:
2682+
mDateFormats[ name ] = editTypeElement.attribute( "dateFormat" );
2683+
break;
2684+
26812685
case Classification:
26822686
case FileName:
26832687
case Immutable:
26842688
case Hidden:
26852689
case LineEdit:
26862690
case TextEdit:
2687-
case Calendar:
26882691
case Enumeration:
26892692
case UniqueValues:
26902693
case UniqueValuesEditable:
@@ -2988,14 +2991,17 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
29882991
}
29892992
break;
29902993

2994+
case Calendar:
2995+
editTypeElement.setAttribute( "dateFormat", mDateFormats[ it.key()] );
2996+
break;
2997+
29912998
case LineEdit:
29922999
case UniqueValues:
29933000
case UniqueValuesEditable:
29943001
case Classification:
29953002
case FileName:
29963003
case Hidden:
29973004
case TextEdit:
2998-
case Calendar:
29993005
case Enumeration:
30003006
case Immutable:
30013007
case UuidGenerator:
@@ -3898,6 +3904,18 @@ QgsVectorLayer::RangeData &QgsVectorLayer::range( int idx )
38983904
return mRanges[fieldName];
38993905
}
39003906

3907+
QString &QgsVectorLayer::dateFormat( int idx )
3908+
{
3909+
const QgsFields &fields = pendingFields();
3910+
3911+
QString fieldName = fields[idx].name();
3912+
3913+
if ( !mDateFormats.contains( fieldName ) )
3914+
mDateFormats[fieldName] = "yyyy-MM-dd";
3915+
3916+
return mDateFormats[fieldName];
3917+
}
3918+
39013919
bool QgsVectorLayer::fieldEditable( int idx )
39023920
{
39033921
const QgsFields &fields = pendingFields();

‎src/core/qgsvectorlayer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
767767
**/
768768
ValueRelationData &valueRelation( int idx );
769769

770+
/**access date format
771+
* @note added in 1.9
772+
*/
773+
QString &dateFormat( int idx );
774+
770775
/**is edit widget editable
771776
* @note added in 1.9
772777
**/
@@ -1074,6 +1079,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
10741079
QMap< QString, RangeData > mRanges;
10751080
QMap< QString, QPair<QString, QString> > mCheckedStates;
10761081
QMap< QString, ValueRelationData > mValueRelations;
1082+
QMap< QString, QString> mDateFormats;
10771083

10781084
/** Defines the default layout to use for the attribute editor (Drag and drop, UI File, Generated) */
10791085
EditorLayout mEditorLayout;

‎src/gui/attributetable/qgsattributetablemodel.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,20 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
643643
}
644644
}
645645

646-
if ( role == Qt::DisplayRole && mValueMaps.contains( fieldId ) )
646+
if ( role == Qt::DisplayRole )
647647
{
648-
return mValueMaps[ fieldId ]->key( val.toString(), QString( "(%1)" ).arg( val.toString() ) );
648+
if ( mValueMaps.contains( fieldId ) )
649+
{
650+
return mValueMaps[ fieldId ]->key( val.toString(), QString( "(%1)" ).arg( val.toString() ) );
651+
}
652+
653+
if ( mLayer->editType( fieldId ) == QgsVectorLayer::Calendar && val.canConvert( QVariant::Date ) )
654+
{
655+
return val.toDate().toString( mLayer->dateFormat( fieldId ) );
656+
}
649657
}
650658

659+
651660
return val.toString();
652661
}
653662

0 commit comments

Comments
 (0)
Please sign in to comment.