Skip to content

Commit 87661a0

Browse files
committed
Move field icon calculation to QgsFields, show correct field type
icons in attribute table column filter menu (instead of new attribute icon) and in field table in vector properties
1 parent 3c0cb2e commit 87661a0

File tree

7 files changed

+91
-40
lines changed

7 files changed

+91
-40
lines changed

python/core/qgsfield.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,22 @@ class QgsFields
348348
//! @note added in 2.6
349349
bool operator!=( const QgsFields& other ) const;
350350

351+
/** Returns an icon corresponding to a field index, based on the field's type and source
352+
* @note added in QGIS 2.14
353+
*/
354+
QIcon iconForField( int fieldIdx ) const /Factory/;
355+
%MethodCode
356+
if ( a0 < 0 || a0 >= sipCpp->count() )
357+
{
358+
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
359+
sipIsErr = 1;
360+
}
361+
else
362+
{
363+
sipRes = new QIcon( sipCpp->iconForField( a0 ) );
364+
}
365+
%End
366+
351367
/* SIP_PYOBJECT __getitem__(int key);
352368
%MethodCode
353369
if (a0 = sipConvertFromSequenceIndex(a0, sipCpp->count()) < 0)

src/app/qgsattributetabledialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ void QgsAttributeTableDialog::columnBoxInit()
326326

327327
if ( mLayer->editFormConfig()->widgetType( idx ) != "Hidden" )
328328
{
329-
QIcon icon = QgsApplication::getThemeIcon( "/mActionNewAttribute.png" );
329+
QIcon icon = mLayer->fields().iconForField( idx );
330330
QString alias = mLayer->attributeDisplayName( idx );
331331

332332
// Generate action for the filter popup button

src/app/qgsfieldsproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void QgsFieldsProperties::setRow( int row, int idx, const QgsField& field )
274274
break;
275275

276276
default:
277-
dataItem->setIcon( QgsApplication::getThemeIcon( "/propertyicons/attributes.png" ) );
277+
dataItem->setIcon( mLayer->fields().iconForField( idx ) );
278278
break;
279279
}
280280
mFieldsList->setItem( row, attrIdCol, dataItem );

src/core/qgsfield.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include "qgsfield.h"
1818
#include "qgsfield_p.h"
1919
#include "qgis.h"
20+
#include "qgsapplication.h"
2021

2122
#include <QSettings>
2223
#include <QDataStream>
2324
#include <QtCore/qmath.h>
25+
#include <QIcon>
2426

2527
/***************************************************************************
2628
* This class is considered CRITICAL and any change MUST be accompanied with
@@ -437,6 +439,61 @@ bool QgsFields::operator==( const QgsFields &other ) const
437439
return d->fields == other.d->fields;
438440
}
439441

442+
QIcon QgsFields::iconForField( int fieldIdx ) const
443+
{
444+
static QIcon intIcon;
445+
if ( intIcon.isNull() )
446+
intIcon = QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" );
447+
static QIcon floatIcon;
448+
if ( floatIcon.isNull() )
449+
floatIcon = QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" );
450+
static QIcon stringIcon;
451+
if ( stringIcon.isNull() )
452+
stringIcon = QgsApplication::getThemeIcon( "/mIconFieldText.svg" );
453+
static QIcon dateIcon;
454+
if ( dateIcon.isNull() )
455+
dateIcon = QgsApplication::getThemeIcon( "/mIconFieldDate.svg" );
456+
static QIcon dateTimeIcon;
457+
if ( dateTimeIcon.isNull() )
458+
dateTimeIcon = QgsApplication::getThemeIcon( "/mIconFieldDateTime.svg" );
459+
static QIcon timeIcon;
460+
if ( timeIcon.isNull() )
461+
timeIcon = QgsApplication::getThemeIcon( "/mIconFieldTime.svg" );
462+
463+
switch ( d->fields.at( fieldIdx ).field.type() )
464+
{
465+
case QVariant::Int:
466+
case QVariant::UInt:
467+
case QVariant::LongLong:
468+
case QVariant::ULongLong:
469+
{
470+
return intIcon;
471+
}
472+
case QVariant::Double:
473+
{
474+
return floatIcon;
475+
}
476+
case QVariant::String:
477+
{
478+
return stringIcon;
479+
}
480+
case QVariant::Date:
481+
{
482+
return dateIcon;
483+
}
484+
case QVariant::DateTime:
485+
{
486+
return dateTimeIcon;
487+
}
488+
case QVariant::Time:
489+
{
490+
return timeIcon;
491+
}
492+
default:
493+
return QIcon();
494+
}
495+
}
496+
440497
/***************************************************************************
441498
* This class is considered CRITICAL and any change MUST be accompanied with
442499
* full unit tests in testqgsfields.cpp.

src/core/qgsfield.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ class CORE_EXPORT QgsField
153153
*/
154154
bool convertCompatible( QVariant& v ) const;
155155

