qBittorrent
DownloadedPiecesBar Class Referencefinal

#include <downloadedpiecesbar.h>

Inheritance diagram for DownloadedPiecesBar:
Collaboration diagram for DownloadedPiecesBar:

Public Member Functions

 DownloadedPiecesBar (QWidget *parent)
 
void setProgress (const QBitArray &pieces, const QBitArray &downloadedPieces)
 
void clear () override
 
- Public Member Functions inherited from PiecesBar
 PiecesBar (QWidget *parent=nullptr)
 
void setTorrent (const BitTorrent::Torrent *torrent)
 
virtual bool event (QEvent *e) override
 

Private Types

using base = PiecesBar
 

Private Member Functions

QVector< float > bitfieldToFloatVector (const QBitArray &vecin, int reqSize)
 
virtual bool updateImage (QImage &image) override
 
QString simpleToolTipText () const override
 

Private Attributes

const QColor m_dlPieceColor
 
QBitArray m_pieces
 
QBitArray m_downloadedPieces
 

Additional Inherited Members

- Protected Member Functions inherited from PiecesBar
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 inherited from PiecesBar
static QRgb mixTwoColors (QRgb rgb1, QRgb rgb2, float ratio)
 
- Static Protected Attributes inherited from PiecesBar
static constexpr int borderWidth = 1
 

Detailed Description

Definition at line 38 of file downloadedpiecesbar.h.

Member Typedef Documentation

◆ base

Definition at line 40 of file downloadedpiecesbar.h.

Constructor & Destructor Documentation

◆ DownloadedPiecesBar()

DownloadedPiecesBar::DownloadedPiecesBar ( QWidget *  parent)

Definition at line 46 of file downloadedpiecesbar.cpp.

47  : base {parent}
49 {
50 }
QColor pieceColor() const
Definition: piecesbar.cpp:216
QColor dlPieceColor(const QColor &pieceColor)

Member Function Documentation

◆ bitfieldToFloatVector()

QVector< float > DownloadedPiecesBar::bitfieldToFloatVector ( const QBitArray &  vecin,
int  reqSize 
)
private

Definition at line 52 of file downloadedpiecesbar.cpp.

53 {
54  QVector<float> result(reqSize, 0.0);
55  if (vecin.isEmpty()) return result;
56 
57  const float ratio = vecin.size() / static_cast<float>(reqSize);
58 
59  // simple linear transformation algorithm
60  // for example:
61  // image.x(0) = pieces.x(0.0 >= x < 1.7)
62  // image.x(1) = pieces.x(1.7 >= x < 3.4)
63 
64  for (int x = 0; x < reqSize; ++x)
65  {
66  // R - real
67  const float fromR = x * ratio;
68  const float toR = (x + 1) * ratio;
69 
70  // C - integer
71  int fromC = fromR; // std::floor not needed
72  int toC = std::ceil(toR);
73  if (toC > vecin.size())
74  --toC;
75 
76  // position in pieces table
77  int x2 = fromC;
78 
79  // little speed up for really big pieces table, 10K+ size
80  const int toCMinusOne = toC - 1;
81 
82  // value in returned vector
83  float value = 0;
84 
85  // case when calculated range is (15.2 >= x < 15.7)
86  if (x2 == toCMinusOne)
87  {
88  if (vecin[x2])
89  value += ratio;
90  ++x2;
91  }
92  // case when (15.2 >= x < 17.8)
93  else
94  {
95  // subcase (15.2 >= x < 16)
96  if (x2 != fromR)
97  {
98  if (vecin[x2])
99  value += 1.0 - (fromR - fromC);
100  ++x2;
101  }
102 
103  // subcase (16 >= x < 17)
104  for (; x2 < toCMinusOne; ++x2)
105  if (vecin[x2])
106  value += 1.0;
107 
108  // subcase (17 >= x < 17.8)
109  if (x2 == toCMinusOne)
110  {
111  if (vecin[x2])
112  value += 1.0 - (toC - toR);
113  ++x2;
114  }
115  }
116 
117  // normalization <0, 1>
118  value /= ratio;
119 
120  // float precision sometimes gives > 1, because it's not possible to store irrational numbers
121  value = std::min(value, 1.0f);
122 
123  result[x] = value;
124  }
125 
126  return result;
127 }
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64

References anonymous_namespace{preferences.cpp}::value().

Referenced by updateImage().

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

◆ clear()

void DownloadedPiecesBar::clear ( )
overridevirtual

Reimplemented from PiecesBar.

Definition at line 181 of file downloadedpiecesbar.cpp.

182 {
183  m_pieces.clear();
184  m_downloadedPieces.clear();
185  base::clear();
186 }
virtual void clear()
Definition: piecesbar.cpp:127

References PiecesBar::clear(), m_downloadedPieces, and m_pieces.

Referenced by PropertiesWidget::clear().

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

◆ setProgress()

