qBittorrent
PiecesBar Class Referenceabstract

#include <piecesbar.h>

Inheritance diagram for PiecesBar:
Collaboration diagram for PiecesBar:

Public Member Functions

 PiecesBar (QWidget *parent=nullptr)
 
void setTorrent (const BitTorrent::Torrent *torrent)
 
virtual void clear ()
 
virtual bool event (QEvent *e) override
 

Protected Member Functions

void enterEvent (QEnterEvent *e) override
 
void leaveEvent (QEvent *e) override
 
void mouseMoveEvent (QMouseEvent *e) override
 
void paintEvent (QPaintEvent *e) override
 
void requestImageUpdate ()
 
QColor backgroundColor () const
 
QColor borderColor () const
 
QColor pieceColor () const
 
QColor colorBoxBorderColor () const
 
const QVector< QRgb > & pieceColors () const
 

Static Protected Member Functions

static QRgb mixTwoColors (QRgb rgb1, QRgb rgb2, float ratio)
 

Static Protected Attributes

static constexpr int borderWidth = 1
 

Private Types

using base = QWidget
 

Private Member Functions

void showToolTip (const QHelpEvent *)
 
void highlightFile (int imagePos)
 
virtual QString simpleToolTipText () const =0
 
virtual bool updateImage (QImage &image)=0
 
void updatePieceColors ()
 

Private Attributes

const BitTorrent::Torrentm_torrent = nullptr
 
QImage m_image
 
QVector< QRgb > m_pieceColors
 
bool m_hovered = false
 
QRect m_highlightedRegion
 

Detailed Description

Definition at line 43 of file piecesbar.h.

Member Typedef Documentation

◆ base

using PiecesBar::base = QWidget
private

Definition at line 45 of file piecesbar.h.

Constructor & Destructor Documentation

◆ PiecesBar()

PiecesBar::PiecesBar ( QWidget *  parent = nullptr)
explicit

Definition at line 113 of file piecesbar.cpp.

114  : QWidget {parent}
115 {
117  setMouseTracking(true);
118 }
void updatePieceColors()
Definition: piecesbar.cpp:334

References updatePieceColors().

Here is the call graph for this function:

Member Function Documentation

◆ backgroundColor()

QColor PiecesBar::backgroundColor ( ) const
protected

Definition at line 206 of file piecesbar.cpp.

207 {
208  return palette().color(QPalette::Base);
209 }

Referenced by paintEvent(), DownloadedPiecesBar::simpleToolTipText(), PieceAvailabilityBar::simpleToolTipText(), DownloadedPiecesBar::updateImage(), PieceAvailabilityBar::updateImage(), and updatePieceColors().

Here is the caller graph for this function:

◆ borderColor()

QColor PiecesBar::borderColor ( ) const
protected

Definition at line 211 of file piecesbar.cpp.

212 {
213  return palette().color(QPalette::Dark);
214 }

Referenced by paintEvent(), DownloadedPiecesBar::simpleToolTipText(), and PieceAvailabilityBar::simpleToolTipText().

Here is the caller graph for this function:

◆ clear()

void PiecesBar::clear ( )
virtual

Reimplemented in PieceAvailabilityBar, and DownloadedPiecesBar.

Definition at line 127 of file piecesbar.cpp.

128 {
129  m_image = QImage();
130  update();
131 }
QImage m_image
Definition: piecesbar.h:95

References m_image.

Referenced by DownloadedPiecesBar::clear(), PieceAvailabilityBar::clear(), and setTorrent().

Here is the caller graph for this function:

◆ colorBoxBorderColor()

QColor PiecesBar::colorBoxBorderColor ( ) const
protected

Definition at line 221 of file piecesbar.cpp.

222 {
223  return palette().color(QPalette::ToolTipText);
224 }

Referenced by DownloadedPiecesBar::simpleToolTipText(), and PieceAvailabilityBar::simpleToolTipText().

Here is the caller graph for this function:

◆ enterEvent()

void PiecesBar::enterEvent ( QEnterEvent *  e)
overrideprotected

Definition at line 145 of file piecesbar.cpp.

149 {
150  m_hovered = true;
151  base::enterEvent(e);
152 }
bool m_hovered
Definition: piecesbar.h:98

