Skip to content

Commit 290a949

Browse files
committed
Merge pull request #1870 from m-kuhn/relref-orderbyvalue
Allow "order features by value" for relation reference widget
2 parents 3c2a92b + 52c2e4e commit 290a949

6 files changed

+86
-9
lines changed

src/gui/editorwidgets/qgsrelationreferenceconfigdlg.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ void QgsRelationReferenceConfigDlg::setConfig( const QMap<QString, QVariant>& co
4545
mCbxAllowNull->setChecked( config[ "AllowNULL" ].toBool() );
4646
}
4747

48+
if ( config.contains( "OrderByValue" ) )
49+
{
50+
mCbxOrderByValue->setChecked( config[ "OrderByValue" ].toBool() );
51+
}
52+
4853
if ( config.contains( "ShowForm" ) )
4954
{
5055
mCbxShowForm->setChecked( config[ "ShowForm" ].toBool() );
@@ -84,6 +89,7 @@ QgsEditorWidgetConfig QgsRelationReferenceConfigDlg::config()
8489
{
8590
QgsEditorWidgetConfig myConfig;
8691
myConfig.insert( "AllowNULL", mCbxAllowNull->isChecked() );
92+
myConfig.insert( "OrderByValue", mCbxOrderByValue->isChecked() );
8793
myConfig.insert( "ShowForm", mCbxShowForm->isChecked() );
8894
myConfig.insert( "MapIdentification", mCbxMapIdentification->isEnabled() && mCbxMapIdentification->isChecked() );
8995
myConfig.insert( "ReadOnly", mCbxReadOnly->isChecked() );

src/gui/editorwidgets/qgsrelationreferencefactory.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ QgsEditorWidgetConfig QgsRelationReferenceFactory::readConfig( const QDomElement
4242
QMap<QString, QVariant> cfg;
4343

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

5960
configElement.setAttribute( "AllowNULL", config["AllowNULL"].toBool() );
61+
configElement.setAttribute( "OrderByValue", config["OrderByValue"].toBool() );
6062
configElement.setAttribute( "ShowForm", config["ShowForm"].toBool() );
6163
configElement.setAttribute( "Relation", config["Relation"].toString() );
6264
configElement.setAttribute( "MapIdentification", config["MapIdentification"].toBool() );

src/gui/editorwidgets/qgsrelationreferencewidget.cpp

+57-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@
3333
#include "qgsrelationreferenceconfigdlg.h"
3434
#include "qgsvectorlayer.h"
3535

36+
bool orderByLessThan( const QgsRelationReferenceWidget::ValueRelationItem& p1
37+
, const QgsRelationReferenceWidget::ValueRelationItem& p2 )
38+
{
39+
switch ( p1.first.type() )
40+
{
41+
case QVariant::String:
42+
return p1.first.toString() < p2.first.toString();
43+
break;
44+
45+
case QVariant::Double:
46+
return p1.first.toDouble() < p2.first.toDouble();
47+
break;
48+
49+
default:
50+
return p1.first.toInt() < p2.first.toInt();
51+
break;
52+
}
53+
}
3654

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

381+
void QgsRelationReferenceWidget::setOrderByValue( bool orderByValue )
382+
{
383+
mOrderByValue = orderByValue;
384+
}
385+
363386
void QgsRelationReferenceWidget::setOpenFormButtonVisible( bool openFormButtonVisible )
364387
{
365388
mOpenFormButton->setVisible( openFormButtonVisible );
@@ -398,15 +421,43 @@ void QgsRelationReferenceWidget::init()
398421
exp.prepare( mReferencedLayer->pendingFields() );
399422

400423
QgsFeature f;
401-
while ( fit.nextFeature( f ) )
424+
if ( mOrderByValue )
425+
{
426+
ValueRelationCache cache;
427+
428+
QgsFeatureId currentSelection;
429+
430+
while ( fit.nextFeature( f ) )
431+
{
432+
QVariant val = exp.evaluate( &f );
433+
cache.append( qMakePair( val, f.id() ) );
434+
mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) );
435+
if ( f.attribute( mFkeyFieldIdx ) == mForeignKey )
436+
currentSelection = f.id();
437+
}
438+
439+
qSort( cache.begin(), cache.end(), orderByLessThan );
440+
441+
Q_FOREACH( const ValueRelationItem& item, cache )
442+
{
443+
mComboBox->addItem( item.first.toString(), item.second );
444+
445+
if ( currentSelection == item.second )
446+
mComboBox->setCurrentIndex( mComboBox->count() - 1 );
447+
}
448+
}
449+
else
402450
{
403-
QString txt = exp.evaluate( &f ).toString();
404-
mComboBox->addItem( txt, f.id() );
451+
while ( fit.nextFeature( f ) )
452+
{
453+
QString txt = exp.evaluate( &f ).toString();
454+
mComboBox->addItem( txt, f.id() );
405455

406-
if ( f.attribute( mFkeyFieldIdx ) == mForeignKey )
407-
mComboBox->setCurrentIndex( mComboBox->count() - 1 );
456+
if ( f.attribute( mFkeyFieldIdx ) == mForeignKey )
457+
mComboBox->setCurrentIndex( mComboBox->count() - 1 );
408458

409-
mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) );
459+
mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) );
460+
}
410461
}
411462

