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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-02-03 19:36:54 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
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>
|
|
|
|
#include <libtransmission/utils.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
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
QString myNameText;
|
|
|
|
QString myStatusText;
|
|
|
|
|
|
|
|
public:
|
|
|
|
QFont nameFont;
|
|
|
|
QFont statusFont;
|
|
|
|
|
|
|
|
QRect iconRect;
|
|
|
|
QRect emblemRect;
|
|
|
|
QRect nameRect;
|
|
|
|
QRect statusRect;
|
|
|
|
QRect barRect;
|
|
|
|
|
|
|
|
public:
|
2017-04-20 16:02:19 +00:00
|
|
|
ItemLayout(QString const& nameText, QString const& statusText, 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).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString nameText() const
|
|
|
|
{
|
|
|
|
return elidedText(nameFont, myNameText, nameRect.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString statusText() const
|
|
|
|
{
|
|
|
|
return myStatusText;
|
|
|
|
}
|
|
|
|
|
|
|
|
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, 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),
|
|
|
|
nameFont(baseFont),
|
|
|
|
statusFont(baseFont)
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QStyle const* style(qApp->style());
|
|
|
|
int const iconSize(style->pixelMetric(QStyle::PM_SmallIconSize));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
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.85));
|
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
|
|
|
|
2018-02-03 19:36:54 +00:00
|
|
|
QStyleOptionProgressBar barStyle;
|
|
|
|
barStyle.rect = QRect(0, 0, BAR_WIDTH, BAR_HEIGHT);
|
|
|
|
barStyle.maximum = 100;
|
|
|
|
barStyle.progress = 100;
|
|
|
|
barStyle.textVisible = true;
|
|
|
|
QSize const barSize(barStyle.rect.width() * 2 - style->subElementRect(QStyle::SE_ProgressBarGroove, &barStyle).width(),
|
|
|
|
barStyle.rect.height());
|
|
|
|
|
|
|
|
QRect baseRect(topLeft, QSize(width, std::max({iconSize, nameSize.height(), statusSize.height(), barSize.height()})));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
iconRect = style->alignedRect(direction, Qt::AlignLeft | Qt::AlignVCenter, QSize(iconSize, iconSize), baseRect);
|
|
|
|
emblemRect = style->alignedRect(direction, Qt::AlignRight | Qt::AlignBottom, emblemIcon.actualSize(iconRect.size() / 2,
|
|
|
|
QIcon::Normal, QIcon::On), iconRect);
|
2018-02-03 19:36:54 +00:00
|
|
|
barRect = style->alignedRect(direction, Qt::AlignRight | Qt::AlignVCenter, barSize, baseRect);
|
2017-04-19 12:04:45 +00:00
|
|
|
Utils::narrowRect(baseRect, iconRect.width() + GUI_PAD, barRect.width() + GUI_PAD, direction);
|
|
|
|
statusRect = style->alignedRect(direction, Qt::AlignRight | Qt::AlignVCenter, QSize(statusSize.width(), baseRect.height()),
|
|
|
|
baseRect);
|
|
|
|
Utils::narrowRect(baseRect, 0, statusRect.width() + GUI_PAD, direction);
|
2015-01-17 16:59:42 +00:00
|
|
|
nameRect = baseRect;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isMagnet(!tor.hasMetadata());
|
|
|
|
QSize const m(margin(*qApp->style()));
|
|
|
|
ItemLayout const layout(isMagnet ? progressString(tor) : tor.name(), shortStatusString(tor), QIcon(), option.font,
|
2017-04-19 12:04:45 +00:00
|
|
|
option.direction, 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
|
|
|
void TorrentDelegateMin::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());
|
|
|
|
bool const isMagnet(!tor.hasMetadata());
|
2017-04-19 12:04:45 +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);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QPalette::ColorGroup cg = QPalette::Normal;
|
2015-01-17 01:23:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (isPaused || !isItemEnabled)
|
|
|
|
{
|
|
|
|
cg = QPalette::Disabled;
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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(isMagnet ? progressString(tor) : tor.name(), shortStatusString(tor), emblemIcon, option.font,
|
2017-04-19 12:04:45 +00:00
|
|
|
option.direction, 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());
|
|
|
|
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;
|
|
|
|
myProgressBarStyle->text = QString::fromLatin1("%1%").arg(static_cast<int>(tr_truncd(100.0 * tor.percentDone(), 0)));
|
|
|
|
myProgressBarStyle->textVisible = true;
|
|
|
|
myProgressBarStyle->textAlignment = Qt::AlignCenter;
|
|
|
|
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
|
|
|
}
|