|
33 | 33 | #include "qgsrelationreferenceconfigdlg.h"
|
34 | 34 | #include "qgsvectorlayer.h"
|
35 | 35 |
|
| 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 | +} |
36 | 54 |
|
37 | 55 | QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget* parent )
|
38 | 56 | : QWidget( parent )
|
@@ -360,6 +378,11 @@ void QgsRelationReferenceWidget::setAllowMapIdentification( bool allowMapIdentif
|
360 | 378 | mAllowMapIdentification = allowMapIdentification;
|
361 | 379 | }
|
362 | 380 |
|
| 381 | +void QgsRelationReferenceWidget::setOrderByValue( bool orderByValue ) |
| 382 | +{ |
| 383 | + mOrderByValue = orderByValue; |
| 384 | +} |
| 385 | + |
363 | 386 | void QgsRelationReferenceWidget::setOpenFormButtonVisible( bool openFormButtonVisible )
|
364 | 387 | {
|
365 | 388 | mOpenFormButton->setVisible( openFormButtonVisible );
|
@@ -398,15 +421,43 @@ void QgsRelationReferenceWidget::init()
|
398 | 421 | exp.prepare( mReferencedLayer->pendingFields() );
|
399 | 422 |
|
400 | 423 | 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 |
402 | 450 | {
|
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() ); |
405 | 455 |
|
406 |
| - if ( f.attribute( mFkeyFieldIdx ) == mForeignKey ) |
407 |
| - mComboBox->setCurrentIndex( mComboBox->count() - 1 ); |
| 456 | + if ( f.attribute( mFkeyFieldIdx ) == mForeignKey ) |
| 457 | + mComboBox->setCurrentIndex( mComboBox->count() - 1 ); |
408 | 458 |
|
409 |
| - mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) ); |
| 459 | + mFidFkMap.insert( f.id(), f.attribute( mFkeyFieldIdx ) ); |
| 460 | + } |
410 | 461 | }
|
411 | 462 |
|
412 | 463 | // Only connect after iterating, to have only one iterator on the referenced table at once
|
|
0 commit comments