Skip to content
Permalink
Browse files

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
  • Loading branch information
nyalldawson committed Feb 17, 2016
1 parent 3c0cb2e commit 87661a03cd7f60bc0b1e6f233f3892e3aa622367
@@ -348,6 +348,22 @@ class QgsFields
//! @note added in 2.6
bool operator!=( const QgsFields& other ) const;

/** Returns an icon corresponding to a field index, based on the field's type and source
* @note added in QGIS 2.14
*/
QIcon iconForField( int fieldIdx ) const /Factory/;
%MethodCode
if ( a0 < 0 || a0 >= sipCpp->count() )
{
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
sipIsErr = 1;
}
else
{
sipRes = new QIcon( sipCpp->iconForField( a0 ) );
}
%End

/* SIP_PYOBJECT __getitem__(int key);
%MethodCode
if (a0 = sipConvertFromSequenceIndex(a0, sipCpp->count()) < 0)
@@ -326,7 +326,7 @@ void QgsAttributeTableDialog::columnBoxInit()

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

// Generate action for the filter popup button
@@ -274,7 +274,7 @@ void QgsFieldsProperties::setRow( int row, int idx, const QgsField& field )
break;

default:
dataItem->setIcon( QgsApplication::getThemeIcon( "/propertyicons/attributes.png" ) );
dataItem->setIcon( mLayer->fields().iconForField( idx ) );
break;
}
mFieldsList->setItem( row, attrIdCol, dataItem );
@@ -17,10 +17,12 @@
#include "qgsfield.h"
#include "qgsfield_p.h"
#include "qgis.h"
#include "qgsapplication.h"

#include <QSettings>
#include <QDataStream>
#include <QtCore/qmath.h>
#include <QIcon>

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

QIcon QgsFields::iconForField( int fieldIdx ) const
{
static QIcon intIcon;
if ( intIcon.isNull() )
intIcon = QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" );
static QIcon floatIcon;
if ( floatIcon.isNull() )
floatIcon = QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" );
static QIcon stringIcon;
if ( stringIcon.isNull() )
stringIcon = QgsApplication::getThemeIcon( "/mIconFieldText.svg" );
static QIcon dateIcon;
if ( dateIcon.isNull() )
dateIcon = QgsApplication::getThemeIcon( "/mIconFieldDate.svg" );
static QIcon dateTimeIcon;
if ( dateTimeIcon.isNull() )
dateTimeIcon = QgsApplication::getThemeIcon( "/mIconFieldDateTime.svg" );
static QIcon timeIcon;
if ( timeIcon.isNull() )
timeIcon = QgsApplication::getThemeIcon( "/mIconFieldTime.svg" );

switch ( d->fields.at( fieldIdx ).field.type() )
{
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
{
return intIcon;
}
case QVariant::Double:
{
return floatIcon;
}
case QVariant::String:
{
return stringIcon;
}
case QVariant::Date:
{
return dateIcon;
}
case QVariant::DateTime:
{
return dateTimeIcon;
}
case QVariant::Time:
{
return timeIcon;
}
default:
return QIcon();
}
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfields.cpp.
@@ -153,7 +153,6 @@ class CORE_EXPORT QgsField
*/
bool convertCompatible( QVariant& v ) const;


private:

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

/** Returns an icon corresponding to a field index, based on the field's type and source
* @note added in QGIS 2.14
*/
QIcon iconForField( int fieldIdx ) const;

private:

QSharedDataPointer<QgsFieldsPrivate> d;
@@ -382,41 +382,7 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
{
if ( exprIdx < 0 )
{
QgsField field = mFields[index.row()];
int fieldType = static_cast< int >( field.type() );

switch ( fieldType )
{
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
{
return intIcon;
}
case QVariant::Double:
{
return floatIcon;
}
case QVariant::String:
{
return stringIcon;
}
case QVariant::Date:
{
return dateIcon;
}
case QVariant::DateTime:
{
return dateTimeIcon;
}
case QVariant::Time:
{
return timeIcon;
}
default:
return QIcon();
}
return mFields.iconForField( index.row() );
}
return QIcon();
}
@@ -15,7 +15,7 @@
import qgis
import os

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

class TestQgsFields(unittest.TestCase):

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

# check no error
fields.iconForField(1)
# check exceptions raised
with self.assertRaises(KeyError):
fields.iconForField(-1)
with self.assertRaises(KeyError):
fields.iconForField(111)

if __name__ == '__main__':
unittest.main()

0 comments on commit 87661a0

Please sign in to comment.
You can’t perform that action at this time.