1/* 2 * This file Copyright (C) Mnemosyne LLC 3 * 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. 7 * 8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 9 * 10 * $Id: torrent-delegate-min.cc 13244 2012-03-04 13:15:43Z jordan $ 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 <libtransmission/transmission.h> 27#include <libtransmission/utils.h> 28 29#include "torrent.h" 30#include "torrent-delegate-min.h" 31#include "torrent-model.h" 32 33enum 34{ 35 GUI_PAD = 6, 36 BAR_WIDTH = 50, 37 BAR_HEIGHT = 12, 38 LINE_SPACING = 4 39}; 40 41/*** 42**** 43**** +---------+-----------------------------------------------+ 44**** | Icon | Title shortStatusString [Progressbar] | 45**** +-------- +-----------------------------------------------+ 46**** 47***/ 48 49QSize 50TorrentDelegateMin :: sizeHint( const QStyleOptionViewItem& option, const Torrent& tor ) const 51{ 52 const QStyle* style( QApplication::style( ) ); 53 static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) ); 54 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 ); 60 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 ); 66 67 const QSize m( margin( *style ) ); 68 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 ) ); 73} 74 75void 76TorrentDelegateMin :: drawTorrent( QPainter * painter, const QStyleOptionViewItem& option, const Torrent& tor ) const 77{ 78 const bool isPaused( tor.isPaused( ) ); 79 const QStyle * style( QApplication::style( ) ); 80 static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) ); 81 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( ) ); 86 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 ) ); 92 93 painter->save( ); 94 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; 100 101 painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight)); 102 } 103 104 QIcon::Mode im; 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; 108 109 QIcon::State qs; 110 if( isPaused ) qs = QIcon::Off; 111 else qs = QIcon::On; 112 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; 116 117 QPalette::ColorRole cr; 118 if( option.state & QStyle::State_Selected ) cr = QPalette::HighlightedText; 119 else cr = QPalette::Text; 120 121 QStyle::State progressBarState( option.state ); 122 if( isPaused ) progressBarState = QStyle::State_None; 123 progressBarState |= QStyle::State_Small; 124 125 // layout 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, 131 iconSize, 132 iconSize ); 133 const QRect barArea( fillArea.x( ) + fillArea.width( ) - BAR_WIDTH, 134 fillArea.y( ) + ( fillArea.height( ) - BAR_HEIGHT ) / 2, 135 BAR_WIDTH, 136 BAR_HEIGHT ); 137 const QRect statusArea( barArea.x( ) - GUI_PAD - statusSize.width( ), 138 fillArea.y( ) + ( fillArea.height( ) - statusSize.height( ) ) / 2, 139 fillArea.width( ), 140 fillArea.height( ) ); 141 const QRect nameArea( iconArea.x( ) + iconArea.width( ) + GUI_PAD, 142 fillArea.y( ), 143 statusArea.x( ) - ( iconArea.x( ) + iconArea.width( ) + GUI_PAD * 2 ), 144 fillArea.height( ) ); 145 146 // render 147 if( tor.hasError( ) ) 148 painter->setPen( QColor( "red" ) ); 149 else 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 ); 161 } 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 ); 166 } 167 myProgressBarStyle->state = progressBarState; 168 char buf[32]; 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 ); 175 176 painter->restore( ); 177} 178