Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
apply #3716: add value relation widget type
  • Loading branch information
jef-n committed May 4, 2011
1 parent d508f56 commit 40bcb99
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 14 deletions.
61 changes: 61 additions & 0 deletions src/app/qgsattributetypedialog.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsattributetypedialog.h"
#include "qgsattributetypeloaddialog.h"
#include "qgsvectordataprovider.h"
#include "qgsmaplayerregistry.h"

#include "qgslogger.h"

Expand All @@ -42,6 +43,17 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl )
connect( loadFromLayerButton, SIGNAL( clicked() ), this, SLOT( loadFromLayerButtonPushed() ) );
connect( loadFromCSVButton, SIGNAL( clicked() ), this, SLOT( loadFromCSVButtonPushed() ) );
connect( tableWidget, SIGNAL( cellChanged( int, int ) ), this, SLOT( vCellChanged( int, int ) ) );

valueRelationLayer->clear();
foreach( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayers() )
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
if ( vl )
valueRelationLayer->addItem( vl->name(), vl->id() );
}

connect( valueRelationLayer, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updateLayerColumns( int ) ) );
valueRelationLayer->setCurrentIndex( -1 );
}

QgsAttributeTypeDialog::~QgsAttributeTypeDialog()
Expand All @@ -59,6 +71,11 @@ QgsVectorLayer::RangeData QgsAttributeTypeDialog::rangeData()
return mRangeData;
}

QgsVectorLayer::ValueRelationData QgsAttributeTypeDialog::valueRelationData()
{
return mValueRelationData;
}

QMap<QString, QVariant> &QgsAttributeTypeDialog::valueMap()
{
return mValueMap;
Expand Down Expand Up @@ -258,6 +275,10 @@ void QgsAttributeTypeDialog::setPageForEditType( QgsVectorLayer::EditType editTy
case QgsVectorLayer::Calendar:
setPage( 11 );
break;

case QgsVectorLayer::ValueRelation:
setPage( 12 );
break;
}
}

Expand All @@ -271,6 +292,11 @@ void QgsAttributeTypeDialog::setRange( QgsVectorLayer::RangeData range )
mRangeData = range;
}

void QgsAttributeTypeDialog::setValueRelation( QgsVectorLayer::ValueRelationData valueRelation )
{
mValueRelationData = valueRelation;
}

void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editType )
{
mIndex = index;
Expand Down Expand Up @@ -396,6 +422,13 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
editableUniqueValues->setChecked( editType == QgsVectorLayer::UniqueValuesEditable );
break;

case QgsVectorLayer::ValueRelation:
valueRelationLayer->setCurrentIndex( valueRelationLayer->findData( mValueRelationData.mLayer ) );
valueRelationKeyColumn->setCurrentIndex( valueRelationKeyColumn->findText( mValueRelationData.mKey ) );
valueRelationValueColumn->setCurrentIndex( valueRelationValueColumn->findText( mValueRelationData.mValue ) );
valueRelationAllowNull->setChecked( mValueRelationData.mAllowNull );
break;

case QgsVectorLayer::LineEdit:
case QgsVectorLayer::UniqueValues:
case QgsVectorLayer::Classification:
Expand Down Expand Up @@ -558,6 +591,13 @@ void QgsAttributeTypeDialog::accept()
case 11:
mEditType = QgsVectorLayer::Calendar;
break;
case 12:
mEditType = QgsVectorLayer::ValueRelation;
mValueRelationData.mLayer = valueRelationLayer->itemData( valueRelationLayer->currentIndex() ).toString();
mValueRelationData.mKey = valueRelationKeyColumn->currentText();
mValueRelationData.mValue = valueRelationValueColumn->currentText();
mValueRelationData.mAllowNull = valueRelationAllowNull->isChecked();
break;
}

QDialog::accept();
Expand All @@ -567,3 +607,24 @@ QString QgsAttributeTypeDialog::defaultWindowTitle()
{
return tr( "Attribute Edit Dialog" );
}

void QgsAttributeTypeDialog::updateLayerColumns( int idx )
{
valueRelationKeyColumn->clear();
valueRelationValueColumn->clear();

QString id = valueRelationLayer->itemData( idx ).toString();

QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsMapLayerRegistry::instance()->mapLayer( id ) );
if ( !vl )
return;

