2 * This file Copyright (C) Mnemosyne LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 * $Id: torrent-delegate-min.cc 13244 2012-03-04 13:15:43Z jordan $
15 #include <QApplication>
18 #include <QFontMetrics>
20 #include <QModelIndex>
23 #include <QPixmapCache>
24 #include <QStyleOptionProgressBarV2>
26 #include <libtransmission/transmission.h>
27 #include <libtransmission/utils.h>
30 #include "torrent-delegate-min.h"
31 #include "torrent-model.h"
43 **** +---------+-----------------------------------------------+
44 **** | Icon | Title shortStatusString [Progressbar] |
45 **** +-------- +-----------------------------------------------+
50 TorrentDelegateMin :: sizeHint( const QStyleOptionViewItem
& option
, const Torrent
& tor
) const
52 const QStyle
* style( QApplication::style( ) );
53 static const int iconSize( style
->pixelMetric( QStyle :: PM_SmallIconSize
) );
55 QFont
nameFont( option
.font
);
56 const QFontMetrics
nameFM( nameFont
);
57 const bool isMagnet( !tor
.hasMetadata( ) );
58 const QString nameStr
= (isMagnet
? progressString( tor
) : tor
.name( ) );
59 const int nameWidth
= nameFM
.width( nameStr
);
61 QFont
statusFont( option
.font
);
62 statusFont
.setPointSize( int( option
.font
.pointSize( ) * 0.85 ) );
63 const QFontMetrics
statusFM( statusFont
);
64 const QString
statusStr( shortStatusString( tor
) );
65 const int statusWidth
= statusFM
.width( statusStr
);
67 const QSize
m( margin( *style
) );
69 return QSize( m
.width()*2 + iconSize
+ GUI_PAD
+ nameWidth
70 + GUI_PAD
+ statusWidth
71 + GUI_PAD
+ BAR_WIDTH
,
72 m
.height()*2 + std::max( nameFM
.height(), (int)BAR_HEIGHT
) );
76 TorrentDelegateMin :: drawTorrent( QPainter
* painter
, const QStyleOptionViewItem
& option
, const Torrent
& tor
) const
78 const bool isPaused( tor
.isPaused( ) );
79 const QStyle
* style( QApplication::style( ) );
80 static const int iconSize( style
->pixelMetric( QStyle :: PM_SmallIconSize
) );
82 QFont
nameFont( option
.font
);
83 const QFontMetrics
nameFM( nameFont
);
84 const bool isMagnet( !tor
.hasMetadata( ) );
85 const QString nameStr
= (isMagnet
? progressString( tor
) : tor
.name( ) );
87 QFont
statusFont( option
.font
);
88 statusFont
.setPointSize( int( option
.font
.pointSize( ) * 0.85 ) );
89 const QFontMetrics
statusFM( statusFont
);
90 const QString
statusStr( shortStatusString( tor
) );
91 const QSize
statusSize( statusFM
.size( 0, statusStr
) );
95 if (option
.state
& QStyle::State_Selected
) {
96 QPalette::ColorGroup cg
= option
.state
& QStyle::State_Enabled
97 ? QPalette::Normal
: QPalette::Disabled
;
98 if (cg
== QPalette::Normal
&& !(option
.state
& QStyle::State_Active
))
99 cg
= QPalette::Inactive
;
101 painter
->fillRect(option
.rect
, option
.palette
.brush(cg
, QPalette::Highlight
));
105 if( isPaused
|| !(option
.state
& QStyle::State_Enabled
) ) im
= QIcon::Disabled
;
106 else if( option
.state
& QStyle::State_Selected
) im
= QIcon::Selected
;
107 else im
= QIcon::Normal
;
110 if( isPaused
) qs
= QIcon::Off
;
113 QPalette::ColorGroup cg
= QPalette::Normal
;
114 if( isPaused
|| !(option
.state
& QStyle::State_Enabled
) ) cg
= QPalette::Disabled
;
115 if( cg
== QPalette::Normal
&& !(option
.state
& QStyle::State_Active
) ) cg
= QPalette::Inactive
;
117 QPalette::ColorRole cr
;
118 if( option
.state
& QStyle::State_Selected
) cr
= QPalette::HighlightedText
;
119 else cr
= QPalette::Text
;
121 QStyle::State
progressBarState( option
.state
);
122 if( isPaused
) progressBarState
= QStyle::State_None
;
123 progressBarState
|= QStyle::State_Small
;
126 const QSize
m( margin( *style
) );
127 QRect
fillArea( option
.rect
);
128 fillArea
.adjust( m
.width(), m
.height(), -m
.width(), -m
.height() );
129 const QRect
iconArea( fillArea
.x( ),
130 fillArea
.y( ) + ( fillArea
.height( ) - iconSize
) / 2,
133 const QRect
barArea( fillArea
.x( ) + fillArea
.width( ) - BAR_WIDTH
,
134 fillArea
.y( ) + ( fillArea
.height( ) - BAR_HEIGHT
) / 2,
137 const QRect
statusArea( barArea
.x( ) - GUI_PAD
- statusSize
.width( ),
138 fillArea
.y( ) + ( fillArea
.height( ) - statusSize
.height( ) ) / 2,
140 fillArea
.height( ) );
141 const QRect
nameArea( iconArea
.x( ) + iconArea
.width( ) + GUI_PAD
,
143 statusArea
.x( ) - ( iconArea
.x( ) + iconArea
.width( ) + GUI_PAD
* 2 ),
144 fillArea
.height( ) );
147 if( tor
.hasError( ) )
148 painter
->setPen( QColor( "red" ) );
150 painter
->setPen( option
.palette
.color( cg
, cr
) );
151 tor
.getMimeTypeIcon().paint( painter
, iconArea
, Qt::AlignCenter
, im
, qs
);
152 painter
->setFont( nameFont
);
153 painter
->drawText( nameArea
, 0, nameFM
.elidedText( nameStr
, Qt::ElideRight
, nameArea
.width( ) ) );
154 painter
->setFont( statusFont
);
155 painter
->drawText( statusArea
, 0, statusStr
);
156 myProgressBarStyle
->rect
= barArea
;
157 if ( tor
.isDownloading() ) {
158 myProgressBarStyle
->palette
.setBrush( QPalette::Highlight
, blueBrush
);
159 myProgressBarStyle
->palette
.setColor( QPalette::Base
, blueBack
);
160 myProgressBarStyle
->palette
.setColor( QPalette::Background
, blueBack
);
162 else if ( tor
.isSeeding() ) {
163 myProgressBarStyle
->palette
.setBrush( QPalette::Highlight
, greenBrush
);
164 myProgressBarStyle
->palette
.setColor( QPalette::Base
, greenBack
);
165 myProgressBarStyle
->palette
.setColor( QPalette::Background
, greenBack
);
167 myProgressBarStyle
->state
= progressBarState
;
169 tr_snprintf( buf
, sizeof( buf
), "%d%%", (int)tr_truncd( 100.0 * tor
.percentDone( ), 0 ) );
170 myProgressBarStyle
->text
= buf
;
171 myProgressBarStyle
->textVisible
= true;
172 myProgressBarStyle
->textAlignment
= Qt::AlignCenter
;
173 setProgressBarPercentDone( option
, tor
);
174 style
->drawControl( QStyle::CE_ProgressBar
, myProgressBarStyle
, painter
);