Skip to content
Permalink
Browse files

Allow "order features by value" for relation reference widget

  • Loading branch information
m-kuhn committed Jan 29, 2015
1 parent 16f309b commit 52c2e4e90a3813ef2ca475e333db68ed7628a044
@@ -45,6 +45,11 @@ void QgsRelationReferenceConfigDlg::setConfig( const QMap<QString, QVariant>& co
mCbxAllowNull->setChecked( config[ "AllowNULL" ].toBool() );
}

if ( config.contains( "OrderByValue" ) )
{
mCbxOrderByValue->setChecked( config[ "OrderByValue" ].toBool() );
}

if ( config.contains( "ShowForm" ) )
{
mCbxShowForm->setChecked( config[ "ShowForm" ].toBool() );
@@ -84,6 +89,7 @@ QgsEditorWidgetConfig QgsRelationReferenceConfigDlg::config()
{
QgsEditorWidgetConfig myConfig;
myConfig.insert( "AllowNULL", mCbxAllowNull->isChecked() );
myConfig.insert( "OrderByValue", mCbxOrderByValue->isChecked() );
myConfig.insert( "ShowForm", mCbxShowForm->isChecked() );
myConfig.insert( "MapIdentification", mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
myConfig.insert( "ReadOnly", mCbxReadOnly->isChecked() );
@@ -42,6 +42,7 @@ QgsEditorWidgetConfig QgsRelationReferenceFactory::readConfig( const QDomElement
QMap<QString, QVariant> cfg;

cfg.insert( "AllowNULL", configElement.attribute( "AllowNULL" ) == "1" );
cfg.insert( "OrderByValue", configElement.attribute( "OrderByValue" ) == "1" );
cfg.insert( "ShowForm", configElement.attribute( "ShowForm" ) == "1" );
cfg.insert( "Relation", configElement.attribute( "Relation" ) );
cfg.insert( "MapIdentification", configElement.attribute( "MapIdentification" ) == "1" );
@@ -57,6 +58,7 @@ void QgsRelationReferenceFactory::writeConfig( const QgsEditorWidgetConfig& conf
Q_UNUSED( fieldIdx );

configElement.setAttribute( "AllowNULL", config["AllowNULL"].toBool() );
configElement.setAttribute( "OrderByValue", config["OrderByValue"].toBool() );
configElement.setAttribute( "ShowForm", config["ShowForm"].toBool() );
configElement.setAttribute( "Relation", config["Relation"].toString() );
configElement.setAttribute( "MapIdentification", config["MapIdentification"].toBool() );
@@ -33,6 +33,24 @@
#include "qgsrelationreferenceconfigdlg.h"
#include "qgsvectorlayer.h"

bool orderByLessThan( const QgsRelationReferenceWidget::ValueRelationItem& p1
, const QgsRelationReferenceWidget::ValueRelationItem& p2 )
{
switch ( p1.first.type() )
{
case QVariant::String:
return p1.first.toString() < p2.first.toString();
break;

case QVariant::Double:
return p1.first.toDouble() < p2.first.toDouble();
break;

default:
return p1.first.toInt() < p2.first.toInt();
break;
}
}

QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget* parent )
: QWidget( parent )
@@ -360,6 +378,11 @@ void QgsRelationReferenceWidget::setAllowMapIdentification( bool allowMapIdentif
mAllowMapIdentification = allowMapIdentification;
}

void QgsRelationReferenceWidget::setOrderByValue( bool orderByValue )
{
mOrderByValue = orderByValue;
}

void QgsRelationReferenceWidget::setOpenFormButtonVisible( bool openFormButtonVisible )
{
mOpenFormButton->setVisible( openFormButtonVisible );
@@ -398,15 +421,43 @@ void QgsRelationReferenceWidget::init()
exp.prepare( mReferencedLayer->pendingFields() );

QgsFeature f;
while ( fit.nextFeature( f ) )
if ( mOrderByValue )
{
ValueRelationCache cache;

QgsFeatureId currentSelection;

while ( fit.nextFeature( f ) )
{
QVariant val = exp.evaluate( &f );
cache.append( qMakePair( val, f.id() ) );
mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) );
if ( f.attribute( mFkeyFieldIdx ) == mForeignKey )
currentSelection = f.id();
}

qSort( cache.begin(), cache.end(), orderByLessThan );

Q_FOREACH( const ValueRelationItem& item, cache )
{
mComboBox->addItem( item.first.toString(), item.second );

if ( currentSelection == item.second )
mComboBox->setCurrentIndex( mComboBox->count() - 1 );
}
}
else
{
QString txt = exp.evaluate( &f ).toString();
mComboBox->addItem( txt, f.id() );
while ( fit.nextFeature( f ) )
{
QString txt = exp.evaluate( &f ).toString();
mComboBox->addItem( txt, f.id() );

if ( f.attribute( mFkeyFieldIdx ) == mForeignKey )
mComboBox->setCurrentIndex( mComboBox->count() - 1 );
if ( f.attribute( mFkeyFieldIdx ) == mForeignKey )
mComboBox->setCurrentIndex( mComboBox->count() - 1 );

mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) );
mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) );
}
}

// Only connect after iterating, to have only one iterator on the referenced table at once
@@ -36,6 +36,9 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
Q_PROPERTY( bool openFormButtonVisible READ openFormButtonVisible WRITE setOpenFormButtonVisible )

public:
typedef QPair < QVariant, QgsFeatureId > ValueRelationItem;
typedef QVector < ValueRelationItem > ValueRelationCache;

enum CanvasExtent
{
Fixed,
@@ -71,6 +74,11 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
bool allowMapIdentification() {return mAllowMapIdentification;}
void setAllowMapIdentification( bool allowMapIdentification );

//! If the widget will order the combobox entries by value
bool orderByValue() { return mOrderByValue; }
//! Set if the widget will order the combobox entries by value
void setOrderByValue( bool orderByValue );

//! determines the open form button is visible in the widget
bool openFormButtonVisible() {return mOpenFormButtonVisible;}
void setOpenFormButtonVisible( bool openFormButtonVisible );
@@ -133,6 +141,7 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
bool mEmbedForm;
bool mReadOnlySelector;
bool mAllowMapIdentification;
bool mOrderByValue;
bool mOpenFormButtonVisible;

// UI
@@ -48,10 +48,12 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget* editor )
bool showForm = config( "ShowForm", true ).toBool();
bool mapIdent = config( "MapIdentification", false ).toBool();
bool readOnlyWidget = config( "ReadOnly", false ).toBool();
bool orderByValue = config( "OrderByValue", false ).toBool();

mWidget->setEmbedForm( showForm );
mWidget->setReadOnlySelector( readOnlyWidget );
mWidget->setAllowMapIdentification( mapIdent );
mWidget->setOrderByValue( orderByValue );

QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() );

@@ -44,27 +44,34 @@
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="mCbxShowForm">
<property name="text">
<string>Show embedded form</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="2">
<item row="9" column="0" colspan="2">
<widget class="QCheckBox" name="mCbxMapIdentification">
<property name="text">
<string>On map identification (for geometric layers only)</string>
</property>
</widget>
</item>
<item row="9" column="0" colspan="2">
<item row="10" column="0" colspan="2">
<widget class="QCheckBox" name="mCbxReadOnly">
<property name="text">
<string>Use a read-only line edit instead of a combobox</string>
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<widget class="QCheckBox" name="mCbxOrderByValue">
<property name="text">
<string>Order by value</string>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>

0 comments on commit 52c2e4e

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