PageRenderTime 412ms CodeModel.GetById 121ms app.highlight 206ms RepoModel.GetById 76ms app.codeStats 1ms

/src/libtomahawk/jobview/JobStatusDelegate.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 120 lines | 75 code | 25 blank | 20 comment | 7 complexity | 59e63524eb561cb3805ecb63ea96bbcc MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *
  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.
  9 *
 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.
 14 *
 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/>.
 17 */
 18
 19#include "JobStatusDelegate.h"
 20
 21#include "JobStatusModel.h"
 22#include "utils/TomahawkUtilsGui.h"
 23#include "utils/Logger.h"
 24
 25#include <QPainter>
 26#include <QApplication>
 27#include <QListView>
 28
 29#define ROW_HEIGHT ( TomahawkUtils::defaultFontHeight() + 6 )
 30#define ICON_PADDING 2
 31#define PADDING 2
 32
 33
 34JobStatusDelegate::JobStatusDelegate( QObject* parent )
 35    : QStyledItemDelegate ( parent )
 36    , m_parentView( qobject_cast< QListView* >( parent ) )
 37{
 38    Q_ASSERT( m_parentView );
 39}
 40
 41
 42JobStatusDelegate::~JobStatusDelegate()
 43{
 44}
 45
 46
 47void
 48JobStatusDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
 49{
 50    QStyleOptionViewItem opt = option;
 51    initStyleOption( &opt, index );
 52    QFontMetrics fm( painter->font() );
 53    const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
 54
 55    opt.state &= ~QStyle::State_MouseOver;
 56    QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget );
 57
 58//     painter->drawLine( opt.rect.topLeft(), opt.rect.topRight() );
 59
 60    painter->setRenderHint( QPainter::Antialiasing );
 61    QRect iconRect( ICON_PADDING, ICON_PADDING + opt.rect.y(), ROW_HEIGHT - 2 * ICON_PADDING, ROW_HEIGHT - 2 * ICON_PADDING );
 62    if ( allowMultiLine )
 63        iconRect.moveTop( opt.rect.top() + opt.rect.height() / 2 - iconRect.height() / 2);
 64    QPixmap p = index.data( Qt::DecorationRole ).value< QPixmap >();
 65    if ( !p.isNull() )
 66    {
 67        p = p.scaledToHeight( iconRect.height(), Qt::SmoothTransformation );
 68        painter->drawPixmap( iconRect, p );
 69    }
 70
 71    // draw right column if there is one
 72    const QString rCol = index.data( JobStatusModel::RightColumnRole ).toString();
 73    int rightEdge = opt.rect.right();
 74    if ( !rCol.isEmpty() )
 75    {
 76        const int w = fm.width( rCol );
 77        const QRect rRect( opt.rect.right() - PADDING - w, PADDING + opt.rect.y(), w, opt.rect.height() - 2 * PADDING );
 78        painter->drawText( rRect, Qt::AlignCenter, rCol );
 79
 80        rightEdge = rRect.left();
 81    }
 82
 83    const int mainW = rightEdge - 6 * PADDING - iconRect.right();
 84    QString mainText = index.data( Qt::DisplayRole ).toString();
 85    QTextOption to( Qt::AlignLeft | Qt::AlignVCenter );
 86    if ( !allowMultiLine )
 87    {
 88        to.setWrapMode( QTextOption::NoWrap );
 89        mainText = fm.elidedText( mainText, Qt::ElideRight, mainW );
 90    }
 91    else
 92        to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
 93
 94    painter->drawText( QRect( iconRect.right() + 4 * PADDING, PADDING + opt.rect.y(), mainW, opt.rect.height() - 2 * PADDING ), mainText, to );
 95}
 96
 97
 98QSize
 99JobStatusDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
100{
101    const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
102
103    if ( !allowMultiLine )
104        return QSize( QStyledItemDelegate::sizeHint( option, index ).width(), ROW_HEIGHT );
105    else if ( m_cachedMultiLineHeights.contains( index ) )
106        return QSize( QStyledItemDelegate::sizeHint( option, index ).width(), m_cachedMultiLineHeights[ index ] );
107
108    // Don't elide, but stretch across as many rows as required
109    QStyleOptionViewItem opt = option;
110    initStyleOption( &opt, index );
111
112    const QString text = index.data( Qt::DisplayRole ).toString();
113    const int leftEdge =  ICON_PADDING + ROW_HEIGHT + 2 * PADDING;
114    const QRect rect = opt.fontMetrics.boundingRect( leftEdge, opt.rect.top(), m_parentView->width() - leftEdge, 200, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text );
115
116    m_cachedMultiLineHeights.insert( index, rect.height() + 4 * PADDING );
117
118    return QSize( QStyledItemDelegate::sizeHint ( option, index ).width(), rect.height() + 4 * PADDING );
119}
120