Skip to content
Permalink
Browse files

Added new static QgsFieldModel::fieldToolTipExtended

  • Loading branch information
suricactus authored and nyalldawson committed Mar 25, 2020
1 parent 16ba6fc commit 6a0ae060bc984c92eb5a2eebb5597b459fc09af2
@@ -128,12 +128,20 @@ Returns the layer associated with the model.
virtual QVariant data( const QModelIndex &index, int role ) const;


static QString fieldToolTip( const QgsField &field, const QString &expression = QString() );
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

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

.. versionadded:: 3.14
%End

void setFields( const QgsFields &fields );
@@ -626,11 +626,8 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
QgsTreeWidgetItem *attrItem = new QgsTreeWidgetItem( QStringList() << QString::number( i ) << value );
featItem->addChild( attrItem );

QString expressionString = fields.fieldOrigin( i ) == QgsFields::OriginExpression
? vlayer->expressionField( i )
: QString();
attrItem->setData( 0, Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
attrItem->setToolTip( 0, QgsFieldModel::fieldToolTip( fields.at( i ), expressionString ) );
attrItem->setToolTip( 0, QgsFieldModel::fieldToolTipExtended( fields.at( i ), vlayer ) );
attrItem->setData( 0, Qt::UserRole, fields.at( i ).name() );
attrItem->setData( 0, Qt::UserRole + 1, i );

@@ -464,7 +464,7 @@ QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const
}
}

QString QgsFieldModel::fieldToolTip( const QgsField &field, const QString &expression )
QString QgsFieldModel::fieldToolTip( const QgsField &field )
{
QString toolTip;
if ( !field.alias().isEmpty() )
@@ -485,9 +485,25 @@ QString QgsFieldModel::fieldToolTip( const QgsField &field, const QString &expre
toolTip += QStringLiteral( "<br><em>%1</em>" ).arg( comment );
}

if ( ! expression.isEmpty() )
return toolTip;
}

QString QgsFieldModel::fieldToolTipExtended( const QgsField &field, const QgsVectorLayer *layer )
{
QString toolTip = QgsFieldModel::fieldToolTip( field );
const QgsFields fields = layer->fields();
int fieldIdx = fields.indexOf( field.name() );

if ( fieldIdx < 0 )
return QString();

QString expressionString = fields.fieldOrigin( fieldIdx ) == QgsFields::OriginExpression
? layer->expressionField( fieldIdx )
: QString();

if ( !expressionString.isEmpty() )
{
toolTip += QStringLiteral( "<br><font style='font-family:monospace;'>%3</font>" ).arg( expression );
toolTip += QStringLiteral( "<br><font style='font-family:monospace;'>%3</font>" ).arg( expressionString );
}

return toolTip;
@@ -135,7 +135,14 @@ class CORE_EXPORT QgsFieldModel : public QAbstractItemModel
* like the field name, alias and type.
* \since QGIS 3.0
*/
static QString fieldToolTip( const QgsField &field, const QString &expression = QString() );
static QString fieldToolTip( const QgsField &field );

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

/**
* Manually sets the \a fields to use for the model.
@@ -624,10 +624,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
else
{
const QgsField field = layer()->fields().at( mAttributes.at( section ) );
QString expressionString = layer()->fields().fieldOrigin( mAttributes.at( section ) ) == QgsFields::OriginExpression
? layer()->expressionField( mAttributes.at( section ) )
: QString();
return QgsFieldModel::fieldToolTip( field, expressionString );
return QgsFieldModel::fieldToolTipExtended( field, layer() );
}
}
else
@@ -1506,11 +1506,8 @@ void QgsAttributeForm::init()
bool labelOnTop = mLayer->editFormConfig().labelOnTop( idx );

// This will also create the widget
QString expressionString = fields.fieldOrigin( idx ) == QgsFields::OriginExpression
? mLayer->expressionField( idx )
: QString();
QLabel *l = new QLabel( labelText );
l->setToolTip( QgsFieldModel::fieldToolTip( field, expressionString ) );
l->setToolTip( QgsFieldModel::fieldToolTipExtended( field, mLayer ) );
QSvgWidget *i = new QSvgWidget();
i->setFixedSize( 18, 18 );

@@ -360,12 +360,19 @@ def testFieldTooltip(self):
self.assertEqual(QgsFieldModel.fieldToolTip(f), "<b>my_real</b><br><font style='font-family:monospace; white-space: nowrap;'>real(8, 3) NULL</font>")
f.setComment('Comment text')
self.assertEqual(QgsFieldModel.fieldToolTip(f), "<b>my_real</b><br><font style='font-family:monospace; white-space: nowrap;'>real(8, 3) NULL</font><br><em>Comment text</em>")
self.assertEqual(QgsFieldModel.fieldToolTip(f, '1+1'), "<b>my_real</b><br><font style='font-family:monospace; white-space: nowrap;'>real(8, 3) NULL</font><br><em>Comment text</em><br><font style='font-family:monospace;'>1+1</font>")

def testFieldTooltipExtended(self):
layer = QgsVectorLayer("Point?", "tooltip", "memory")
f = QgsField('my_real', QVariant.Double, 'real', 8, 3, 'Comment text')
layer.addExpressionField('1+1', f)
layer.updateFields()
self.assertEqual(QgsFieldModel.fieldToolTipExtended(QgsField('my_string', QVariant.String, 'string'), layer), '')
self.assertEqual(QgsFieldModel.fieldToolTipExtended(f, layer), "<b>my_real</b><br><font style='font-family:monospace; white-space: nowrap;'>real(8, 3) NULL</font><br><em>Comment text</em><br><font style='font-family:monospace;'>1+1</font>")
f.setAlias('my alias')
constraints = f.constraints()
constraints.setConstraint(QgsFieldConstraints.ConstraintUnique)
f.setConstraints(constraints)
self.assertEqual(QgsFieldModel.fieldToolTip(f, '1+1'), "<b>my alias</b> (my_real)<br><font style='font-family:monospace; white-space: nowrap;'>real(8, 3) NULL UNIQUE</font><br><em>Comment text</em><br><font style='font-family:monospace;'>1+1</font>")
self.assertEqual(QgsFieldModel.fieldToolTipExtended(f, layer), "<b>my alias</b> (my_real)<br><font style='font-family:monospace; white-space: nowrap;'>real(8, 3) NULL UNIQUE</font><br><em>Comment text</em><br><font style='font-family:monospace;'>1+1</font>")


if __name__ == '__main__':

0 comments on commit 6a0ae06

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