Skip to content

Commit

Permalink
Add field kits for a lot of common field types
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent 570fc4e commit 9a53b7d
Show file tree
Hide file tree
Showing 39 changed files with 800 additions and 353 deletions.
13 changes: 0 additions & 13 deletions python/gui/editorwidgets/core/qgseditorwidgetfactory.sip
Expand Up @@ -110,19 +110,6 @@ class QgsEditorWidgetFactory
*/
//virtual QHash<const char*, int> supportedWidgetTypes();

/**
* Create a pretty String representation of the value.
*
* @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 string representation of the provided value as implied by the field definition is returned.
*/
virtual QString representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const;

/**
* If the default sort order should be overwritten for this widget, you can transform the value in here.
*
Expand Down
19 changes: 19 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -127,6 +127,8 @@ SET(QGIS_CORE_SRCS
qgsfeaturestore.cpp
qgsfield.cpp
qgsfieldconstraints.cpp
qgsfieldkit.cpp
qgsfieldkitregistry.cpp
qgsfields.cpp
qgsfontutils.cpp
qgsgeometrycache.cpp
Expand Down Expand Up @@ -367,6 +369,14 @@ SET(QGIS_CORE_SRCS
geometry/qgswkbptr.cpp
geometry/qgswkbtypes.cpp


fieldkit/qgsdatetimefieldkit.cpp
fieldkit/qgskeyvaluefieldkit.cpp
fieldkit/qgslistfieldkit.cpp
fieldkit/qgsrelationreferencefieldkit.cpp
fieldkit/qgsvaluemapfieldkit.cpp
fieldkit/qgsvaluerelationfieldkit.cpp

${CMAKE_CURRENT_BINARY_DIR}/qgscontexthelp_texts.cpp
${CMAKE_CURRENT_BINARY_DIR}/qgsexpression_texts.cpp
)
Expand Down Expand Up @@ -659,6 +669,8 @@ SET(QGIS_CORE_HDRS
qgsfeatureiterator.h
qgsfeaturerequest.h
qgsfeaturestore.h
qgsfieldkit.h
qgsfieldkitregistry.h
qgsfields.h
qgsfontutils.h
qgsgeometrycache.h
Expand Down Expand Up @@ -875,6 +887,13 @@ SET(QGIS_CORE_HDRS
geometry/qgssurface.h
geometry/qgswkbptr.h
geometry/qgswkbtypes.h

fieldkit/qgsdatetimefieldkit.h
fieldkit/qgskeyvaluefieldkit.h
fieldkit/qgslistfieldkit.h
fieldkit/qgsrelationreferencefieldkit.h
fieldkit/qgsvaluemapfieldkit.h
fieldkit/qgsvaluerelationfieldkit.h
)

IF (QT_MOBILITY_LOCATION_FOUND OR Qt5Positioning_FOUND)
Expand Down
75 changes: 75 additions & 0 deletions src/core/fieldkit/qgsdatetimefieldkit.cpp
@@ -0,0 +1,75 @@
/***************************************************************************
qgsdatetimefieldkit.cpp - QgsDateTimeFieldKit
---------------------
begin : 2.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 "qgsdatetimefieldkit.h"

#include <QSettings>

#include "qgsfield.h"
#include "qgsvectorlayer.h"

QgsDateTimeFieldKit::QgsDateTimeFieldKit()
{

}

bool QgsDateTimeFieldKit::supportsField( QgsVectorLayer* layer, int fieldIdx )
{
}

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

QString result;

if ( value.isNull() )
{
QSettings settings;
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
}

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

QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );

if ( date.isValid() )
{
result = date.toString( displayFormat );
}
else
{
result = value.toString();
}

return result;
}

QString QgsDateTimeFieldKit::defaultFormat( const QVariant::Type type )
{
switch ( type )
{
case QVariant::DateTime:
return QGSDATETIMEEDIT_DATETIMEFORMAT;
break;
case QVariant::Time:
return QGSDATETIMEEDIT_TIMEFORMAT;
break;
default:
return QGSDATETIMEEDIT_DATEFORMAT;
}
}
41 changes: 41 additions & 0 deletions src/core/fieldkit/qgsdatetimefieldkit.h
@@ -0,0 +1,41 @@
/***************************************************************************
qgsdatetimefieldkit.h - QgsDateTimeFieldKit
---------------------
begin : 2.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 QGSDATETIMEFIELDKIT_H
#define QGSDATETIMEFIELDKIT_H

#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" )

class 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;

/**
* Get the default format in function of the type
* @param type the field type
* @return the date/time format
*/
static QString defaultFormat( const QVariant::Type type );
};

#endif // QGSDATETIMEFIELDKIT_H
46 changes: 46 additions & 0 deletions src/core/fieldkit/qgskeyvaluefieldkit.cpp
@@ -0,0 +1,46 @@
/***************************************************************************
qgskeyvaluefieldkit.cpp - QgsKeyValueFieldKit
---------------------
begin : 3.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 "qgskeyvaluefieldkit.h"

#include <QSettings>

QgsKeyValueFieldKit::QgsKeyValueFieldKit()
{

}

QString QgsKeyValueFieldKit::representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
{
Q_UNUSED( vl );
Q_UNUSED( fieldIdx );
Q_UNUSED( config );
Q_UNUSED( cache );

if ( value.isNull() )
{
QSettings settings;
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
}

QString result;
const QVariantMap map = value.toMap();
for ( QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i )
{
if ( !result.isEmpty() ) result.append( ", " );
result.append( i.key() ).append( ": " ).append( i.value().toString() );
}
return result;
}
29 changes: 29 additions & 0 deletions src/core/fieldkit/qgskeyvaluefieldkit.h
@@ -0,0 +1,29 @@
/***************************************************************************
qgskeyvaluefieldkit.h - QgsKeyValueFieldKit
---------------------
begin : 3.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 QGSKEYVALUEFIELDKIT_H
#define QGSKEYVALUEFIELDKIT_H

#include "qgsfieldkit.h"

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

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

#endif // QGSKEYVALUEFIELDKIT_H
46 changes: 46 additions & 0 deletions src/core/fieldkit/qgslistfieldkit.cpp
@@ -0,0 +1,46 @@
/***************************************************************************
qgslistfieldkit.cpp - QgsListFieldKit
---------------------
begin : 3.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 "qgslistfieldkit.h"

#include <QSettings>

QgsListFieldKit::QgsListFieldKit()
{

}

QString QgsListFieldKit::representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
{
Q_UNUSED( vl );
Q_UNUSED( fieldIdx );
Q_UNUSED( config );
Q_UNUSED( cache );

if ( value.isNull() )
{
QSettings settings;
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
}

QString result;
const QVariantList list = value.toList();
for ( QVariantList::const_iterator i = list.constBegin(); i != list.constEnd(); ++i )
{
if ( !result.isEmpty() ) result.append( ", " );
result.append( i->toString() );
}
return result;
}
29 changes: 29 additions & 0 deletions src/core/fieldkit/qgslistfieldkit.h
@@ -0,0 +1,29 @@
/***************************************************************************
qgslistfieldkit.h - QgsListFieldKit
---------------------
begin : 3.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 QGSLISTFIELDKIT_H
#define QGSLISTFIELDKIT_H

#include "qgsfieldkit.h"

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

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

#endif // QGSLISTFIELDKIT_H

0 comments on commit 9a53b7d

Please sign in to comment.