Skip to content

Commit b6a878e

Browse files
author
jef
committed
[FEATURE] feature form updates:
- make NULL value string representation configurable - fix feature updates in feature form from attribute table - add support for NULL values in value maps (comboboxes) - use layer names instead of ids in drop down list when loading value maps from layers. - support feature form expression fields: line edits on the form which name prefix "expr_" are evaluated. Their value is interpreted as field calculator string and replaced with the calculated value. - support search for NULL in attribute table git-svn-id: http://svn.osgeo.org/qgis/trunk@15054 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 44187c0 commit b6a878e

File tree

9 files changed

+144
-33
lines changed

9 files changed

+144
-33
lines changed

src/app/attributetable/qgsattributetabledialog.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,25 @@ void QgsAttributeTableDialog::search()
647647
sensString = "LIKE";
648648
}
649649

650-
QString str = QString( "%1 %2 '%3'" )
651-
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
652-
.arg( numeric ? "=" : sensString )
653-
.arg( numeric ? mQuery->displayText().replace( "'", "''" ) :
654-
"%" + mQuery->displayText().replace( "'", "''" ) + "%" ); // escape quotes
650+
QSettings settings;
651+
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
652+
653+
QString str;
654+
if ( mQuery->displayText() == nullValue )
655+
{
656+
str = QString( "%1 IS NULL" ).arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) );
657+
}
658+
else
659+
{
660+
str = QString( "%1 %2 '%3'" )
661+
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
662+
.arg( numeric ? "=" : sensString )
663+
.arg( numeric
664+
? mQuery->displayText().replace( "'", "''" )
665+
:
666+
"%" + mQuery->displayText().replace( "'", "''" ) + "%" ); // escape quotes
667+
}
668+
655669
doSearch( str );
656670
}
657671

src/app/attributetable/qgsattributetablemodel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
414414
}
415415
else
416416
{
417-
return QVariant( "NULL" );
417+
QSettings settings;
418+
return settings.value( "qgis/nullValue", "NULL" );
418419
}
419420
}
420421

@@ -497,6 +498,7 @@ void QgsAttributeTableModel::featureForm( QModelIndex &idx )
497498
QgsFeature f;
498499
QgsAttributeMap attributes;
499500

501+
f.setFeatureId( rowToId( idx.row() ) );
500502
for ( int i = 0; i < mAttributes.size(); i++ )
501503
{
502504
f.changeAttribute( i, data( index( idx.row(), i ), Qt::EditRole ) );

src/app/qgsattributedialog.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "qgssymbol.h"
2626
#include "qgsattributeeditor.h"
2727
#include "qgsrubberband.h"
28+
#include "qgssearchstring.h"
29+
#include "qgssearchtreenode.h"
2830

2931
#include "qgisapp.h"
3032

@@ -193,6 +195,48 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
193195
mpIndizes << it.key();
194196
mpWidgets << myWidget;
195197
}
198+
199+
foreach( QLineEdit *le, mDialog->findChildren<QLineEdit*>() )
200+
{
201+
if ( !le->objectName().startsWith( "expr_" ) )
202+
continue;
203+
204+
le->setReadOnly( true );
205+
QString expr = le->text();
206+
le->setText( tr( "Error" ) );
207+
208+
QgsSearchString ss;
209+
if ( !ss.setString( expr ) )
210+
continue;
211+
212+
QgsSearchTreeNode *st = ss.tree();
213+
if ( !st )
214+
continue;
215+
216+
if ( !mFeature->geometry() && st->needsGeometry() )
217+
{
218+
QgsFeature f;
219+
if ( vl->featureAtId( mFeature->id(), f, true, false ) && f.geometry() )
220+
{
221+
mFeature->setGeometry( *f.geometry() );
222+
}
223+
}
224+
225+
QgsSearchTreeValue value;
226+
st->getValue( value, st, vl->pendingFields(), *mFeature );
227+
228+
if ( !value.isError() )
229+
{
230+
if ( value.isNumeric() )
231+
le->setText( QString::number( value.number() ) );
232+
else
233+
le->setText( value.string() );
234+
}
235+
else
236+
{
237+
le->setText( tr( "Error: %1" ).arg( st->errorMsg() ) );
238+
}
239+
}
196240
}
197241

198242
if ( mDialog )

src/app/qgsattributetypeloaddialog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void QgsAttributeTypeLoadDialog::fillLayerList()
7474
QMap<QString, QgsMapLayer*>::iterator layer_it = QgsMapLayerRegistry::instance()->mapLayers().begin();
7575
for ( ; layer_it != QgsMapLayerRegistry::instance()->mapLayers().end(); layer_it++ )
7676
{
77-
layerComboBox->addItem( layer_it.key() );
77+
layerComboBox->addItem( layer_it.value()->name(), layer_it.key() );
7878
}
7979
}
8080

@@ -89,7 +89,7 @@ void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
8989
return;
9090
}
9191

