1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 18:18:10 +00:00
transmission/qt/TorrentDelegateMin.cc
Mike Gelfand be74cb6356
Qt 6 support (#2069)
* Bump minimum Qt version to 5.6

* Switch from QRegExp to QRegularExpression

While still available, QRegExp has been moved to Qt6::Core5Compat module
and is not part of Qt6::Core.

* Use qIsEffectiveTLD instead of QUrl::topLevelDomain

The latter is not part of Qt6::Core. The former is a private utility in
Qt6::Network; using it for now, until (and if) we switch to something
non-Qt-specific.

* Use QStyle::State_Horizontal state when drawing progress bars

Although available for a long time, this state either didn't apply to
progress bars before Qt 6, or was deduced based on bar size. With Qt 6,
failing to specify it results in bad rendering.

* Don't use QStringRef (and associated methods)

While still available, QStringRef has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core. Related method (e.g.
QString::midRef) have been removed in Qt 6.

* Use Qt::ItemIsAutoTristate instead of Qt::ItemIsTristate

The latter was deprecated and replaced with the former in Qt 5.6.

* Don't use QApplication::globalStrut

This property has been deprecated in Qt 5.15 and removed in Qt 6.

* Use QImage::fromHICON instead of QtWin::fromHICON

WinExtras module (providind the latter helper) has been removed in Qt 6.

* Use QStringDecoder instead of QTextCodec

While still available, QTextCodec has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core.

* Don't forward-declare QStringList

Instead of being a standalone class, its definition has changed to
QList<QString> template specialization in Qt 6.

* Use explicit (since Qt 6) QFileInfo constructor

* Use QDateTime's {to,from}SecsSinceEpoch instead of {to,from}Time_t

The latter was deprecated in Qt 5.8 and removed in Qt 6.

* Don't use QFuture<>'s operator==

It has been removed in Qt 6. Since the original issue this code was
solving was caused by future reuse, just don't reuse futures and create
new finished ones when necessary.

* Use std::vector<> instead of QVector<>

The latter has been changed to a typedef for QList<>, which might not be
what one wants, and which also changed behavior a bit leading to
compilation errors.

* Don't use + for flags, cast to int explicitly

Operator+ for enum values has been deleted in Qt 6, so using operator|
instead. Then, there's no conversion from QFlags<> to QVariant, so need
to cast to int.

* Support Qt 6 in CMake and for MSI packaging

* Remove extra (empty) CMake variable use when constructing Qt target names

* Simplify logic in tr_qt_add_translation CMake helper

Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-11-04 00:20:11 +03:00

313 lines
8.6 KiB
C++

/*
* This file Copyright (C) 2009-2015 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#include <algorithm>
#include <QApplication>
#include <QBrush>
#include <QFont>
#include <QFontMetrics>
#include <QIcon>
#include <QModelIndex>
#include <QPainter>
#include <QPixmap>
#include <QPixmapCache>
#include <QStyleOptionProgressBar>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
#include "Torrent.h"
#include "TorrentDelegateMin.h"
#include "TorrentModel.h"
#include "Utils.h"
enum
{
GUI_PAD = 6,
BAR_WIDTH = 50,
BAR_HEIGHT = 16,
LINE_SPACING = 4
};
/***
****
**** +---------+-----------------------------------------------+
**** | Icon | Title shortStatusString [Progressbar] |
**** +-------- +-----------------------------------------------+
****
***/
namespace
{
class ItemLayout
{
public:
QFont name_font;
QFont status_font;
QRect icon_rect;
QRect emblem_rect;
QRect name_rect;
QRect status_rect;
QRect bar_rect;
ItemLayout(
QString name_text,
QString status_text,
QIcon const& emblem_icon,
QFont const& base_font,
Qt::LayoutDirection direction,
QPoint const& top_left,
int width);
[[nodiscard]] QSize size() const
{
return (icon_rect | name_rect | status_rect | bar_rect).size();
}
[[nodiscard]] QString nameText() const
{
return elidedText(name_font, name_text_, name_rect.width());
}
[[nodiscard]] QString statusText() const
{
return status_text_;
}
private:
QString name_text_;
QString status_text_;
[[nodiscard]] QString elidedText(QFont const& font, QString const& text, int width) const
{
return QFontMetrics(font).elidedText(text, Qt::ElideRight, width);
}
};
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))
{
auto const* style = QApplication::style();
int const icon_size = style->pixelMetric(QStyle::PM_SmallIconSize);
auto const name_fm = QFontMetrics(name_font);
auto const name_size = QSize(name_fm.size(0, name_text_));
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;
QSize const bar_size(
bar_style.rect.width() * 2 - style->subElementRect(QStyle::SE_ProgressBarGroove, &bar_style).width(),
bar_style.rect.height());
QRect base_rect(
top_left,
QSize(width, std::max({ icon_size, name_size.height(), status_size.height(), bar_size.height() })));
icon_rect = QStyle::alignedRect(direction, Qt::AlignLeft | Qt::AlignVCenter, QSize(icon_size, icon_size), base_rect);
emblem_rect = QStyle::alignedRect(
direction,
Qt::AlignRight | Qt::AlignBottom,
emblem_icon.actualSize(icon_rect.size() / 2, QIcon::Normal, QIcon::On),
icon_rect);
bar_rect = QStyle::alignedRect(direction, Qt::AlignRight | Qt::AlignVCenter, bar_size, base_rect);
Utils::narrowRect(base_rect, icon_rect.width() + GUI_PAD, bar_rect.width() + GUI_PAD, direction);
status_rect = QStyle::alignedRect(
direction,
Qt::AlignRight | Qt::AlignVCenter,
QSize(status_size.width(), base_rect.height()),
base_rect);
Utils::narrowRect(base_rect, 0, status_rect.width() + GUI_PAD, direction);
name_rect = base_rect;
}
} // namespace
QSize TorrentDelegateMin::sizeHint(QStyleOptionViewItem const& option, Torrent const& tor) const
{
auto const is_magnet = bool(!tor.hasMetadata());
auto const m = QSize(margin(*QApplication::style()));
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);
return layout.size() + m * 2;
}
void TorrentDelegateMin::drawTorrent(QPainter* painter, QStyleOptionViewItem const& option, Torrent const& tor) const
{
auto const* style = QApplication::style();
bool const is_paused(tor.isPaused());
bool const is_magnet(!tor.hasMetadata());
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);
painter->save();
if (is_item_selected)
{
QPalette::ColorGroup cg = is_item_enabled ? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !is_item_active)
{
cg = QPalette::Inactive;
}
painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
}
QIcon::Mode im;
if (is_paused || !is_item_enabled)
{
im = QIcon::Disabled;
}
else if (is_item_selected)
{
im = QIcon::Selected;
}
else
{
im = QIcon::Normal;
}
QIcon::State qs;
if (is_paused)
{
qs = QIcon::Off;
}
else
{
qs = QIcon::On;
}
QPalette::ColorGroup cg = QPalette::Normal;
if (is_paused || !is_item_enabled)
{
cg = QPalette::Disabled;
}
if (cg == QPalette::Normal && !is_item_active)
{
cg = QPalette::Inactive;
}
QPalette::ColorRole cr;
if (is_item_selected)
{
cr = QPalette::HighlightedText;
}
else
{
cr = QPalette::Text;
}
QStyle::State progress_bar_state(option.state | QStyle::State_Horizontal);
if (is_paused)
{
progress_bar_state = QStyle::State_None;
}
progress_bar_state |= QStyle::State_Small;
QIcon::Mode const emblem_im = is_item_selected ? QIcon::Selected : QIcon::Normal;
QIcon const emblem_icon = tor.hasError() ? getWarningEmblem() : QIcon();
// layout
QSize const m(margin(*style));
QRect const content_rect(option.rect.adjusted(m.width(), m.height(), -m.width(), -m.height()));
ItemLayout const layout(
is_magnet ? progressString(tor) : tor.name(),
shortStatusString(tor),
emblem_icon,
option.font,
option.direction,
content_rect.topLeft(),
content_rect.width());
// render
if (tor.hasError() && !is_item_selected)
{
painter->setPen(QColor("red"));
}
else
{
painter->setPen(option.palette.color(cg, cr));
}
tor.getMimeTypeIcon().paint(painter, layout.icon_rect, Qt::AlignCenter, im, qs);
if (!emblem_icon.isNull())
{
emblem_icon.paint(painter, layout.emblem_rect, Qt::AlignCenter, emblem_im, qs);
}
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());
progress_bar_style_.rect = layout.bar_rect;
if (tor.isDownloading())
{
progress_bar_style_.palette.setBrush(QPalette::Highlight, BlueBrush);
progress_bar_style_.palette.setColor(QPalette::Base, BlueBack);
progress_bar_style_.palette.setColor(QPalette::Window, BlueBack);
}
else if (tor.isSeeding())
{
progress_bar_style_.palette.setBrush(QPalette::Highlight, GreenBrush);
progress_bar_style_.palette.setColor(QPalette::Base, GreenBack);
progress_bar_style_.palette.setColor(QPalette::Window, GreenBack);
}
else
{
progress_bar_style_.palette.setBrush(QPalette::Highlight, SilverBrush);
progress_bar_style_.palette.setColor(QPalette::Base, SilverBack);
progress_bar_style_.palette.setColor(QPalette::Window, SilverBack);
}
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;
setProgressBarPercentDone(option, tor);
style->drawControl(QStyle::CE_ProgressBar, &progress_bar_style_, painter);
painter->restore();
}