References nova3.nova2dl::e.

◆ event()

bool PiecesBar::event ( QEvent *  e)
overridevirtual

Definition at line 133 of file piecesbar.cpp.

134 {
135  if (e->type() == QEvent::ToolTip)
136  {
137  showToolTip(static_cast<QHelpEvent *>(e));
138  return true;
139  }
140 
141  return base::event(e);
142 }
void showToolTip(const QHelpEvent *)
Definition: piecesbar.cpp:249

References nova3.nova2dl::e, and showToolTip().

Here is the call graph for this function:

◆ highlightFile()

void PiecesBar::highlightFile ( int  imagePos)
private

Definition at line 305 of file piecesbar.cpp.

306 {
307  if (!m_torrent || !m_torrent->hasMetadata() || (imagePos < 0) || (imagePos >= m_image.width()))
308  return;
309 
310  const BitTorrent::TorrentInfo torrentInfo = m_torrent->info();
311  PieceIndexToImagePos transform {torrentInfo, m_image};
312 
313  int pieceIndex = transform.pieceIndex(imagePos);
314  QVector<int> fileIndices {torrentInfo.fileIndicesForPiece(pieceIndex)};
315  if (fileIndices.count() == 1)
316  {
317  BitTorrent::TorrentInfo::PieceRange filePieces = torrentInfo.filePieces(fileIndices.first());
318 
319  ImageRange imageRange = transform.imagePos(filePieces);
320  QRect newHighlightedRegion {imageRange.first(), 0, imageRange.size(), m_image.height()};
321  if (newHighlightedRegion != m_highlightedRegion)
322  {
323  m_highlightedRegion = newHighlightedRegion;
324  update();
325  }
326  }
327  else if (!m_highlightedRegion.isEmpty())
328  {
329  m_highlightedRegion = {};
330  update();
331  }
332 }
virtual TorrentInfo info() const =0
virtual bool hasMetadata() const =0
PieceRange filePieces(const QString &file) const
QVector< int > fileIndicesForPiece(int pieceIndex) const
const BitTorrent::Torrent * m_torrent
Definition: piecesbar.h:94
QRect m_highlightedRegion
Definition: piecesbar.h:99

References BitTorrent::TorrentInfo::fileIndicesForPiece(), BitTorrent::TorrentInfo::filePieces(), BitTorrent::Torrent::hasMetadata(), BitTorrent::Torrent::info(), m_highlightedRegion, m_image, and m_torrent.

Referenced by mouseMoveEvent().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ leaveEvent()

void PiecesBar::leaveEvent ( QEvent *  e)
overrideprotected

Definition at line 154 of file piecesbar.cpp.

155 {
156  m_hovered = false;
157  m_highlightedRegion = {};
159  base::leaveEvent(e);
160 }
void requestImageUpdate()
Definition: piecesbar.cpp:200

References nova3.nova2dl::e, m_highlightedRegion, m_hovered, and requestImageUpdate().

Here is the call graph for this function:

◆ mixTwoColors()

QRgb PiecesBar::mixTwoColors ( QRgb  rgb1,
QRgb  rgb2,
float  ratio 
)
staticprotected

Definition at line 231 of file piecesbar.cpp.

232 {
233  int r1 = qRed(rgb1);
234  int g1 = qGreen(rgb1);
235  int b1 = qBlue(rgb1);
236 
237  int r2 = qRed(rgb2);
238  int g2 = qGreen(rgb2);
239  int b2 = qBlue(rgb2);
240 
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);
245 
246  return qRgb(r, g, b);
247 }

Referenced by DownloadedPiecesBar::updateImage(), and updatePieceColors().

Here is the caller graph for this function:

◆ mouseMoveEvent()

void PiecesBar::mouseMoveEvent ( QMouseEvent *  e)
overrideprotected

Definition at line 162 of file piecesbar.cpp.

163 {
164  // if user pointed to a piece which is a part of a single large file,
165  // we highlight the space, occupied by this file
166  highlightFile(e->pos().x() - borderWidth);
167  base::mouseMoveEvent(e);
168 }
void highlightFile(int imagePos)
Definition: piecesbar.cpp:305
static constexpr int borderWidth
Definition: piecesbar.h:81