92-
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
92+
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
9393
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
9494
if ( vLayer == NULL )
9595
{
@@ -120,7 +120,7 @@ void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
120120
}
121121
int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
122122
int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
123-
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
123+
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
124124
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
125125
if ( vLayer == NULL )
126126
{
@@ -170,7 +170,7 @@ void QgsAttributeTypeLoadDialog::loadDataToValueMap()
170170
mValueMap.clear();
171171
int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
172172
int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
173-
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
173+
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
174174
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
175175
if ( vLayer == NULL )
176176
{

src/app/qgsclipboard.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QString>
2424
#include <QStringList>
2525
#include <QClipboard>
26+
#include <QSettings>
2627

2728
#include "qgsclipboard.h"
2829
#include "qgsfeature.h"
@@ -75,7 +76,10 @@ void QgsClipboard::replaceWithCopyOf( const QgsFieldMap& fields, QgsFeatureList&
7576
if ( it->geometry() )
7677
textFields += it->geometry()->exportToWkt();
7778
else
78-
textFields += "NULL";
79+
{
80+
QSettings settings;
81+
textFields += settings.value( "qgis/nullValue", "NULL" ).toString();
82+
}
7983

8084
// QgsDebugMsg("about to traverse fields.");
8185
//

src/app/qgsoptions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
253253
cbxAddPostgisDC->setChecked( settings.value( "/qgis/addPostgisDC", false ).toBool() );
254254
cbxAddNewLayersToCurrentGroup->setChecked( settings.value( "/qgis/addNewLayersToCurrentGroup", false ).toBool() );
255255
cbxCreateRasterLegendIcons->setChecked( settings.value( "/qgis/createRasterLegendIcons", true ).toBool() );
256+
leNullValue->setText( settings.value( "qgis/nullValue", "NULL" ).toString() );
256257

257258
//set the color for selections
258259
int myRed = settings.value( "/qgis/default_selection_color_red", 255 ).toInt();
@@ -524,6 +525,7 @@ void QgsOptions::saveOptions()
524525
settings.setValue( "qgis/capitaliseLayerName", capitaliseCheckBox->isChecked() );
525526
settings.setValue( "qgis/askToSaveProjectChanges", chbAskToSaveProjectChanges->isChecked() );
526527
settings.setValue( "qgis/warnOldProjectVersion", chbWarnOldProjectVersion->isChecked() );
528+
settings.setValue( "qgis/nullValue", leNullValue->text() );
527529

528530
//overlay placement method
529531
int overlayIndex = mOverlayAlgorithmComboBox->currentIndex();

src/gui/qgsattributeeditor.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <QDial>
3838
#include <QCalendarWidget>
3939
#include <QDialogButtonBox>
40+
#include <QSettings>
4041

4142
void QgsAttributeEditor::selectFileName( void )
4243
{
@@ -451,12 +452,15 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
451452
bool modified = false;
452453
QString text;
453454

455+
QSettings settings;
456+
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
457+
454458
QLineEdit *le = qobject_cast<QLineEdit *>( widget );
455459
if ( le )
456460
{
457461
text = le->text();
458462
modified = le->isModified();
459-
if ( text == "NULL" )
463+
if ( text == nullValue )
460464
{
461465
text = QString::null;
462466
}
@@ -467,7 +471,7 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
467471
{
468472
text = te->toHtml();
469473
modified = te->document()->isModified();
470-
if ( text == "NULL" )
474+
if ( text == nullValue )
471475
{
472476
text = QString::null;
473477
}
@@ -478,7 +482,7 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
478482
{
479483
text = pte->toPlainText();
480484
modified = pte->document()->isModified();
481-
if ( text == "NULL" )
485+
if ( text == nullValue )
482486
{
483487
text = QString::null;
484488
}
@@ -492,11 +496,16 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
492496
editType == QgsVectorLayer::Classification )
493497
{
494498
text = cb->itemData( cb->currentIndex() ).toString();
499+
if ( text == nullValue )
500+
{
501+
text = QString::null;
502+
}
495503
}
496504
else
497505
{
498506
text = cb->currentText();
499507
}
508+
modified = true;
500509
}
501510

502511
QSpinBox *sb = qobject_cast<QSpinBox *>( widget );
@@ -614,18 +623,27 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
614623
const QgsField &field = vl->pendingFields()[idx];
615624
QVariant::Type myFieldType = field.type();
616625

626+
QSettings settings;
627+
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
628+
617629
switch ( editType )
618630
{
619631
case QgsVectorLayer::Classification:
620632
case QgsVectorLayer::UniqueValues:
621633
case QgsVectorLayer::Enumeration:
622634
case QgsVectorLayer::ValueMap:
623635
{
636+
QVariant v = value;
624637
QComboBox *cb = qobject_cast<QComboBox *>( editor );
625638
if ( cb == NULL )
626639
return false;
627640

628-
int idx = cb->findData( value );
641+
if ( v.isNull() )
642+
{
643+
v = nullValue;
644+
}
645+
646+
int idx = cb->findData( v );
629647
if ( idx < 0 )
630648
return false;
631649

@@ -693,7 +711,7 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
693711
if ( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong )
694712
text = "";
695713
else
696-
text = "NULL";
714+
text = nullValue;
697715
else
698716
text = value.toString();
699717

src/gui/qgsattributeeditor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class GUI_EXPORT QgsAttributeEditor : public QObject
3131
Q_OBJECT
3232

3333
public:
34-
QgsAttributeEditor( QObject *parent ) : QObject( parent ) {}
34+
QgsAttributeEditor( QObject *parent ) : QObject( parent ) {};
3535
static QWidget *createAttributeEditor( QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value );
3636
static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value );
3737
static bool setValue( QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value );

0 commit comments

Comments
 (0)