mirror of
				https://github.com/qgis/QGIS.git
				synced 2025-11-04 00:04:25 -05:00 
			
		
		
		
	Make attribute table aware of disappearing field formatters
This commit is contained in:
		
							parent
							
								
									f4bc53695e
								
							
						
					
					
						commit
						928f360c77
					
				@ -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( "}" ) ) )
 | 
			
		||||
  {
 | 
			
		||||
 | 
			
		||||
@ -38,27 +38,27 @@ class TestQgsValueMapFieldFormatter(unittest.TestCase):
 | 
			
		||||
        f = QgsFeature()
 | 
			
		||||
        f.setAttributes([2, 2.5, 'NULL', None, None, None])
 | 
			
		||||
        layer.dataProvider().addFeatures([f])
 | 
			
		||||
        fieldKit = QgsValueMapFieldFormatter()
 | 
			
		||||
        fieldFormatter = QgsValueMapFieldFormatter()
 | 
			
		||||
 | 
			
		||||
        # Tests with different value types occurring in the value map
 | 
			
		||||
        config = {'map': {'two': '2', 'twoandhalf': '2.5', 'NULL text': 'NULL',
 | 
			
		||||
                          'nothing': self.VALUEMAP_NULL_TEXT}}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 0, config, None, 2), 'two')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 1, config, None, 2.5), 'twoandhalf')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 2, config, None, 'NULL'), 'NULL text')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 0, config, None, 2), 'two')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 1, config, None, 2.5), 'twoandhalf')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 2, config, None, 'NULL'), 'NULL text')
 | 
			
		||||
        # Tests with null values of different types, if value map contains null
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 3, config, None, None), 'nothing')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 4, config, None, None), 'nothing')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 5, config, None, None), 'nothing')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 3, config, None, None), 'nothing')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 4, config, None, None), 'nothing')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 5, config, None, None), 'nothing')
 | 
			
		||||
        # Tests with fallback display for different value types
 | 
			
		||||
        config = {}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 0, config, None, 2), '(2)')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 1, config, None, 2.5), '(2.50000)')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 2, config, None, 'NULL'), '(NULL)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 0, config, None, 2), '(2)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 1, config, None, 2.5), '(2.50000)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 2, config, None, 'NULL'), '(NULL)')
 | 
			
		||||
        # Tests with fallback display for null in different types of fields
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 3, config, None, None), '(NULL)')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 4, config, None, None), '(NULL)')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(layer, 5, config, None, None), '(NULL)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 3, config, None, None), '(NULL)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 4, config, None, None), '(NULL)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(layer, 5, config, None, None), '(NULL)')
 | 
			
		||||
 | 
			
		||||
        QgsMapLayerRegistry.instance().removeAllMapLayers()
 | 
			
		||||
 | 
			
		||||
@ -81,31 +81,31 @@ class TestQgsValueRelationFieldFormatter(unittest.TestCase):
 | 
			
		||||
        f.setAttributes([123, 'decoded_val'])
 | 
			
		||||
        second_layer.dataProvider().addFeatures([f])
 | 
			
		||||
 | 
			
		||||
        fieldKit = QgsValueRelationFieldFormatter()
 | 
			
		||||
        fieldFormatter = QgsValueRelationFieldFormatter()
 | 
			
		||||
 | 
			
		||||
        # Everything valid
 | 
			
		||||
        config = {'Layer': second_layer.id(), 'Key': 'pkid', 'Value': 'decoded'}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '123'), 'decoded_val')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '123'), 'decoded_val')
 | 
			
		||||
 | 
			
		||||
        # Code not find match in foreign layer
 | 
			
		||||
        config = {'Layer': second_layer.id(), 'Key': 'pkid', 'Value': 'decoded'}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
 | 
			
		||||
        # Missing Layer
 | 
			
		||||
        config = {'Key': 'pkid', 'Value': 'decoded'}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
 | 
			
		||||
        # Invalid Layer
 | 
			
		||||
        config = {'Layer': 'invalid', 'Key': 'pkid', 'Value': 'decoded'}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
 | 
			
		||||
        # Invalid Key
 | 
			
		||||
        config = {'Layer': second_layer.id(), 'Key': 'invalid', 'Value': 'decoded'}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
 | 
			
		||||
        # Invalid Value
 | 
			
		||||
        config = {'Layer': second_layer.id(), 'Key': 'pkid', 'Value': 'invalid'}
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '456'), '(456)')
 | 
			
		||||
 | 
			
		||||
        QgsMapLayerRegistry.instance().removeMapLayer(second_layer.id())
 | 
			
		||||
 | 
			
		||||
