qBittorrent
pieceavailabilitybar.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2006 Christophe Dumez <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "pieceavailabilitybar.h"
30 
31 #include <algorithm>
32 #include <cmath>
33 
34 #include <QDebug>
35 
37  : base {parent}
38 {
39 }
40 
41 QVector<float> PieceAvailabilityBar::intToFloatVector(const QVector<int> &vecin, int reqSize)
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 }
126 
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 }
154 
155 void PieceAvailabilityBar::setAvailability(const QVector<int> &avail)
156 {
157  m_pieces = avail;
158 
160 }
161 
163 {
164  m_pieces.clear();
165  base::clear();
166 }
167 
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 }
PieceAvailabilityBar(QWidget *parent)
void setAvailability(const QVector< int > &avail)
QString simpleToolTipText() const override
QVector< float > intToFloatVector(const QVector< int > &vecin, int reqSize)
bool updateImage(QImage &image) override
QColor colorBoxBorderColor() const
Definition: piecesbar.cpp:221
QColor pieceColor() const
Definition: piecesbar.cpp:216
QColor backgroundColor() const
Definition: piecesbar.cpp:206
void requestImageUpdate()
Definition: piecesbar.cpp:200
static constexpr int borderWidth
Definition: piecesbar.h:81
virtual void clear()
Definition: piecesbar.cpp:127
const QVector< QRgb > & pieceColors() const
Definition: piecesbar.cpp:226
QColor borderColor() const
Definition: piecesbar.cpp:211
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64