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

« back to all changes in this revision

Viewing changes to src/libtomahawk/jobview/JobStatusDelegate.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, 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
 
 
34
JobStatusDelegate::JobStatusDelegate( QObject* parent )
 
35
    : QStyledItemDelegate ( parent )
 
36
    , m_parentView( qobject_cast< QListView* >( parent ) )
 
37
{
 
38
    Q_ASSERT( m_parentView );
 
39
}
 
40
 
 
41
 
 
42
JobStatusDelegate::~JobStatusDelegate()
 
43
{
 
44
 
 
45
}
 
46
 
 
47
 
 
48
void
 
49
JobStatusDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
 
50
{
 
51
    QStyleOptionViewItemV4 opt = option;
 
52
    initStyleOption( &opt, index );
 
53
    QFontMetrics fm( opt.font );
 
54
    const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
 
55
 
 
56
    opt.state &= ~QStyle::State_MouseOver;
 
57
    QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget );
 
58
 
 
59
//     painter->drawLine( opt.rect.topLeft(), opt.rect.topRight() );
 
60
 
 
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 );
 
63
    if ( allowMultiLine )
 
64
        iconRect.moveTop( opt.rect.top() + opt.rect.height() / 2 - iconRect.height() / 2);
 
65
    QPixmap p = index.data( Qt::DecorationRole ).value< QPixmap >();
 
66
    if ( !p.isNull() )
 
67
    {
 
68
        p = p.scaledToHeight( iconRect.height(), Qt::SmoothTransformation );
 
69
        painter->drawPixmap( iconRect, p );
 
70
    }
 
71
 
 
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() )
 
76
    {
 
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 );
 
80
 
 
81
        rightEdge = rRect.left();
 
82
    }
 
83
 
 
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 );
 
89
    else
 
90
        to.setWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
 
91
 
 
92
    painter->drawText( QRect( iconRect.right() + 4 * PADDING, PADDING + opt.rect.y(), mainW, opt.rect.height() - 2 * PADDING ), mainText, to );
 
93
}
 
94
 
 
95
 
 
96
QSize
 
97
JobStatusDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
 
98
{
 
99
    const bool allowMultiLine = index.data( JobStatusModel::AllowMultiLineRole ).toBool();
 
100
 
 
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 ] );
 
105
 
 
106
    // Don't elide, but stretch across as many rows as required
 
107
    QStyleOptionViewItemV4 opt = option;
 
108
    initStyleOption( &opt, index );
 
109
 
 
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 );
 
113
 
 
114
    m_cachedMultiLineHeights.insert( index, rect.height() + 4 * PADDING );
 
115
 
 
116
    return QSize( QStyledItemDelegate::sizeHint ( option, index ).width(), rect.height() + 4 * PADDING );
 
117
}
 
118