1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
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: torrent-delegate.cc 10822 2010-06-22 22:30:58Z Longinus00 $ |
---|
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.h" |
---|
28 | #include "torrent-model.h" |
---|
29 | #include "utils.h" |
---|
30 | |
---|
31 | enum |
---|
32 | { |
---|
33 | GUI_PAD = 6, |
---|
34 | BAR_HEIGHT = 12 |
---|
35 | }; |
---|
36 | |
---|
37 | TorrentDelegate :: TorrentDelegate( QObject * parent ): |
---|
38 | QItemDelegate( parent ), |
---|
39 | myProgressBarStyle( new QStyleOptionProgressBarV2 ) |
---|
40 | { |
---|
41 | myProgressBarStyle->minimum = 0; |
---|
42 | myProgressBarStyle->maximum = 1000; |
---|
43 | } |
---|
44 | |
---|
45 | TorrentDelegate :: ~TorrentDelegate( ) |
---|
46 | { |
---|
47 | delete myProgressBarStyle; |
---|
48 | } |
---|
49 | |
---|
50 | /*** |
---|
51 | **** |
---|
52 | ***/ |
---|
53 | |
---|
54 | QSize |
---|
55 | TorrentDelegate :: margin( const QStyle& style ) const |
---|
56 | { |
---|
57 | Q_UNUSED( style ); |
---|
58 | |
---|
59 | return QSize( 4, 4 ); |
---|
60 | } |
---|
61 | |
---|
62 | QString |
---|
63 | TorrentDelegate :: progressString( const Torrent& tor ) const |
---|
64 | { |
---|
65 | const bool isMagnet( !tor.hasMetadata( ) ); |
---|
66 | const bool isDone( tor.isDone( ) ); |
---|
67 | const bool isSeed( tor.isSeed( ) ); |
---|
68 | const uint64_t haveTotal( tor.haveTotal( ) ); |
---|
69 | QString str; |
---|
70 | double seedRatio; |
---|
71 | const bool hasSeedRatio( tor.getSeedRatio( seedRatio ) ); |
---|
72 | |
---|
73 | if( isMagnet ) // magnet link with no metadata |
---|
74 | { |
---|
75 | /* %1 is the percentage of torrent metadata downloaded */ |
---|
76 | str = tr( "Magnetized transfer - retrieving metadata (%1%)" ) |
---|
77 | .arg( Utils::percentToString( tor.metadataPercentDone() * 100.0 ) ); |
---|
78 | } |
---|
79 | else if( !isDone ) // downloading |
---|
80 | { |
---|
81 | /* %1 is how much we've got, |
---|
82 | %2 is how much we'll have when done, |
---|
83 | %3 is a percentage of the two */ |
---|
84 | str = tr( "%1 of %2 (%3%)" ).arg( Utils::sizeToString( haveTotal ) ) |
---|
85 | .arg( Utils::sizeToString( tor.sizeWhenDone( ) ) ) |
---|
86 | .arg( Utils::percentToString( tor.percentDone( ) * 100.0 ) ); |
---|
87 | } |
---|
88 | else if( !isSeed ) // partial seed |
---|
89 | { |
---|
90 | if( hasSeedRatio ) |
---|
91 | { |
---|
92 | /* %1 is how much we've got, |
---|
93 | %2 is the torrent's total size, |
---|
94 | %3 is a percentage of the two, |
---|
95 | %4 is how much we've uploaded, |
---|
96 | %5 is our upload-to-download ratio |
---|
97 | %6 is the ratio we want to reach before we stop uploading */ |
---|
98 | str = tr( "%1 of %2 (%3%), uploaded %4 (Ratio: %5 Goal: %6)" ) |
---|
99 | .arg( Utils::sizeToString( haveTotal ) ) |
---|
100 | .arg( Utils::sizeToString( tor.totalSize( ) ) ) |
---|
101 | .arg( Utils::percentToString( tor.percentComplete( ) * 100.0 ) ) |
---|
102 | .arg( Utils::sizeToString( tor.uploadedEver( ) ) ) |
---|
103 | .arg( Utils::ratioToString( tor.ratio( ) ) ) |
---|
104 | .arg( Utils::ratioToString( seedRatio ) ); |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | /* %1 is how much we've got, |
---|
109 | %2 is the torrent's total size, |
---|
110 | %3 is a percentage of the two, |
---|
111 | %4 is how much we've uploaded, |
---|
112 | %5 is our upload-to-download ratio */ |
---|
113 | str = tr( "%1 of %2 (%3%), uploaded %4 (Ratio: %5)" ) |
---|
114 | .arg( Utils::sizeToString( haveTotal ) ) |
---|
115 | .arg( Utils::sizeToString( tor.totalSize( ) ) ) |
---|
116 | .arg( Utils::percentToString( tor.percentComplete( ) * 100.0 ) ) |
---|
117 | .arg( Utils::sizeToString( tor.uploadedEver( ) ) ) |
---|
118 | .arg( Utils::ratioToString( tor.ratio( ) ) ); |
---|
119 | } |
---|
120 | } |
---|
121 | else // seeding |
---|
122 | { |
---|
123 | if( hasSeedRatio ) |
---|
124 | { |
---|
125 | /* %1 is the torrent's total size, |
---|
126 | %2 is how much we've uploaded, |
---|
127 | %3 is our upload-to-download ratio, |
---|
128 | %4 is the ratio we want to reach before we stop uploading */ |
---|
129 | str = tr( "%1, uploaded %2 (Ratio: %3 Goal %4)" ) |
---|
130 | .arg( Utils::sizeToString( haveTotal ) ) |
---|
131 | .arg( Utils::sizeToString( tor.uploadedEver( ) ) ) |
---|
132 | .arg( Utils::ratioToString( tor.ratio( ) ) ) |
---|
133 | .arg( Utils::ratioToString( seedRatio ) ); |
---|
134 | } |
---|
135 | else /* seeding w/o a ratio */ |
---|
136 | { |
---|
137 | /* %1 is the torrent's total size, |
---|
138 | %2 is how much we've uploaded, |
---|
139 | %3 is our upload-to-download ratio */ |
---|
140 | str = tr( "%1, uploaded %2 (Ratio: %3)" ) |
---|
141 | .arg( Utils::sizeToString( haveTotal ) ) |
---|
142 | .arg( Utils::sizeToString( tor.uploadedEver( ) ) ) |
---|
143 | .arg( Utils::ratioToString( tor.ratio( ) ) ); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /* add time when downloading */ |
---|
148 | if( ( hasSeedRatio && tor.isSeeding( ) ) || tor.isDownloading( ) ) |
---|
149 | { |
---|
150 | str += tr( " - " ); |
---|
151 | if( tor.hasETA( ) ) |
---|
152 | str += tr( "%1 left" ).arg( Utils::timeToString( tor.getETA( ) ) ); |
---|
153 | else |
---|
154 | str += tr( "Remaining time unknown" ); |
---|
155 | } |
---|
156 | |
---|
157 | return str; |
---|
158 | } |
---|
159 | |
---|
160 | QString |
---|
161 | TorrentDelegate :: shortTransferString( const Torrent& tor ) const |
---|
162 | { |
---|
163 | const bool haveMeta( tor.hasMetadata( ) ); |
---|
164 | const bool haveDown( haveMeta && tor.peersWeAreDownloadingFrom( ) > 0 ); |
---|
165 | const bool haveUp( haveMeta && tor.peersWeAreUploadingTo( ) > 0 ); |
---|
166 | QString downStr, upStr, str; |
---|
167 | |
---|
168 | if( haveDown ) |
---|
169 | downStr = Utils :: speedToString( tor.downloadSpeed( ) ); |
---|
170 | if( haveUp ) |
---|
171 | upStr = Utils :: speedToString( tor.uploadSpeed( ) ); |
---|
172 | |
---|
173 | if( haveDown && haveUp ) |
---|
174 | str = tr( "Down: %1, Up: %2" ).arg(downStr).arg(upStr); |
---|
175 | else if( haveDown ) |
---|
176 | str = tr( "Down: %1" ).arg( downStr ); |
---|
177 | else if( haveUp ) |
---|
178 | str = tr( "Up: %1" ).arg( upStr ); |
---|
179 | else if( tor.hasMetadata( ) ) |
---|
180 | str = tr( "Idle" ); |
---|
181 | |
---|
182 | return str; |
---|
183 | } |
---|
184 | |
---|
185 | QString |
---|
186 | TorrentDelegate :: shortStatusString( const Torrent& tor ) const |
---|
187 | { |
---|
188 | QString str; |
---|
189 | |
---|
190 | switch( tor.getActivity( ) ) |
---|
191 | { |
---|
192 | case TR_STATUS_CHECK: |
---|
193 | str = tr( "Verifying local data (%1% tested)" ).arg( Utils::percentToString( tor.getVerifyProgress()*100.0 ) ); |
---|
194 | break; |
---|
195 | |
---|
196 | case TR_STATUS_DOWNLOAD: |
---|
197 | case TR_STATUS_SEED: |
---|
198 | if( !tor.isDownloading( ) ) |
---|
199 | str = tr( "Ratio: %1, " ).arg( Utils::ratioToString( tor.ratio( ) ) ); |
---|
200 | str += shortTransferString( tor ); |
---|
201 | break; |
---|
202 | |
---|
203 | default: |
---|
204 | str = tor.activityString( ); |
---|
205 | break; |
---|
206 | } |
---|
207 | |
---|
208 | return str; |
---|
209 | } |
---|
210 | |
---|
211 | QString |
---|
212 | TorrentDelegate :: statusString( const Torrent& tor ) const |
---|
213 | { |
---|
214 | QString str; |
---|
215 | |
---|
216 | if( tor.hasError( ) ) |
---|
217 | { |
---|
218 | str = tor.getError( ); |
---|
219 | } |
---|
220 | else switch( tor.getActivity( ) ) |
---|
221 | { |
---|
222 | case TR_STATUS_STOPPED: |
---|
223 | case TR_STATUS_CHECK_WAIT: |
---|
224 | case TR_STATUS_CHECK: |
---|
225 | str = shortStatusString( tor ); |
---|
226 | break; |
---|
227 | |
---|
228 | case TR_STATUS_DOWNLOAD: |
---|
229 | if( tor.hasMetadata( ) ) |
---|
230 | str = tr( "Downloading from %1 of %n connected peer(s)", 0, tor.connectedPeersAndWebseeds( ) ) |
---|
231 | .arg( tor.peersWeAreDownloadingFrom( ) ); |
---|
232 | else |
---|
233 | str = tr( "Downloading metadata from %n peer(s) (%1% done)", 0, tor.peersWeAreDownloadingFrom( ) ) |
---|
234 | .arg( Utils::percentToString( 100.0 * tor.metadataPercentDone( ) ) ); |
---|
235 | break; |
---|
236 | |
---|
237 | case TR_STATUS_SEED: |
---|
238 | str = tr( "Seeding to %1 of %n connected peer(s)", 0, tor.connectedPeers( ) ) |
---|
239 | .arg( tor.peersWeAreUploadingTo( ) ); |
---|
240 | break; |
---|
241 | |
---|
242 | default: |
---|
243 | str = "Error"; |
---|
244 | break; |
---|
245 | } |
---|
246 | |
---|
247 | if( tor.isReadyToTransfer( ) ) { |
---|
248 | QString s = shortTransferString( tor ); |
---|
249 | if( !s.isEmpty( ) ) |
---|
250 | str += tr( " - " ) + s; |
---|
251 | } |
---|
252 | |
---|
253 | return str; |
---|
254 | } |
---|
255 | |
---|
256 | /*** |
---|
257 | **** |
---|
258 | ***/ |
---|
259 | |
---|
260 | namespace |
---|
261 | { |
---|
262 | int MAX3( int a, int b, int c ) |
---|
263 | { |
---|
264 | const int ab( a > b ? a : b ); |
---|
265 | return ab > c ? ab : c; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | QSize |
---|
270 | TorrentDelegate :: sizeHint( const QStyleOptionViewItem& option, const Torrent& tor ) const |
---|
271 | { |
---|
272 | const QStyle* style( QApplication::style( ) ); |
---|
273 | static const int iconSize( style->pixelMetric( QStyle::PM_MessageBoxIconSize ) ); |
---|
274 | |
---|
275 | QFont nameFont( option.font ); |
---|
276 | nameFont.setWeight( QFont::Bold ); |
---|
277 | const QFontMetrics nameFM( nameFont ); |
---|
278 | const QString nameStr( tor.name( ) ); |
---|
279 | const QSize nameSize( nameFM.size( 0, nameStr ) ); |
---|
280 | QFont statusFont( option.font ); |
---|
281 | statusFont.setPointSize( int( option.font.pointSize( ) * 0.9 ) ); |
---|
282 | const QFontMetrics statusFM( statusFont ); |
---|
283 | const QString statusStr( statusString( tor ) ); |
---|
284 | const QSize statusSize( statusFM.size( 0, statusStr ) ); |
---|
285 | QFont progressFont( statusFont ); |
---|
286 | const QFontMetrics progressFM( progressFont ); |
---|
287 | const QString progressStr( progressString( tor ) ); |
---|
288 | const QSize progressSize( progressFM.size( 0, progressStr ) ); |
---|
289 | const QSize m( margin( *style ) ); |
---|
290 | return QSize( m.width()*2 + iconSize + GUI_PAD + MAX3( nameSize.width(), statusSize.width(), progressSize.width() ), |
---|
291 | //m.height()*3 + nameFM.lineSpacing() + statusFM.lineSpacing()*2 + progressFM.lineSpacing() ); |
---|
292 | m.height()*3 + nameFM.lineSpacing() + statusFM.lineSpacing() + BAR_HEIGHT + progressFM.lineSpacing() ); |
---|
293 | } |
---|
294 | |
---|
295 | QSize |
---|
296 | TorrentDelegate :: sizeHint( const QStyleOptionViewItem & option, |
---|
297 | const QModelIndex & index ) const |
---|
298 | { |
---|
299 | const Torrent * tor( index.model()->data( index, TorrentModel::TorrentRole ).value<const Torrent*>() ); |
---|
300 | return sizeHint( option, *tor ); |
---|
301 | } |
---|
302 | |
---|
303 | void |
---|
304 | TorrentDelegate :: paint( QPainter * painter, |
---|
305 | const QStyleOptionViewItem & option, |
---|
306 | const QModelIndex & index) const |
---|
307 | { |
---|
308 | const Torrent * tor( index.model()->data( index, TorrentModel::TorrentRole ).value<const Torrent*>() ); |
---|
309 | painter->save( ); |
---|
310 | painter->setClipRect( option.rect ); |
---|
311 | drawBackground( painter, option, index ); |
---|
312 | drawTorrent( painter, option, *tor ); |
---|
313 | drawFocus(painter, option, option.rect ); |
---|
314 | painter->restore( ); |
---|
315 | } |
---|
316 | |
---|
317 | void |
---|
318 | TorrentDelegate :: drawTorrent( QPainter * painter, const QStyleOptionViewItem& option, const Torrent& tor ) const |
---|
319 | { |
---|
320 | const QStyle * style( QApplication::style( ) ); |
---|
321 | static const int iconSize( style->pixelMetric( QStyle::PM_LargeIconSize ) ); |
---|
322 | QFont nameFont( option.font ); |
---|
323 | nameFont.setWeight( QFont::Bold ); |
---|
324 | const QFontMetrics nameFM( nameFont ); |
---|
325 | const QString nameStr( tor.name( ) ); |
---|
326 | const QSize nameSize( nameFM.size( 0, nameStr ) ); |
---|
327 | QFont statusFont( option.font ); |
---|
328 | statusFont.setPointSize( int( option.font.pointSize( ) * 0.9 ) ); |
---|
329 | const QFontMetrics statusFM( statusFont ); |
---|
330 | const QString statusStr( progressString( tor ) ); |
---|
331 | QFont progressFont( statusFont ); |
---|
332 | const QFontMetrics progressFM( progressFont ); |
---|
333 | const QString progressStr( statusString( tor ) ); |
---|
334 | const bool isPaused( tor.isPaused( ) ); |
---|
335 | |
---|
336 | painter->save( ); |
---|
337 | |
---|
338 | if (option.state & QStyle::State_Selected) { |
---|
339 | QPalette::ColorGroup cg = option.state & QStyle::State_Enabled |
---|
340 | ? QPalette::Normal : QPalette::Disabled; |
---|
341 | if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) |
---|
342 | cg = QPalette::Inactive; |
---|
343 | |
---|
344 | painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight)); |
---|
345 | } |
---|
346 | |
---|
347 | QIcon::Mode im; |
---|
348 | if( isPaused || !(option.state & QStyle::State_Enabled ) ) im = QIcon::Disabled; |
---|
349 | else if( option.state & QStyle::State_Selected ) im = QIcon::Selected; |
---|
350 | else im = QIcon::Normal; |
---|
351 | |
---|
352 | QIcon::State qs; |
---|
353 | if( isPaused ) qs = QIcon::Off; |
---|
354 | else qs = QIcon::On; |
---|
355 | |
---|
356 | QPalette::ColorGroup cg = QPalette::Normal; |
---|
357 | if( isPaused || !(option.state & QStyle::State_Enabled ) ) cg = QPalette::Disabled; |
---|
358 | if( cg == QPalette::Normal && !(option.state & QStyle::State_Active ) ) cg = QPalette::Inactive; |
---|
359 | |
---|
360 | QPalette::ColorRole cr; |
---|
361 | if( option.state & QStyle::State_Selected ) cr = QPalette::HighlightedText; |
---|
362 | else cr = QPalette::Text; |
---|
363 | |
---|
364 | QStyle::State progressBarState( option.state ); |
---|
365 | if( isPaused ) progressBarState = QStyle::State_None; |
---|
366 | progressBarState |= QStyle::State_Small; |
---|
367 | |
---|
368 | // layout |
---|
369 | const QSize m( margin( *style ) ); |
---|
370 | QRect fillArea( option.rect ); |
---|
371 | fillArea.adjust( m.width(), m.height(), -m.width(), -m.height() ); |
---|
372 | QRect iconArea( fillArea.x( ), fillArea.y( ) + ( fillArea.height( ) - iconSize ) / 2, iconSize, iconSize ); |
---|
373 | QRect nameArea( iconArea.x( ) + iconArea.width( ) + GUI_PAD, fillArea.y( ), |
---|
374 | fillArea.width( ) - GUI_PAD - iconArea.width( ), nameSize.height( ) ); |
---|
375 | QRect statusArea( nameArea ); |
---|
376 | statusArea.moveTop( nameArea.y( ) + nameFM.lineSpacing( ) ); |
---|
377 | statusArea.setHeight( nameSize.height( ) ); |
---|
378 | QRect barArea( statusArea ); |
---|
379 | barArea.setHeight( BAR_HEIGHT ); |
---|
380 | barArea.moveTop( statusArea.y( ) + statusFM.lineSpacing( ) ); |
---|
381 | QRect progArea( statusArea ); |
---|
382 | progArea.moveTop( barArea.y( ) + barArea.height( ) ); |
---|
383 | |
---|
384 | // render |
---|
385 | if( tor.hasError( ) ) |
---|
386 | painter->setPen( QColor( "red" ) ); |
---|
387 | else |
---|
388 | painter->setPen( option.palette.color( cg, cr ) ); |
---|
389 | tor.getMimeTypeIcon().paint( painter, iconArea, Qt::AlignCenter, im, qs ); |
---|
390 | painter->setFont( nameFont ); |
---|
391 | painter->drawText( nameArea, 0, nameFM.elidedText( nameStr, Qt::ElideRight, nameArea.width( ) ) ); |
---|
392 | painter->setFont( statusFont ); |
---|
393 | painter->drawText( statusArea, 0, statusFM.elidedText( statusStr, Qt::ElideRight, statusArea.width( ) ) ); |
---|
394 | painter->setFont( progressFont ); |
---|
395 | painter->drawText( progArea, 0, progressFM.elidedText( progressStr, Qt::ElideRight, progArea.width( ) ) ); |
---|
396 | const bool isMagnet( !tor.hasMetadata( ) ); |
---|
397 | myProgressBarStyle->rect = barArea; |
---|
398 | myProgressBarStyle->direction = option.direction; |
---|
399 | myProgressBarStyle->palette = option.palette; |
---|
400 | myProgressBarStyle->palette.setCurrentColorGroup( cg ); |
---|
401 | myProgressBarStyle->state = progressBarState; |
---|
402 | myProgressBarStyle->progress = int(myProgressBarStyle->minimum + (((isMagnet ? tor.metadataPercentDone() : tor.percentDone()) * (myProgressBarStyle->maximum - myProgressBarStyle->minimum)))); |
---|
403 | style->drawControl( QStyle::CE_ProgressBar, myProgressBarStyle, painter ); |
---|
404 | |
---|
405 | painter->restore( ); |
---|
406 | } |
---|