Skip to content

Commit

Permalink
[FEATURE] attribute editing extension
Browse files Browse the repository at this point in the history
- 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 2c4ac08
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 41 deletions.
2 changes: 1 addition & 1 deletion python/gui/qgsfieldvalidator.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 17 additions & 3 deletions src/app/qgsattributetypedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ QgsVectorLayer::ValueRelationData QgsAttributeTypeDialog::valueRelationData()
return mValueRelationData;
}

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

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

void QgsAttributeTypeDialog::setFieldEditable(bool editable)
void QgsAttributeTypeDialog::setFieldEditable( bool editable )
{
isFieldEditableCheckBox->setChecked( editable );
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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:
Expand All @@ -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 );
Expand Down Expand Up @@ -639,6 +652,7 @@ void QgsAttributeTypeDialog::accept()
break;
case 11:
mEditType = QgsVectorLayer::Calendar;
mDateFormat = leDateFormat->text();
break;
case 12:
mEditType = QgsVectorLayer::ValueRelation;
Expand Down
12 changes: 12 additions & 0 deletions src/app/qgsattributetypedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -181,6 +192,7 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
QgsVectorLayer::RangeData mRangeData;
QgsVectorLayer::ValueRelationData mValueRelationData;
QgsVectorLayer::EditType mEditType;
QString mDateFormat;
};

#endif
16 changes: 13 additions & 3 deletions src/app/qgsfieldsproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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:
Expand All @@ -526,7 +531,6 @@ void QgsFieldsProperties::attributeTypeDialog()
case QgsVectorLayer::Enumeration:
case QgsVectorLayer::Immutable:
case QgsVectorLayer::Hidden:
case QgsVectorLayer::Calendar:
case QgsVectorLayer::UuidGenerator:
break;
}
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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:
Expand All @@ -865,7 +876,6 @@ void QgsFieldsProperties::apply()
case QgsVectorLayer::Immutable:
case QgsVectorLayer::Hidden:
case QgsVectorLayer::TextEdit:
case QgsVectorLayer::Calendar:
case QgsVectorLayer::UuidGenerator:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsfieldsproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsidentifyresultsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 20 additions & 2 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
**/
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Loading

0 comments on commit 2c4ac08

Please sign in to comment.