@ -130,7 +130,7 @@ class TestQgsRelationReferenceFieldFormatter(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
        relMgr = QgsProject.instance().relationManager()
 | 
			
		||||
 | 
			
		||||
        fieldKit = QgsRelationReferenceFieldFormatter()
 | 
			
		||||
        fieldFormatter = QgsRelationReferenceFieldFormatter()
 | 
			
		||||
 | 
			
		||||
        rel = QgsRelation()
 | 
			
		||||
        rel.setRelationId('rel1')
 | 
			
		||||
@ -145,42 +145,42 @@ class TestQgsRelationReferenceFieldFormatter(unittest.TestCase):
 | 
			
		||||
        # Everything valid
 | 
			
		||||
        config = {'Relation': rel.id()}
 | 
			
		||||
        second_layer.setDisplayExpression('decoded')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '123'), 'decoded_val')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '123'), 'decoded_val')
 | 
			
		||||
 | 
			
		||||
        # Code not find match in foreign layer
 | 
			
		||||
        config = {'Relation': rel.id()}
 | 
			
		||||
        second_layer.setDisplayExpression('decoded')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '456'), '456')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '456'), '456')
 | 
			
		||||
 | 
			
		||||
        # Invalid relation id
 | 
			
		||||
        config = {'Relation': 'invalid'}
 | 
			
		||||
        second_layer.setDisplayExpression('decoded')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
 | 
			
		||||
        # No display expression
 | 
			
		||||
        config = {'Relation': rel.id()}
 | 
			
		||||
        second_layer.setDisplayExpression(None)
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
 | 
			
		||||
        # Invalid display expression
 | 
			
		||||
        config = {'Relation': rel.id()}
 | 
			
		||||
        second_layer.setDisplayExpression('invalid +')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
 | 
			
		||||
        # Missing relation
 | 
			
		||||
        config = {}
 | 
			
		||||
        second_layer.setDisplayExpression('decoded')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
 | 
			
		||||
        # Inconsistent layer provided to representValue()
 | 
			
		||||
        config = {'Relation': rel.id()}
 | 
			
		||||
        second_layer.setDisplayExpression('decoded')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(second_layer, 0, config, None, '123'), '123')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(second_layer, 0, config, None, '123'), '123')
 | 
			
		||||
 | 
			
		||||
        # Inconsistent idx provided to representValue()
 | 
			
		||||
        config = {'Relation': rel.id()}
 | 
			
		||||
        second_layer.setDisplayExpression('decoded')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 1, config, None, '123'), '123')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 1, config, None, '123'), '123')
 | 
			
		||||
 | 
			
		||||
        # Invalid relation
 | 
			
		||||
        rel = QgsRelation()
 | 
			
		||||
@ -194,7 +194,7 @@ class TestQgsRelationReferenceFieldFormatter(unittest.TestCase):
 | 
			
		||||
 | 
			
		||||
        config = {'Relation': rel.id()}
 | 
			
		||||
        second_layer.setDisplayExpression('decoded')
 | 
			
		||||
        self.assertEqual(fieldKit.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
        self.assertEqual(fieldFormatter.representValue(first_layer, 0, config, None, '123'), '123')
 | 
			
		||||
 | 
			
		||||
        QgsMapLayerRegistry.instance().removeAllMapLayers()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user