foreach( const QgsField &f, vl->pendingFields() )
{
valueRelationKeyColumn->addItem( f.name() );
valueRelationValueColumn->addItem( f.name() );
}

valueRelationKeyColumn->setCurrentIndex( valueRelationKeyColumn->findText( mValueRelationData.mKey ) );
valueRelationValueColumn->setCurrentIndex( valueRelationValueColumn->findText( mValueRelationData.mValue ) );
}
17 changes: 17 additions & 0 deletions src/app/qgsattributetypedialog.h
Expand Up @@ -77,6 +77,12 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
*/
void setCheckedState( QString checked, QString unchecked );

/**
* Setter to value relation to be displayed and edited in this dialog
* @param valueRelation value relation data which is to be displayed
*/
void setValueRelation( QgsVectorLayer::ValueRelationData valueRelationData );

/**
* Getter for checked state after editing
* @return string representing the checked
Expand All @@ -95,6 +101,11 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
*/
QgsVectorLayer::RangeData rangeData();

/**
* Getter for value relation data
*/
QgsVectorLayer::ValueRelationData valueRelationData();

private slots:
/**
* Slot to handle change of index in combobox to select correct page
Expand Down Expand Up @@ -124,6 +135,11 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
*/
void vCellChanged( int row, int column );

/**
* update columns list
*/
void updateLayerColumns( int idx );

private:

QString defaultWindowTitle();
Expand All @@ -147,6 +163,7 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
int mIndex;

QgsVectorLayer::RangeData mRangeData;
QgsVectorLayer::ValueRelationData mValueRelationData;
QgsVectorLayer::EditType mEditType;
};

Expand Down
16 changes: 14 additions & 2 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -245,6 +245,7 @@ void QgsVectorLayerProperties::attributeTypeDialog()

attributeTypeDialog.setValueMap( mValueMaps.value( index, layer->valueMap( index ) ) );
attributeTypeDialog.setRange( mRanges.value( index, layer->range( index ) ) );
attributeTypeDialog.setValueRelation( mValueRelationData.value( index, layer->valueRelation( index ) ) );

QPair<QString, QString> checkStates = mCheckedStates.value( index, layer->checkedState( index ) );
attributeTypeDialog.setCheckedState( checkStates.first, checkStates.second );
Expand Down Expand Up @@ -272,6 +273,9 @@ void QgsVectorLayerProperties::attributeTypeDialog()
case QgsVectorLayer::CheckBox:
mCheckedStates.insert( index, attributeTypeDialog.checkedState() );
break;
case QgsVectorLayer::ValueRelation:
mValueRelationData.insert( index, attributeTypeDialog.valueRelationData() );
break;
case QgsVectorLayer::LineEdit:
case QgsVectorLayer::TextEdit:
case QgsVectorLayer::UniqueValues:
Expand Down Expand Up @@ -554,6 +558,7 @@ void QgsVectorLayerProperties::setupEditTypes()
editTypeMap.insert( QgsVectorLayer::CheckBox, tr( "Checkbox" ) );
editTypeMap.insert( QgsVectorLayer::TextEdit, tr( "Text edit" ) );
editTypeMap.insert( QgsVectorLayer::Calendar, tr( "Calendar" ) );
editTypeMap.insert( QgsVectorLayer::ValueRelation, tr( "Value relation" ) );
}

QString QgsVectorLayerProperties::editTypeButtonText( QgsVectorLayer::EditType type )
Expand Down Expand Up @@ -635,6 +640,13 @@ void QgsVectorLayerProperties::apply()
}
break;

case QgsVectorLayer::ValueRelation:
if ( mValueRelationData.contains( idx ) )
{
layer->valueRelation( idx ) = mValueRelationData[idx];
}
break;

case QgsVectorLayer::LineEdit:
case QgsVectorLayer::UniqueValues:
case QgsVectorLayer::UniqueValuesEditable:
Expand Down Expand Up @@ -945,8 +957,8 @@ QString QgsVectorLayerProperties::metadata()
}

myMetadata += tr( "In layer spatial reference system units : " )
+ tr( "xMin,yMin %1,%2 : xMax,yMax %3,%4" )
.arg( xMin ).arg( yMin ).arg( xMax ).arg( yMax );
+ tr( "xMin,yMin %1,%2 : xMax,yMax %3,%4" )
.arg( xMin ).arg( yMin ).arg( xMax ).arg( yMax );
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -173,6 +173,7 @@ class QgsVectorLayerProperties : public QDialog, private Ui::QgsVectorLayerPrope
QMap<int, QgsVectorLayer::EditType> mEditTypeMap;
QMap<int, QMap<QString, QVariant> > mValueMaps;
QMap<int, QgsVectorLayer::RangeData> mRanges;
QMap<int, QgsVectorLayer::ValueRelationData> mValueRelationData;
QMap<int, QPair<QString, QString> > mCheckedStates;

