2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QPixmapCache>
|
2013-02-14 23:32:37 +00:00
|
|
|
#include <QStyleOptionProgressBar>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Formatter.h"
|
|
|
|
#include "Torrent.h"
|
|
|
|
#include "TorrentDelegate.h"
|
|
|
|
#include "TorrentModel.h"
|
|
|
|
#include "Utils.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
GUI_PAD = 6,
|
|
|
|
BAR_HEIGHT = 12
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
2014-12-12 23:05:10 +00:00
|
|
|
QColor TorrentDelegate::greenBrush;
|
|
|
|
QColor TorrentDelegate::blueBrush;
|
|
|
|
QColor TorrentDelegate::silverBrush;
|
|
|
|
QColor TorrentDelegate::greenBack;
|
|
|
|
QColor TorrentDelegate::blueBack;
|
|
|
|
QColor TorrentDelegate::silverBack;
|
|
|
|
|
2015-01-17 16:59:42 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
class ItemLayout
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
QString myNameText;
|
|
|
|
QString myStatusText;
|
|
|
|
QString myProgressText;
|
|
|
|
|
|
|
|
public:
|
|
|
|
QFont nameFont;
|
|
|
|
QFont statusFont;
|
|
|
|
QFont progressFont;
|
|
|
|
|
|
|
|
QRect iconRect;
|
|
|
|
QRect emblemRect;
|
|
|
|
QRect nameRect;
|
|
|
|
QRect statusRect;
|
|
|
|
QRect barRect;
|
|
|
|
QRect progressRect;
|
|
|
|
|
|
|
|
public:
|
2017-04-20 16:02:19 +00:00
|
|
|
ItemLayout(QString const& nameText, QString const& statusText, QString const& progressText, QIcon const& emblemIcon,
|
|
|
|
QFont const& baseFont, Qt::LayoutDirection direction, QPoint const& topLeft, int width);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QSize size() const
|
|
|
|
{
|
|
|
|
return (iconRect | nameRect | statusRect | barRect | progressRect).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString nameText() const
|
|
|
|
{
|
|
|
|
return elidedText(nameFont, myNameText, nameRect.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString statusText() const
|
|
|
|
{
|
|
|
|
return elidedText(statusFont, myStatusText, statusRect.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString progressText() const
|
|
|
|
{
|
|
|
|
return elidedText(progressFont, myProgressText, progressRect.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-04-20 16:02:19 +00:00
|
|
|
QString elidedText(QFont const& font, QString const& text, int width) const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return QFontMetrics(font).elidedText(text, Qt::ElideRight, width);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
ItemLayout::ItemLayout(QString const& nameText, QString const& statusText, QString const& progressText, QIcon const& emblemIcon,
|
|
|
|
QFont const& baseFont, Qt::LayoutDirection direction, QPoint const& topLeft, int width) :
|
2017-04-19 12:04:45 +00:00
|
|
|
myNameText(nameText),
|
|
|
|
myStatusText(statusText),
|
|
|
|
myProgressText(progressText),
|
|
|
|
nameFont(baseFont),
|
|
|
|
statusFont(baseFont),
|
|
|
|
progressFont(baseFont)
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QStyle const* style(qApp->style());
|
|
|
|
int const iconSize(style->pixelMetric(QStyle::PM_LargeIconSize));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
nameFont.setWeight(QFont::Bold);
|
2017-04-20 16:02:19 +00:00
|
|
|
QFontMetrics const nameFM(nameFont);
|
|
|
|
QSize const nameSize(nameFM.size(0, myNameText));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
statusFont.setPointSize(static_cast<int>(statusFont.pointSize() * 0.9));
|
2017-04-20 16:02:19 +00:00
|
|
|
QFontMetrics const statusFM(statusFont);
|
|
|
|
QSize const statusSize(statusFM.size(0, myStatusText));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
progressFont.setPointSize(static_cast<int>(progressFont.pointSize() * 0.9));
|
2017-04-20 16:02:19 +00:00
|
|
|
QFontMetrics const progressFM(progressFont);
|
|
|
|
QSize const progressSize(progressFM.size(0, myProgressText));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QRect baseRect(topLeft, QSize(width, 0));
|
|
|
|
Utils::narrowRect(baseRect, iconSize + GUI_PAD, 0, direction);
|
|
|
|
|
|
|
|
nameRect = baseRect.adjusted(0, 0, 0, nameSize.height());
|
|
|
|
statusRect = nameRect.adjusted(0, nameRect.height() + 1, 0, statusSize.height() + 1);
|
|
|
|
barRect = statusRect.adjusted(0, statusRect.height() + 1, 0, BAR_HEIGHT + 1);
|
|
|
|
progressRect = barRect.adjusted(0, barRect.height() + 1, 0, progressSize.height() + 1);
|
|
|
|
iconRect = style->alignedRect(direction, Qt::AlignLeft | Qt::AlignVCenter, QSize(iconSize, iconSize),
|
|
|
|
QRect(topLeft, QSize(width, progressRect.bottom() - nameRect.top())));
|
|
|
|
emblemRect = style->alignedRect(direction, Qt::AlignRight | Qt::AlignBottom,
|
|
|
|
emblemIcon.actualSize(iconRect.size() / 2, QIcon::Normal, QIcon::On), iconRect);
|
2015-01-17 16:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TorrentDelegate::TorrentDelegate(QObject* parent) :
|
|
|
|
QStyledItemDelegate(parent),
|
|
|
|
myProgressBarStyle(new QStyleOptionProgressBar)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myProgressBarStyle->minimum = 0;
|
|
|
|
myProgressBarStyle->maximum = 1000;
|
2012-03-04 13:15:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
greenBrush = QColor("forestgreen");
|
|
|
|
greenBack = QColor("darkseagreen");
|
2012-03-04 13:15:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
blueBrush = QColor("steelblue");
|
|
|
|
blueBack = QColor("lightgrey");
|
2013-02-14 23:32:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
silverBrush = QColor("silver");
|
|
|
|
silverBack = QColor("grey");
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
TorrentDelegate::~TorrentDelegate()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
delete myProgressBarStyle;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize TorrentDelegate::margin(QStyle const& style) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_UNUSED(style);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return QSize(4, 4);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString TorrentDelegate::progressString(Torrent const& tor)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isMagnet(!tor.hasMetadata());
|
|
|
|
bool const isDone(tor.isDone());
|
|
|
|
bool const isSeed(tor.isSeed());
|
|
|
|
uint64_t const haveTotal(tor.haveTotal());
|
2017-04-19 12:04:45 +00:00
|
|
|
QString str;
|
|
|
|
double seedRatio;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const hasSeedRatio(tor.getSeedRatio(seedRatio));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (isMagnet) // magnet link with no metadata
|
|
|
|
{
|
|
|
|
//: First part of torrent progress string;
|
|
|
|
//: %1 is the percentage of torrent metadata downloaded
|
|
|
|
str = tr("Magnetized transfer - retrieving metadata (%1%)").
|
|
|
|
arg(Formatter::percentToString(tor.metadataPercentDone() * 100.0));
|
|
|
|
}
|
|
|
|
else if (!isDone) // downloading
|
|
|
|
{
|
|
|
|
//: First part of torrent progress string;
|
|
|
|
//: %1 is how much we've got,
|
|
|
|
//: %2 is how much we'll have when done,
|
|
|
|
//: %3 is a percentage of the two
|
|
|
|
str = tr("%1 of %2 (%3%)").arg(Formatter::sizeToString(haveTotal)).arg(Formatter::sizeToString(tor.sizeWhenDone())).
|
|
|
|
arg(Formatter::percentToString(tor.percentDone() * 100.0));
|
|
|
|
}
|
|
|
|
else if (!isSeed) // partial seed
|
|
|
|
{
|
|
|
|
if (hasSeedRatio)
|
2010-04-01 06:08:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: First part of torrent progress string;
|
|
|
|
//: %1 is how much we've got,
|
|
|
|
//: %2 is the torrent's total size,
|
|
|
|
//: %3 is a percentage of the two,
|
|
|
|
//: %4 is how much we've uploaded,
|
|
|
|
//: %5 is our upload-to-download ratio,
|
|
|
|
//: %6 is the ratio we want to reach before we stop uploading
|
|
|
|
str = tr("%1 of %2 (%3%), uploaded %4 (Ratio: %5 Goal: %6)").arg(Formatter::sizeToString(haveTotal)).
|
|
|
|
arg(Formatter::sizeToString(tor.totalSize())).
|
|
|
|
arg(Formatter::percentToString(tor.percentComplete() * 100.0)).
|
|
|
|
arg(Formatter::sizeToString(tor.uploadedEver())).arg(Formatter::ratioToString(tor.ratio())).
|
|
|
|
arg(Formatter::ratioToString(seedRatio));
|
2010-04-01 06:08:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-18 02:09:44 +00:00
|
|
|
//: First part of torrent progress string;
|
|
|
|
//: %1 is how much we've got,
|
|
|
|
//: %2 is the torrent's total size,
|
|
|
|
//: %3 is a percentage of the two,
|
|
|
|
//: %4 is how much we've uploaded,
|
|
|
|
//: %5 is our upload-to-download ratio
|
2017-04-19 12:04:45 +00:00
|
|
|
str = tr("%1 of %2 (%3%), uploaded %4 (Ratio: %5)").arg(Formatter::sizeToString(haveTotal)).
|
|
|
|
arg(Formatter::sizeToString(tor.totalSize())).
|
|
|
|
arg(Formatter::percentToString(tor.percentComplete() * 100.0)).
|
|
|
|
arg(Formatter::sizeToString(tor.uploadedEver())).arg(Formatter::ratioToString(tor.ratio()));
|
2010-04-01 06:08:20 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else // seeding
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (hasSeedRatio)
|
2010-04-01 06:08:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: First part of torrent progress string;
|
|
|
|
//: %1 is the torrent's total size,
|
|
|
|
//: %2 is how much we've uploaded,
|
|
|
|
//: %3 is our upload-to-download ratio,
|
|
|
|
//: %4 is the ratio we want to reach before we stop uploading
|
|
|
|
str = tr("%1, uploaded %2 (Ratio: %3 Goal: %4)").arg(Formatter::sizeToString(haveTotal)).
|
|
|
|
arg(Formatter::sizeToString(tor.uploadedEver())).arg(Formatter::ratioToString(tor.ratio())).
|
|
|
|
arg(Formatter::ratioToString(seedRatio));
|
2010-04-01 06:08:20 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else // seeding w/o a ratio
|
2010-04-01 06:08:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
//: First part of torrent progress string;
|
|
|
|
//: %1 is the torrent's total size,
|
|
|
|
//: %2 is how much we've uploaded,
|
|
|
|
//: %3 is our upload-to-download ratio
|
|
|
|
str = tr("%1, uploaded %2 (Ratio: %3)").arg(Formatter::sizeToString(haveTotal)).
|
|
|
|
arg(Formatter::sizeToString(tor.uploadedEver())).arg(Formatter::ratioToString(tor.ratio()));
|
2010-04-01 06:08:20 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// add time when downloading
|
|
|
|
if ((hasSeedRatio && tor.isSeeding()) || tor.isDownloading())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor.hasETA())
|
|
|
|
{
|
|
|
|
//: Second (optional) part of torrent progress string;
|
|
|
|
//: %1 is duration;
|
|
|
|
//: notice that leading space (before the dash) is included here
|
|
|
|
str += tr(" - %1 left").arg(Formatter::timeToString(tor.getETA()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//: Second (optional) part of torrent progress string;
|
|
|
|
//: notice that leading space (before the dash) is included here
|
|
|
|
str += tr(" - Remaining time unknown");
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return str.trimmed();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString TorrentDelegate::shortTransferString(Torrent const& tor)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QString str;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const haveMeta(tor.hasMetadata());
|
|
|
|
bool const haveDown(haveMeta && ((tor.webseedsWeAreDownloadingFrom() > 0) || (tor.peersWeAreDownloadingFrom() > 0)));
|
|
|
|
bool const haveUp(haveMeta && tor.peersWeAreUploadingTo() > 0);
|
2013-01-30 00:22:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (haveDown)
|
|
|
|
{
|
|
|
|
str = Formatter::downloadSpeedToString(tor.downloadSpeed()) + QLatin1String(" ") +
|
|
|
|
Formatter::uploadSpeedToString(tor.uploadSpeed());
|
|
|
|
}
|
|
|
|
else if (haveUp)
|
|
|
|
{
|
|
|
|
str = Formatter::uploadSpeedToString(tor.uploadSpeed());
|
|
|
|
}
|
2013-01-30 00:22:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return str.trimmed();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString TorrentDelegate::shortStatusString(Torrent const& tor)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QString str;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (tor.getActivity())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_STATUS_CHECK:
|
|
|
|
str = tr("Verifying local data (%1% tested)").arg(Formatter::percentToString(tor.getVerifyProgress() * 100.0));
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_STATUS_DOWNLOAD:
|
|
|
|
case TR_STATUS_SEED:
|
|
|
|
str = shortTransferString(tor) + QLatin1String(" ") + tr("Ratio: %1").arg(Formatter::ratioToString(tor.ratio()));
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
str = tor.activityString();
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return str.trimmed();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString TorrentDelegate::statusString(Torrent const& tor)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QString str;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor.hasError())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
str = tor.getError();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (tor.getActivity())
|
|
|
|
{
|
|
|
|
case TR_STATUS_STOPPED:
|
|
|
|
case TR_STATUS_CHECK_WAIT:
|
|
|
|
case TR_STATUS_CHECK:
|
|
|
|
case TR_STATUS_DOWNLOAD_WAIT:
|
|
|
|
case TR_STATUS_SEED_WAIT:
|
|
|
|
str = shortStatusString(tor);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
|
|
|
if (!tor.hasMetadata())
|
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
str = tr("Downloading metadata from %Ln peer(s) (%1% done)", nullptr, tor.peersWeAreDownloadingFrom()).
|
2017-04-19 12:04:45 +00:00
|
|
|
arg(Formatter::percentToString(100.0 * tor.metadataPercentDone()));
|
|
|
|
}
|
2015-01-18 02:09:44 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
/* it would be nicer for translation if this was all one string, but I don't see how to do multiple %n's in
|
|
|
|
* tr() */
|
|
|
|
if (tor.connectedPeersAndWebseeds() == 0)
|
|
|
|
{
|
|
|
|
//: First part of phrase "Downloading from ... peer(s) and ... web seed(s)"
|
2017-04-30 09:29:58 +00:00
|
|
|
str = tr("Downloading from %Ln peer(s)", nullptr, tor.peersWeAreDownloadingFrom());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//: First part of phrase "Downloading from ... of ... connected peer(s) and ... web seed(s)"
|
2017-04-30 09:29:58 +00:00
|
|
|
str = tr("Downloading from %1 of %Ln connected peer(s)", nullptr, tor.connectedPeersAndWebseeds()).
|
2017-04-19 12:04:45 +00:00
|
|
|
arg(tor.peersWeAreDownloadingFrom());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tor.webseedsWeAreDownloadingFrom())
|
|
|
|
{
|
|
|
|
//: Second (optional) part of phrase "Downloading from ... of ... connected peer(s) and ... web
|
|
|
|
// seed(s)";
|
|
|
|
//: notice that leading space (before "and") is included here
|
2017-04-30 09:29:58 +00:00
|
|
|
str += tr(" and %Ln web seed(s)", nullptr, tor.webseedsWeAreDownloadingFrom());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_SEED:
|
|
|
|
if (tor.connectedPeers() == 0)
|
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
str = tr("Seeding to %Ln peer(s)", nullptr, tor.peersWeAreUploadingTo());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
str = tr("Seeding to %1 of %Ln connected peer(s)", nullptr, tor.connectedPeers()).arg(tor.peersWeAreUploadingTo());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
str = tr("Error");
|
|
|
|
break;
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor.isReadyToTransfer())
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QString s = shortTransferString(tor);
|
|
|
|
|
|
|
|
if (!s.isEmpty())
|
|
|
|
{
|
|
|
|
str += tr(" - ") + s;
|
|
|
|
}
|
2009-12-04 05:05:19 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return str.trimmed();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize TorrentDelegate::sizeHint(QStyleOptionViewItem const& option, Torrent const& tor) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const m(margin(*qApp->style()));
|
|
|
|
ItemLayout const layout(tor.name(), progressString(tor), statusString(tor), QIcon(), option.font, option.direction,
|
2017-04-19 12:04:45 +00:00
|
|
|
QPoint(0, 0), option.rect.width() - m.width() * 2);
|
|
|
|
return layout.size() + m * 2;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize TorrentDelegate::sizeHint(QStyleOptionViewItem const& option, QModelIndex const& index) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
Torrent const* tor(index.data(TorrentModel::TorrentRole).value<Torrent const*>());
|
2017-04-19 12:04:45 +00:00
|
|
|
return sizeHint(option, *tor);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void TorrentDelegate::paint(QPainter* painter, QStyleOptionViewItem const& option, QModelIndex const& index) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
Torrent const* tor(index.data(TorrentModel::TorrentRole).value<Torrent const*>());
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setClipRect(option.rect);
|
|
|
|
drawTorrent(painter, option, *tor);
|
|
|
|
painter->restore();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void TorrentDelegate::setProgressBarPercentDone(QStyleOptionViewItem const& option, Torrent const& tor) const
|
2011-05-28 00:09:15 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
double seedRatioLimit;
|
|
|
|
|
|
|
|
if (tor.isSeeding() && tor.getSeedRatio(seedRatioLimit))
|
2011-05-28 00:09:15 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
double const seedRateRatio = tor.ratio() / seedRatioLimit;
|
|
|
|
int const scaledProgress = seedRateRatio * (myProgressBarStyle->maximum - myProgressBarStyle->minimum);
|
2017-04-19 12:04:45 +00:00
|
|
|
myProgressBarStyle->progress = myProgressBarStyle->minimum + scaledProgress;
|
2011-05-28 00:09:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2011-05-28 00:09:15 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isMagnet(!tor.hasMetadata());
|
2017-04-19 12:04:45 +00:00
|
|
|
myProgressBarStyle->direction = option.direction;
|
|
|
|
myProgressBarStyle->progress = static_cast<int>(myProgressBarStyle->minimum + (((isMagnet ? tor.metadataPercentDone() :
|
|
|
|
tor.percentDone()) * (myProgressBarStyle->maximum - myProgressBarStyle->minimum))));
|
2011-05-28 00:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void TorrentDelegate::drawTorrent(QPainter* painter, QStyleOptionViewItem const& option, Torrent const& tor) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QStyle const* style(qApp->style());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isPaused(tor.isPaused());
|
2015-01-17 16:59:42 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isItemSelected((option.state & QStyle::State_Selected) != 0);
|
|
|
|
bool const isItemEnabled((option.state & QStyle::State_Enabled) != 0);
|
|
|
|
bool const isItemActive((option.state & QStyle::State_Active) != 0);
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->save();
|
2015-01-17 01:23:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isItemSelected)
|
|
|
|
{
|
|
|
|
QPalette::ColorGroup cg = isItemEnabled ? QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
if (cg == QPalette::Normal && !isItemActive)
|
|
|
|
{
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon::Mode im;
|
|
|
|
|
|
|
|
if (isPaused || !isItemEnabled)
|
|
|
|
{
|
|
|
|
im = QIcon::Disabled;
|
|
|
|
}
|
|
|
|
else if (isItemSelected)
|
|
|
|
{
|
|
|
|
im = QIcon::Selected;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
im = QIcon::Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon::State qs;
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isPaused)
|
|
|
|
{
|
|
|
|
qs = QIcon::Off;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qs = QIcon::On;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPalette::ColorGroup cg = QPalette::Normal;
|
|
|
|
|
|
|
|
if (isPaused || !isItemEnabled)
|
|
|
|
{
|
|
|
|
cg = QPalette::Disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cg == QPalette::Normal && !isItemActive)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
|
|
|
cg = QPalette::Inactive;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPalette::ColorRole cr;
|
|
|
|
|
|
|
|
if (isItemSelected)
|
|
|
|
{
|
|
|
|
cr = QPalette::HighlightedText;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cr = QPalette::Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStyle::State progressBarState(option.state);
|
|
|
|
|
|
|
|
if (isPaused)
|
|
|
|
{
|
|
|
|
progressBarState = QStyle::State_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
progressBarState |= QStyle::State_Small;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QIcon::Mode const emblemIm = isItemSelected ? QIcon::Selected : QIcon::Normal;
|
|
|
|
QIcon const emblemIcon = tor.hasError() ? QIcon::fromTheme(QLatin1String("emblem-important"),
|
2017-04-19 12:04:45 +00:00
|
|
|
style->standardIcon(QStyle::SP_MessageBoxWarning)) : QIcon();
|
|
|
|
|
|
|
|
// layout
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const m(margin(*style));
|
|
|
|
QRect const contentRect(option.rect.adjusted(m.width(), m.height(), -m.width(), -m.height()));
|
|
|
|
ItemLayout const layout(tor.name(), progressString(tor), statusString(tor), emblemIcon, option.font, option.direction,
|
2017-04-19 12:04:45 +00:00
|
|
|
contentRect.topLeft(), contentRect.width());
|
|
|
|
|
|
|
|
// render
|
|
|
|
if (tor.hasError() && !isItemSelected)
|
|
|
|
{
|
|
|
|
painter->setPen(QColor("red"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
painter->setPen(option.palette.color(cg, cr));
|
|
|
|
}
|
|
|
|
|
|
|
|
tor.getMimeTypeIcon().paint(painter, layout.iconRect, Qt::AlignCenter, im, qs);
|
|
|
|
|
|
|
|
if (!emblemIcon.isNull())
|
|
|
|
{
|
|
|
|
emblemIcon.paint(painter, layout.emblemRect, Qt::AlignCenter, emblemIm, qs);
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->setFont(layout.nameFont);
|
|
|
|
painter->drawText(layout.nameRect, Qt::AlignLeft | Qt::AlignVCenter, layout.nameText());
|
|
|
|
painter->setFont(layout.statusFont);
|
|
|
|
painter->drawText(layout.statusRect, Qt::AlignLeft | Qt::AlignVCenter, layout.statusText());
|
|
|
|
painter->setFont(layout.progressFont);
|
|
|
|
painter->drawText(layout.progressRect, Qt::AlignLeft | Qt::AlignVCenter, layout.progressText());
|
|
|
|
myProgressBarStyle->rect = layout.barRect;
|
|
|
|
|
|
|
|
if (tor.isDownloading())
|
|
|
|
{
|
|
|
|
myProgressBarStyle->palette.setBrush(QPalette::Highlight, blueBrush);
|
|
|
|
myProgressBarStyle->palette.setColor(QPalette::Base, blueBack);
|
|
|
|
myProgressBarStyle->palette.setColor(QPalette::Window, blueBack);
|
|
|
|
}
|
|
|
|
else if (tor.isSeeding())
|
|
|
|
{
|
|
|
|
myProgressBarStyle->palette.setBrush(QPalette::Highlight, greenBrush);
|
|
|
|
myProgressBarStyle->palette.setColor(QPalette::Base, greenBack);
|
|
|
|
myProgressBarStyle->palette.setColor(QPalette::Window, greenBack);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
myProgressBarStyle->palette.setBrush(QPalette::Highlight, silverBrush);
|
|
|
|
myProgressBarStyle->palette.setColor(QPalette::Base, silverBack);
|
|
|
|
myProgressBarStyle->palette.setColor(QPalette::Window, silverBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
myProgressBarStyle->state = progressBarState;
|
|
|
|
setProgressBarPercentDone(option, tor);
|
|
|
|
|
|
|
|
style->drawControl(QStyle::CE_ProgressBar, myProgressBarStyle, painter);
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
painter->restore();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|