~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/playlist/PlaylistItemDelegate.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include "PlaylistItemDelegate.h"
 
21
 
 
22
#include <QApplication>
 
23
#include <QPainter>
 
24
 
 
25
#include "Query.h"
 
26
#include "Result.h"
 
27
#include "Artist.h"
 
28
#include "Source.h"
 
29
#include "SourceList.h"
 
30
 
 
31
#include "PlayableModel.h"
 
32
#include "PlayableItem.h"
 
33
#include "PlayableProxyModel.h"
 
34
#include "TrackView.h"
 
35
#include "ViewHeader.h"
 
36
 
 
37
#include "utils/TomahawkUtilsGui.h"
 
38
#include "utils/Logger.h"
 
39
 
 
40
using namespace Tomahawk;
 
41
 
 
42
 
 
43
PlaylistItemDelegate::PlaylistItemDelegate( TrackView* parent, PlayableProxyModel* proxy )
 
44
    : QStyledItemDelegate( (QObject*)parent )
 
45
    , m_view( parent )
 
46
    , m_model( proxy )
 
47
{
 
48
    m_topOption = QTextOption( Qt::AlignTop );
 
49
    m_topOption.setWrapMode( QTextOption::NoWrap );
 
50
 
 
51
    m_bottomOption = QTextOption( Qt::AlignBottom );
 
52
    m_bottomOption.setWrapMode( QTextOption::NoWrap );
 
53
}
 
54
 
 
55
 
 
56
void
 
57
PlaylistItemDelegate::updateRowSize( const QModelIndex& index )
 
58
{
 
59
    emit sizeHintChanged( index );
 
60
}
 
61
 
 
62
 
 
63
QSize
 
64
PlaylistItemDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
 
65
{
 
66
    QSize size = QStyledItemDelegate::sizeHint( option, index );
 
67
 
 
68
    {
 
69
        if ( m_model->style() == PlayableProxyModel::Short || m_model->style() == PlayableProxyModel::ShortWithAvatars )
 
70
        {
 
71
            int rowHeight = option.fontMetrics.height() + 8;
 
72
            size.setHeight( rowHeight * 2 );
 
73
        }
 
74
        else if ( m_model->style() == PlayableProxyModel::Detailed )
 
75
        {
 
76
            int rowHeight = option.fontMetrics.height() * 1.6;
 
77
            size.setHeight( rowHeight );
 
78
        }
 
79
    }
 
80
 
 
81
    return size;
 
82
}
 
83
 
 
84
 
 
85
QWidget*
 
86
PlaylistItemDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
 
87
{
 
88
    Q_UNUSED( parent );
 
89
    Q_UNUSED( option );
 
90
    Q_UNUSED( index );
 
91
    return 0;
 
92
}
 
93
 
 
94
 
 
95
void
 
96
PlaylistItemDelegate::prepareStyleOption( QStyleOptionViewItemV4* option, const QModelIndex& index, PlayableItem* item ) const
 
97
{
 
98
    initStyleOption( option, index );
 
99
 
 
100
    TomahawkUtils::prepareStyleOption( option, index, item );
 
101
}
 
102
 
 
103
 
 
104
void
 
105
PlaylistItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
 
106
{
 
107
    int style = index.data( PlayableProxyModel::StyleRole ).toInt();
 
108
    switch ( style )
 
109
    {
 
110
        case PlayableProxyModel::Detailed:
 
111
            paintDetailed( painter, option, index );
 
112
            break;
 
113
 
 
114
        case PlayableProxyModel::Short:
 
115
            paintShort( painter, option, index );
 
116
            break;
 
117
        case PlayableProxyModel::ShortWithAvatars:
 
118
            paintShort( painter, option, index, true );
 
119
            break;
 
120
    }
 
121
}
 
122
 
 
123
 
 
124
void
 
125
PlaylistItemDelegate::paintShort( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index, bool useAvatars ) const
 
