32 #include <QApplication>
36 #include <QPainterPath>
37 #include <QTextStream>
57 {((image.width() > 0) && (torrentInfo.
totalSize() >= image.width()))
58 ? torrentInfo.
totalSize() / image.width() : -1}
59 , m_torrentInfo {torrentInfo}
61 if ((m_bytesPerPixel > 0) && (m_bytesPerPixel < 10))
62 qDebug() <<
"PieceIndexToImagePos: torrent size is too small for correct computaions."
63 <<
"Torrent size =" << torrentInfo.totalSize() <<
"Image width = " << image.width();
68 if (m_bytesPerPixel < 0)
72 const qlonglong pieceLength = m_torrentInfo.pieceLength();
73 return makeInterval<ImageRange::IndexType>(
74 (pieces.
first() * pieceLength) / m_bytesPerPixel,
75 (pieces.
last() * pieceLength + m_torrentInfo.pieceLength(pieces.
last()) - 1) / m_bytesPerPixel);
80 return m_bytesPerPixel < 0 ? 0 : (imagePos * m_bytesPerPixel + m_bytesPerPixel / 2) / m_torrentInfo.pieceLength();
95 << R
"(<table style="width:100%; padding: 3px; vertical-align: middle;">)";
100 m_stream << "</table>";
105 m_stream << R
"(<tr><td style="white-space:nowrap">)" << size << "</td><td>" << path <<
"</td></tr>";
117 setMouseTracking(
true);
135 if (
e->type() == QEvent::ToolTip)
141 return base::event(
e);
144 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
167 base::mouseMoveEvent(
e);
172 QPainter painter(
this);
177 painter.drawRect(imageRect);
181 if (
m_image.width() != imageRect.width())
183 painter.drawImage(imageRect,
m_image);
188 QColor highlightColor {this->palette().color(QPalette::Active, QPalette::Highlight)};
189 highlightColor.setAlphaF(0.35f);
191 painter.fillRect(targetHighlightRect, highlightColor);
195 border.addRect(0, 0, width(), height());
197 painter.drawPath(border);
208 return palette().color(QPalette::Base);
213 return palette().color(QPalette::Dark);
218 return palette().color(QPalette::Highlight);
223 return palette().color(QPalette::ToolTipText);
234 int g1 = qGreen(rgb1);
235 int b1 = qBlue(rgb1);
238 int g2 = qGreen(rgb2);
239 int b2 = qBlue(rgb2);
241 float ratioN = 1.0f - ratio;
242 int r = (r1 * ratioN) + (r2 * ratio);
243 int g = (g1 * ratioN) + (g2 * ratio);
244 int b = (b1 * ratioN) + (b2 * ratio);
246 return qRgb(r, g, b);
255 QTextStream stream(&toolTipText, QIODevice::WriteOnly);
256 const bool showDetailedInformation = QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier);
261 if ((imagePos >=0) && (imagePos <
m_image.width()))
263 stream <<
"<html><body>";
264 PieceIndexToImagePos transform {torrentInfo,
m_image};
265 int pieceIndex = transform.pieceIndex(imagePos);
268 QString tooltipTitle;
269 if (
files.count() > 1)
271 tooltipTitle = tr(
"Files in this piece:");
276 tooltipTitle = tr(
"File in this piece");
278 tooltipTitle = tr(
"File in these pieces");
281 DetailedTooltipRenderer renderer(stream, tooltipTitle);
285 const QString filePath {torrentInfo.
filePath(
f)};
288 stream <<
"</body></html>";
294 if (showDetailedInformation)
295 stream <<
'\n' << tr(
"Wait until metadata become available to see detailed information");
297 stream <<
'\n' << tr(
"Hold Shift key for detailed information");
302 QToolTip::showText(
e->globalPos(), toolTipText,
this);
311 PieceIndexToImagePos transform {torrentInfo,
m_image};
313 int pieceIndex = transform.pieceIndex(imagePos);
315 if (fileIndices.count() == 1)
319 ImageRange imageRange = transform.imagePos(filePieces);
320 QRect newHighlightedRegion {imageRange.first(), 0, imageRange.size(),
m_image.height()};
337 for (
int i = 0; i < 256; ++i)
339 float ratio = (i / 255.0);
virtual TorrentInfo info() const =0
virtual bool hasMetadata() const =0
qlonglong totalSize() const
QString filePath(int index) const
PieceRange filePieces(const QString &file) const
qlonglong fileSize(int index) const
QVector< int > fileIndicesForPiece(int pieceIndex) const
constexpr IndexType last() const
constexpr IndexType first() const
static QRgb mixTwoColors(QRgb rgb1, QRgb rgb2, float ratio)
virtual bool event(QEvent *e) override
QColor colorBoxBorderColor() const
QColor pieceColor() const
QColor backgroundColor() const
void requestImageUpdate()
void mouseMoveEvent(QMouseEvent *e) override
const BitTorrent::Torrent * m_torrent
virtual bool updateImage(QImage &image)=0
PiecesBar(QWidget *parent=nullptr)
void enterEvent(QEnterEvent *e) override
void highlightFile(int imagePos)
QVector< QRgb > m_pieceColors
static constexpr int borderWidth
void showToolTip(const QHelpEvent *)
void paintEvent(QPaintEvent *e) override
void setTorrent(const BitTorrent::Torrent *torrent)
void leaveEvent(QEvent *e) override
virtual QString simpleToolTipText() const =0
QRect m_highlightedRegion
const QVector< QRgb > & pieceColors() const
QColor borderColor() const
const BitTorrent::TorrentInfo m_torrentInfo
const qlonglong m_bytesPerPixel
ImageRange imagePos(const BitTorrent::TorrentInfo::PieceRange &pieces) const
PieceIndexToImagePos(const BitTorrent::TorrentInfo &torrentInfo, const QImage &image)
int pieceIndex(int imagePos) const
flag icons free of to any person obtaining a copy of this software and associated documentation files(the "Software")
QString friendlyUnit(qint64 bytes, bool isSpeed=false)
IndexRange< int > ImageRange