QFont mDiagramFont;
Expand Down
37 changes: 37 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -3020,6 +3020,14 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
{
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";
mValueRelations[ name ] = ValueRelationData( id, key, value, allowNull );
}
}
}

Expand Down Expand Up @@ -3215,6 +3223,17 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
}
break;

case ValueRelation:
if ( mValueRelations.contains( it.key() ) )
{
const ValueRelationData &data = mValueRelations[ it.key()];
editTypeElement.setAttribute( "layer", data.mLayer );
editTypeElement.setAttribute( "key", data.mKey );
editTypeElement.setAttribute( "value", data.mValue );
editTypeElement.setAttribute( "allowNull", data.mAllowNull ? "true" : "false" );
}
break;

case LineEdit:
case UniqueValues:
case UniqueValuesEditable:
Expand Down Expand Up @@ -5263,3 +5282,21 @@ void QgsVectorLayer::setDiagramLayerSettings( const QgsDiagramLayerSettings& s )
mDiagramLayerSettings = new QgsDiagramLayerSettings();
*mDiagramLayerSettings = s;
}

QgsVectorLayer::ValueRelationData &QgsVectorLayer::valueRelation( int idx )
{
const QgsFieldMap &fields = pendingFields();

// FIXME: throw an exception!?
if ( fields.contains( idx ) )
{
QgsDebugMsg( QString( "field %1 not found" ).arg( idx ) );
}

if ( !mValueRelations.contains( fields[idx].name() ) )
{
mValueRelations[ fields[idx].name()] = ValueRelationData();
}

return mValueRelations[ fields[idx].name()];
}
31 changes: 25 additions & 6 deletions src/core/qgsvectorlayer.h
Expand Up @@ -98,14 +98,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
Classification,
EditRange,
SliderRange,
CheckBox, /* added in 1.4 */
CheckBox, /* added in 1.4 */
FileName,
Enumeration,
Immutable, /* The attribute value should not be changed in the attribute form*/
Hidden, /* The attribute value should not be shown in the attribute form @added in 1.4 */
TextEdit, /* multiline edit @added in 1.4*/
Calendar, /* calendar widget @added in 1.5 */
DialRange, /* dial range @added in 1.5 */
Immutable, /* The attribute value should not be changed in the attribute form*/
Hidden, /* The attribute value should not be shown in the attribute form @added in 1.4 */
TextEdit, /* multiline edit @added in 1.4*/
Calendar, /* calendar widget @added in 1.5 */
DialRange, /* dial range @added in 1.5 */
ValueRelation, /* value map from an table @added in 1.8 */
};

struct RangeData
Expand All @@ -119,6 +120,18 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
QVariant mStep;
};

struct ValueRelationData
{
ValueRelationData() {}
ValueRelationData( QString layer, QString key, QString value, bool allowNull )
: mLayer( layer ), mKey( key ), mValue( value ), mAllowNull( allowNull ) {}

QString mLayer;
QString mKey;
QString mValue;
bool mAllowNull;
};

/** Constructor */
QgsVectorLayer( QString path = QString::null, QString baseName = QString::null,
QString providerLib = QString::null, bool loadDefaultStyleFlag = true );
Expand Down Expand Up @@ -587,6 +600,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/**access range */
RangeData &range( int idx );

/**access relations
* @note added in 1.8
**/
ValueRelationData &valueRelation( int idx );

/**Adds a new overlay to this class. QgsVectorLayer takes ownership of the object
@note this method was added in version 1.1
*/
Expand Down Expand Up @@ -928,6 +946,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
QMap< QString, QMap<QString, QVariant> > mValueMaps;
QMap< QString, RangeData > mRanges;
QMap< QString, QPair<QString, QString> > mCheckedStates;
QMap< QString, ValueRelationData > mValueRelations;

QString mEditForm, mEditFormInit;
//annotation form for this layer
Expand Down

0 comments on commit 40bcb99

Please sign in to comment.