Skip to content

Commit

Permalink
Move field icon calculation to QgsFields, show correct field type
Browse files Browse the repository at this point in the history
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 87661a0
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 40 deletions.
16 changes: 16 additions & 0 deletions python/core/qgsfield.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsfieldsproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
57 changes: 57 additions & 0 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ class CORE_EXPORT QgsField
*/
bool convertCompatible( QVariant& v ) const;


private:

QSharedDataPointer<QgsFieldPrivate> d;
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 1 addition & 35 deletions src/gui/qgsfieldmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 10 additions & 2 deletions tests/src/python/test_qgsfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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()
Expand Down Expand Up @@ -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.