Commit 5f78dc28 authored by Francois Cartegnie's avatar Francois Cartegnie

Qt: ExtensionItemDelegate: rewrite (fix #10407)

does it the right way
parent 09680206
...@@ -430,6 +430,7 @@ ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf, ...@@ -430,6 +430,7 @@ ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
QListView *view ) QListView *view )
: QStyledItemDelegate( view ), view( view ), p_intf( p_intf ) : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
{ {
margins = QMargins( 4, 4, 4, 4 );
} }
ExtensionItemDelegate::~ExtensionItemDelegate() ExtensionItemDelegate::~ExtensionItemDelegate()
...@@ -440,80 +441,73 @@ void ExtensionItemDelegate::paint( QPainter *painter, ...@@ -440,80 +441,73 @@ void ExtensionItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option, const QStyleOptionViewItem &option,
const QModelIndex &index ) const const QModelIndex &index ) const
{ {
int width = option.rect.width(); QStyleOptionViewItemV4 opt = option;
initStyleOption( &opt, index );
// Pixmap: buffer where to draw
QPixmap pix(option.rect.size());
// Draw background // Draw background
pix.fill( Qt::transparent ); // FIXME if ( opt.state & QStyle::State_Selected )
painter->fillRect( opt.rect, opt.palette.highlight() );
// ItemView primitive style
QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
&option,
painter );
// Painter on the pixmap
QPainter *pixpaint = new QPainter(&pix);
// Text font & pen
QFont font = painter->font();
QPen pen = painter->pen();
if( view->selectionModel()->selectedIndexes().contains( index ) )
{
pen.setBrush( option.palette.highlightedText() );
}
else
{
pen.setBrush( option.palette.text() );
}
pixpaint->setPen( pen );
QFontMetrics metrics = option.fontMetrics;
// Icon // Icon
QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>(); QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
if( !icon.isNull() ) if( !icon.isNull() )
{ {
pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(), painter->drawPixmap( opt.rect.left() + margins.left(),
icon ); opt.rect.top() + margins.top(),
icon.scaled( opt.decorationSize,
Qt::KeepAspectRatio,
Qt::SmoothTransformation )
);
} }
// Title: bold painter->save();
pixpaint->setRenderHint( QPainter::TextAntialiasing ); painter->setRenderHint( QPainter::TextAntialiasing );
if ( opt.state & QStyle::State_Selected )
painter->setPen( opt.palette.highlightedText().color() );
QFont font( option.font );
font.setBold( true ); font.setBold( true );
pixpaint->setFont( font ); painter->setFont( font );
pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7, QRect textrect( opt.rect );
width - 40 - 2 * metrics.height(), textrect.adjust( 2 * margins.left() + margins.right() + opt.decorationSize.width(),
metrics.height() ), margins.top(),
Qt::AlignLeft, index.data( Qt::DisplayRole ).toString() ); - margins.right(),
- margins.bottom() - opt.fontMetrics.height() );
// Short description: normal painter->drawText( textrect, Qt::AlignLeft,
font.setBold( false ); index.data( Qt::DisplayRole ).toString() );
pixpaint->setFont( font );
pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
7 + metrics.height(), width - 40,
metrics.height() ),
Qt::AlignLeft, index.data( ExtensionListModel::DescriptionRole ).toString() );
// Flush paint operations font.setBold( false );
delete pixpaint; painter->setFont( font );
painter->drawText( textrect.translated( 0, option.fontMetrics.height() ),
Qt::AlignLeft,
index.data( ExtensionListModel::DescriptionRole ).toString() );
// Draw it on the screen! painter->restore();
painter->drawPixmap( option.rect, pix );
} }
QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option, QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
const QModelIndex &index ) const const QModelIndex &index ) const
{ {
if (index.isValid() && index.column() == 0) if ( index.isValid() )
{ {
QFontMetrics metrics = option.fontMetrics; return QSize( 200, 2 * option.fontMetrics.height()
return QSize( 200, 14 + 2 * metrics.height() ); + margins.top() + margins.bottom() );
} }
else else
return QSize(); return QSize();
} }
void ExtensionItemDelegate::initStyleOption( QStyleOptionViewItem *option,
const QModelIndex &index ) const
{
QStyledItemDelegate::initStyleOption( option, index );
option->decorationSize = QSize( option->rect.height(), option->rect.height() );
option->decorationSize -= QSize( margins.left() + margins.right(),
margins.top() + margins.bottom() );
}
/* "More information" dialog */ /* "More information" dialog */
ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index, ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index,
......
...@@ -183,8 +183,11 @@ public: ...@@ -183,8 +183,11 @@ public:
const QModelIndex &index ) const; const QModelIndex &index ) const;
virtual QSize sizeHint( const QStyleOptionViewItem &option, virtual QSize sizeHint( const QStyleOptionViewItem &option,
const QModelIndex &index ) const; const QModelIndex &index ) const;
virtual void initStyleOption( QStyleOptionViewItem *option,
const QModelIndex &index ) const;
private: private:
QMargins margins;
QListView *view; QListView *view;
intf_thread_t *p_intf; intf_thread_t *p_intf;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment