Skip to content
Permalink
Browse files

Show field type in tooltip in attribute table header

  • Loading branch information
nyalldawson committed Jan 25, 2018
1 parent b91b9f6 commit 975ef8e899ec74862f2fccc9814de47d422631be
@@ -126,6 +126,14 @@ Returns the layer associated with the model.
virtual QVariant data( const QModelIndex &index, int role ) const;


static QString fieldToolTip( const QgsField &field );
%Docstring
Returns a HTML formatted tooltip string for a ``field``, containing details
like the field name, alias and type.

.. versionadded:: 3.0
%End

public slots:

void setLayer( QgsVectorLayer *layer );
@@ -361,6 +361,7 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const

case Qt::DisplayRole:
case Qt::EditRole:
case Qt::ToolTipRole:
{
if ( isEmpty )
{
@@ -374,6 +375,10 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
{
return mFields.at( index.row() - fieldOffset ).name();
}
else if ( role == Qt::ToolTipRole )
{
return fieldToolTip( mFields.at( index.row() - fieldOffset ) );
}
else if ( mLayer )
{
return mLayer->attributeDisplayName( index.row() - fieldOffset );
@@ -426,3 +431,34 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
return QVariant();
}
}

QString QgsFieldModel::fieldToolTip( const QgsField &field )
{
QString toolTip;
if ( !field.alias().isEmpty() )
{
toolTip = QStringLiteral( "<b>%1</b> (%2)" ).arg( field.alias(), field.name() );
}
else
{
toolTip = QStringLiteral( "<b>%1</b>" ).arg( field.name() );
}
QString typeString;
if ( field.length() > 0 )
{
if ( field.precision() > 0 )
{
typeString = QStringLiteral( "%1 (%2, %3)" ).arg( field.typeName() ).arg( field.length() ).arg( field.precision() );
}
else
{
typeString = QStringLiteral( "%1 (%2)" ).arg( field.typeName() ).arg( field.length() );
}
}
else
{
typeString = field.typeName();
}
toolTip += QStringLiteral( "<p>%1</p>" ).arg( typeString );
return toolTip;
}
@@ -128,6 +128,13 @@ class CORE_EXPORT QgsFieldModel : public QAbstractItemModel
int columnCount( const QModelIndex &parent ) const override;
QVariant data( const QModelIndex &index, int role ) const override;

/**
* Returns a HTML formatted tooltip string for a \a field, containing details
* like the field name, alias and type.
* \since QGIS 3.0
*/
static QString fieldToolTip( const QgsField &field );

public slots:

/**
@@ -37,6 +37,7 @@
#include "qgsexpressionnodeimpl.h"
#include "qgsvectorlayerjoininfo.h"
#include "qgsvectorlayerjoinbuffer.h"
#include "qgsfieldmodel.h"

#include <QVariant>

@@ -583,8 +584,8 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
}
else
{
QgsField field = layer()->fields().at( mAttributes.at( section ) );
return field.name();
const QgsField field = layer()->fields().at( mAttributes.at( section ) );
return QgsFieldModel::fieldToolTip( field );
}
}
else
@@ -14,8 +14,10 @@

import qgis # NOQA

from qgis.core import QgsFields, QgsVectorLayer
from qgis.core import QgsFieldModel
from qgis.core import (QgsField,
QgsFields,
QgsVectorLayer,
QgsFieldModel)
from qgis.PyQt.QtCore import QVariant, Qt

from qgis.testing import start_app, unittest
@@ -38,7 +40,6 @@ def create_model():


class TestQgsFieldModel(unittest.TestCase):

def testGettersSetters(self):
""" test model getters/setters """
l = create_layer()
@@ -245,6 +246,16 @@ def testDisplayRole(self):
m.setAllowEmptyFieldName(True)
self.assertFalse(m.data(m.indexFromName(None), Qt.DisplayRole))

def testFieldTooltip(self):
f = QgsField('my_string', QVariant.String, 'string')
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my_string</b><p>string</p>')
f.setAlias('my alias')
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my alias</b> (my_string)<p>string</p>')
f.setLength(20)
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my alias</b> (my_string)<p>string (20)</p>')
f = QgsField('my_real', QVariant.Double, 'real', 8, 3)
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my_real</b><p>real (8, 3)</p>')


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

0 comments on commit 975ef8e

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