Skip to content

Commit

Permalink
Show field type in tooltip in attribute table header
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 25, 2018
1 parent b91b9f6 commit 975ef8e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsfieldmodel.sip.in
Expand Up @@ -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 );
Expand Down
36 changes: 36 additions & 0 deletions src/core/qgsfieldmodel.cpp
Expand Up @@ -361,6 +361,7 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const

case Qt::DisplayRole:
case Qt::EditRole:
case Qt::ToolTipRole:
{
if ( isEmpty )
{
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions src/core/qgsfieldmodel.h
Expand Up @@ -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:

/**
Expand Down
5 changes: 3 additions & 2 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -37,6 +37,7 @@
#include "qgsexpressionnodeimpl.h"
#include "qgsvectorlayerjoininfo.h"
#include "qgsvectorlayerjoinbuffer.h"
#include "qgsfieldmodel.h"

#include <QVariant>

Expand Down Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions tests/src/python/test_qgsfieldmodel.py 100644 → 100755
Expand Up @@ -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
Expand All @@ -38,7 +40,6 @@ def create_model():


class TestQgsFieldModel(unittest.TestCase):

def testGettersSetters(self):
""" test model getters/setters """
l = create_layer()
Expand Down Expand Up @@ -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.