~hendrik-grewe/transmission/private-patch

« back to all changes in this revision

Viewing changes to qt/torrent-delegate-min.cc

  • Committer: charles
  • Date: 2009-04-09 17:55:47 UTC
  • Revision ID: svn-v4:f4695dd4-2c0a-0410-b89c-da849a56a58e:trunk:8188
(trunk) add the Qt beta into svn 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
 
3
 *
 
4
 * This file is licensed by the GPL version 2.  Works owned by the
 
5
 * Transmission project are granted a special exemption to clause 2(b)
 
6
 * so that the bulk of its code can remain under the MIT license.
 
7
 * This exemption does not extend to derived works not owned by
 
8
 * the Transmission project.
 
9
 *
 
10
 * $Id:$
 
11
 */
 
12
 
 
13
#include <iostream>
 
14
 
 
15
#include <QApplication>
 
16
#include <QBrush>
 
17
#include <QFont>
 
18
#include <QFontMetrics>
 
19
#include <QIcon>
 
20
#include <QModelIndex>
 
21
#include <QPainter>
 
22
#include <QPixmap>
 
23
#include <QPixmapCache>
 
24
#include <QStyleOptionProgressBarV2>
 
25
 
 
26
#include "torrent.h"
 
27
#include "torrent-delegate-min.h"
 
28
#include "torrent-model.h"
 
29
#include "utils.h"
 
30
#include "qticonloader.h"
 
31
 
 
32
enum
 
33
{
 
34
   GUI_PAD = 6,
 
35
   BAR_HEIGHT = 12,
 
36
   LINE_SPACING = 4
 
37
};
 
38
 
 
39
/***
 
40
****
 
41
****   +---------+-----------------------------------------------+
 
42
****   |  Icon   |   Title                   shortStatusString   |
 
43
****   |  Icon   |   [ Progressbar.......................... ]   |
 
44
****   +-------- +-----------------------------------------------+
 
45
****
 
46
***/
 
47
 
 
48
QSize
 
49
TorrentDelegateMin :: sizeHint( const QStyleOptionViewItem& option, const Torrent& tor ) const
 
50
{
 
51
    const QStyle* style( QApplication::style( ) );
 
52
    static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) );
 
53
 
 
54
    QFont nameFont( option.font );
 
55
    const QFontMetrics nameFM( nameFont );
 
56
    const QString nameStr( tor.name( ) );
 
57
    const QSize nameSize( nameFM.size( 0, nameStr ) );
 
58
 
 
59
    QFont statusFont( option.font );
 
60
    statusFont.setPointSize( int( option.font.pointSize( ) * 0.85 ) );
 
61
    const QFontMetrics statusFM( statusFont );
 
62
    const QString statusStr( shortStatusString( tor ) );
 
63
    const QSize statusSize( statusFM.size( 0, statusStr ) );
 
64
 
 
65
    const QSize m( margin( *style ) );
 
66
 
 
67
    return QSize( m.width() + iconSize + GUI_PAD + nameSize.width() + GUI_PAD + statusSize.width() + m.width(),
 
68
                  m.height() + nameSize.height() + LINE_SPACING + BAR_HEIGHT  + m.height() );
 
69
}
 
70
 
 
71
void
 
72
TorrentDelegateMin :: drawTorrent( QPainter * painter, const QStyleOptionViewItem& option, const Torrent& tor ) const
 
73
{
 
74
    const bool isPaused( tor.isPaused( ) );
 
75
    const QStyle * style( QApplication::style( ) );
 
76
    static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) );
 
77
 
 
78
    QFont nameFont( option.font );
 
79
    const QFontMetrics nameFM( nameFont );
 
80
    const QString nameStr( tor.name( ) );
 
81
    const QSize nameSize( nameFM.size( 0, nameStr ) );
 
82
 
 
83
    QFont statusFont( option.font );
 
84
    statusFont.setPointSize( int( option.font.pointSize( ) * 0.85 ) );
 
85
    const QFontMetrics statusFM( statusFont );
 
86
    const QString statusStr( shortStatusString( tor ) );
 
