1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
5
* Tomahawk is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Tomahawk is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
19
#include "JobStatusDelegate.h"
21
#include "JobStatusModel.h"
22
#include "utils/TomahawkUtilsGui.h"
23
#include "utils/Logger.h"
26
#include <QApplication>
29
#define ROW_HEIGHT ( TomahawkUtils::defaultFontHeight() + 6 )
30
#define ICON_PADDING 2
34
JobStatusDelegate::JobStatusDelegate( QObject* parent )
35
: QStyledItemDelegate ( parent )
36
, m_parentView( qobject_cast< QListView* >( parent ) )
38
Q_ASSERT( m_parentView );
42
JobStatusDelegate::~JobStatusDelegate()
49
JobStatusDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
51
QStyleOptionViewItemV4 opt = option;
52
initStyleOption( &opt, index );
53
QFontMetrics fm( opt.font );
54
const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
56
opt.state &= ~QStyle::State_MouseOver;
57
QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget );
59
// painter->drawLine( opt.rect.topLeft(), opt.rect.topRight() );
61
painter->setRenderHint( QPainter::Antialiasing );
62
QRect iconRect( ICON_PADDING, ICON_PADDING + opt.rect.y(), ROW_HEIGHT - 2 * ICON_PADDING, ROW_HEIGHT - 2 * ICON_PADDING );
64
iconRect.moveTop( opt.rect.top() + opt.rect.height() / 2 - iconRect.height() / 2);
65
QPixmap p = index.data( Qt::DecorationRole ).value< QPixmap >();
68
p = p.scaledToHeight( iconRect.height(), Qt::SmoothTransformation );
69
painter->drawPixmap( iconRect, p );
72
// draw right column if there is one
73
const QString rCol = index.data( JobStatusModel::RightColumnRole ).toString();
74
int rightEdge = opt.rect.right();
75
if ( !rCol.isEmpty() )
77
const int w = fm.width( rCol );
78
const QRect rRect( opt.rect.right() - PADDING - w, PADDING + opt.rect.y(), w, opt.rect.height() - 2 * PADDING );
79
painter->drawText( rRect, Qt::AlignCenter, rCol );
81
rightEdge = rRect.left();
84
const int mainW = rightEdge - 6 * PADDING - iconRect.right();
85
QString mainText = index.data( Qt::DisplayRole ).toString();
86
QTextOption to( Qt::AlignLeft | Qt::AlignVCenter );
87
if ( !allowMultiLine )
88
mainText = fm.elidedText( mainText, Qt::ElideRight, mainW );
90
to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
92
painter->drawText( QRect( iconRect.right() + 4 * PADDING, PADDING + opt.rect.y(), mainW, opt.rect.height() - 2 * PADDING ), mainText, to );
97
JobStatusDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
99
const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
101
if ( !allowMultiLine )
102
return QSize( QStyledItemDelegate::sizeHint( option, index ).width(), ROW_HEIGHT );
103
else if ( m_cachedMultiLineHeights.contains( index ) )
104
return QSize( QStyledItemDelegate::sizeHint( option, index ).width(), m_cachedMultiLineHeights[ index ] );
106
// Don't elide, but stretch across as many rows as required
107
QStyleOptionViewItemV4 opt = option;
108
initStyleOption( &opt, index );
110
const QString text = index.data( Qt::DisplayRole ).toString();
111
const int leftEdge = ICON_PADDING + ROW_HEIGHT + 2 * PADDING;
112
const QRect rect = opt.fontMetrics.boundingRect( leftEdge, opt.rect.top(), m_parentView->width() - leftEdge, 200, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text );
114
m_cachedMultiLineHeights.insert( index, rect.height() + 4 * PADDING );
116
return QSize( QStyledItemDelegate::sizeHint ( option, index ).width(), rect.height() + 4 * PADDING );