2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
|
|
|
|
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"
|
2021-12-09 08:13:04 +00:00
|
|
|
#include "IconCache.h"
|
2022-12-28 14:46:23 +00:00
|
|
|
#include "StyleHelper.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#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
|
|
|
};
|
|
|
|
|
2015-01-17 16:59:42 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
class ItemLayout
|
|
|
|
{
|
|
|
|
private:
|
2020-05-27 21:53:12 +00:00
|
|
|
QString name_text_;
|
|
|
|
QString status_text_;
|
|
|
|
QString progress_text_;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
public:
|
2020-05-27 21:53:12 +00:00
|
|
|
QFont name_font;
|
|
|
|
QFont status_font;
|
|
|
|
QFont progress_font;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QRect icon_rect;
|
|
|
|
QRect emblem_rect;
|
|
|
|
QRect name_rect;
|
|
|
|
QRect status_rect;
|
|
|
|
QRect bar_rect;
|
|
|
|
QRect progress_rect;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
ItemLayout(
|
|
|
|
QString name_text,
|
|
|
|
QString status_text,
|
|
|
|
QString progress_text,
|
|
|
|
QIcon const& emblem_icon,
|
|
|
|
QFont const& base_font,
|
|
|
|
Qt::LayoutDirection direction,
|
|
|
|
QPoint const& top_left,
|
|
|
|
int width);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QSize size() const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return (icon_rect | name_rect | status_rect | bar_rect | progress_rect).size();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QString nameText() const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return elidedText(name_font, name_text_, name_rect.width());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QString statusText() const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return elidedText(status_font, status_text_, status_rect.width());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QString progressText() const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return elidedText(progress_font, progress_text_, progress_rect.width());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-06-05 19:02:11 +00:00
|
|
|
[[nodiscard]] QString elidedText(QFont const& font, QString const& text, int width) const
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-07-18 15:20:17 +00:00
|
|
|
return QFontMetrics{ font }.elidedText(text, Qt::ElideRight, width);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
ItemLayout::ItemLayout(
|
|
|
|
QString name_text,
|
|
|
|
QString status_text,
|
|
|
|
QString progress_text,
|
|
|
|
QIcon const& emblem_icon,
|
|
|
|
QFont const& base_font,
|
|
|
|
Qt::LayoutDirection direction,
|
|
|
|
QPoint const& top_left,
|
|
|
|
int width)
|
2023-07-18 15:20:17 +00:00
|
|
|
: name_text_{ std::move(name_text) }
|
|
|
|
, status_text_{ std::move(status_text) }
|
|
|
|
, progress_text_{ std::move(progress_text) }
|
|
|
|
, name_font{ base_font }
|
|
|
|
, status_font{ base_font }
|
|
|
|
, progress_font{ base_font }
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
QStyle const* style = QApplication::style();
|
2020-05-27 21:53:12 +00:00
|
|
|
int const icon_size(style->pixelMetric(QStyle::PM_LargeIconSize));
|
|
|
|
|
|
|
|
name_font.setWeight(QFont::Bold);
|
2023-07-18 15:20:17 +00:00
|
|
|
auto const name_fm = QFontMetrics{ name_font };
|
|
|
|
auto const name_size = name_fm.size(0, name_text_);
|
2020-05-27 21:53:12 +00:00
|
|
|
|
|
|
|
status_font.setPointSize(static_cast<int>(status_font.pointSize() * 0.9));
|
2023-07-18 15:20:17 +00:00
|
|
|
auto const status_fm = QFontMetrics{ status_font };
|
|
|
|
auto const status_size = status_fm.size(0, status_text_);
|
2020-05-27 21:53:12 +00:00
|
|
|
|
|
|
|
progress_font.setPointSize(static_cast<int>(progress_font.pointSize() * 0.9));
|
2023-07-18 15:20:17 +00:00
|
|
|
auto const progress_fm = QFontMetrics{ progress_font };
|
|
|
|
auto const progress_size = progress_fm.size(0, progress_text_);
|
2020-05-27 21:53:12 +00:00
|
|
|
|
2023-07-18 15:20:17 +00:00
|
|
|
auto base_rect = QRect{ top_left, QSize{ width, 0 } };
|
2020-05-27 21:53:12 +00:00
|
|
|
Utils::narrowRect(base_rect, icon_size + GUI_PAD, 0, direction);
|
|
|
|
|
|
|
|
name_rect = base_rect.adjusted(0, 0, 0, name_size.height());
|
|
|
|
status_rect = name_rect.adjusted(0, name_rect.height() + 1, 0, status_size.height() + 1);
|
|
|
|
bar_rect = status_rect.adjusted(0, status_rect.height() + 1, 0, BAR_HEIGHT + 1);
|
|
|
|
progress_rect = bar_rect.adjusted(0, bar_rect.height() + 1, 0, progress_size.height() + 1);
|
2021-08-15 09:41:48 +00:00
|
|
|
icon_rect = QStyle::alignedRect(
|
|
|
|
direction,
|
|
|
|
Qt::AlignLeft | Qt::AlignVCenter,
|
2023-07-18 15:20:17 +00:00
|
|
|
QSize{ icon_size, icon_size },
|
|
|
|
QRect{ top_left, QSize{ width, progress_rect.bottom() - name_rect.top() } });
|
2021-08-15 09:41:48 +00:00
|
|
|
emblem_rect = QStyle::alignedRect(
|
|
|
|
direction,
|
|
|
|
Qt::AlignRight | Qt::AlignBottom,
|
|
|
|
emblem_icon.actualSize(icon_rect.size() / 2, QIcon::Normal, QIcon::On),
|
|
|
|
icon_rect);
|
2015-01-17 16:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
TorrentDelegate::TorrentDelegate(QObject* parent)
|
|
|
|
: QStyledItemDelegate{ parent }
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-06-15 14:30:29 +00:00
|
|
|
progress_bar_style_.minimum = 0;
|
|
|
|
progress_bar_style_.maximum = 1000;
|
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
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
Q_UNUSED(style)
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
return { 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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const is_magnet(!tor.hasMetadata());
|
|
|
|
bool const is_done(tor.isDone());
|
|
|
|
bool const is_seed(tor.isSeed());
|
|
|
|
uint64_t const have_total(tor.haveTotal());
|
2017-04-19 12:04:45 +00:00
|
|
|
QString str;
|
2022-02-08 03:56:04 +00:00
|
|
|
auto const seed_ratio_limit = tor.getSeedRatioLimit();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_magnet) // magnet link with no metadata
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: First part of torrent progress string,
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 is the percentage of torrent metadata downloaded
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("Magnetized transfer - retrieving metadata (%1%)")
|
2023-11-16 04:15:40 +00:00
|
|
|
.arg(Formatter::percent_to_string(tor.metadataPercentDone() * 100.0));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (!is_done) // downloading
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: First part of torrent progress string,
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 is how much we've got,
|
|
|
|
//: %2 is how much we'll have when done,
|
|
|
|
//: %3 is a percentage of the two
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("%1 of %2 (%3%)")
|
2023-11-16 04:15:40 +00:00
|
|
|
.arg(Formatter::storage_to_string(have_total))
|
|
|
|
.arg(Formatter::storage_to_string(tor.sizeWhenDone()))
|
|
|
|
.arg(Formatter::percent_to_string(tor.percentDone() * 100.0));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (!is_seed) // partial seed
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
if (seed_ratio_limit)
|
2010-04-01 06:08:20 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: First part of torrent progress string,
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %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
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("%1 of %2 (%3%), uploaded %4 (Ratio: %5 Goal: %6)")
|
2023-11-16 04:15:40 +00:00
|
|
|
.arg(Formatter::storage_to_string(have_total))
|
|
|
|
.arg(Formatter::storage_to_string(tor.totalSize()))
|
|
|
|
.arg(Formatter::percent_to_string(tor.percentComplete() * 100.0))
|
|
|
|
.arg(Formatter::storage_to_string(tor.uploadedEver()))
|
|
|
|
.arg(Formatter::ratio_to_string(tor.ratio()))
|
|
|
|
.arg(Formatter::ratio_to_string(*seed_ratio_limit));
|
2010-04-01 06:08:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: First part of torrent progress string,
|
2015-01-18 02:09:44 +00:00
|
|
|
//: %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
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("%1 of %2 (%3%), uploaded %4 (Ratio: %5)")
|
2023-11-16 04:15:40 +00:00
|
|
|
.arg(Formatter::storage_to_string(have_total))
|
|
|
|
.arg(Formatter::storage_to_string(tor.totalSize()))
|
|
|
|
.arg(Formatter::percent_to_string(tor.percentComplete() * 100.0))
|
|
|
|
.arg(Formatter::storage_to_string(tor.uploadedEver()))
|
|
|
|
.arg(Formatter::ratio_to_string(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
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
if (seed_ratio_limit)
|
2010-04-01 06:08:20 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: First part of torrent progress string,
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %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
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("%1, uploaded %2 (Ratio: %3 Goal: %4)")
|
2023-11-16 04:15:40 +00:00
|
|
|
.arg(Formatter::storage_to_string(have_total))
|
|
|
|
.arg(Formatter::storage_to_string(tor.uploadedEver()))
|
|
|
|
.arg(Formatter::ratio_to_string(tor.ratio()))
|
|
|
|
.arg(Formatter::ratio_to_string(*seed_ratio_limit));
|
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
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: First part of torrent progress string,
|
2017-04-19 12:04:45 +00:00
|
|
|
//: %1 is the torrent's total size,
|
|
|
|
//: %2 is how much we've uploaded,
|
|
|
|
//: %3 is our upload-to-download ratio
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("%1, uploaded %2 (Ratio: %3)")
|
2023-11-16 04:15:40 +00:00
|
|
|
.arg(Formatter::storage_to_string(have_total))
|
|
|
|
.arg(Formatter::storage_to_string(tor.uploadedEver()))
|
|
|
|
.arg(Formatter::ratio_to_string(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
|
2022-02-08 03:56:04 +00:00
|
|
|
if ((seed_ratio_limit && tor.isSeeding()) || tor.isDownloading())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tor.hasETA())
|
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: Second (optional) part of torrent progress string,
|
|
|
|
//: %1 is duration,
|
2017-04-19 12:04:45 +00:00
|
|
|
//: notice that leading space (before the dash) is included here
|
2023-11-16 04:15:40 +00:00
|
|
|
str += tr(" - %1 left").arg(Formatter::time_to_string(tor.getETA()));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: Second (optional) part of torrent progress string,
|
2017-04-19 12:04:45 +00:00
|
|
|
//: 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;
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const have_meta(tor.hasMetadata());
|
|
|
|
bool const have_down(have_meta && ((tor.webseedsWeAreDownloadingFrom() > 0) || (tor.peersWeAreDownloadingFrom() > 0)));
|
|
|
|
bool const have_up(have_meta && tor.peersWeAreUploadingTo() > 0);
|
2013-01-30 00:22:52 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (have_down)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-11-13 17:13:17 +00:00
|
|
|
str = tor.downloadSpeed().to_download_qstring() + QStringLiteral(" ") + tor.uploadSpeed().to_upload_qstring();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (have_up)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-11-13 17:13:17 +00:00
|
|
|
str = tor.uploadSpeed().to_upload_qstring();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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:
|
2023-11-16 04:15:40 +00:00
|
|
|
str = tr("Verifying local data (%1% tested)").arg(Formatter::percent_to_string(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:
|
2023-11-16 04:15:40 +00:00
|
|
|
str = shortTransferString(tor) + QStringLiteral(" ") + tr("Ratio: %1").arg(Formatter::ratio_to_string(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())
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("Downloading metadata from %Ln peer(s) (%1% done)", nullptr, tor.peersWeAreDownloadingFrom())
|
2023-11-16 04:15:40 +00:00
|
|
|
.arg(Formatter::percent_to_string(100.0 * tor.metadataPercentDone()));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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)"
|
2021-08-15 09:41:48 +00:00
|
|
|
str = tr("Downloading from %1 of %Ln connected peer(s)", nullptr, tor.connectedPeersAndWebseeds())
|
|
|
|
.arg(tor.peersWeAreDownloadingFrom());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tor.webseedsWeAreDownloadingFrom())
|
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
//: Second (optional) part of phrase "Downloading from ... of ... connected peer(s) and ... web seed(s)",
|
2017-04-19 12:04:45 +00:00
|
|
|
//: 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
|
|
|
|
{
|
2021-08-15 09:41:48 +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;
|
|
|
|
}
|
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
|
|
|
{
|
2022-07-27 14:03:13 +00:00
|
|
|
QString const s = shortTransferString(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
auto const m = margin(*QApplication::style());
|
2023-07-18 15:20:17 +00:00
|
|
|
auto const layout = ItemLayout{ tor.name(), progressString(tor), statusString(tor), QIcon{},
|
|
|
|
option.font, option.direction, QPoint(0, 0), option.rect.width() - m.width() * 2 };
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2019-11-07 00:17:48 +00:00
|
|
|
// if the font changed, invalidate the height cache
|
2020-05-27 21:53:12 +00:00
|
|
|
if (height_font_ != option.font)
|
2019-11-07 00:17:48 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
height_font_ = option.font;
|
|
|
|
height_hint_.reset();
|
2019-11-07 00:17:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure the height is cached
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!height_hint_)
|
2019-11-07 00:17:48 +00:00
|
|
|
{
|
|
|
|
auto const* tor = index.data(TorrentModel::TorrentRole).value<Torrent const*>();
|
2020-05-27 21:53:12 +00:00
|
|
|
height_hint_ = sizeHint(option, *tor).height();
|
2019-11-07 00:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
return { option.rect.width(), *height_hint_ };
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
QIcon& TorrentDelegate::getWarningEmblem() const
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
auto& icon = warning_emblem_;
|
2019-11-12 01:37:05 +00:00
|
|
|
|
|
|
|
if (icon.isNull())
|
|
|
|
{
|
2021-12-09 08:13:04 +00:00
|
|
|
icon = IconCache::get().getThemeIcon(QStringLiteral("emblem-important"), QStyle::SP_MessageBoxWarning);
|
2019-11-12 01:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-05-20 01:32:51 +00:00
|
|
|
auto 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
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
auto const seed_ratio_limit = tor.getSeedRatioLimit();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
if (tor.isSeeding() && seed_ratio_limit)
|
2011-05-28 00:09:15 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
auto const seed_rate_ratio = tor.ratio() / *seed_ratio_limit;
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const scaled_progress = static_cast<int>(
|
|
|
|
seed_rate_ratio * (progress_bar_style_.maximum - progress_bar_style_.minimum));
|
2020-06-15 14:30:29 +00:00
|
|
|
progress_bar_style_.progress = progress_bar_style_.minimum + scaled_progress;
|
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
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const is_magnet(!tor.hasMetadata());
|
2020-06-15 14:30:29 +00:00
|
|
|
progress_bar_style_.direction = option.direction;
|
2021-08-15 09:41:48 +00:00
|
|
|
progress_bar_style_.progress = static_cast<int>(
|
|
|
|
progress_bar_style_.minimum +
|
|
|
|
(is_magnet ? tor.metadataPercentDone() : tor.percentDone()) *
|
|
|
|
(progress_bar_style_.maximum - progress_bar_style_.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
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
auto const* style = QApplication::style();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const is_paused(tor.isPaused());
|
2015-01-17 16:59:42 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
bool const is_item_selected((option.state & QStyle::State_Selected) != 0);
|
|
|
|
bool const is_item_enabled((option.state & QStyle::State_Enabled) != 0);
|
|
|
|
bool const is_item_active((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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_item_selected)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QPalette::ColorGroup cg = is_item_enabled ? QPalette::Normal : QPalette::Disabled;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (cg == QPalette::Normal && !is_item_active)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
cg = QPalette::Inactive;
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
|
|
|
|
}
|
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto icon_mode = QIcon::Mode{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_paused || !is_item_enabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
icon_mode = QIcon::Disabled;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
else if (is_item_selected)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
icon_mode = QIcon::Selected;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
icon_mode = QIcon::Normal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto const icon_state = is_paused ? QIcon::Off : QIcon::On;
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto color_group = QPalette::Normal;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_paused || !is_item_enabled)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
color_group = QPalette::Disabled;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
if (color_group == QPalette::Normal && !is_item_active)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
color_group = QPalette::Inactive;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto const color_role = is_item_selected ? QPalette::HighlightedText : QPalette::Text;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-31 13:36:16 +00:00
|
|
|
QStyle::State progress_bar_state(option.state);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_paused)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
progress_bar_state = QStyle::State_None;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:36:16 +00:00
|
|
|
progress_bar_state |= QStyle::State_Small | QStyle::State_Horizontal;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-07-18 15:20:17 +00:00
|
|
|
auto const emblem_im = is_item_selected ? QIcon::Selected : QIcon::Normal;
|
|
|
|
auto const emblem_icon = tor.hasError() ? getWarningEmblem() : QIcon{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// layout
|
2023-07-18 15:20:17 +00:00
|
|
|
auto const m = margin(*style);
|
|
|
|
auto const content_rect = QRect{ option.rect.adjusted(m.width(), m.height(), -m.width(), -m.height()) };
|
|
|
|
auto const layout = ItemLayout{ tor.name(), progressString(tor), statusString(tor), emblem_icon,
|
|
|
|
option.font, option.direction, content_rect.topLeft(), content_rect.width() };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// render
|
2020-05-27 21:53:12 +00:00
|
|
|
if (tor.hasError() && !is_item_selected)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
painter->setPen(QColor("red"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
painter->setPen(option.palette.color(color_group, color_role));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
tor.getMimeTypeIcon().paint(painter, layout.icon_rect, Qt::AlignCenter, icon_mode, icon_state);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!emblem_icon.isNull())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
emblem_icon.paint(painter, layout.emblem_rect, Qt::AlignCenter, emblem_im, icon_state);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
painter->setFont(layout.name_font);
|
|
|
|
painter->drawText(layout.name_rect, Qt::AlignLeft | Qt::AlignVCenter, layout.nameText());
|
|
|
|
painter->setFont(layout.status_font);
|
|
|
|
painter->drawText(layout.status_rect, Qt::AlignLeft | Qt::AlignVCenter, layout.statusText());
|
|
|
|
painter->setFont(layout.progress_font);
|
|
|
|
painter->drawText(layout.progress_rect, Qt::AlignLeft | Qt::AlignVCenter, layout.progressText());
|
2020-06-15 14:30:29 +00:00
|
|
|
progress_bar_style_.rect = layout.bar_rect;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tor.isDownloading())
|
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
progress_bar_style_.palette.setBrush(QPalette::Highlight, BlueBrush);
|
|
|
|
progress_bar_style_.palette.setColor(QPalette::Base, BlueBack);
|
|
|
|
progress_bar_style_.palette.setColor(QPalette::Window, BlueBack);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else if (tor.isSeeding())
|
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
progress_bar_style_.palette.setBrush(QPalette::Highlight, GreenBrush);
|
|
|
|
progress_bar_style_.palette.setColor(QPalette::Base, GreenBack);
|
|
|
|
progress_bar_style_.palette.setColor(QPalette::Window, GreenBack);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
progress_bar_style_.palette.setBrush(QPalette::Highlight, SilverBrush);
|
|
|
|
progress_bar_style_.palette.setColor(QPalette::Base, SilverBack);
|
|
|
|
progress_bar_style_.palette.setColor(QPalette::Window, SilverBack);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:30:29 +00:00
|
|
|
progress_bar_style_.state = progress_bar_state;
|
2017-04-19 12:04:45 +00:00
|
|
|
setProgressBarPercentDone(option, tor);
|
|
|
|
|
2022-12-28 14:46:23 +00:00
|
|
|
StyleHelper::drawProgressBar(*style, *painter, progress_bar_style_);
|
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
|
|
|
}
|