qBittorrent
PieceAvailabilityBar Class Referencefinal

#include <pieceavailabilitybar.h>

Inheritance diagram for PieceAvailabilityBar:
Collaboration diagram for PieceAvailabilityBar:

Public Member Functions

 PieceAvailabilityBar (QWidget *parent)
 
void setAvailability (const QVector< int > &avail)
 
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

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

Private Attributes

QVector< int > m_pieces
 

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 33 of file pieceavailabilitybar.h.

Member Typedef Documentation

◆ base

Definition at line 35 of file pieceavailabilitybar.h.

Constructor & Destructor Documentation

◆ PieceAvailabilityBar()

PieceAvailabilityBar::PieceAvailabilityBar ( QWidget *  parent)

Definition at line 36 of file pieceavailabilitybar.cpp.

37  : base {parent}
38 {
39 }

Member Function Documentation

◆ clear()

void PieceAvailabilityBar::clear ( )
overridevirtual

Reimplemented from PiecesBar.

Definition at line 162 of file pieceavailabilitybar.cpp.

163 {
164  m_pieces.clear();
165  base::clear();
166 }
virtual void clear()
Definition: piecesbar.cpp:127

References PiecesBar::clear(), and m_pieces.

Referenced by PropertiesWidget::clear().

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

◆ intToFloatVector()

QVector< float > PieceAvailabilityBar::intToFloatVector ( const QVector< int > &  vecin,
int  reqSize 
)
private

Definition at line 41 of file pieceavailabilitybar.cpp.

42 {
43  QVector<float> result(reqSize, 0.0);
44  if (vecin.isEmpty()) return result;
45 
46  const float ratio = static_cast<float>(vecin.size()) / reqSize;
47 
48  const int maxElement = *std::max_element(vecin.begin(), vecin.end());
49 
50  // std::max because in normalization we don't want divide by 0
51  // if maxElement == 0 check will be disabled please enable this line:
52  // const int maxElement = std::max(*std::max_element(avail.begin(), avail.end()), 1);
53 
54  if (maxElement == 0)
55  return result;
56 
57  // simple linear transformation algorithm
58  // for example:
59  // image.x(0) = pieces.x(0.0 >= x < 1.7)
60  // image.x(1) = pieces.x(1.7 >= x < 3.4)
61 
62  for (int x = 0; x < reqSize; ++x)
63  {
64  // R - real
65  const float fromR = x * ratio;
66  const float toR = (x + 1) * ratio;
67 
68  // C - integer
69  int fromC = fromR;// std::floor not needed
70  int toC = std::ceil(toR);
71  if (toC > vecin.size())
72  --toC;
73 
74  // position in pieces table
75  int x2 = fromC;
76 
77  // little speed up for really big pieces table, 10K+ size
78  const int toCMinusOne = toC - 1;
79 
80  // value in returned vector
81  float value = 0;
82 
83  // case when calculated range is (15.2 >= x < 15.7)
84  if (x2 == toCMinusOne)
85  {
86  if (vecin[x2])
87  value += ratio * vecin[x2];
88  ++x2;
89  }
90  // case when (15.2 >= x < 17.8)
91  else
92  {
93  // subcase (15.2 >= x < 16)
94  if (x2 != fromR)
95  {
96  if (vecin[x2])
97  value += (1.0 - (fromR - fromC)) * vecin[x2];
98  ++x2;
99  }
100 
101  // subcase (16 >= x < 17)
102  for (; x2 < toCMinusOne; ++x2)
103  if (vecin[x2])
104  value += vecin[x2];
105 
106  // subcase (17 >= x < 17.8)
107  if (x2 == toCMinusOne)
108  {
109  if (vecin[x2])
110  value += (1.0 - (toC - toR)) * vecin[x2];
111  ++x2;
112  }
113  }
114 
115  // normalization <0, 1>
116  value /= ratio * maxElement;
117 
118  // float precision sometimes gives > 1, because it's not possible to store irrational numbers
119  value = std::min(value, 1.0f);
120 
121  result[x] = value;
122  }
123 
124  return result;
125 }
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:

◆ setAvailability()

void PieceAvailabilityBar::setAvailability ( const QVector< int > &  avail)

Definition at line 155 of file pieceavailabilitybar.cpp.

156 {
157  m_pieces = avail;
158 
160 }
void requestImageUpdate()
Definition: piecesbar.cpp:200

References 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 PieceAvailabilityBar::simpleToolTipText ( ) const
overrideprivatevirtual

Implements PiecesBar.

Definition at line 168 of file pieceavailabilitybar.cpp.

169 {
170  const QString borderColor = colorBoxBorderColor().name();
171  const QString rowHTML = QString::fromLatin1("<tr><td width=20 bgcolor='%1' style='border: 1px solid \"%2\";'></td><td>%3</td></tr>");
172  return QLatin1String("<table cellspacing=4>")
173  + rowHTML.arg(backgroundColor().name(), borderColor, tr("Unavailable pieces"))
174  + rowHTML.arg(pieceColor().name(), borderColor, tr("Available pieces"))
175  + QLatin1String("</table>");
176 }
QColor colorBoxBorderColor() const
Definition: piecesbar.cpp:221
QColor pieceColor() const
Definition: piecesbar.cpp:216
QColor backgroundColor() const
Definition: piecesbar.cpp:206
QColor borderColor() const
Definition: piecesbar.cpp:211

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

Here is the call graph for this function:

◆ updateImage()

bool PieceAvailabilityBar::updateImage ( QImage &  image)
overrideprivatevirtual

Implements PiecesBar.

Definition at line 127 of file pieceavailabilitybar.cpp.

128 {
129  QImage image2(width() - 2 * borderWidth, 1, QImage::Format_RGB888);
130  if (image2.isNull())
131  {
132  qDebug() << "QImage image2() allocation failed, width():" << width();
133  return false;
134  }
135 
136  if (m_pieces.empty())
137  {
138  image2.fill(backgroundColor());
139  image = image2;
140  return true;
141  }
142 
143  QVector<float> scaledPieces = intToFloatVector(m_pieces, image2.width());
144 
145  // filling image
146  for (int x = 0; x < scaledPieces.size(); ++x)
147  {
148  float piecesToValue = scaledPieces.at(x);
149  image2.setPixel(x, 0, pieceColors()[piecesToValue * 255]);
150  }
151  image = image2;
152  return true;
153 }
QVector< float > intToFloatVector(const QVector< int > &vecin, int reqSize)
static constexpr int borderWidth
Definition: piecesbar.h:81
const QVector< QRgb > & pieceColors() const
Definition: piecesbar.cpp:226

References PiecesBar::backgroundColor(), PiecesBar::borderWidth, intToFloatVector(), m_pieces, and PiecesBar::pieceColors().

Here is the call graph for this function:

Member Data Documentation

◆ m_pieces

QVector<int> PieceAvailabilityBar::m_pieces
private

Definition at line 53 of file pieceavailabilitybar.h.

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


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