Skip to content
Permalink
Browse files

Manage null representation value in QgsApplication::nullRepresentation()

  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent 35a2be6 commit f4bc53695ec4ac6290c3ca01b4c2c5cb255addb5
Showing with 99 additions and 54 deletions.
  1. +1 −1 src/app/qgsclipboard.cpp
  2. +2 −2 src/app/qgsoptions.cpp
  3. +1 −1 src/core/fieldformatter/qgsdatetimefieldformatter.cpp
  4. +2 −1 src/core/fieldformatter/qgskeyvaluefieldformatter.cpp
  5. +2 −2 src/core/fieldformatter/qgslistfieldformatter.cpp
  6. +2 −3 src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
  7. +19 −0 src/core/qgsapplication.cpp
  8. +21 −0 src/core/qgsapplication.h
  9. +1 −1 src/core/qgsfield.cpp
  10. +1 −1 src/gui/attributetable/qgsfeaturelistmodel.cpp
  11. +1 −1 src/gui/editorwidgets/qgsdatetimeedit.cpp
  12. +1 −1 src/gui/editorwidgets/qgsdefaultsearchwidgetwrapper.cpp
  13. +4 −4 src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp
  14. +3 −3 src/gui/editorwidgets/qgsfilenamewidgetwrapper.cpp
  15. +3 −3 src/gui/editorwidgets/qgsphotowidgetwrapper.cpp
  16. +2 −2 src/gui/editorwidgets/qgsrangewidgetwrapper.cpp
  17. +2 −2 src/gui/editorwidgets/qgsrelationreferencesearchwidgetwrapper.cpp
  18. +5 −5 src/gui/editorwidgets/qgsrelationreferencewidget.cpp
  19. +3 −3 src/gui/editorwidgets/qgstexteditwrapper.cpp
  20. +3 −3 src/gui/editorwidgets/qgsuniquevaluewidgetwrapper.cpp
  21. +4 −3 src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp
  22. +2 −2 src/gui/editorwidgets/qgsvaluerelationsearchwidgetwrapper.cpp
  23. +3 −3 src/gui/editorwidgets/qgswebviewwidgetwrapper.cpp
  24. +2 −1 src/gui/qgsfieldvalidator.cpp
  25. +3 −2 src/gui/qgsfilewidget.cpp
  26. +1 −1 src/gui/qgsquerybuilder.cpp
  27. +1 −0 tests/src/core/CMakeLists.txt
  28. +4 −3 tests/src/core/testqgsfield.cpp
@@ -123,7 +123,7 @@ QString QgsClipboard::generateClipboardText() const
textFields += it->geometry().exportToWkt();
else
{
textFields += settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
textFields += QgsApplication::nullRepresentation();
}
}

@@ -640,7 +640,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl )
else
mComboCopyFeatureFormat->setCurrentIndex( mComboCopyFeatureFormat->findData( mSettings->value( QStringLiteral( "/qgis/copyGeometryAsWKT" ), true ).toBool() ?
QgsClipboard::AttributesWithWKT : QgsClipboard::AttributesOnly ) );
leNullValue->setText( mSettings->value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
leNullValue->setText( QgsApplication::nullRepresentation() );
cbxIgnoreShapeEncoding->setChecked( mSettings->value( QStringLiteral( "/qgis/ignoreShapeEncoding" ), true ).toBool() );

cmbLegendDoubleClickAction->setCurrentIndex( mSettings->value( QStringLiteral( "/qgis/legendDoubleClickAction" ), 0 ).toInt() );
@@ -1254,7 +1254,7 @@ void QgsOptions::saveOptions()
}
mSettings->setValue( QStringLiteral( "/qgis/enableMacros" ), cmbEnableMacros->currentIndex() );

mSettings->setValue( QStringLiteral( "/qgis/nullValue" ), leNullValue->text() );
QgsApplication::setNullRepresentation( leNullValue->text() );
mSettings->setValue( QStringLiteral( "/qgis/style" ), cmbStyle->currentText() );
mSettings->setValue( QStringLiteral( "/IconSize" ), cmbIconSize->currentText() );

@@ -34,7 +34,7 @@ QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer* layer, int fi
if ( value.isNull() )
{
QSettings settings;
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
return QgsApplication::nullRepresentation();
}

