2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2009-2023 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
|
|
|
|
2018-02-03 19:36:54 +00:00
|
|
|
#include <algorithm>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2018-02-03 19:36:54 +00:00
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QBrush>
|
|
|
|
#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
|
|
|
|
2010-07-25 22:14:13 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-08-04 13:44:18 +00:00
|
|
|
|
2010-07-25 22:14:13 +00:00
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
2022-12-28 14:46:23 +00:00
|
|
|
#include "StyleHelper.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Torrent.h"
|
|
|
|
#include "TorrentDelegateMin.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_WIDTH = 50,
|
|
|
|
BAR_HEIGHT = 16,
|
|
|
|
LINE_SPACING = 4
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** +---------+-----------------------------------------------+
|
2010-07-24 02:43:06 +00:00
|
|
|
**** | Icon | Title shortStatusString [Progressbar] |
|
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
|
|
|
|
{
|
|
|
|
public:
|
2020-05-27 21:53:12 +00:00
|
|
|
QFont name_font;
|
|
|
|
QFont status_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;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
ItemLayout(
|
|
|
|
QString name_text,
|
|
|
|
QString status_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).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 status_text_;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-11-02 01:13:32 +00:00
|
|
|
QString name_text_;
|
|
|
|
QString status_text_;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
return QFontMetrics(font).elidedText(text, Qt::ElideRight, width);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
ItemLayout::ItemLayout(
|
|
|
|
QString name_text,
|
|
|
|
QString status_text,
|
|
|
|
QIcon const& emblem_icon,
|
|
|
|
QFont const& base_font,
|
|
|
|
Qt::LayoutDirection direction,
|
|
|
|
QPoint const& top_left,
|
|
|
|
int width)
|
|
|
|
: name_font(base_font)
|
|
|
|
, status_font(base_font)
|
|
|
|
, name_text_(std::move(name_text))
|
|
|
|
, status_text_(std::move(status_text))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
auto const* style = QApplication::style();
|
|
|
|
int const icon_size = style->pixelMetric(QStyle::PM_SmallIconSize);
|
2020-05-27 21:53:12 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
auto const name_fm = QFontMetrics(name_font);
|
2022-09-08 23:26:18 +00:00
|
|
|
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.85));
|
|
|
|
QFontMetrics const status_fm(status_font);
|
|
|
|
QSize const status_size(status_fm.size(0, status_text_));
|
|
|
|
|
|
|
|
QStyleOptionProgressBar bar_style;
|
|
|
|
bar_style.rect = QRect(0, 0, BAR_WIDTH, BAR_HEIGHT);
|
|
|
|
bar_style.maximum = 100;
|
|
|
|
bar_style.progress = 100;
|
|
|
|
bar_style.textVisible = true;
|
2021-08-15 09:41:48 +00:00
|
|
|
QSize const bar_size(
|
|
|
|
bar_style.rect.width() * 2 - style->subElementRect(QStyle::SE_ProgressBarGroove, &bar_style).width(),
|
2020-05-27 21:53:12 +00:00
|
|
|
bar_style.rect.height());
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
QRect base_rect(
|
|
|
|
top_left,
|
2020-05-27 21:53:12 +00:00
|
|
|
QSize(width, std::max({ icon_size, name_size.height(), status_size.height(), bar_size.height() })));
|
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
icon_rect = QStyle::alignedRect(direction, Qt::AlignLeft | Qt::AlignVCenter, QSize(icon_size, icon_size), base_rect);
|
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);
|
2020-11-02 01:13:32 +00:00
|
|
|
bar_rect = QStyle::alignedRect(direction, Qt::AlignRight | Qt::AlignVCenter, bar_size, base_rect);
|
2020-05-27 21:53:12 +00:00
|
|
|
Utils::narrowRect(base_rect, icon_rect.width() + GUI_PAD, bar_rect.width() + GUI_PAD, direction);
|
2021-08-15 09:41:48 +00:00
|
|
|
status_rect = QStyle::alignedRect(
|
|
|
|
direction,
|
|
|
|
Qt::AlignRight | Qt::AlignVCenter,
|
|
|
|
QSize(status_size.width(), base_rect.height()),
|
2020-05-27 21:53:12 +00:00
|
|
|
base_rect);
|
|
|
|
Utils::narrowRect(base_rect, 0, status_rect.width() + GUI_PAD, direction);
|
|
|
|
name_rect = base_rect;
|
2015-01-17 16:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize TorrentDelegateMin::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 is_magnet = !tor.hasMetadata();
|
|
|
|
auto const m = margin(*QApplication::style());
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const layout = ItemLayout(
|
|
|
|
is_magnet ? progressString(tor) : tor.name(),
|
|
|
|
shortStatusString(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
|
|
|
void TorrentDelegateMin::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());
|
|
|
|
bool const is_magnet(!tor.hasMetadata());
|
2017-04-19 12:04:45 +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);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_item_selected)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
auto color_group = is_item_enabled ? QPalette::Normal : 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)
|
2017-04-19 12:04:45 +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
|
|
|
painter->fillRect(option.rect, option.palette.brush(color_group, QPalette::Highlight));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
QPalette::ColorGroup color_group = QPalette::Normal;
|
2015-01-17 01:23:51 +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
|
|
|
}
|
2013-09-14 22:45:04 +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-05-27 21:53:12 +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
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QIcon::Mode const emblem_im = is_item_selected ? QIcon::Selected : QIcon::Normal;
|
|
|
|
QIcon const emblem_icon = tor.hasError() ? getWarningEmblem() : QIcon();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// layout
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const m(margin(*style));
|
2020-05-27 21:53:12 +00:00
|
|
|
QRect const content_rect(option.rect.adjusted(m.width(), m.height(), -m.width(), -m.height()));
|
2021-08-15 09:41:48 +00:00
|
|
|
ItemLayout const layout(
|
|
|
|
is_magnet ? progressString(tor) : tor.name(),
|
|
|
|
shortStatusString(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());
|
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;
|
|
|
|
progress_bar_style_.text = QStringLiteral("%1%").arg(static_cast<int>(tr_truncd(100.0 * tor.percentDone(), 0)));
|
|
|
|
progress_bar_style_.textVisible = true;
|
|
|
|
progress_bar_style_.textAlignment = Qt::AlignCenter;
|
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
|
|
|
}
|