Skip to content
Permalink
Browse files

Make attribute table aware of disappearing field formatters

  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent f4bc536 commit 928f360c77d1ecc9895fa8109ea4abe17e2e2e65
@@ -403,7 +403,7 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
*
* @note Added in QGIS 3.0
*/
static QgsFieldFormatterRegistry* fieldKitRegistry();
static QgsFieldFormatterRegistry* fieldFormatterRegistry();
public slots:

/** Causes the application instance to emit the settingsChanged() signal. This should
@@ -226,34 +226,4 @@ class QgsAttributeTableModel : QAbstractTableModel
//! @note not available in python bindings
// void progress( int i, bool &cancel );
void finished();

protected slots:
/**
* Launched when attribute value has been changed
* @param fid feature id
* @param idx attribute index
* @param value new value
*/
virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
/**
* Launched when eatures have been deleted
* @param fids feature ids
*/
virtual void featuresDeleted( const QgsFeatureIds& fids );
/**
* Launched when a feature has been added
* @param fid feature id
*/
virtual void featureAdded( QgsFeatureId fid );

/**
* Launched when layer has been deleted
*/
virtual void layerDeleted();

protected:
/**
* Gets mFieldCount, mAttributes and mValueMaps
*/
virtual void loadAttributes();
};
@@ -6399,8 +6399,8 @@ QVariant QgisAppFieldValueConverter::convert( int idx, const QVariant& value )
return value;
}
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( mLayer, mLayer->fields().field( idx ).name() );
QgsFieldFormatter* fieldKit = QgsApplication::fieldKitRegistry()->fieldFormatter( setup.type() );
return fieldKit->representValue( mLayer, idx, setup.config(), QVariant(), value );
QgsFieldFormatter* fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
return fieldFormatter->representValue( mLayer, idx, setup.config(), QVariant(), value );
}

///@endcond
@@ -350,9 +350,9 @@ void QgsAttributeTypeDialog::defaultExpressionChanged()
return;
}

QgsFieldFormatter* fieldKit = QgsApplication::fieldKitRegistry()->fieldFormatter( editorWidgetType() );
QgsFieldFormatter* fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( editorWidgetType() );

QString previewText = fieldKit->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );
QString previewText = fieldFormatter->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );

mDefaultPreviewLabel->setText( "<i>" + previewText + "</i>" );
}
@@ -734,7 +734,7 @@ QString QgsIdentifyResultsDialog::representValue( QgsVectorLayer* vlayer, const
QMap<QString, QVariant>& layerCaches = mWidgetCaches[vlayer->id()];

QgsEditorWidgetFactory* factory = QgsEditorWidgetRegistry::instance()->factory( setup.type() );
QgsFieldFormatter* fieldKit = QgsApplication::fieldKitRegistry()->fieldFormatter( setup.type() );
QgsFieldFormatter* fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );

int idx = vlayer->fields().lookupField( fieldName );

@@ -747,11 +747,11 @@ QString QgsIdentifyResultsDialog::representValue( QgsVectorLayer* vlayer, const
}
else
{
cache = fieldKit->createCache( vlayer, idx, setup.config() );
cache = fieldFormatter->createCache( vlayer, idx, setup.config() );
layerCaches.insert( fieldName, cache );
}

return fieldKit->representValue( vlayer, idx, setup.config(), cache, value );
return fieldFormatter->representValue( vlayer, idx, setup.config(), cache, value );
}

void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
@@ -20,6 +20,11 @@
#include "qgsfield.h"
#include "qgsvectorlayer.h"

const QString QgsDateTimeFieldFormatter::DefaultDateFormat = QStringLiteral( "yyyy-MM-dd" );
const QString QgsDateTimeFieldFormatter::DefaultTimeFormat = QStringLiteral( "HH:mm:ss" );
const QString QgsDateTimeFieldFormatter::DefaultDateTimeFormat = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );


QString QgsDateTimeFieldFormatter::id() const
{
return QStringLiteral( "DateTime" );
@@ -60,12 +65,12 @@ QString QgsDateTimeFieldFormatter::defaultFormat( const QVariant::Type type )
switch ( type )
{
case QVariant::DateTime:
return QGSDATETIMEFIELDKIT_DATETIMEFORMAT;
return QgsDateTimeFieldFormatter::DefaultDateTimeFormat;
break;
case QVariant::Time:
return QGSDATETIMEFIELDKIT_TIMEFORMAT;
return QgsDateTimeFieldFormatter::DefaultTimeFormat;
break;
default:
return QGSDATETIMEFIELDKIT_DATEFORMAT;
return QgsDateTimeFieldFormatter::DefaultDateFormat;
}
}
@@ -18,13 +18,13 @@

#include "qgsfieldformatter.h"

