Skip to content

Commit

Permalink
Manage null representation value in QgsApplication::nullRepresentation()
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent 35a2be6 commit f4bc536
Show file tree
Hide file tree
Showing 28 changed files with 99 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/app/qgsclipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ QString QgsClipboard::generateClipboardText() const
textFields += it->geometry().exportToWkt();
else
{
textFields += settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
textFields += QgsApplication::nullRepresentation();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down Expand Up @@ -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() );

Expand Down
2 changes: 1 addition & 1 deletion src/core/fieldformatter/qgsdatetimefieldformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion src/core/fieldformatter/qgskeyvaluefieldformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* *
***************************************************************************/
#include "qgskeyvaluefieldformatter.h"
#include "qgsapplication.h"

#include <QSettings>

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/core/fieldformatter/qgslistfieldformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* *
***************************************************************************/
#include "qgslistfieldformatter.h"

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

QString QgsListFieldFormatter::id() const
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer* layer, i
Q_UNUSED( layer )
Q_UNUSED( fieldIndex )

QgsValueRelationFieldFormatter::ValueRelationCache vrCache;
ValueRelationCache vrCache;

if ( cache.isValid() )
{
Expand Down Expand Up @@ -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 )
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/core/qgsapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );
Expand Down Expand Up @@ -472,6 +492,7 @@ class CORE_EXPORT QgsApplication : public QApplication
QgsRuntimeProfiler* mProfiler;
QgsTaskManager* mTaskManager;
QgsFieldFormatterRegistry* mFieldFormatterRegistry;
QString mNullRepresentation;
};

#endif
2 changes: 1 addition & 1 deletion src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsfeaturelistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsdatetimeedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsdefaultsearchwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
8 changes: 4 additions & 4 deletions src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions src/gui/editorwidgets/qgsfilenamewidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 ) ) );
Expand All @@ -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() );
}
Expand Down
6 changes: 3 additions & 3 deletions src/gui/editorwidgets/qgsphotowidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 ) ) );
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/gui/editorwidgets/qgsrangewidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand All @@ -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() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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( "" );
Expand Down Expand Up @@ -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 );
Expand All @@ -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();
Expand Down Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/gui/editorwidgets/qgstexteditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit f4bc536

Please sign in to comment.