Skip to content

Commit 9a53b7d

Browse files
committed
Add field kits for a lot of common field types
1 parent 570fc4e commit 9a53b7d

39 files changed

+800
-353
lines changed

python/gui/editorwidgets/core/qgseditorwidgetfactory.sip

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,6 @@ class QgsEditorWidgetFactory
110110
*/
111111
//virtual QHash<const char*, int> supportedWidgetTypes();
112112

113-
/**
114-
* Create a pretty String representation of the value.
115-
*
116-
* @param vl The vector layer.
117-
* @param fieldIdx The index of the field.
118-
* @param config The editor widget config.
119-
* @param cache The editor widget cache.
120-
* @param value The value to represent.
121-
*
122-
* @return By default the string representation of the provided value as implied by the field definition is returned.
123-
*/
124-
virtual QString representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const;
125-
126113
/**
127114
* If the default sort order should be overwritten for this widget, you can transform the value in here.
128115
*

src/core/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ SET(QGIS_CORE_SRCS
127127
qgsfeaturestore.cpp
128128
qgsfield.cpp
129129
qgsfieldconstraints.cpp
130+
qgsfieldkit.cpp
131+
qgsfieldkitregistry.cpp
130132
qgsfields.cpp
131133
qgsfontutils.cpp
132134
qgsgeometrycache.cpp
@@ -367,6 +369,14 @@ SET(QGIS_CORE_SRCS
367369
geometry/qgswkbptr.cpp
368370
geometry/qgswkbtypes.cpp
369371

372+
373+
fieldkit/qgsdatetimefieldkit.cpp
374+
fieldkit/qgskeyvaluefieldkit.cpp
375+
fieldkit/qgslistfieldkit.cpp
376+
fieldkit/qgsrelationreferencefieldkit.cpp
377+
fieldkit/qgsvaluemapfieldkit.cpp
378+
fieldkit/qgsvaluerelationfieldkit.cpp
379+
370380
${CMAKE_CURRENT_BINARY_DIR}/qgscontexthelp_texts.cpp
371381
${CMAKE_CURRENT_BINARY_DIR}/qgsexpression_texts.cpp
372382
)
@@ -659,6 +669,8 @@ SET(QGIS_CORE_HDRS
659669
qgsfeatureiterator.h
660670
qgsfeaturerequest.h
661671
qgsfeaturestore.h
672+
qgsfieldkit.h
673+
qgsfieldkitregistry.h
662674
qgsfields.h
663675
qgsfontutils.h
664676
qgsgeometrycache.h
@@ -875,6 +887,13 @@ SET(QGIS_CORE_HDRS
875887
geometry/qgssurface.h
876888
geometry/qgswkbptr.h
877889
geometry/qgswkbtypes.h
890+
891+
fieldkit/qgsdatetimefieldkit.h
892+
fieldkit/qgskeyvaluefieldkit.h
893+
fieldkit/qgslistfieldkit.h
894+
fieldkit/qgsrelationreferencefieldkit.h
895+
fieldkit/qgsvaluemapfieldkit.h
896+
fieldkit/qgsvaluerelationfieldkit.h
878897
)
879898

880899
IF (QT_MOBILITY_LOCATION_FOUND OR Qt5Positioning_FOUND)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/***************************************************************************
2+
qgsdatetimefieldkit.cpp - QgsDateTimeFieldKit
3+
4+
---------------------
5+
begin : 2.12.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#include "qgsdatetimefieldkit.h"
17+
18+
#include <QSettings>
19+
20+
#include "qgsfield.h"
21+
#include "qgsvectorlayer.h"
22+
23+
QgsDateTimeFieldKit::QgsDateTimeFieldKit()
24+
{
25+
26+
}
27+
28+
bool QgsDateTimeFieldKit::supportsField( QgsVectorLayer* layer, int fieldIdx )
29+
{
30+
}
31+
32+
QString QgsDateTimeFieldKit::representValue( QgsVectorLayer* layer, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
33+
{
34+
Q_UNUSED( cache )
35+
36+
QString result;
37+
38+
if ( value.isNull() )
39+
{
40+
QSettings settings;
41+
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
42+
}
43+
44+
const QgsField field = layer->fields().at( fieldIdx );
45+
const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultFormat( field.type() ) ).toString();
46+
const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();
47+
48+
QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );
49+
50+
if ( date.isValid() )
51+
{
52+
result = date.toString( displayFormat );
53+
}
54+
else
55+
{
56+
result = value.toString();
57+
}
58+
59+
return result;
60+
}
61+
62+
QString QgsDateTimeFieldKit::defaultFormat( const QVariant::Type type )
63+
{
64+
switch ( type )
65+
{
66+
case QVariant::DateTime:
67+
return QGSDATETIMEEDIT_DATETIMEFORMAT;
68+
break;
69+
case QVariant::Time:
70+
return QGSDATETIMEEDIT_TIMEFORMAT;
71+
break;
72+
default:
73+
return QGSDATETIMEEDIT_DATEFORMAT;
74+
}
75+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/***************************************************************************
2+
qgsdatetimefieldkit.h - QgsDateTimeFieldKit
3+
4+
---------------------
5+
begin : 2.12.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSDATETIMEFIELDKIT_H
17+
#define QGSDATETIMEFIELDKIT_H
18+
19+
#include "qgsfieldkit.h"
20+
21+
#define QGSDATETIMEEDIT_DATEFORMAT QStringLiteral( "yyyy-MM-dd" )
22+
#define QGSDATETIMEEDIT_TIMEFORMAT QStringLiteral( "HH:mm:ss" )
23+
#define QGSDATETIMEEDIT_DATETIMEFORMAT QStringLiteral( "yyyy-MM-dd HH:mm:ss" )
24+
25+
class QgsDateTimeFieldKit : public QgsFieldKit
26+
{
27+
public:
28+
QgsDateTimeFieldKit();
29+
30+
virtual bool supportsField( QgsVectorLayer* layer, int fieldIdx ) override;
31+
virtual QString representValue( QgsVectorLayer* layer, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;
32+
33+
/**
34+
* Get the default format in function of the type
35+
* @param type the field type
36+
* @return the date/time format
37+
*/
38+
static QString defaultFormat( const QVariant::Type type );
39+
};
40+
41+
#endif // QGSDATETIMEFIELDKIT_H
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************
2+
qgskeyvaluefieldkit.cpp - QgsKeyValueFieldKit
3+
4+
---------------------
5+
begin : 3.12.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#include "qgskeyvaluefieldkit.h"
17+
18+
#include <QSettings>
19+
20+
QgsKeyValueFieldKit::QgsKeyValueFieldKit()
21+
{
22+
23+
}
24+
25+
QString QgsKeyValueFieldKit::representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
26+
{
27+
Q_UNUSED( vl );
28+
Q_UNUSED( fieldIdx );
29+
Q_UNUSED( config );
30+
Q_UNUSED( cache );
31+
32+
if ( value.isNull() )
33+
{
34+
QSettings settings;
35+
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
36+
}
37+
38+
QString result;
39+
const QVariantMap map = value.toMap();
40+
for ( QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i )
41+
{
42+
if ( !result.isEmpty() ) result.append( ", " );
43+
result.append( i.key() ).append( ": " ).append( i.value().toString() );
44+
}
45+
return result;
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/***************************************************************************
2+
qgskeyvaluefieldkit.h - QgsKeyValueFieldKit
3+
4+
---------------------
5+
begin : 3.12.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSKEYVALUEFIELDKIT_H
17+
#define QGSKEYVALUEFIELDKIT_H
18+
19+
#include "qgsfieldkit.h"
20+
21+
class QgsKeyValueFieldKit : public QgsFieldKit
22+
{
23+
public:
24+
QgsKeyValueFieldKit();
25+
26+
QString representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;
27+
};
28+
29+
#endif // QGSKEYVALUEFIELDKIT_H

src/core/fieldkit/qgslistfieldkit.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************
2+
qgslistfieldkit.cpp - QgsListFieldKit
3+
4+
---------------------
5+
begin : 3.12.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#include "qgslistfieldkit.h"
17+
18+
#include <QSettings>
19+
20+
QgsListFieldKit::QgsListFieldKit()
21+
{
22+
23+
}
24+
25+
QString QgsListFieldKit::representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const
26+
{
27+
Q_UNUSED( vl );
28+
Q_UNUSED( fieldIdx );
29+
Q_UNUSED( config );
30+
Q_UNUSED( cache );
31+
32+
if ( value.isNull() )
33+
{
34+
QSettings settings;
35+
return settings.value( QStringLiteral( "qgis/nullValue" ), "NULL" ).toString();
36+
}
37+
38+
QString result;
39+
const QVariantList list = value.toList();
40+
for ( QVariantList::const_iterator i = list.constBegin(); i != list.constEnd(); ++i )
41+
{
42+
if ( !result.isEmpty() ) result.append( ", " );
43+
result.append( i->toString() );
44+
}
45+
return result;
46+
}

src/core/fieldkit/qgslistfieldkit.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/***************************************************************************
2+
qgslistfieldkit.h - QgsListFieldKit
3+
4+
---------------------
5+
begin : 3.12.2016
6+
copyright : (C) 2016 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
#ifndef QGSLISTFIELDKIT_H
17+
#define QGSLISTFIELDKIT_H
18+
19+
#include "qgsfieldkit.h"
20+
21+
class QgsListFieldKit : public QgsFieldKit
22+
{
23+
public:
24+
QgsListFieldKit();
25+
26+
QString representValue( QgsVectorLayer* vl, int fieldIdx, const QVariantMap& config, const QVariant& cache, const QVariant& value ) const override;
27+
};
28+
29+
#endif // QGSLISTFIELDKIT_H

0 commit comments

Comments
 (0)