References borderWidth, nova3.nova2dl::e, and highlightFile().

Here is the call graph for this function:

◆ paintEvent()

void PiecesBar::paintEvent ( QPaintEvent *  e)
overrideprotected

Definition at line 170 of file piecesbar.cpp.

171 {
172  QPainter painter(this);
173  QRect imageRect(borderWidth, borderWidth, width() - 2 * borderWidth, height() - 2 * borderWidth);
174  if (m_image.isNull())
175  {
176  painter.setBrush(backgroundColor());
177  painter.drawRect(imageRect);
178  }
179  else
180  {
181  if (m_image.width() != imageRect.width())
183  painter.drawImage(imageRect, m_image);
184  }
185 
186  if (!m_highlightedRegion.isNull())
187  {
188  QColor highlightColor {this->palette().color(QPalette::Active, QPalette::Highlight)};
189  highlightColor.setAlphaF(0.35f);
190  QRect targetHighlightRect {m_highlightedRegion.adjusted(borderWidth, borderWidth, borderWidth, height() - 2 * borderWidth)};
191  painter.fillRect(targetHighlightRect, highlightColor);
192  }
193 
194  QPainterPath border;
195  border.addRect(0, 0, width(), height());
196  painter.setPen(borderColor());
197  painter.drawPath(border);
198 }
QColor backgroundColor() const
Definition: piecesbar.cpp:206
virtual bool updateImage(QImage &image)=0
QColor borderColor() const
Definition: piecesbar.cpp:211

References backgroundColor(), borderColor(), borderWidth, m_highlightedRegion, m_image, and updateImage().

Here is the call graph for this function:

◆ pieceColor()

QColor PiecesBar::pieceColor ( ) const
protected

Definition at line 216 of file piecesbar.cpp.

217 {
218  return palette().color(QPalette::Highlight);
219 }

Referenced by DownloadedPiecesBar::simpleToolTipText(), PieceAvailabilityBar::simpleToolTipText(), DownloadedPiecesBar::updateImage(), and updatePieceColors().

Here is the caller graph for this function:

◆ pieceColors()

const QVector< QRgb > & PiecesBar::pieceColors ( ) const
protected

Definition at line 226 of file piecesbar.cpp.

227 {
228  return m_pieceColors;
229 }
QVector< QRgb > m_pieceColors
Definition: piecesbar.h:97

References m_pieceColors.

Referenced by DownloadedPiecesBar::updateImage(), and PieceAvailabilityBar::updateImage().

Here is the caller graph for this function:

◆ requestImageUpdate()

void PiecesBar::requestImageUpdate ( )
protected

Definition at line 200 of file piecesbar.cpp.

201 {
202  if (updateImage(m_image))
203  update();
204 }

References m_image, and updateImage().

Referenced by leaveEvent(), PieceAvailabilityBar::setAvailability(), and DownloadedPiecesBar::setProgress().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setTorrent()

void PiecesBar::setTorrent ( const BitTorrent::Torrent torrent)

Definition at line 120 of file piecesbar.cpp.

121 {
122  m_torrent = torrent;
123  if (!m_torrent)
124  clear();
125 }
virtual void clear()
Definition: piecesbar.cpp:127

References clear(), and m_torrent.

Referenced by PropertiesWidget::loadTorrentInfos().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ showToolTip()

void PiecesBar::showToolTip ( const QHelpEvent *  e)
private

Definition at line 249 of file piecesbar.cpp.

250 {
251  if (!m_torrent)
252  return;
253 
254  QString toolTipText;
255  QTextStream stream(&toolTipText, QIODevice::WriteOnly);
256  const bool showDetailedInformation = QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier);
257  if (showDetailedInformation && m_torrent->hasMetadata())
258  {
259  const BitTorrent::TorrentInfo torrentInfo = m_torrent->info();
260  const int imagePos = e->pos().x() - borderWidth;
261  if ((imagePos >=0) && (imagePos < m_image.width()))
262  {
263  stream << "<html><body>";
264  PieceIndexToImagePos transform {torrentInfo, m_image};
265  int pieceIndex = transform.pieceIndex(imagePos);
266  const QVector<int> files {torrentInfo.fileIndicesForPiece(pieceIndex)};
267 
268  QString tooltipTitle;
269  if (files.count() > 1)
270  {
271  tooltipTitle = tr("Files in this piece:");
272  }
273  else
274  {
275  if (torrentInfo.fileSize(files.front()) == torrentInfo.pieceLength(pieceIndex))
276  tooltipTitle = tr("File in this piece");
277  else
278  tooltipTitle = tr("File in these pieces");
279  }
280 
281  DetailedTooltipRenderer renderer(stream, tooltipTitle);
282 
283  for (int f : files)
284  {
285  const QString filePath {torrentInfo.filePath(f)};
286  renderer(Utils::Misc::friendlyUnit(torrentInfo.fileSize(f)), filePath);
287  }
288  stream << "</body></html>";
289  }
290  }
291  else
292  {
293  stream << simpleToolTipText();
294  if (showDetailedInformation) // metadata are not available at this point
295  stream << '\n' << tr("Wait until metadata become available to see detailed information");
296  else
297  stream << '\n' << tr("Hold Shift key for detailed information");
298  }
299 
300  stream.flush();
301 
302  QToolTip::showText(e->globalPos(), toolTipText, this);
303 }
QString filePath(int index) const
qlonglong fileSize(int index) const
virtual QString simpleToolTipText() const =0
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)
Definition: misc.cpp:261
void f()
Definition: test2.c:1

References borderWidth, nova3.nova2dl::e, f(), BitTorrent::TorrentInfo::fileIndicesForPiece(), BitTorrent::TorrentInfo::filePath(), files(), BitTorrent::TorrentInfo::fileSize(), Utils::Misc::friendlyUnit(), BitTorrent::Torrent::hasMetadata(), BitTorrent::Torrent::info(), m_image, m_torrent, BitTorrent::TorrentInfo::pieceLength(), and simpleToolTipText().

Referenced by event().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ simpleToolTipText()

virtual QString PiecesBar::simpleToolTipText ( ) const
privatepure virtual

Implemented in PieceAvailabilityBar, and DownloadedPiecesBar.

Referenced by showToolTip().

Here is the caller graph for this function:

◆ updateImage()

virtual bool PiecesBar::updateImage ( QImage &  image)
privatepure virtual

Implemented in PieceAvailabilityBar, and DownloadedPiecesBar.

Referenced by paintEvent(), and requestImageUpdate().

Here is the caller graph for this function:

◆ updatePieceColors()

void PiecesBar::updatePieceColors ( )
private

Definition at line 334 of file piecesbar.cpp.

335 {
336  m_pieceColors = QVector<QRgb>(256);
337  for (int i = 0; i < 256; ++i)
338  {
339  float ratio = (i / 255.0);
340  m_pieceColors[i] = mixTwoColors(backgroundColor().rgb(), pieceColor().rgb(), ratio);
341  }
342 }
static QRgb mixTwoColors(QRgb rgb1, QRgb rgb2, float ratio)
Definition: piecesbar.cpp:231
QColor pieceColor() const
Definition: piecesbar.cpp:216

References backgroundColor(), m_pieceColors, mixTwoColors(), and pieceColor().

Referenced by PiecesBar().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ borderWidth

constexpr int PiecesBar::borderWidth = 1
staticconstexprprotected

◆ m_highlightedRegion

QRect PiecesBar::m_highlightedRegion
private

Definition at line 99 of file piecesbar.h.

Referenced by highlightFile(), leaveEvent(), and paintEvent().

◆ m_hovered

bool PiecesBar::m_hovered = false
private

Definition at line 98 of file piecesbar.h.

Referenced by leaveEvent().

◆ m_image

QImage PiecesBar::m_image
private

Definition at line 95 of file piecesbar.h.

Referenced by clear(), highlightFile(), paintEvent(), requestImageUpdate(), and showToolTip().

◆ m_pieceColors

QVector<QRgb> PiecesBar::m_pieceColors
private

Definition at line 97 of file piecesbar.h.

Referenced by pieceColors(), and updatePieceColors().

◆ m_torrent

const BitTorrent::Torrent* PiecesBar::m_torrent = nullptr
private

Definition at line 94 of file piecesbar.h.

Referenced by highlightFile(), setTorrent(), and showToolTip().


The documentation for this class was generated from the following files: