Skip to content

Commit 975ef8e

Browse files
committed
Show field type in tooltip in attribute table header
1 parent b91b9f6 commit 975ef8e

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

python/core/qgsfieldmodel.sip.in

+8
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ Returns the layer associated with the model.
126126
virtual QVariant data( const QModelIndex &index, int role ) const;
127127

128128

129+
static QString fieldToolTip( const QgsField &field );
130+
%Docstring
131+
Returns a HTML formatted tooltip string for a ``field``, containing details
132+
like the field name, alias and type.
133+
134+
.. versionadded:: 3.0
135+
%End
136+
129137
public slots:
130138

131139
void setLayer( QgsVectorLayer *layer );

src/core/qgsfieldmodel.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
361361

362362
case Qt::DisplayRole:
363363
case Qt::EditRole:
364+
case Qt::ToolTipRole:
364365
{
365366
if ( isEmpty )
366367
{
@@ -374,6 +375,10 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
374375
{
375376
return mFields.at( index.row() - fieldOffset ).name();
376377
}
378+
else if ( role == Qt::ToolTipRole )
379+
{
380+
return fieldToolTip( mFields.at( index.row() - fieldOffset ) );
381+
}
377382
else if ( mLayer )
378383
{
379384
return mLayer->attributeDisplayName( index.row() - fieldOffset );
@@ -426,3 +431,34 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
426431
return QVariant();
427432
}
428433
}
434+
435+
QString QgsFieldModel::fieldToolTip( const QgsField &field )
436+
{
437+
QString toolTip;
438+
if ( !field.alias().isEmpty() )
439+
{
440+
toolTip = QStringLiteral( "<b>%1</b> (%2)" ).arg( field.alias(), field.name() );
441+
}
442+
else
443+
{
444+
toolTip = QStringLiteral( "<b>%1</b>" ).arg( field.name() );
445+
}
446+
QString typeString;
447+
if ( field.length() > 0 )
448+
{
449+
if ( field.precision() > 0 )
450+
{
451+
typeString = QStringLiteral( "%1 (%2, %3)" ).arg( field.typeName() ).arg( field.length() ).arg( field.precision() );
452+
}
453+
else
454+
{
455+
typeString = QStringLiteral( "%1 (%2)" ).arg( field.typeName() ).arg( field.length() );
456+
}
457+
}
458+
else
459+
{
460+
typeString = field.typeName();
461+
}
462+
toolTip += QStringLiteral( "<p>%1</p>" ).arg( typeString );
463+
return toolTip;
464+
}

src/core/qgsfieldmodel.h

+7
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ class CORE_EXPORT QgsFieldModel : public QAbstractItemModel
128128
int columnCount( const QModelIndex &parent ) const override;
129129
QVariant data( const QModelIndex &index, int role ) const override;
130130

131+
/**
132+
* Returns a HTML formatted tooltip string for a \a field, containing details
133+
* like the field name, alias and type.
134+
* \since QGIS 3.0
135+
*/
136+
static QString fieldToolTip( const QgsField &field );
137+
131138
public slots:
132139

133140
/**

src/gui/attributetable/qgsattributetablemodel.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "qgsexpressionnodeimpl.h"
3838
#include "qgsvectorlayerjoininfo.h"
3939
#include "qgsvectorlayerjoinbuffer.h"
40+
#include "qgsfieldmodel.h"
4041

4142
#include <QVariant>
4243

@@ -583,8 +584,8 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
583584
}
584585
else
585586
{
586-
QgsField field = layer()->fields().at( mAttributes.at( section ) );
587-
return field.name();
587+
const QgsField field = layer()->fields().at( mAttributes.at( section ) );
588+
return QgsFieldModel::fieldToolTip( field );
588589
}
589590
}
590591
else

tests/src/python/test_qgsfieldmodel.py

100644100755
+14-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
import qgis # NOQA
1616

17-
from qgis.core import QgsFields, QgsVectorLayer
18-
from qgis.core import QgsFieldModel
17+
from qgis.core import (QgsField,
18+
QgsFields,
19+
QgsVectorLayer,
20+
QgsFieldModel)
1921
from qgis.PyQt.QtCore import QVariant, Qt
2022

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

3941

4042
class TestQgsFieldModel(unittest.TestCase):
41-
4243
def testGettersSetters(self):
4344
""" test model getters/setters """
4445
l = create_layer()
@@ -245,6 +246,16 @@ def testDisplayRole(self):
245246
m.setAllowEmptyFieldName(True)
246247
self.assertFalse(m.data(m.indexFromName(None), Qt.DisplayRole))
247248

249+
def testFieldTooltip(self):
250+
f = QgsField('my_string', QVariant.String, 'string')
251+
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my_string</b><p>string</p>')
252+
f.setAlias('my alias')
253+
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my alias</b> (my_string)<p>string</p>')
254+
f.setLength(20)
255+
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my alias</b> (my_string)<p>string (20)</p>')
256+
f = QgsField('my_real', QVariant.Double, 'real', 8, 3)
257+
self.assertEqual(QgsFieldModel.fieldToolTip(f), '<b>my_real</b><p>real (8, 3)</p>')
258+
248259

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

0 commit comments

Comments
 (0)