PageRenderTime 395ms CodeModel.GetById 100ms app.highlight 195ms RepoModel.GetById 97ms app.codeStats 0ms

/src/libtomahawk/playlist/treeitemdelegate.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 193 lines | 136 code | 37 blank | 20 comment | 30 complexity | 0a5f148ec9e00039a16bdfc1e763c481 MD5 | raw file
  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 2012       Leo Franchi            <lfranchi@kde.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 "TreeItemDelegate.h"
 21
 22#include <QApplication>
 23#include <QPainter>
 24#include <QAbstractItemView>
 25#include <QHeaderView>
 26
 27#include "Query.h"
 28#include "Result.h"
 29
 30#include "utils/TomahawkUtilsGui.h"
 31#include "utils/Logger.h"
 32#include "utils/Closure.h"
 33#include "utils/PixmapDelegateFader.h"
 34
 35#include "PlayableItem.h"
 36#include "TreeProxyModel.h"
 37#include "TreeView.h"
 38
 39
 40TreeItemDelegate::TreeItemDelegate( TreeView* parent, TreeProxyModel* proxy )
 41    : QStyledItemDelegate( (QObject*)parent )
 42    , m_view( parent )
 43    , m_model( proxy )
 44{
 45}
 46
 47
 48void
 49TreeItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
 50{
 51    PlayableItem* item = m_model->sourceModel()->itemFromIndex( m_model->mapToSource( index ) );
 52    if ( !item )
 53        return;
 54
 55    QTextOption textOption( Qt::AlignVCenter | (Qt::Alignment)index.data( Qt::TextAlignmentRole ).toUInt() );
 56    textOption.setWrapMode( QTextOption::NoWrap );
 57
 58    QString text;
 59    if ( !item->artist().isNull() )
 60    {
 61        text = item->artist()->name();
 62    }
 63    else if ( !item->album().isNull() )
 64    {
 65        text = item->album()->name();
 66    }
 67    else if ( !item->result().isNull() || !item->query().isNull() )
 68    {
 69        float opacity = item->result().isNull() ? 0.0 : item->result()->score();
 70        opacity = qMax( (float)0.3, opacity );
 71        QColor textColor = TomahawkUtils::alphaBlend( option.palette.color( QPalette::Foreground ), option.palette.color( QPalette::Background ), opacity );
 72
 73        {
 74            QStyleOptionViewItemV4 o = option;
 75            initStyleOption( &o, QModelIndex() );
 76
 77            painter->save();
 78            o.palette.setColor( QPalette::Text, textColor );
 79
 80            if ( o.state & QStyle::State_Selected && o.state & QStyle::State_Active )
 81            {
 82                o.palette.setColor( QPalette::Text, o.palette.color( QPalette::HighlightedText ) );
 83            }
 84
 85            if ( item->isPlaying() )
 86            {
 87                o.palette.setColor( QPalette::Highlight, o.palette.color( QPalette::Mid ) );
 88                if ( o.state & QStyle::State_Selected )
 89                    o.palette.setColor( QPalette::Text, textColor );
 90                o.state |= QStyle::State_Selected;
 91            }
 92
 93            int oldX = 0;
 94            if ( m_view->header()->visualIndex( index.column() ) == 0 )
 95            {
 96                oldX = o.rect.x();
 97                o.rect.setX( 0 );
 98            }
 99            qApp->style()->drawControl( QStyle::CE_ItemViewItem, &o, painter );
100            if ( oldX > 0 )
101                o.rect.setX( oldX );
102
103            if ( m_view->hoveredIndex().row() == index.row() && m_view->hoveredIndex().column() == index.column() &&
104                 !index.data().toString().isEmpty() && index.column() == 0 )
105            {
106                o.rect.setWidth( o.rect.width() - 16 );
107                QRect arrowRect( o.rect.x() + o.rect.width(), o.rect.y() + 1, o.rect.height() - 2, o.rect.height() - 2 );
108
109                QPixmap infoIcon = TomahawkUtils::defaultPixmap( TomahawkUtils::InfoIcon, TomahawkUtils::Original, arrowRect.size() );
110                painter->drawPixmap( arrowRect, infoIcon );
111            }
112
113            {
114                QRect r = o.rect.adjusted( 3, 0, 0, 0 );
115
116                // Paint Now Playing Speaker Icon
117                if ( item->isPlaying() && m_view->header()->visualIndex( index.column() ) == 0 )
118                {
119                    r.adjust( 0, 0, 0, -3 );
120                    QRect npr = r.adjusted( 3, 1, 18 - r.width(), 1 );
121                    painter->drawPixmap( npr, TomahawkUtils::defaultPixmap( TomahawkUtils::NowPlayingSpeaker, TomahawkUtils::Original, npr.size() ) );
122                    r.adjust( 25, 0, 0, 3 );
123                }
124
125                painter->setPen( o.palette.text().color() );
126
127                QString text = painter->fontMetrics().elidedText( index.data().toString(), Qt::ElideRight, r.width() - 3 );
128                painter->drawText( r.adjusted( 0, 1, 0, 0 ), text, textOption );
129            }
130            painter->restore();
131        }
132
133        return;
134    }
135    else
136        return;
137
138    if ( text.trimmed().isEmpty() )
139        text = tr( "Unknown" );
140
141    QStyleOptionViewItemV4 opt = option;
142    initStyleOption( &opt, QModelIndex() );
143    qApp->style()->drawControl( QStyle::CE_ItemViewItem, &opt, painter );
144
145    if ( option.state & QStyle::State_Selected )
146    {
147        opt.palette.setColor( QPalette::Text, opt.palette.color( QPalette::HighlightedText ) );
148    }
149
150    if ( index.column() > 0 )
151        return;
152
153    painter->save();
154    painter->setRenderHint( QPainter::Antialiasing );
155    painter->setPen( opt.palette.color( QPalette::Text ) );
156
157    QRect r = option.rect.adjusted( 4, 4, -option.rect.width() + option.rect.height() - 4, -4 );
158//    painter->drawPixmap( r, QPixmap( RESPATH "images/cover-shadow.png" ) );
159
160    if ( !m_pixmaps.contains( index ) )
161    {
162        if ( !item->album().isNull() )
163        {
164            m_pixmaps.insert( index, QSharedPointer< Tomahawk::PixmapDelegateFader >( new Tomahawk::PixmapDelegateFader( item->album(), r.size(), TomahawkUtils::ScaledCover, false ) ) );
165            _detail::Closure* closure = NewClosure( m_pixmaps[ index ], SIGNAL( repaintRequest() ), const_cast<TreeItemDelegate*>(this), SLOT( doUpdateIndex( const QPersistentModelIndex& ) ), QPersistentModelIndex( index ) );
166            closure->setAutoDelete( false );
167        }
168        else if ( !item->artist().isNull() )
169        {
170            m_pixmaps.insert( index, QSharedPointer< Tomahawk::PixmapDelegateFader >( new Tomahawk::PixmapDelegateFader( item->artist(), r.size(), TomahawkUtils::ScaledCover, false ) ) );
171            _detail::Closure* closure = NewClosure( m_pixmaps[ index ], SIGNAL( repaintRequest() ), const_cast<TreeItemDelegate*>(this), SLOT( doUpdateIndex( const QPersistentModelIndex& ) ), QPersistentModelIndex( index ) );
172            closure->setAutoDelete( false );
173        }
174    }
175
176    const QPixmap cover = m_pixmaps[ index ]->currentPixmap();
177
178    painter->drawPixmap( r, cover );
179
180    r = option.rect.adjusted( option.rect.height(), 6, -4, -option.rect.height() + 22 );
181    text = painter->fontMetrics().elidedText( text, Qt::ElideRight, r.width() );
182    painter->drawText( r, text, textOption );
183
184    painter->restore();
185}
186
187
188void
189TreeItemDelegate::doUpdateIndex( const QPersistentModelIndex& index )
190{
191    emit updateIndex( index );
192}
193