void DownloadedPiecesBar::setProgress ( const QBitArray &  pieces,
const QBitArray &  downloadedPieces 
)

Definition at line 173 of file downloadedpiecesbar.cpp.

174 {
175  m_pieces = pieces;
176  m_downloadedPieces = downloadedPieces;
177 
179 }
void requestImageUpdate()
Definition: piecesbar.cpp:200

References m_downloadedPieces, m_pieces, and PiecesBar::requestImageUpdate().

Referenced by PropertiesWidget::loadDynamicData().

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

◆ simpleToolTipText()

QString DownloadedPiecesBar::simpleToolTipText ( ) const
overrideprivatevirtual

Implements PiecesBar.

Definition at line 188 of file downloadedpiecesbar.cpp.

189 {
190  const QString borderColor = colorBoxBorderColor().name();
191  const QString rowHTML = QString::fromLatin1("<tr><td width=20 bgcolor='%1' style='border: 1px solid \"%2\";'></td><td>%3</td></tr>");
192  return QLatin1String("<table cellspacing=4>")
193  + rowHTML.arg(backgroundColor().name(), borderColor, tr("Missing pieces"))
194  + rowHTML.arg(m_dlPieceColor.name(), borderColor, tr("Partial pieces"))
195  + rowHTML.arg(pieceColor().name(), borderColor, tr("Completed pieces"))
196  + QLatin1String("</table>");
197 }
QColor colorBoxBorderColor() const
Definition: piecesbar.cpp:221
QColor backgroundColor() const
Definition: piecesbar.cpp:206
QColor borderColor() const
Definition: piecesbar.cpp:211

References PiecesBar::backgroundColor(), PiecesBar::borderColor(), PiecesBar::colorBoxBorderColor(), m_dlPieceColor, and PiecesBar::pieceColor().

Here is the call graph for this function:

◆ updateImage()

bool DownloadedPiecesBar::updateImage ( QImage &  image)
overrideprivatevirtual

Implements PiecesBar.

Definition at line 129 of file downloadedpiecesbar.cpp.

130 {
131  // qDebug() << "updateImage";
132  QImage image2(width() - 2 * borderWidth, 1, QImage::Format_RGB888);
133  if (image2.isNull())
134  {
135  qDebug() << "QImage image2() allocation failed, width():" << width();
136  return false;
137  }
138 
139  if (m_pieces.isEmpty())
140  {
141  image2.fill(backgroundColor());
142  image = image2;
143  return true;
144  }
145 
146  QVector<float> scaledPieces = bitfieldToFloatVector(m_pieces, image2.width());
147  QVector<float> scaledPiecesDl = bitfieldToFloatVector(m_downloadedPieces, image2.width());
148 
149  // filling image
150  for (int x = 0; x < scaledPieces.size(); ++x)
151  {
152  float piecesToValue = scaledPieces.at(x);
153  float piecesToValueDl = scaledPiecesDl.at(x);
154  if (piecesToValueDl != 0)
155  {
156  float fillRatio = piecesToValue + piecesToValueDl;
157  float ratio = piecesToValueDl / fillRatio;
158 
159  QRgb mixedColor = mixTwoColors(pieceColor().rgb(), m_dlPieceColor.rgb(), ratio);
160  mixedColor = mixTwoColors(backgroundColor().rgb(), mixedColor, fillRatio);
161 
162  image2.setPixel(x, 0, mixedColor);
163  }
164  else
165  {
166  image2.setPixel(x, 0, pieceColors()[piecesToValue * 255]);
167  }
168  }
169  image = image2;
170  return true;
171 }
QVector< float > bitfieldToFloatVector(const QBitArray &vecin, int reqSize)
static QRgb mixTwoColors(QRgb rgb1, QRgb rgb2, float ratio)
Definition: piecesbar.cpp:231
static constexpr int borderWidth
Definition: piecesbar.h:81
const QVector< QRgb > & pieceColors() const
Definition: piecesbar.cpp:226

References PiecesBar::backgroundColor(), bitfieldToFloatVector(), PiecesBar::borderWidth, m_dlPieceColor, m_downloadedPieces, m_pieces, PiecesBar::mixTwoColors(), PiecesBar::pieceColor(), and PiecesBar::pieceColors().

Here is the call graph for this function:

Member Data Documentation

◆ m_dlPieceColor

const QColor DownloadedPiecesBar::m_dlPieceColor
private

Definition at line 59 of file downloadedpiecesbar.h.

Referenced by simpleToolTipText(), and updateImage().

◆ m_downloadedPieces

QBitArray DownloadedPiecesBar::m_downloadedPieces
private

Definition at line 63 of file downloadedpiecesbar.h.

Referenced by clear(), setProgress(), and updateImage().

◆ m_pieces

QBitArray DownloadedPiecesBar::m_pieces
private

Definition at line 62 of file downloadedpiecesbar.h.

Referenced by clear(), setProgress(), and updateImage().


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