#define QGSDATETIMEFIELDKIT_DATEFORMAT QStringLiteral( "yyyy-MM-dd" )
#define QGSDATETIMEFIELDKIT_TIMEFORMAT QStringLiteral( "HH:mm:ss" )
#define QGSDATETIMEFIELDKIT_DATETIMEFORMAT QStringLiteral( "yyyy-MM-dd HH:mm:ss" )

class CORE_EXPORT QgsDateTimeFieldFormatter : public QgsFieldFormatter
{
public:
static const QString DefaultDateFormat;
static const QString DefaultTimeFormat;
static const QString DefaultDateTimeFormat;

virtual QString id() const override;

virtual QString representValue( QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;
@@ -1445,7 +1445,7 @@ void QgsApplication::emitSettingsChanged()
}


QgsFieldFormatterRegistry* QgsApplication::fieldKitRegistry()
QgsFieldFormatterRegistry* QgsApplication::fieldFormatterRegistry()
{
return instance()->mFieldFormatterRegistry;
}
@@ -400,7 +400,7 @@ class CORE_EXPORT QgsApplication : public QApplication
/**
* Get the registry of available field formatters.
*/
static QgsFieldFormatterRegistry* fieldKitRegistry();
static QgsFieldFormatterRegistry* fieldFormatterRegistry();

/**
* This string is used to represent the value `NULL` throughout QGIS.
@@ -26,7 +26,7 @@ class QgsFieldFormatter;
* \ingroup core
* The QgsFieldFormatterRegistry manages registered classes of QgsFieldFormatter.
* A reference to the QgsFieldFormatterRegistry can be obtained from
* QgsApplication::fieldKitRegistry().
* QgsApplication::fieldFormatterRegistry().
*
* @note Added in QGIS 3.0
*/
@@ -39,7 +39,7 @@ class CORE_EXPORT QgsFieldFormatterRegistry : public QObject
/**
* You should not normally need to create your own field formatter registry.
*
* Use the one provided by `QgsApplication::fieldKitRegistry()` instead.
* Use the one provided by `QgsApplication::fieldFormatterRegistry()` instead.
*/
explicit QgsFieldFormatterRegistry( QObject* parent = nullptr );
~QgsFieldFormatterRegistry();
@@ -217,10 +217,10 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
}
else
{
QgsFieldFormatter* fieldKit = mFieldFormatters.at( mSortFieldIndex );
QgsFieldFormatter* fieldFormatter = mFieldFormatters.at( mSortFieldIndex );
const QVariant& widgetCache = mAttributeWidgetCaches.at( mSortFieldIndex );
const QVariantMap& widgetConfig = mWidgetConfigs.at( mSortFieldIndex );
QVariant sortValue = fieldKit->representValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, mFeat.attribute( mSortFieldIndex ) );
QVariant sortValue = fieldFormatter->representValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, mFeat.attribute( mSortFieldIndex ) );
mSortCache.insert( mFeat.id(), sortValue );
}

@@ -267,6 +267,15 @@ void QgsAttributeTableModel::layerDeleted()
mFieldFormatters.clear();
}

void QgsAttributeTableModel::fieldFormatterRemoved( QgsFieldFormatter* fieldFormatter )
{
for ( QVector<QgsFieldFormatter*>::Iterator it = mFieldFormatters.begin(); it != mFieldFormatters.end(); ++it )
{
if ( it.value() == fieldFormatter )
it.setValue( QgsApplication::fieldFormatterRegistry()->defaultFormatter() );
}
}

void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
{
QgsDebugMsgLevel( QString( "(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.filterType() ), 3 );
@@ -281,10 +290,10 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c
}
else
{
QgsFieldFormatter* fieldKit = mFieldFormatters.at( mSortFieldIndex );
QgsFieldFormatter* fieldFormatter = mFieldFormatters.at( mSortFieldIndex );
const QVariant& widgetCache = mAttributeWidgetCaches.at( mSortFieldIndex );
const QVariantMap& widgetConfig = mWidgetConfigs.at( mSortFieldIndex );
QVariant sortValue = fieldKit->representValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, value );
QVariant sortValue = fieldFormatter->representValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, value );
mSortCache.insert( fid, sortValue );
}
}
@@ -343,14 +352,14 @@ void QgsAttributeTableModel::loadAttributes()
{
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( layer(), fields[idx].name() );
QgsEditorWidgetFactory* widgetFactory = QgsEditorWidgetRegistry::instance()->factory( setup.type() );
QgsFieldFormatter* fieldKit = QgsApplication::fieldKitRegistry()->fieldFormatter( setup.type() );
QgsFieldFormatter* fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );

