Fix broken display of conditional styles in feature list view

While the model was correctly returning the right values for the
font/color/decoration roles, these were not being utilised by
the delegate and had no impact on the rendering of the list.
This commit is contained in:
Nyall Dawson 2019-09-25 09:22:55 +10:00
parent c15c727189
commit 5f7264d4d1
2 changed files with 36 additions and 10 deletions

View File

@ -151,11 +151,11 @@ QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
if ( style.isValid() )
{
if ( role == Qt::BackgroundColorRole && style.validBackgroundColor() )
return style.backgroundColor();
return style.backgroundColor().isValid() ? style.backgroundColor() : QVariant();
if ( role == Qt::TextColorRole && style.validTextColor() )
return style.textColor();
return style.textColor().isValid() ? style.textColor() : QVariant();
if ( role == Qt::DecorationRole )
return style.icon();
return style.icon().isNull() ? QVariant() : style.icon();
if ( role == Qt::FontRole )
return style.font();
}

View File

@ -70,6 +70,17 @@ QSize QgsFeatureListViewDelegate::sizeHint( const QStyleOptionViewItem &option,
void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
const bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
QStyleOptionViewItem textOption = option;
textOption.state |= QStyle::State_Enabled;
if ( isEditSelection )
{
textOption.state |= QStyle::State_Selected;
}
drawBackground( painter, textOption, index );
static QPixmap sSelectedIcon;
if ( sSelectedIcon.isNull() )
sSelectedIcon = QgsApplication::getThemePixmap( QStringLiteral( "/mIconSelected.svg" ) );
@ -80,8 +91,6 @@ void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionVie
QString text = index.model()->data( index, Qt::EditRole ).toString();
QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
// Icon layout options
QStyleOptionViewItem iconOption;
@ -95,14 +104,32 @@ void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionVie
icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
}
drawDecoration( painter, iconOption, iconLayoutBounds, icon );
// if conditional style also has an icon, draw that too
const QVariant conditionalIcon = index.model()->data( index, Qt::DecorationRole );
if ( conditionalIcon.isValid() )
{
const QPixmap pixmap = conditionalIcon.value< QPixmap >();
iconLayoutBounds.moveLeft( iconLayoutBounds.x() + icon.width() + QFontMetrics( textOption.font ).width( 'X' ) );
iconLayoutBounds.setTop( option.rect.y() + ( option.rect.height() - pixmap.height() ) / 2.0 );
iconLayoutBounds.setHeight( pixmap.height() );
drawDecoration( painter, iconOption, iconLayoutBounds, pixmap );
}
// Text layout options
QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
QStyleOptionViewItem textOption = option;
textOption.state |= QStyle::State_Enabled;
if ( isEditSelection )
// start with font and foreground color from model's FontRole
const QVariant font = index.model()->data( index, Qt::FontRole );
if ( font.isValid() )
{
textOption.state |= QStyle::State_Selected;
textOption.font = font.value< QFont >();
}
const QVariant textColor = index.model()->data( index, Qt::TextColorRole );
if ( textColor.isValid() )
{
textOption.palette.setColor( QPalette::Text, textColor.value< QColor >() );
}
if ( featInfo.isNew )
@ -119,5 +146,4 @@ void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionVie
}
drawDisplay( painter, textOption, textLayoutBounds, text );
drawDecoration( painter, iconOption, iconLayoutBounds, icon );
}