Skip to content

Commit

Permalink
Merge pull request #1870 from m-kuhn/relref-orderbyvalue
Browse files Browse the repository at this point in the history
Allow "order features by value" for relation reference widget
  • Loading branch information
jef-n committed Feb 3, 2015
2 parents 3c2a92b + 52c2e4e commit 290a949
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferenceconfigdlg.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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() );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencefactory.cpp
Expand Up @@ -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" );
Expand All @@ -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() );
Expand Down
63 changes: 57 additions & 6 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidget.h
Expand Up @@ -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,
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -133,6 +141,7 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
bool mEmbedForm;
bool mReadOnlySelector;
bool mAllowMapIdentification;
bool mOrderByValue;
bool mOpenFormButtonVisible;

// UI
Expand Down
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp
Expand Up @@ -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() );

Expand Down
13 changes: 10 additions & 3 deletions src/ui/editorwidgets/qgsrelationreferenceconfigdlgbase.ui
Expand Up @@ -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>
Expand Down

0 comments on commit 290a949

Please sign in to comment.