Skip to content

Commit

Permalink
[FEATURE] feature form updates:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jef committed Jan 17, 2011
1 parent 44187c0 commit b6a878e
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 33 deletions.
24 changes: 19 additions & 5 deletions src/app/attributetable/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,25 @@ void QgsAttributeTableDialog::search()
sensString = "LIKE";
}

QString str = QString( "%1 %2 '%3'" )
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
.arg( numeric ? "=" : sensString )
.arg( numeric ? mQuery->displayText().replace( "'", "''" ) :
"%" + mQuery->displayText().replace( "'", "''" ) + "%" ); // escape quotes
QSettings settings;
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();

QString str;
if ( mQuery->displayText() == nullValue )
{
str = QString( "%1 IS NULL" ).arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) );
}
else
{
str = QString( "%1 %2 '%3'" )
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
.arg( numeric ? "=" : sensString )
.arg( numeric
? mQuery->displayText().replace( "'", "''" )
:
"%" + mQuery->displayText().replace( "'", "''" ) + "%" ); // escape quotes
}

doSearch( str );
}

Expand Down
4 changes: 3 additions & 1 deletion src/app/attributetable/qgsattributetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
}
else
{
return QVariant( "NULL" );
QSettings settings;
return settings.value( "qgis/nullValue", "NULL" );
}
}

Expand Down Expand Up @@ -497,6 +498,7 @@ void QgsAttributeTableModel::featureForm( QModelIndex &idx )
QgsFeature f;
QgsAttributeMap attributes;

f.setFeatureId( rowToId( idx.row() ) );
for ( int i = 0; i < mAttributes.size(); i++ )
{
f.changeAttribute( i, data( index( idx.row(), i ), Qt::EditRole ) );
Expand Down
44 changes: 44 additions & 0 deletions src/app/qgsattributedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "qgssymbol.h"
#include "qgsattributeeditor.h"
#include "qgsrubberband.h"
#include "qgssearchstring.h"
#include "qgssearchtreenode.h"

#include "qgisapp.h"

Expand Down Expand Up @@ -193,6 +195,48 @@ QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeat
mpIndizes << it.key();
mpWidgets << myWidget;
}

foreach( QLineEdit *le, mDialog->findChildren<QLineEdit*>() )
{
if ( !le->objectName().startsWith( "expr_" ) )
continue;

le->setReadOnly( true );
QString expr = le->text();
le->setText( tr( "Error" ) );

QgsSearchString ss;
if ( !ss.setString( expr ) )
continue;

QgsSearchTreeNode *st = ss.tree();
if ( !st )
continue;

if ( !mFeature->geometry() && st->needsGeometry() )
{
QgsFeature f;
if ( vl->featureAtId( mFeature->id(), f, true, false ) && f.geometry() )
{
mFeature->setGeometry( *f.geometry() );
}
}

QgsSearchTreeValue value;
st->getValue( value, st, vl->pendingFields(), *mFeature );

if ( !value.isError() )
{
if ( value.isNumeric() )
le->setText( QString::number( value.number() ) );
else
le->setText( value.string() );
}
else
{
le->setText( tr( "Error: %1" ).arg( st->errorMsg() ) );
}
}
}

if ( mDialog )
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsattributetypeloaddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void QgsAttributeTypeLoadDialog::fillLayerList()
QMap<QString, QgsMapLayer*>::iterator layer_it = QgsMapLayerRegistry::instance()->mapLayers().begin();
for ( ; layer_it != QgsMapLayerRegistry::instance()->mapLayers().end(); layer_it++ )
{
layerComboBox->addItem( layer_it.key() );
layerComboBox->addItem( layer_it.value()->name(), layer_it.key() );
}
}

Expand All @@ -89,7 +89,7 @@ void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
return;
}

QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
if ( vLayer == NULL )
{
Expand Down Expand Up @@ -120,7 +120,7 @@ void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
}
int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
if ( vLayer == NULL )
{
Expand Down Expand Up @@ -170,7 +170,7 @@ void QgsAttributeTypeLoadDialog::loadDataToValueMap()
mValueMap.clear();
int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->itemData( layerComboBox->currentIndex() ).toString() );
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
if ( vLayer == NULL )
{
Expand Down
6 changes: 5 additions & 1 deletion src/app/qgsclipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QString>
#include <QStringList>
#include <QClipboard>
#include <QSettings>

#include "qgsclipboard.h"
#include "qgsfeature.h"
Expand Down Expand Up @@ -75,7 +76,10 @@ void QgsClipboard::replaceWithCopyOf( const QgsFieldMap& fields, QgsFeatureList&
if ( it->geometry() )
textFields += it->geometry()->exportToWkt();
else
textFields += "NULL";
{
QSettings settings;
textFields += settings.value( "qgis/nullValue", "NULL" ).toString();
}

// QgsDebugMsg("about to traverse fields.");
//
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
cbxAddPostgisDC->setChecked( settings.value( "/qgis/addPostgisDC", false ).toBool() );
cbxAddNewLayersToCurrentGroup->setChecked( settings.value( "/qgis/addNewLayersToCurrentGroup", false ).toBool() );
cbxCreateRasterLegendIcons->setChecked( settings.value( "/qgis/createRasterLegendIcons", true ).toBool() );
leNullValue->setText( settings.value( "qgis/nullValue", "NULL" ).toString() );

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

//overlay placement method
int overlayIndex = mOverlayAlgorithmComboBox->currentIndex();
Expand Down
28 changes: 23 additions & 5 deletions src/gui/qgsattributeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <QDial>
#include <QCalendarWidget>
#include <QDialogButtonBox>
#include <QSettings>

void QgsAttributeEditor::selectFileName( void )
{
Expand Down Expand Up @@ -451,12 +452,15 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
bool modified = false;
QString text;

QSettings settings;
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();

QLineEdit *le = qobject_cast<QLineEdit *>( widget );
if ( le )
{
text = le->text();
modified = le->isModified();
if ( text == "NULL" )
if ( text == nullValue )
{
text = QString::null;
}
Expand All @@ -467,7 +471,7 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
{
text = te->toHtml();
modified = te->document()->isModified();
if ( text == "NULL" )
if ( text == nullValue )
{
text = QString::null;
}
Expand All @@ -478,7 +482,7 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
{
text = pte->toPlainText();
modified = pte->document()->isModified();
if ( text == "NULL" )
if ( text == nullValue )
{
text = QString::null;
}
Expand All @@ -492,11 +496,16 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
editType == QgsVectorLayer::Classification )
{
text = cb->itemData( cb->currentIndex() ).toString();
if ( text == nullValue )
{
text = QString::null;
}
}
else
{
text = cb->currentText();
}
modified = true;
}

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

QSettings settings;
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();

switch ( editType )
{
case QgsVectorLayer::Classification:
case QgsVectorLayer::UniqueValues:
case QgsVectorLayer::Enumeration:
case QgsVectorLayer::ValueMap:
{
QVariant v = value;
QComboBox *cb = qobject_cast<QComboBox *>( editor );
if ( cb == NULL )
return false;

int idx = cb->findData( value );
if ( v.isNull() )
{
v = nullValue;
}

int idx = cb->findData( v );
if ( idx < 0 )
return false;

Expand Down Expand Up @@ -693,7 +711,7 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
if ( myFieldType == QVariant::Int || myFieldType == QVariant::Double || myFieldType == QVariant::LongLong )
text = "";
else
text = "NULL";
text = nullValue;
else
text = value.toString();

Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsattributeeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GUI_EXPORT QgsAttributeEditor : public QObject
Q_OBJECT

public:
QgsAttributeEditor( QObject *parent ) : QObject( parent ) {}
QgsAttributeEditor( QObject *parent ) : QObject( parent ) {};
static QWidget *createAttributeEditor( QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value );
static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value );
static bool setValue( QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value );
Expand Down
Loading

0 comments on commit b6a878e

Please sign in to comment.