87
    const QSize statusSize( statusFM.size( 0, statusStr ) );
 
88
 
 
89
    painter->save( );
 
90
 
 
91
    if (option.state & QStyle::State_Selected) {
 
92
        QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
 
93
                                  ? QPalette::Normal : QPalette::Disabled;
 
94
        if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
 
95
            cg = QPalette::Inactive;
 
96
 
 
97
        painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
 
98
    }
 
99
 
 
100
    QIcon::Mode im;
 
101
    if( isPaused || !(option.state & QStyle::State_Enabled ) ) im = QIcon::Disabled;
 
102
    else if( option.state & QStyle::State_Selected ) im = QIcon::Selected;
 
103
    else im = QIcon::Normal;
 
104
 
 
105
    QIcon::State qs;
 
106
    if( isPaused ) qs = QIcon::Off;
 
107
    else qs = QIcon::On;
 
108
 
 
109
    QPalette::ColorGroup cg = QPalette::Normal;
 
110
    if( isPaused || !(option.state & QStyle::State_Enabled ) ) cg = QPalette::Disabled;
 
111
    if( cg == QPalette::Normal && !(option.state & QStyle::State_Active ) ) cg = QPalette::Inactive;
 
112
 
 
113
    QPalette::ColorRole cr;
 
114
    if( option.state & QStyle::State_Selected ) cr = QPalette::HighlightedText;
 
115
    else cr = QPalette::Text;
 
116
 
 
117
    // layout
 
118
    const QSize m( margin( *style ) );
 
119
    QRect fillArea( option.rect );
 
120
    fillArea.adjust( m.width(), m.height(), -m.width(), -m.height() );
 
121
    const QRect iconArea( fillArea.x( ),
 
122
                          fillArea.y( ) + ( fillArea.height( ) - iconSize ) / 2,
 
123
                          iconSize,
 
124
                          iconSize );
 
125
    const QRect statusArea( fillArea.width( ) - statusSize.width( ),
 
126
                            fillArea.top( ) + ((nameSize.height()-statusSize.height())/2),
 
127
                            statusSize.width( ),
 
128
                            statusSize.height( ) );
 
129
    const QRect nameArea( iconArea.x( ) + iconArea.width( ) + GUI_PAD,
 
130
                          fillArea.y( ),
 
131
                          fillArea.width( ) - statusArea.width( ) - (GUI_PAD*2) - iconArea.width( ),
 
132
                          nameSize.height( ) );
 
133
    const QRect barArea( nameArea.left( ),
 
134
                         nameArea.bottom( ),
 
135
                         statusArea.right( ) - nameArea.left( ),
 
136
                         BAR_HEIGHT );               
 
137
                           
 
138
    // render
 
139
    if( tor.hasError( ) )
 
140
        painter->setPen( QColor( "red" ) );
 
141
    else
 
142
        painter->setPen( option.palette.color( cg, cr ) );
 
143
    tor.getMimeTypeIcon().paint( painter, iconArea, Qt::AlignCenter, im, qs );
 
144
    painter->setFont( nameFont );
 
145
    painter->drawText( nameArea, 0, nameFM.elidedText( nameStr, Qt::ElideRight, nameArea.width( ) ) );
 
146
    painter->setFont( statusFont );
 
147
    painter->drawText( statusArea, 0, statusStr );
 
148
    myProgressBarStyle->rect = barArea;
 
149
    myProgressBarStyle->direction = option.direction;
 
150
    myProgressBarStyle->palette = option.palette;
 
151
    myProgressBarStyle->palette.setCurrentColorGroup( QPalette::Disabled );
 
152
    myProgressBarStyle->state = QStyle::State_Off;
 
153
    myProgressBarStyle->progress = int(myProgressBarStyle->minimum + ((tor.percentDone() * (myProgressBarStyle->maximum - myProgressBarStyle->minimum))));
 
154
    style->drawControl( QStyle::CE_ProgressBar, myProgressBarStyle, painter );
 
155
 
 
156
    painter->restore( );
 
157
}