1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
6
* Tomahawk is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Tomahawk is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
20
#include "PlaylistItemDelegate.h"
22
#include <QApplication>
29
#include "SourceList.h"
31
#include "PlayableModel.h"
32
#include "PlayableItem.h"
33
#include "PlayableProxyModel.h"
34
#include "TrackView.h"
35
#include "ViewHeader.h"
37
#include "utils/TomahawkUtilsGui.h"
38
#include "utils/Logger.h"
40
using namespace Tomahawk;
43
PlaylistItemDelegate::PlaylistItemDelegate( TrackView* parent, PlayableProxyModel* proxy )
44
: QStyledItemDelegate( (QObject*)parent )
48
m_topOption = QTextOption( Qt::AlignTop );
49
m_topOption.setWrapMode( QTextOption::NoWrap );
51
m_bottomOption = QTextOption( Qt::AlignBottom );
52
m_bottomOption.setWrapMode( QTextOption::NoWrap );
57
PlaylistItemDelegate::updateRowSize( const QModelIndex& index )
59
emit sizeHintChanged( index );
64
PlaylistItemDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
66
QSize size = QStyledItemDelegate::sizeHint( option, index );
69
if ( m_model->style() == PlayableProxyModel::Short || m_model->style() == PlayableProxyModel::ShortWithAvatars )
71
int rowHeight = option.fontMetrics.height() + 8;
72
size.setHeight( rowHeight * 2 );
74
else if ( m_model->style() == PlayableProxyModel::Detailed )
76
int rowHeight = option.fontMetrics.height() * 1.6;
77
size.setHeight( rowHeight );
86
PlaylistItemDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
96
PlaylistItemDelegate::prepareStyleOption( QStyleOptionViewItemV4* option, const QModelIndex& index, PlayableItem* item ) const
98
initStyleOption( option, index );
100
TomahawkUtils::prepareStyleOption( option, index, item );
105
PlaylistItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
107
int style = index.data( PlayableProxyModel::StyleRole ).toInt();
110
case PlayableProxyModel::Detailed:
111
paintDetailed( painter, option, index );
114
case PlayableProxyModel::Short:
115
paintShort( painter, option, index );
117
case PlayableProxyModel::ShortWithAvatars:
118
paintShort( painter, option, index, true );
125
PlaylistItemDelegate::paintShort( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index, bool useAvatars ) const
127
PlayableItem* item = m_model->itemFromIndex( m_model->mapToSource( index ) );
130
QStyleOptionViewItemV4 opt = option;
131
prepareStyleOption( &opt, index, item );
134
qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
136
if ( m_view->header()->visualIndex( index.column() ) > 0 )
139
const query_ptr q = item->query()->displayQuery();
140
QString artist = q->artist();
141
QString track = q->track();
143
QString upperText, lowerText;
144
source_ptr source = item->query()->playedBy().first;
146
if ( source.isNull() )
153
upperText = QString( "%1 - %2" ).arg( artist ).arg( track );
154
QString playtime = TomahawkUtils::ageToString( QDateTime::fromTime_t( item->query()->playedBy().second ), true );
156
if ( source == SourceList::instance()->getLocal() )
157
lowerText = QString( tr( "played %1 by you" ) ).arg( playtime );
159
lowerText = QString( tr( "played %1 by %2" ) ).arg( playtime ).arg( source->friendlyName() );
164
QRect r = opt.rect.adjusted( 3, 6, 0, -6 );
166
// Paint Now Playing Speaker Icon
167
if ( item->isPlaying() )
169
const int pixMargin = 2;
170
const int pixHeight = r.height() - pixMargin * 2;
171
QRect npr = r.adjusted( pixMargin, pixMargin + 1, pixHeight - r.width() + pixMargin, -pixMargin + 1 );
172
painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeaker, TomahawkUtils::Original, npr.size() ) );
173
r.adjust( pixHeight + 8, 0, 0, 0 );
176
painter->setPen( opt.palette.text().color() );
178
QRect ir = r.adjusted( 4, 0, -option.rect.width() + option.rect.height() - 8 + r.left(), 0 );
182
if ( !source.isNull() )
183
pixmap = source->avatar( TomahawkUtils::RoundedCorners, ir.size() );
186
pixmap = item->query()->cover( ir.size(), false );
188
if ( pixmap.isNull() )
191
pixmap = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultTrackImage, TomahawkUtils::Original, ir.size() );
193
pixmap = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultSourceAvatar, TomahawkUtils::RoundedCorners, ir.size() );
196
painter->drawPixmap( ir, pixmap );
198
QFont boldFont = opt.font;
199
boldFont.setBold( true );
201
r.adjust( ir.width() + 12, 0, -12, 0 );
202
painter->setFont( boldFont );
203
QString text = painter->fontMetrics().elidedText( upperText, Qt::ElideRight, r.width() );
204
painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, m_topOption );
206
painter->setFont( opt.font );
207
if ( !( option.state & QStyle::State_Selected || item->isPlaying() ) )
208
painter->setPen( Qt::gray );
210
text = painter->fontMetrics().elidedText( lowerText, Qt::ElideRight, r.width() );
211
painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, m_bottomOption );
218
PlaylistItemDelegate::paintDetailed( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
220
PlayableItem* item = m_model->itemFromIndex( m_model->mapToSource( index ) );
223
QTextOption textOption( Qt::AlignVCenter | (Qt::Alignment)index.data( Qt::TextAlignmentRole ).toUInt() );
224
textOption.setWrapMode( QTextOption::NoWrap );
226
QStyleOptionViewItemV4 opt = option;
227
prepareStyleOption( &opt, index, item );
229
qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
231
if ( m_view->hoveredIndex().row() == index.row() && m_view->hoveredIndex().column() == index.column() && !index.data().toString().isEmpty() &&
232
( index.column() == PlayableModel::Artist || index.column() == PlayableModel::Album || index.column() == PlayableModel::Track ) )
234
opt.rect.setWidth( opt.rect.width() - opt.rect.height() - 2 );
235
QRect arrowRect( opt.rect.x() + opt.rect.width(), opt.rect.y() + 1, opt.rect.height() - 2, opt.rect.height() - 2 );
237
QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, arrowRect.size() );
238
painter->drawPixmap( arrowRect, infoIcon );
243
/* if ( index.column() == PlayableModel::Score )
245
QColor barColor( 167, 183, 211 ); // This matches the sidebar (sourcetreeview.cpp:672)
246
if ( opt.state & QStyle::State_Selected && !item->isPlaying() )
247
painter->setPen( Qt::white );
249
painter->setPen( barColor );
251
QRect r = opt.rect.adjusted( 3, 3, -6, -4 );
252
painter->drawRect( r );
255
int fillerWidth = (int)( index.data().toFloat() * (float)fillR.width() );
256
fillR.adjust( 0, 0, -( fillR.width() - fillerWidth ), 0 );
258
if ( opt.state & QStyle::State_Selected && !item->isPlaying() )
259
painter->setBrush( TomahawkUtils::Colors::NOW_PLAYING_ITEM.lighter() );
261
painter->setBrush( barColor );
263
painter->drawRect( fillR );
265
else */ if ( item->isPlaying() )
267
QRect r = opt.rect.adjusted( 3, 0, 0, 0 );
269
// Paint Now Playing Speaker Icon
270
if ( m_view->header()->visualIndex( index.column() ) == 0 )
272
const int pixMargin = 1;
273
const int pixHeight = r.height() - pixMargin * 2;
274
QRect npr = r.adjusted( pixMargin, pixMargin, pixHeight - r.width() + pixMargin, -pixMargin );
275
painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeaker, TomahawkUtils::Original, npr.size() ) );
276
r.adjust( pixHeight + 6, 0, 0, 0 );
279
painter->setPen( opt.palette.text().color() );
280
QString text = painter->fontMetrics().elidedText( index.data().toString(), Qt::ElideRight, r.width() - 3 );
281
painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, textOption );
285
painter->setPen( opt.palette.text().color() );
286
QString text = painter->fontMetrics().elidedText( index.data().toString(), Qt::ElideRight, opt.rect.width() - 6 );
287
painter->drawText( opt.rect.adjusted( 3, 1, -3, 0 ), text, textOption );