Skip to content

Commit

Permalink
Put field kit into use
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent 9a53b7d commit 82d1871
Show file tree
Hide file tree
Showing 49 changed files with 434 additions and 363 deletions.
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ INCLUDE_DIRECTORIES(
../src/core/composer
../src/core/diagram
../src/core/effects
../src/core/fieldkit
../src/core/dxf
../src/core/geometry
../src/core/gps
Expand Down
36 changes: 0 additions & 36 deletions python/gui/editorwidgets/core/qgseditorwidgetfactory.sip
Original file line number Diff line number Diff line change
Expand Up @@ -110,42 +110,6 @@ class QgsEditorWidgetFactory
*/
//virtual QHash<const char*, int> supportedWidgetTypes();

/**
* If the default sort order should be overwritten for this widget, you can transform the value in here.
*
* @param vl The vector layer.
* @param fieldIdx The index of the field.
* @param config The editor widget config.
* @param cache The editor widget cache.
* @param value The value to represent.
*
* @return By default the value is returned unmodified.
*
* @note Added in 2.16
*/
virtual QVariant sortValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const;

/**
* Return the alignment for a particular field. By default this will consider the field type but can be overwritten if mapped
* values are represented.
* @param vl The vector layer.
* @param fieldIdx The index of the field.
* @param config The editor widget config.
* @return The alignment flag, normally Qt::AlignRight or Qt::AlignLeft
*/
virtual Qt::AlignmentFlag alignmentFlag( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config ) const;

/**
* Create a cache for a given field.
*
* @param vl The vector layer.
* @param fieldIdx The index of the field.
* @param config The editor widget config.
*
* @return The default implementation returns an invalid QVariant
*/
virtual QVariant createCache( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config );

/**
* Read the config from an XML file and map it to a proper {@link QgsEditorWidgetConfig}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,4 @@ class QgsValueRelationSearchWidgetWrapper : QgsSearchWidgetWrapper
QWidget* createWidget( QWidget* parent );
void initWidget( QWidget* editor );

public slots:

//! Called when current value of search widget changes
void onValueChanged();

protected slots:
void setExpression( QString exp );

};
10 changes: 4 additions & 6 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@
#include "qgsfeature.h"
#include "qgsformannotationitem.h"
#include "qgsfieldcalculator.h"
#include "qgsfieldkit.h"
#include "qgsfieldkitregistry.h"
#include "qgshtmlannotationitem.h"
#include "qgsgenericprojectionselector.h"
#include "qgsgpsinformationwidget.h"
Expand Down Expand Up @@ -6397,12 +6399,8 @@ QVariant QgisAppFieldValueConverter::convert( int idx, const QVariant& value )
return value;
}
const QgsEditorWidgetSetup setup = QgsEditorWidgetRegistry::instance()->findBest( mLayer, mLayer->fields().field( idx ).name() );
QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( setup.type() );
if ( factory )
{
return QVariant( factory->representValue( mLayer, idx, setup.config(), QVariant(), value ) );
}
return value;
QgsFieldKit* fieldKit = QgsApplication::fieldKitRegistry()->fieldKit( setup.type() );
return fieldKit->representValue( mLayer, idx, setup.config(), QVariant(), value );
}

///@endcond
Expand Down
10 changes: 4 additions & 6 deletions src/app/qgsattributetypedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "qgisapp.h"
#include "qgsproject.h"
#include "qgslogger.h"
#include "qgsfieldkitregistry.h"
#include "qgsfieldkit.h"
#include "qgseditorwidgetfactory.h"
#include "qgseditorwidgetregistry.h"

Expand Down Expand Up @@ -348,13 +350,9 @@ void QgsAttributeTypeDialog::defaultExpressionChanged()
return;
}

QString previewText = val.toString();
QgsFieldKit* fieldKit = QgsApplication::fieldKitRegistry()->fieldKit( editorWidgetType() );

QgsEditorWidgetFactory *factory = QgsEditorWidgetRegistry::instance()->factory( editorWidgetType() );
if ( factory )
{
previewText = factory->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );
}
QString previewText = fieldKit->representValue( mLayer, mFieldIdx, editorWidgetConfig(), QVariant(), val );

mDefaultPreviewLabel->setText( "<i>" + previewText + "</i>" );
}
7 changes: 5 additions & 2 deletions src/app/qgsidentifyresultsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include "qgsstringutils.h"
#include "qgstreewidgetitem.h"
#include "qgsfiledownloader.h"
#include "qgsfieldkitregistry.h"
#include "qgsfieldkit.h"

#include <QCloseEvent>
#include <QLabel>
Expand Down Expand Up @@ -732,6 +734,7 @@ QString QgsIdentifyResultsDialog::representValue( QgsVectorLayer* vlayer, const
QMap<QString, QVariant>& layerCaches = mWidgetCaches[vlayer->id()];

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

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

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

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

void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
Expand Down
5 changes: 4 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ SET(QGIS_CORE_SRCS


fieldkit/qgsdatetimefieldkit.cpp
fieldkit/qgsfallbackfieldkit.cpp
fieldkit/qgskeyvaluefieldkit.cpp
fieldkit/qgslistfieldkit.cpp
fieldkit/qgsrelationreferencefieldkit.cpp
Expand Down Expand Up @@ -477,6 +478,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsfeedback.h
qgsfield.h
qgsfieldconstraints.h
qgsfieldkitregistry.h
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h
Expand Down Expand Up @@ -670,7 +672,6 @@ SET(QGIS_CORE_HDRS
qgsfeaturerequest.h
qgsfeaturestore.h
qgsfieldkit.h
qgsfieldkitregistry.h
qgsfields.h
qgsfontutils.h
qgsgeometrycache.h
Expand Down Expand Up @@ -889,6 +890,7 @@ SET(QGIS_CORE_HDRS
geometry/qgswkbtypes.h

fieldkit/qgsdatetimefieldkit.h
fieldkit/qgsfallbackfieldkit.h
fieldkit/qgskeyvaluefieldkit.h
fieldkit/qgslistfieldkit.h
fieldkit/qgsrelationreferencefieldkit.h
Expand All @@ -909,6 +911,7 @@ INCLUDE_DIRECTORIES(
composer
dxf
effects
fieldkit
geometry
layertree
pal
Expand Down
13 changes: 7 additions & 6 deletions src/core/fieldkit/qgsdatetimefieldkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ QgsDateTimeFieldKit::QgsDateTimeFieldKit()

}

bool QgsDateTimeFieldKit::supportsField( QgsVectorLayer* layer, int fieldIdx )
QString QgsDateTimeFieldKit::id() const
{
return QStringLiteral( "DateTime" );
}

QString QgsDateTimeFieldKit::representValue( QgsVectorLayer* layer, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
QString QgsDateTimeFieldKit::representValue( QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
{
Q_UNUSED( cache )

Expand All @@ -41,7 +42,7 @@ QString QgsDateTimeFieldKit::representValue( QgsVectorLayer* layer, int fieldIdx
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
}

const QgsField field = layer->fields().at( fieldIdx );
const QgsField field = layer->fields().at( fieldIndex );
const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultFormat( field.type() ) ).toString();
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();

Expand All @@ -64,12 +65,12 @@ QString QgsDateTimeFieldKit::defaultFormat( const QVariant::Type type )
switch ( type )
{
case QVariant::DateTime:
return QGSDATETIMEEDIT_DATETIMEFORMAT;
return QGSDATETIMEFIELDKIT_DATETIMEFORMAT;
break;
case QVariant::Time:
return QGSDATETIMEEDIT_TIMEFORMAT;
return QGSDATETIMEFIELDKIT_TIMEFORMAT;
break;
default:
return QGSDATETIMEEDIT_DATEFORMAT;
return QGSDATETIMEFIELDKIT_DATEFORMAT;
}
}
22 changes: 13 additions & 9 deletions src/core/fieldkit/qgsdatetimefieldkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@

#include "qgsfieldkit.h"

#define QGSDATETIMEEDIT_DATEFORMAT QStringLiteral( "yyyy-MM-dd" )
#define QGSDATETIMEEDIT_TIMEFORMAT QStringLiteral( "HH:mm:ss" )
#define QGSDATETIMEEDIT_DATETIMEFORMAT QStringLiteral( "yyyy-MM-dd HH:mm:ss" )
#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 QgsDateTimeFieldKit : public QgsFieldKit
class CORE_EXPORT QgsDateTimeFieldKit : public QgsFieldKit
{
public:
QgsDateTimeFieldKit();

virtual bool supportsField( QgsVectorLayer* layer, int fieldIdx ) override;
virtual QString representValue( QgsVectorLayer* layer, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;
QString id() const override;

virtual QString representValue( QgsVectorLayer* layer, int fieldIndex, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;

/**
* Get the default format in function of the type
* @param type the field type
* @return the date/time format
* Get the default format in function of the type.
* The type is expected to be one of
*
* - QVariant::DateTime
* - QVariant::Date
* - QVariant::Time
*/
static QString defaultFormat( const QVariant::Type type );
};
Expand Down
26 changes: 26 additions & 0 deletions src/core/fieldkit/qgsfallbackfieldkit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/***************************************************************************
qgsfallbackfieldkit.cpp - QgsFallbackFieldKit
---------------------
begin : 4.12.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsfallbackfieldkit.h"

QgsFallbackFieldKit::QgsFallbackFieldKit()
{

}

QString QgsFallbackFieldKit::id() const
{
return QString();
}
29 changes: 29 additions & 0 deletions src/core/fieldkit/qgsfallbackfieldkit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/***************************************************************************
qgsfallbackfieldkit.h - QgsFallbackFieldKit
---------------------
begin : 4.12.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSFALLBACKFIELDKIT_H
#define QGSFALLBACKFIELDKIT_H

#include "qgsfieldkit.h"

class CORE_EXPORT QgsFallbackFieldKit : public QgsFieldKit
{
public:
QgsFallbackFieldKit();

QString id() const override;
};

#endif // QGSFALLBACKFIELDKIT_H
5 changes: 5 additions & 0 deletions src/core/fieldkit/qgskeyvaluefieldkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ QgsKeyValueFieldKit::QgsKeyValueFieldKit()

}

QString QgsKeyValueFieldKit::id() const
{
return QStringLiteral( "KeyValue" );
}

QString QgsKeyValueFieldKit::representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
{
Q_UNUSED( vl );
Expand Down
3 changes: 2 additions & 1 deletion src/core/fieldkit/qgskeyvaluefieldkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

#include "qgsfieldkit.h"

class QgsKeyValueFieldKit : public QgsFieldKit
class CORE_EXPORT QgsKeyValueFieldKit : public QgsFieldKit
{
public:
QgsKeyValueFieldKit();

QString id() const override;
QString representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;
};

Expand Down
5 changes: 5 additions & 0 deletions src/core/fieldkit/qgslistfieldkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ QgsListFieldKit::QgsListFieldKit()

}

QString QgsListFieldKit::id() const
{
return QStringLiteral( "List" );
}

QString QgsListFieldKit::representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
{
Q_UNUSED( vl );
Expand Down
4 changes: 3 additions & 1 deletion src/core/fieldkit/qgslistfieldkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

#include "qgsfieldkit.h"

class QgsListFieldKit : public QgsFieldKit
class CORE_EXPORT QgsListFieldKit : public QgsFieldKit
{
public:
QgsListFieldKit();

QString id() const override;

QString representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;
};

Expand Down
5 changes: 5 additions & 0 deletions src/core/fieldkit/qgsrelationreferencefieldkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ QgsRelationReferenceFieldKit::QgsRelationReferenceFieldKit()

}

QString QgsRelationReferenceFieldKit::id() const
{
return QStringLiteral( "RelationReference" );
}

QString QgsRelationReferenceFieldKit::representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
{
Q_UNUSED( cache );
Expand Down
4 changes: 3 additions & 1 deletion src/core/fieldkit/qgsrelationreferencefieldkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

#include "qgsfieldkit.h"

class QgsRelationReferenceFieldKit : public QgsFieldKit
class CORE_EXPORT QgsRelationReferenceFieldKit : public QgsFieldKit
{
public:
QgsRelationReferenceFieldKit();

QString id() const override;

virtual QString representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;

virtual QVariant sortValue( QgsVectorLayer *vl, int fieldIdx, const QVariantMap&config, const QVariant& cache, const QVariant& value ) const override;
Expand Down
Loading

0 comments on commit 82d1871

Please sign in to comment.