412463
// Only connect after iterating, to have only one iterator on the referenced table at once

src/gui/editorwidgets/qgsrelationreferencewidget.h

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
3636
Q_PROPERTY( bool openFormButtonVisible READ openFormButtonVisible WRITE setOpenFormButtonVisible )
3737

3838
public:
39+
typedef QPair < QVariant, QgsFeatureId > ValueRelationItem;
40+
typedef QVector < ValueRelationItem > ValueRelationCache;
41+
3942
enum CanvasExtent
4043
{
4144
Fixed,
@@ -71,6 +74,11 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
7174
bool allowMapIdentification() {return mAllowMapIdentification;}
7275
void setAllowMapIdentification( bool allowMapIdentification );
7376

77+
//! If the widget will order the combobox entries by value
78+
bool orderByValue() { return mOrderByValue; }
79+
//! Set if the widget will order the combobox entries by value
80+
void setOrderByValue( bool orderByValue );
81+
7482
//! determines the open form button is visible in the widget
7583
bool openFormButtonVisible() {return mOpenFormButtonVisible;}
7684
void setOpenFormButtonVisible( bool openFormButtonVisible );
@@ -133,6 +141,7 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
133141
bool mEmbedForm;
134142
bool mReadOnlySelector;
135143
bool mAllowMapIdentification;
144+
bool mOrderByValue;
136145
bool mOpenFormButtonVisible;
137146

138147
// UI

src/gui/editorwidgets/qgsrelationreferencewidgetwrapper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ void QgsRelationReferenceWidgetWrapper::initWidget( QWidget* editor )
4848
bool showForm = config( "ShowForm", true ).toBool();
4949
bool mapIdent = config( "MapIdentification", false ).toBool();
5050
bool readOnlyWidget = config( "ReadOnly", false ).toBool();
51+
bool orderByValue = config( "OrderByValue", false ).toBool();
5152

5253
mWidget->setEmbedForm( showForm );
5354
mWidget->setReadOnlySelector( readOnlyWidget );
5455
mWidget->setAllowMapIdentification( mapIdent );
56+
mWidget->setOrderByValue( orderByValue );
5557

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

src/ui/editorwidgets/qgsrelationreferenceconfigdlgbase.ui

+10-3
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,34 @@
4444
</property>
4545
</widget>
4646
</item>
47-
<item row="7" column="0" colspan="2">
47+
<item row="8" column="0" colspan="2">
4848
<widget class="QCheckBox" name="mCbxShowForm">
4949
<property name="text">
5050
<string>Show embedded form</string>
5151
</property>
5252
</widget>
5353
</item>
54-
<item row="8" column="0" colspan="2">
54+
<item row="9" column="0" colspan="2">
5555
<widget class="QCheckBox" name="mCbxMapIdentification">
5656
<property name="text">
5757
<string>On map identification (for geometric layers only)</string>
5858
</property>
5959
</widget>
6060
</item>
61-
<item row="9" column="0" colspan="2">
61+
<item row="10" column="0" colspan="2">
6262
<widget class="QCheckBox" name="mCbxReadOnly">
6363
<property name="text">
6464
<string>Use a read-only line edit instead of a combobox</string>
6565
</property>
6666
</widget>
6767
</item>
68+
<item row="7" column="0" colspan="2">
69+
<widget class="QCheckBox" name="mCbxOrderByValue">
70+
<property name="text">
71+
<string>Order by value</string>
72+
</property>
73+
</widget>
74+
</item>
6875
</layout>
6976
</widget>
7077
<customwidgets>

0 commit comments

Comments
 (0)