if ( widgetFactory )
{
mWidgetFactories.append( widgetFactory );
mWidgetConfigs.append( setup.config() );
mAttributeWidgetCaches.append( fieldKit->createCache( layer(), idx, setup.config() ) );
mFieldFormatters.append( fieldKit );
mAttributeWidgetCaches.append( fieldFormatter->createCache( layer(), idx, setup.config() ) );
mFieldFormatters.append( fieldFormatter );

attributes << idx;
}
@@ -786,7 +795,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString& expressionString )
mSortFieldIndex = -1;
mSortCacheExpression = QgsExpression( expressionString );

QgsFieldFormatter* fieldKit = nullptr;
QgsFieldFormatter* fieldFormatter = nullptr;
QVariant widgetCache;
QVariantMap widgetConfig;

@@ -811,7 +820,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString& expressionString )

widgetCache = mAttributeWidgetCaches.at( mSortFieldIndex );
widgetConfig = mWidgetConfigs.at( mSortFieldIndex );
fieldKit = mFieldFormatters.at( mSortFieldIndex );
fieldFormatter = mFieldFormatters.at( mSortFieldIndex );
}

QgsFeatureRequest request = QgsFeatureRequest( mFeatureRequest )
@@ -829,7 +838,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString& expressionString )
}
else
{
QVariant sortValue = fieldKit->sortValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, f.attribute( mSortFieldIndex ) );
QVariant sortValue = fieldFormatter->sortValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, f.attribute( mSortFieldIndex ) );
mSortCache.insert( f.id(), sortValue );
}
}
@@ -294,8 +294,6 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
*/
virtual void attributeDeleted( int idx );

protected slots:

/**
* Launched when attribute value has been changed
* @param fid feature id
@@ -321,7 +319,9 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
*/
virtual void layerDeleted();

protected:
virtual void fieldFormatterRemoved( QgsFieldFormatter* fieldFormatter );

private:
QgsVectorLayerCache *mLayerCache;
int mFieldCount;

@@ -344,8 +344,6 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
*/
virtual void loadAttributes();

private:

/**
* Load feature fid into local cache (mFeat)
*
@@ -57,15 +57,15 @@ void QgsDateTimeEditConfig::updateFieldFormat( int idx )
{
if ( idx == 0 )
{
mFieldFormatEdit->setText( QGSDATETIMEFIELDKIT_DATEFORMAT );
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DefaultDateFormat );
}
else if ( idx == 1 )
{
mFieldFormatEdit->setText( QGSDATETIMEFIELDKIT_TIMEFORMAT );
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DefaultTimeFormat );
}
else if ( idx == 2 )
{
mFieldFormatEdit->setText( QGSDATETIMEFIELDKIT_DATETIMEFORMAT );
mFieldFormatEdit->setText( QgsDateTimeFieldFormatter::DefaultDateTimeFormat );
}

mFieldFormatEdit->setVisible( idx == 3 );
@@ -126,11 +126,11 @@ void QgsDateTimeEditConfig::setConfig( const QVariantMap &config )
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( fieldDef.type() ) ).toString();
mFieldFormatEdit->setText( fieldFormat );

if ( fieldFormat == QGSDATETIMEFIELDKIT_DATEFORMAT )
if ( fieldFormat == QgsDateTimeFieldFormatter::DefaultDateFormat )
mFieldFormatComboBox->setCurrentIndex( 0 );
else if ( fieldFormat == QGSDATETIMEFIELDKIT_TIMEFORMAT )
else if ( fieldFormat == QgsDateTimeFieldFormatter::DefaultTimeFormat )
mFieldFormatComboBox->setCurrentIndex( 1 );
else if ( fieldFormat == QGSDATETIMEFIELDKIT_DATETIMEFORMAT )
else if ( fieldFormat == QgsDateTimeFieldFormatter::DefaultDateTimeFormat )
mFieldFormatComboBox->setCurrentIndex( 2 );
else
mFieldFormatComboBox->setCurrentIndex( 3 );
@@ -3383,8 +3383,8 @@ QDomElement QgsWmsServer::createFeatureGML(
QString QgsWmsServer::replaceValueMapAndRelation( QgsVectorLayer* vl, int idx, const QString& attributeVal )
{
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( vl, vl->fields().field( idx ).name() );
QgsFieldFormatter* fieldKit = QgsApplication::fieldKitRegistry()->fieldFormatter( setup.type() );
QString value( fieldKit->representValue( vl, idx, setup.config(), QVariant(), attributeVal ) );
QgsFieldFormatter* fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
QString value( fieldFormatter->representValue( vl, idx, setup.config(), QVariant(), attributeVal ) );

if ( setup.config().value( QStringLiteral( "AllowMulti" ) ).toBool() && value.startsWith( QLatin1String( "{" ) ) && value.endsWith( QLatin1String( "}" ) ) )
{

0 comments on commit 928f360

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