qBittorrent
downloadedpiecesbar.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 "downloadedpiecesbar.h"
30 
31 #include <algorithm>
32 #include <cmath>
33 
34 #include <QDebug>
35 #include <QVector>
36 
37 namespace
38 {
39  QColor dlPieceColor(const QColor &pieceColor)
40  {
41  const QColor green {Qt::green};
42  return QColor::fromHsl(green.hslHue(), pieceColor.hslSaturation(), pieceColor.lightness());
43  }
44 }
45 
47  : base {parent}
48  , m_dlPieceColor {dlPieceColor(pieceColor())}
49 {
50 }
51 
52 QVector<float> DownloadedPiecesBar::bitfieldToFloatVector(const QBitArray &vecin, int reqSize)
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 }
128 
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 }
172 
173 void DownloadedPiecesBar::setProgress(const QBitArray &pieces, const QBitArray &downloadedPieces)
174 {
175  m_pieces = pieces;
176  m_downloadedPieces = downloadedPieces;
177 
179 }
180 
182 {
183  m_pieces.clear();
184  m_downloadedPieces.clear();
185  base::clear();
186 }
187 
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 }
DownloadedPiecesBar(QWidget *parent)
QVector< float > bitfieldToFloatVector(const QBitArray &vecin, int reqSize)
QString simpleToolTipText() const override
virtual bool updateImage(QImage &image) override
void setProgress(const QBitArray &pieces, const QBitArray &downloadedPieces)
static QRgb mixTwoColors(QRgb rgb1, QRgb rgb2, float ratio)
Definition: piecesbar.cpp:231
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
QColor dlPieceColor(const QColor &pieceColor)
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64