156-
157156
private:
158157

159158
QSharedDataPointer<QgsFieldPrivate> d;
@@ -285,6 +284,11 @@ class CORE_EXPORT QgsFields
285284
//! @note added in 2.6
286285
bool operator!=( const QgsFields& other ) const { return !( *this == other ); }
287286

287+
/** Returns an icon corresponding to a field index, based on the field's type and source
288+
* @note added in QGIS 2.14
289+
*/
290+
QIcon iconForField( int fieldIdx ) const;
291+
288292
private:
289293

290294
QSharedDataPointer<QgsFieldsPrivate> d;

src/gui/qgsfieldmodel.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -382,41 +382,7 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
382382
{
383383
if ( exprIdx < 0 )
384384
{
385-
QgsField field = mFields[index.row()];
386-
int fieldType = static_cast< int >( field.type() );
387-
388-
switch ( fieldType )
389-
{
390-
case QVariant::Int:
391-
case QVariant::UInt:
392-
case QVariant::LongLong:
393-
case QVariant::ULongLong:
394-
{
395-
return intIcon;
396-
}
397-
case QVariant::Double:
398-
{
399-
return floatIcon;
400-
}
401-
case QVariant::String:
402-
{
403-
return stringIcon;
404-
}
405-
case QVariant::Date:
406-
{
407-
return dateIcon;
408-
}
409-
case QVariant::DateTime:
410-
{
411-
return dateTimeIcon;
412-
}
413-
case QVariant::Time:
414-
{
415-
return timeIcon;
416-
}
417-
default:
418-
return QIcon();
419-
}
385+
return mFields.iconForField( index.row() );
420386
}
421387
return QIcon();
422388
}

tests/src/python/test_qgsfield.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import qgis
1616
import os
1717

18-
from qgis.core import QgsField, QgsVectorLayer, NULL
18+
from qgis.core import QgsField, QgsFields, QgsVectorLayer, NULL
1919
from qgis.testing import (start_app,
2020
unittest
2121
)
@@ -26,7 +26,7 @@
2626

2727
class TestQgsFields(unittest.TestCase):
2828

29-
def test_expections(self):
29+
def test_exceptions(self):
3030
ml = QgsVectorLayer("Point?crs=epsg:4236&field=id:integer&field=value:double",
3131
"test_data", "memory")
3232
assert ml.isValid()
@@ -79,5 +79,13 @@ def test_expections(self):
7979
with self.assertRaises(KeyError):
8080
fields.fieldOriginIndex(111)
8181

82+
# check no error
83+
fields.iconForField(1)
84+
# check exceptions raised
85+
with self.assertRaises(KeyError):
86+
fields.iconForField(-1)
87+
with self.assertRaises(KeyError):
88+
fields.iconForField(111)
89+
8290
if __name__ == '__main__':
8391
unittest.main()

0 commit comments

Comments
 (0)