const QgsField field = layer->fields().at( fieldIndex );
@@ -14,6 +14,7 @@
* *
***************************************************************************/
#include "qgskeyvaluefieldformatter.h"
#include "qgsapplication.h"

#include <QSettings>

@@ -32,7 +33,7 @@ QString QgsKeyValueFieldFormatter::representValue( QgsVectorLayer* layer, int fi
if ( value.isNull() )
{
QSettings settings;
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
return QgsApplication::nullRepresentation();
}

QString result;
@@ -14,7 +14,7 @@
* *
***************************************************************************/
#include "qgslistfieldformatter.h"

#include "qgsapplication.h"
#include <QSettings>

QString QgsListFieldFormatter::id() const
@@ -32,7 +32,7 @@ QString QgsListFieldFormatter::representValue( QgsVectorLayer* layer, int fieldI
if ( value.isNull() )
{
QSettings settings;
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
return QgsApplication::nullRepresentation();
}

QString result;
@@ -47,7 +47,7 @@ QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer* layer, i
Q_UNUSED( layer )
Q_UNUSED( fieldIndex )

QgsValueRelationFieldFormatter::ValueRelationCache vrCache;
ValueRelationCache vrCache;

if ( cache.isValid() )
{
@@ -77,8 +77,7 @@ QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer* layer, i
{
if ( value.isNull() )
{
QSettings settings;
return settings.value( "qgis/nullValue", "NULL" ).toString();
return QgsApplication::nullRepresentation();
}

Q_FOREACH ( const QgsValueRelationFieldFormatter::ValueRelationItem& item, vrCache )
@@ -1258,6 +1258,25 @@ void QgsApplication::copyPath( const QString& src, const QString& dst )
}
}

QString QgsApplication::nullRepresentation()
{
QgsApplication* app = instance();
if ( app->mNullRepresentation.isNull() )
app->mNullRepresentation = QSettings().value( QStringLiteral( "qgis/nullValue" ), QStringLiteral( "NULL" ) ).toString();
return app->mNullRepresentation;
}

void QgsApplication::setNullRepresentation( const QString& nullRepresentation )
{
QgsApplication* app = instance();
if ( app->mNullRepresentation == nullRepresentation )
return;

app->mNullRepresentation = nullRepresentation;
QSettings().setValue( QStringLiteral( "qgis/nullValue" ), nullRepresentation );
emit app->nullRepresentationChanged();
}

QgsActionScopeRegistry* QgsApplication::actionScopeRegistry()
{
return instance()->mActionScopeRegistry;
@@ -402,6 +402,21 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
static QgsFieldFormatterRegistry* fieldKitRegistry();

/**
* This string is used to represent the value `NULL` throughout QGIS.
*
* In general, when passing values around, prefer to use a null QVariant
* `QVariant( field.type() )` or `QVariant( QVariant::Int )`. This value
* should only be used in the final presentation step when showing values
* in a widget or sending it to a web browser.
*/
static QString nullRepresentation();

/**
* \copydoc nullRepresentation()
*/
static void setNullRepresentation( const QString& nullRepresentation );

public slots:

/** Causes the application instance to emit the settingsChanged() signal. This should
@@ -422,6 +437,11 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
void settingsChanged();

/**
* \copydoc nullRepresentation()
*/
void nullRepresentationChanged();

private:
static void copyPath( const QString& src, const QString& dst );
static QObject* ABISYM( mFileOpenEventReceiver );
@@ -472,6 +492,7 @@ class CORE_EXPORT QgsApplication : public QApplication
QgsRuntimeProfiler* mProfiler;
QgsTaskManager* mTaskManager;
QgsFieldFormatterRegistry* mFieldFormatterRegistry;
QString mNullRepresentation;
};

#endif
@@ -211,7 +211,7 @@ QString QgsField::displayString( const QVariant& v ) const
if ( v.isNull() )
{
QSettings settings;
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
return QgsApplication::nullRepresentation();
}

if ( d->type == QVariant::Double && d->precision > 0 )
@@ -73,7 +73,7 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
{
if ( role == Qt::DisplayRole )
{
return QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
return QgsApplication::nullRepresentation();
}
else if ( role == QgsAttributeTableModel::FeatureIdRole )
{
@@ -37,7 +37,7 @@ QgsDateTimeEdit::QgsDateTimeEdit( QWidget *parent )
mClearButton->hide();
connect( mClearButton, SIGNAL( clicked() ), this, SLOT( clear() ) );

mNullLabel = new QLineEdit( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString(), this );
mNullLabel = new QLineEdit( QgsApplication::nullRepresentation(), this );
mNullLabel->setReadOnly( true );
mNullLabel->setStyleSheet( QStringLiteral( "position: absolute; border: none; font-style: italic; color: grey;" ) );
mNullLabel->hide();
@@ -58,7 +58,7 @@ void QgsDefaultSearchWidgetWrapper::setExpression( QString exp )
bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong );

QSettings settings;
QString nullValue = settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
QString nullValue = QgsApplication::nullRepresentation();
QString fieldName = layer()->fields().at( mFieldIdx ).name();
QString str;
if ( exp == nullValue )
@@ -41,7 +41,7 @@ QVariant QgsExternalResourceWidgetWrapper::value() const

if ( mLineEdit )
{
if ( mLineEdit->text().isEmpty() || mLineEdit->text() == QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() )
if ( mLineEdit->text().isEmpty() || mLineEdit->text() == QgsApplication::nullRepresentation() )
{
return QVariant( field().type() );
}
@@ -93,7 +93,7 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget* editor )
QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
if ( fle )
{
fle->setNullValue( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
fle->setNullValue( QgsApplication::nullRepresentation() );
}
}
else
@@ -151,7 +151,7 @@ void QgsExternalResourceWidgetWrapper::setValue( const QVariant& value )
{
if ( value.isNull() )
{
mLineEdit->setText( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
mLineEdit->setText( QgsApplication::nullRepresentation() );
}
else
{
@@ -169,7 +169,7 @@ void QgsExternalResourceWidgetWrapper::setValue( const QVariant& value )
{
if ( value.isNull() )
{
mQgsWidget->setDocumentPath( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
mQgsWidget->setDocumentPath( QgsApplication::nullRepresentation() );
}
else
{
@@ -36,7 +36,7 @@ QVariant QgsFileNameWidgetWrapper::value() const

if ( mLineEdit )
{
if ( mLineEdit->text() == QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() )
if ( mLineEdit->text() == QgsApplication::nullRepresentation() )
value = QVariant( field().type() );
else
value = mLineEdit->text();
@@ -103,7 +103,7 @@ void QgsFileNameWidgetWrapper::initWidget( QWidget* editor )
QgsFilterLineEdit* fle = qobject_cast<QgsFilterLineEdit*>( editor );
if ( fle )
{
fle->setNullValue( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
fle->setNullValue( QgsApplication::nullRepresentation() );
}

connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
@@ -115,7 +115,7 @@ void QgsFileNameWidgetWrapper::setValue( const QVariant& value )
if ( mLineEdit )
{
if ( value.isNull() )
mLineEdit->setText( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
mLineEdit->setText( QgsApplication::nullRepresentation() );
else
mLineEdit->setText( value.toString() );
}
@@ -137,7 +137,7 @@ QVariant QgsPhotoWidgetWrapper::value() const

if ( mLineEdit )
{
if ( mLineEdit->text() == QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() )
if ( mLineEdit->text() == QgsApplication::nullRepresentation() )
v = QVariant( QVariant::String );
else
v = mLineEdit->text();
@@ -223,7 +223,7 @@ void QgsPhotoWidgetWrapper::initWidget( QWidget* editor )
QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
if ( fle )
{
fle->setNullValue( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
fle->setNullValue( QgsApplication::nullRepresentation() );
}

connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( valueChanged( QString ) ) );
@@ -246,7 +246,7 @@ void QgsPhotoWidgetWrapper::setValue( const QVariant& value )
{
if ( value.isNull() )
{
whileBlocking( mLineEdit )->setText( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
whileBlocking( mLineEdit )->setText( QgsApplication::nullRepresentation() );
clearPicture();
}
else
@@ -116,7 +116,7 @@ void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
minval -= stepval;
}
mDoubleSpinBox->setValue( minval );
mDoubleSpinBox->setSpecialValueText( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
mDoubleSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
}
mDoubleSpinBox->setMinimum( min.isValid() ? min.toDouble() : std::numeric_limits<double>::min() );
mDoubleSpinBox->setMaximum( max.isValid() ? max.toDouble() : std::numeric_limits<double>::max() );
@@ -137,7 +137,7 @@ void QgsRangeWidgetWrapper::initWidget( QWidget* editor )
int stepval = step.toInt();
minval -= stepval;
mIntSpinBox->setValue( minval );
mIntSpinBox->setSpecialValueText( QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() );
mIntSpinBox->setSpecialValueText( QgsApplication::nullRepresentation() );
}
setupIntEditor( min, max, step, mIntSpinBox, this );
if ( config( QStringLiteral( "Suffix" ) ).isValid() )
@@ -137,7 +137,7 @@ void QgsRelationReferenceSearchWidgetWrapper::onValueChanged( const QVariant& va
else
{
QSettings settings;
setExpression( value.isNull() ? settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() : value.toString() );
setExpression( value.isNull() ? QgsApplication::nullRepresentation() : value.toString() );
emit valueChanged();
}
emit expressionChanged( mExpression );
@@ -146,7 +146,7 @@ void QgsRelationReferenceSearchWidgetWrapper::onValueChanged( const QVariant& va
void QgsRelationReferenceSearchWidgetWrapper::setExpression( QString exp )
{
QSettings settings;
QString nullValue = settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
QString nullValue = QgsApplication::nullRepresentation();
QString fieldName = layer()->fields().at( mFieldIdx ).name();

QString str;
@@ -302,7 +302,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )

void QgsRelationReferenceWidget::deleteForeignKey()
{
QVariant nullValue = QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" );
QVariant nullValue = QgsApplication::nullRepresentation();
if ( mReadOnlySelector )
{
QString nullText = QLatin1String( "" );
@@ -477,7 +477,7 @@ void QgsRelationReferenceWidget::init()
mFilterComboBoxes << cb;
mReferencedLayer->uniqueValues( idx, uniqueValues );
cb->addItem( mReferencedLayer->attributeDisplayName( idx ) );
QVariant nullValue = QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" );
QVariant nullValue = QgsApplication::nullRepresentation();
cb->addItem( nullValue.toString(), QVariant( mReferencedLayer->fields().at( idx ).type() ) );

qSort( uniqueValues.begin(), uniqueValues.end(), qgsVariantLessThan );
@@ -496,7 +496,7 @@ void QgsRelationReferenceWidget::init()

if ( mChainFilters )
{
QVariant nullValue = QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" );
QVariant nullValue = QgsApplication::nullRepresentation();

QgsFeature ft;
QgsFeatureIterator fit = layerCache->getFeatures();
@@ -552,7 +552,7 @@ void QgsRelationReferenceWidget::init()
mComboBox->setCompleter( completer );


QVariant nullValue = QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" );
QVariant nullValue = QgsApplication::nullRepresentation();

if ( mChainFilters && mFeature.isValid() )
{
@@ -797,7 +797,7 @@ void QgsRelationReferenceWidget::mapToolDeactivated()

void QgsRelationReferenceWidget::filterChanged()
{
QVariant nullValue = QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" );
QVariant nullValue = QgsApplication::nullRepresentation();

QStringList filters;
QgsAttributeList attrs;
@@ -56,7 +56,7 @@ QVariant QgsTextEditWrapper::value() const
}

if (( v.isEmpty() && ( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) ) ||
v == QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString() )
v == QgsApplication::nullRepresentation() )
return QVariant( field().type() );

if ( !defaultValue().isNull() && v == defaultValue().toString() )
@@ -119,7 +119,7 @@ void QgsTextEditWrapper::initWidget( QWidget* editor )
QVariant defVal = defaultValue();
if ( defVal.isNull() )
{
defVal = QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" );
defVal = QgsApplication::nullRepresentation();
}

QgsFilterLineEdit *fle = qobject_cast<QgsFilterLineEdit*>( mLineEdit );
@@ -215,7 +215,7 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant& val )
if ( val.isNull() )
{
if ( !( field().type() == QVariant::Int || field().type() == QVariant::Double || field().type() == QVariant::LongLong || field().type() == QVariant::Date ) )
v = QSettings().value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
v = QgsApplication::nullRepresentation();
}
else
v = val.toString();

0 comments on commit f4bc536

Please sign in to comment.
You can’t perform that action at this time.