Skip to content

Commit

Permalink
[FEATURE] add multiple selection option in value relations
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored and jef-n committed Jun 28, 2012
1 parent 416cc51 commit 67b3acf
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 14 deletions.
3 changes: 2 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsattributetypedialog.cpp
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsvectorlayer.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions src/core/qgsvectorlayer.h
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
85 changes: 77 additions & 8 deletions src/gui/qgsattributeeditor.cpp
Expand Up @@ -31,6 +31,7 @@
#include <QTextEdit>
#include <QFileDialog>
#include <QComboBox>
#include <QListWidget>
#include <QCheckBox>
#include <QSpinBox>
#include <QCompleter>
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsattributeeditor.h
Expand Up @@ -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
Expand All @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/ui/qgsattributetypeedit.ui
Expand Up @@ -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">
Expand Down

0 comments on commit 67b3acf

Please sign in to comment.