Skip to content
Permalink
Browse files

[FEATURE] add multiple selection option in value relations

  • Loading branch information
3nids authored and jef-n committed Jun 28, 2012
1 parent 416cc51 commit 67b3acf678f67bd551f6247fcaf1704bfd8b9985
@@ -83,13 +83,14 @@ public:
struct ValueRelationData
{
ValueRelationData();
ValueRelationData( QString layer, QString key, QString value, bool allowNull, bool orderByValue );
ValueRelationData( QString layer, QString key, QString value, bool allowNull, bool orderByValue, bool allowMulti = false );

QString mLayer;
QString mKey;
QString mValue;
bool mAllowNull;
bool mOrderByValue;
bool mAllowMulti; /* allow selection of multiple keys @added in 1.9 */
};

/** Constructor */
@@ -432,6 +432,7 @@ void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editT
valueRelationValueColumn->setCurrentIndex( valueRelationValueColumn->findText( mValueRelationData.mValue ) );
valueRelationAllowNull->setChecked( mValueRelationData.mAllowNull );
valueRelationOrderByValue->setChecked( mValueRelationData.mOrderByValue );
valueRelationAllowMulti->setChecked( mValueRelationData.mAllowMulti );
break;

case QgsVectorLayer::LineEdit:
@@ -604,6 +605,7 @@ void QgsAttributeTypeDialog::accept()
mValueRelationData.mValue = valueRelationValueColumn->currentText();
mValueRelationData.mAllowNull = valueRelationAllowNull->isChecked();
mValueRelationData.mOrderByValue = valueRelationOrderByValue->isChecked();
mValueRelationData.mAllowMulti = valueRelationAllowMulti->isChecked();
break;
case 13:
mEditType = QgsVectorLayer::UuidGenerator;
@@ -3132,7 +3132,8 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
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 );
bool allowMulti = editTypeElement.attribute( "allowMulti", "false" ) == "true";
mValueRelations[ name ] = ValueRelationData( id, key, value, allowNull, orderByValue, allowMulti );
}
break;

@@ -3353,6 +3354,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
editTypeElement.setAttribute( "value", data.mValue );
editTypeElement.setAttribute( "allowNull", data.mAllowNull ? "true" : "false" );
editTypeElement.setAttribute( "orderByValue", data.mOrderByValue ? "true" : "false" );
editTypeElement.setAttribute( "allowMulti", data.mAllowMulti ? "true" : "false" );
}
break;

@@ -122,14 +122,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
struct ValueRelationData
{
ValueRelationData() {}
ValueRelationData( QString layer, QString key, QString value, bool allowNull, bool orderByValue )
: mLayer( layer ), mKey( key ), mValue( value ), mAllowNull( allowNull ), mOrderByValue( orderByValue ) {}
ValueRelationData( QString layer, QString key, QString value, bool allowNull, bool orderByValue, bool allowMulti = false )
: mLayer( layer ), mKey( key ), mValue( value ), mAllowNull( allowNull ), mOrderByValue( orderByValue ), mAllowMulti( allowMulti ) {}

QString mLayer;
QString mKey;
QString mValue;
bool mAllowNull;
bool mOrderByValue;
bool mAllowMulti; /* allow selection of multiple keys @added in 1.9 */
};

/** Constructor */
@@ -614,8 +615,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
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
*/
@note this method was added in version 1.1
*/
void addOverlay( QgsVectorOverlay* overlay );

/**Removes all overlays of a given type
@@ -31,6 +31,7 @@
#include <QTextEdit>
#include <QFileDialog>
#include <QComboBox>
#include <QListWidget>
#include <QCheckBox>
#include <QSpinBox>
#include <QCompleter>
@@ -112,6 +113,17 @@ QComboBox *QgsAttributeEditor::comboBox( QWidget *editor, QWidget *parent )
return cb;
}

QListWidget *QgsAttributeEditor::listWidget( QWidget *editor, QWidget *parent )
{
QListWidget *lw = NULL;
if ( editor )
lw = qobject_cast<QListWidget *>( editor );
else
lw = new QListWidget( parent );

return lw;
}

QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value )
{
if ( !vl )
@@ -206,18 +218,49 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
}
}

QComboBox *cb = comboBox( editor, parent );
if ( cb )
if ( !data.mAllowMulti )
{
for ( QMap< QString, QString >::const_iterator it = map.begin(); it != map.end(); it++ )
QComboBox *cb = comboBox( editor, parent );
if ( cb )
{
if ( data.mOrderByValue )
cb->addItem( it.key(), it.value() );
else
cb->addItem( it.value(), it.key() );
for ( QMap< QString, QString >::const_iterator it = map.begin(); it != map.end(); it++ )
{
if ( data.mOrderByValue )
cb->addItem( it.key(), it.value() );
else
cb->addItem( it.value(), it.key() );
}

myWidget = cb;
}
}
else
{
QListWidget *lw = listWidget( editor, parent );
if ( lw )
{
QStringList checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( "," );

myWidget = cb;
for ( QMap< QString, QString >::const_iterator it = map.begin(); it != map.end(); it++ )
{
QListWidgetItem *item;
if ( data.mOrderByValue )
{
item = new QListWidgetItem( it.key() );
item->setData( Qt::UserRole, it.value() );
item->setCheckState( checkList.contains( it.value() ) ? Qt::Checked : Qt::Unchecked );
}
else
{
item = new QListWidgetItem( it.value() );
item->setData( Qt::UserRole, it.key() );
item->setCheckState( checkList.contains( it.key() ) ? Qt::Checked : Qt::Unchecked );
}
lw->addItem( item );
}

myWidget = lw;
}
}
}
break;
@@ -551,6 +594,32 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
modified = true;
}

QListWidget *lw = qobject_cast<QListWidget *>( widget );
if ( lw )
{
if ( editType == QgsVectorLayer::ValueRelation )
{
text = '{';
for ( int i = 0; i < lw->count(); i++ )
{
if ( lw->item( i )->checkState() == Qt::Checked )
{
if ( i > 0 )
{
text.append( ',' );
}
text.append( lw->item( i )->data( Qt::UserRole ).toString() );
}
}
text.append( '}' );
}
else
{
text = QString::null;
}
modified = true;
}

QSpinBox *sb = qobject_cast<QSpinBox *>( widget );
if ( sb )
{
@@ -23,6 +23,7 @@ class QObject;
class QWidget;
class QgsVectorLayer;
class QComboBox;
class QListWidget;

/* \brief create attribute widget for editing */
class GUI_EXPORT QgsAttributeEditor : public QObject
@@ -37,6 +38,7 @@ class GUI_EXPORT QgsAttributeEditor : public QObject

private:
static QComboBox *comboBox( QWidget *editor, QWidget *parent );
static QListWidget *listWidget( QWidget *editor, QWidget *parent );

public slots:
void selectFileName();
@@ -682,6 +682,16 @@
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QCheckBox" name="valueRelationAllowMulti">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Allow multiple selections</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="uuidGenPage">

0 comments on commit 67b3acf

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