126
{
 
127
    PlayableItem* item = m_model->itemFromIndex( m_model->mapToSource( index ) );
 
128
    Q_ASSERT( item );
 
129
 
 
130
    QStyleOptionViewItemV4 opt = option;
 
131
    prepareStyleOption( &opt, index, item );
 
132
    opt.text.clear();
 
133
 
 
134
    qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
 
135
 
 
136
    if ( m_view->header()->visualIndex( index.column() ) > 0 )
 
137
        return;
 
138
 
 
139
    const query_ptr q = item->query()->displayQuery();
 
140
    QString artist = q->artist();
 
141
    QString track = q->track();
 
142
    QPixmap pixmap;
 
143
    QString upperText, lowerText;
 
144
    source_ptr source = item->query()->playedBy().first;
 
145
 
 
146
    if ( source.isNull() )
 
147
    {
 
148
        upperText = track;
 
149
        lowerText = artist;
 
150
    }
 
151
    else
 
152
    {
 
153
        upperText = QString( "%1 - %2" ).arg( artist ).arg( track );
 
154
        QString playtime = TomahawkUtils::ageToString( QDateTime::fromTime_t( item->query()->playedBy().second ), true );
 
155
 
 
156
        if ( source == SourceList::instance()->getLocal() )
 
157
            lowerText = QString( tr( "played %1 by you" ) ).arg( playtime );
 
158
        else
 
159
            lowerText = QString( tr( "played %1 by %2" ) ).arg( playtime ).arg( source->friendlyName() );
 
160
    }
 
161
 
 
162
    painter->save();
 
163
    {
 
164
        QRect r = opt.rect.adjusted( 3, 6, 0, -6 );
 
165
 
 
166
        // Paint Now Playing Speaker Icon
 
167
        if ( item->isPlaying() )
 
168
        {
 
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 );
 
174
        }
 
175
 
 
176
        painter->setPen( opt.palette.text().color() );
 
177
 
 
178
        QRect ir = r.adjusted( 4, 0, -option.rect.width() + option.rect.height() - 8 + r.left(), 0 );
 
179
 
 
180
        if ( useAvatars )
 
181
        {
 
182
            if ( !source.isNull() )
 
183
                pixmap = source->avatar( TomahawkUtils::RoundedCorners, ir.size() );
 
184
        }
 
185
        else
 
186
            pixmap = item->query()->cover( ir.size(), false );
 
187
 
 
188
        if ( pixmap.isNull() )
 
189
        {
 
190
            if ( !useAvatars )
 
191
                pixmap = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultTrackImage, TomahawkUtils::Original, ir.size() );
 
192
            else
 
193
                pixmap = TomahawkUtils::defaultPixmap( TomahawkUtils::DefaultSourceAvatar, TomahawkUtils::RoundedCorners, ir.size() );
 
194
        }
 
195
 
 
196
        painter->drawPixmap( ir, pixmap );
 
197
 
 
198
        QFont boldFont = opt.font;
 
199
        boldFont.setBold( true );
 
200
 
 
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 );
 
205
 
 
206
        painter->setFont( opt.font );
 
207
        if ( !( option.state & QStyle::State_Selected || item->isPlaying() ) )
 
208
            painter->setPen( Qt::gray );
 
209
 
 
210
        text = painter->fontMetrics().elidedText( lowerText, Qt::ElideRight, r.width() );
 
211
        painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, m_bottomOption );
 
212
    }
 
213
    painter->restore();
 
214
}
 
215
 
 
216
 
 
217
void
 
218
PlaylistItemDelegate::paintDetailed( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
 
219
{
 
220
    PlayableItem* item = m_model->itemFromIndex( m_model->mapToSource( index ) );
 
221
    Q_ASSERT( item );
 
222
 
 
223
    QTextOption textOption( Qt::AlignVCenter | (Qt::Alignment)index.data( Qt::TextAlignmentRole ).toUInt() );
 
224
    textOption.setWrapMode( QTextOption::NoWrap );
 
225
 
 
226
    QStyleOptionViewItemV4 opt = option;
 
227
    prepareStyleOption( &opt, index, item );
 
228
    opt.text.clear();
 
229
    qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
 
230
 
 
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 ) )
 
233
    {
 
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 );
 
236
 
 
237
        QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, arrowRect.size() );
 
238
        painter->drawPixmap( arrowRect, infoIcon );
 
239
    }
 
240
 
 
241
    painter->save();
 
242
 
 
243
/*    if ( index.column() == PlayableModel::Score )
 
244
    {
 
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 );
 
248
        else
 
249
            painter->setPen( barColor );
 
250
 
 
251
        QRect r = opt.rect.adjusted( 3, 3, -6, -4 );
 
252
        painter->drawRect( r );
 
253
 
 
254
        QRect fillR = r;
 
255
        int fillerWidth = (int)( index.data().toFloat() * (float)fillR.width() );
 
256
        fillR.adjust( 0, 0, -( fillR.width() - fillerWidth ), 0 );
 
257
 
 
258
        if ( opt.state & QStyle::State_Selected && !item->isPlaying() )
 
259
            painter->setBrush( TomahawkUtils::Colors::NOW_PLAYING_ITEM.lighter() );
 
260
        else
 
261
            painter->setBrush( barColor );
 
262
 
 
263
        painter->drawRect( fillR );
 
264
    }
 
265
    else */ if ( item->isPlaying() )
 
266
    {
 
267
        QRect r = opt.rect.adjusted( 3, 0, 0, 0 );
 
268
 
 
269
        // Paint Now Playing Speaker Icon
 
270
        if ( m_view->header()->visualIndex( index.column() ) == 0 )
 
271
        {
 
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 );
 
277
        }
 
278
 
 
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 );
 
282
    }
 
283
    else
 
284
    {
 
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 );
 
288
    }
 
289
 
 
290
    painter->restore();
 
291
}