2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2009-05-31 19:33:48 +00:00
|
|
|
* $Id$
|
2009-04-09 18:55:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QBrush>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QPixmapCache>
|
|
|
|
#include <QStyleOptionProgressBarV2>
|
|
|
|
|
2010-07-25 22:14:13 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#include "torrent-delegate-min.h"
|
|
|
|
#include "torrent-model.h"
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
GUI_PAD = 6,
|
2010-07-24 02:43:06 +00:00
|
|
|
BAR_WIDTH = 50,
|
2009-04-09 18:55:47 +00:00
|
|
|
BAR_HEIGHT = 12,
|
|
|
|
LINE_SPACING = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** +---------+-----------------------------------------------+
|
2010-07-24 02:43:06 +00:00
|
|
|
**** | Icon | Title shortStatusString [Progressbar] |
|
2009-04-09 18:55:47 +00:00
|
|
|
**** +-------- +-----------------------------------------------+
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
QSize
|
|
|
|
TorrentDelegateMin :: sizeHint( const QStyleOptionViewItem& option, const Torrent& tor ) const
|
|
|
|
{
|
|
|
|
const QStyle* style( QApplication::style( ) );
|
|
|
|
static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) );
|
|
|
|
|
|
|
|
QFont nameFont( option.font );
|
|
|
|
const QFontMetrics nameFM( nameFont );
|
2010-06-16 03:02:17 +00:00
|
|
|
const bool isMagnet( !tor.hasMetadata( ) );
|
|
|
|
const QString nameStr = (isMagnet ? progressString( tor ) : tor.name( ) );
|
2010-08-02 18:31:27 +00:00
|
|
|
const int nameWidth = nameFM.width( nameStr );
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
QFont statusFont( option.font );
|
|
|
|
statusFont.setPointSize( int( option.font.pointSize( ) * 0.85 ) );
|
|
|
|
const QFontMetrics statusFM( statusFont );
|
|
|
|
const QString statusStr( shortStatusString( tor ) );
|
2010-08-02 18:31:27 +00:00
|
|
|
const int statusWidth = statusFM.width( statusStr );
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
const QSize m( margin( *style ) );
|
|
|
|
|
2010-08-02 18:31:27 +00:00
|
|
|
return QSize( m.width()*2 + iconSize + GUI_PAD + nameWidth
|
|
|
|
+ GUI_PAD + statusWidth
|
2010-07-24 02:43:06 +00:00
|
|
|
+ GUI_PAD + BAR_WIDTH,
|
2010-08-02 18:31:27 +00:00
|
|
|
m.height()*2 + std::max( nameFM.height(), (int)BAR_HEIGHT ) );
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TorrentDelegateMin :: drawTorrent( QPainter * painter, const QStyleOptionViewItem& option, const Torrent& tor ) const
|
|
|
|
{
|
|
|
|
const bool isPaused( tor.isPaused( ) );
|
|
|
|
const QStyle * style( QApplication::style( ) );
|
|
|
|
static const int iconSize( style->pixelMetric( QStyle :: PM_SmallIconSize ) );
|
|
|
|
|
|
|
|
QFont nameFont( option.font );
|
|
|
|
const QFontMetrics nameFM( nameFont );
|
2010-06-16 03:02:17 +00:00
|
|
|
const bool isMagnet( !tor.hasMetadata( ) );
|
|
|
|
const QString nameStr = (isMagnet ? progressString( tor ) : tor.name( ) );
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
QFont statusFont( option.font );
|
|
|
|
statusFont.setPointSize( int( option.font.pointSize( ) * 0.85 ) );
|
|
|
|
const QFontMetrics statusFM( statusFont );
|
|
|
|
const QString statusStr( shortStatusString( tor ) );
|
|
|
|
const QSize statusSize( statusFM.size( 0, statusStr ) );
|
|
|
|
|
|
|
|
painter->save( );
|
|
|
|
|
|
|
|
if (option.state & QStyle::State_Selected) {
|
|
|
|
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
|
|
|
|
? QPalette::Normal : QPalette::Disabled;
|
|
|
|
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
|
|
|
|
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon::Mode im;
|
|
|
|
if( isPaused || !(option.state & QStyle::State_Enabled ) ) im = QIcon::Disabled;
|
|
|
|
else if( option.state & QStyle::State_Selected ) im = QIcon::Selected;
|
|
|
|
else im = QIcon::Normal;
|
|
|
|
|
|
|
|
QIcon::State qs;
|
|
|
|
if( isPaused ) qs = QIcon::Off;
|
|
|
|
else qs = QIcon::On;
|
|
|
|
|
|
|
|
QPalette::ColorGroup cg = QPalette::Normal;
|
|
|
|
if( isPaused || !(option.state & QStyle::State_Enabled ) ) cg = QPalette::Disabled;
|
|
|
|
if( cg == QPalette::Normal && !(option.state & QStyle::State_Active ) ) cg = QPalette::Inactive;
|
|
|
|
|
|
|
|
QPalette::ColorRole cr;
|
|
|
|
if( option.state & QStyle::State_Selected ) cr = QPalette::HighlightedText;
|
|
|
|
else cr = QPalette::Text;
|
|
|
|
|
2009-05-04 16:38:07 +00:00
|
|
|
QStyle::State progressBarState( option.state );
|
|
|
|
if( isPaused ) progressBarState = QStyle::State_None;
|
|
|
|
progressBarState |= QStyle::State_Small;
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
// layout
|
|
|
|
const QSize m( margin( *style ) );
|
|
|
|
QRect fillArea( option.rect );
|
|
|
|
fillArea.adjust( m.width(), m.height(), -m.width(), -m.height() );
|
|
|
|
const QRect iconArea( fillArea.x( ),
|
|
|
|
fillArea.y( ) + ( fillArea.height( ) - iconSize ) / 2,
|
|
|
|
iconSize,
|
|
|
|
iconSize );
|
2010-07-24 02:43:06 +00:00
|
|
|
const QRect barArea( fillArea.x( ) + fillArea.width( ) - BAR_WIDTH,
|
|
|
|
fillArea.y( ) + ( fillArea.height( ) - BAR_HEIGHT ) / 2,
|
|
|
|
BAR_WIDTH,
|
|
|
|
BAR_HEIGHT );
|
|
|
|
const QRect statusArea( barArea.x( ) - GUI_PAD - statusSize.width( ),
|
2010-07-25 22:14:13 +00:00
|
|
|
fillArea.y( ) + ( fillArea.height( ) - statusSize.height( ) ) / 2,
|
2010-07-24 02:43:06 +00:00
|
|
|
fillArea.width( ),
|
|
|
|
fillArea.height( ) );
|
2009-04-09 18:55:47 +00:00
|
|
|
const QRect nameArea( iconArea.x( ) + iconArea.width( ) + GUI_PAD,
|
|
|
|
fillArea.y( ),
|
2010-07-24 02:43:06 +00:00
|
|
|
statusArea.x( ) - ( iconArea.x( ) + iconArea.width( ) + GUI_PAD * 2 ),
|
|
|
|
fillArea.height( ) );
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
// render
|
|
|
|
if( tor.hasError( ) )
|
|
|
|
painter->setPen( QColor( "red" ) );
|
|
|
|
else
|
|
|
|
painter->setPen( option.palette.color( cg, cr ) );
|
|
|
|
tor.getMimeTypeIcon().paint( painter, iconArea, Qt::AlignCenter, im, qs );
|
|
|
|
painter->setFont( nameFont );
|
|
|
|
painter->drawText( nameArea, 0, nameFM.elidedText( nameStr, Qt::ElideRight, nameArea.width( ) ) );
|
|
|
|
painter->setFont( statusFont );
|
|
|
|
painter->drawText( statusArea, 0, statusStr );
|
|
|
|
myProgressBarStyle->rect = barArea;
|
|
|
|
myProgressBarStyle->direction = option.direction;
|
|
|
|
myProgressBarStyle->palette = option.palette;
|
2009-05-04 18:22:18 +00:00
|
|
|
myProgressBarStyle->palette.setCurrentColorGroup( cg );
|
2009-05-04 16:38:07 +00:00
|
|
|
myProgressBarStyle->state = progressBarState;
|
2010-07-25 22:14:13 +00:00
|
|
|
char buf[32];
|
|
|
|
tr_snprintf( buf, sizeof( buf ), "%d%%", (int)tr_truncd( 100.0 * tor.percentDone( ), 0 ) );
|
|
|
|
myProgressBarStyle->text = buf;
|
|
|
|
myProgressBarStyle->textVisible = true;
|
2010-06-16 03:02:17 +00:00
|
|
|
myProgressBarStyle->progress = int(myProgressBarStyle->minimum + (((isMagnet ? tor.metadataPercentDone() : tor.percentDone()) * (myProgressBarStyle->maximum - myProgressBarStyle->minimum))));
|
2009-04-09 18:55:47 +00:00
|
|
|
style->drawControl( QStyle::CE_ProgressBar, myProgressBarStyle, painter );
|
|
|
|
|
|
|
|
painter->restore( );
|
|
|
|
}
|