Browse Source

Cosmetic changes to the piece availability and downloaded pieces bars

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
35c1389794
  1. 54
      src/properties/downloadedpiecesbar.h
  2. 60
      src/properties/pieceavailabilitybar.h

54
src/properties/downloadedpiecesbar.h

@ -86,39 +86,9 @@ public: @@ -86,39 +86,9 @@ public:
}
++scaled_index;
}
QPixmap pix = QPixmap(scaled_pieces.size(), 1);
//pix.fill();
QPainter painter(&pix);
for(uint i=0; i<scaled_pieces.size(); ++i) {
if(scaled_pieces[i]) {
painter.setPen(Qt::blue);
} else {
if(scaled_downloading[i]) {
painter.setPen(Qt::yellow);
} else {
painter.setPen(Qt::white);
}
}
painter.drawPoint(i,0);
}
pixmap = pix;
updatePixmap(scaled_pieces, scaled_downloading);
} else {
QPixmap pix = QPixmap(pieces.size(), 1);
//pix.fill();
QPainter painter(&pix);
for(uint i=0; i<pieces.size(); ++i) {
if(pieces[i]) {
painter.setPen(Qt::blue);
} else {
if(downloading_pieces[i]) {
painter.setPen(Qt::yellow);
} else {
painter.setPen(Qt::white);
}
}
painter.drawPoint(i,0);
}
pixmap = pix;
updatePixmap(pieces, downloading_pieces);
}
}
update();
@ -136,6 +106,26 @@ protected: @@ -136,6 +106,26 @@ protected:
painter.drawPixmap(rect(), pixmap);
}
private:
void updatePixmap(const libtorrent::bitfield &pieces, const libtorrent::bitfield &downloading_pieces) {
QPixmap pix = QPixmap(pieces.size(), 1);
//pix.fill();
QPainter painter(&pix);
for(uint i=0; i<pieces.size(); ++i) {
if(pieces[i]) {
painter.setPen(Qt::blue);
} else {
if(downloading_pieces[i]) {
painter.setPen(Qt::green);
} else {
painter.setPen(Qt::white);
}
}
painter.drawPoint(i,0);
}
pixmap = pix;
}
};
#endif // DOWNLOADEDPIECESBAR_H

60
src/properties/pieceavailabilitybar.h

@ -36,7 +36,8 @@ @@ -36,7 +36,8 @@
#include <QPixmap>
#include <QColor>
#include <numeric>
#include <math.h>
#include <cmath>
#include <algorithm>
#define BAR_HEIGHT 18
@ -53,52 +54,30 @@ public: @@ -53,52 +54,30 @@ public:
}
void setAvailability(const std::vector<int>& avail) {
qreal average = 0;
if(avail.empty()) {
// Empty bar
QPixmap pix = QPixmap(1, 1);
pix.fill();
pixmap = pix;
} else {
// Look for maximum value
const qulonglong nb_pieces = avail.size();
average = std::accumulate(avail.begin(), avail.end(), 0)/(double)nb_pieces;
// Reduce the number of pieces before creating the pixmap
// otherwise it can crash when there are too many pieces
const qulonglong nb_pieces = avail.size();
const uint w = width();
if(nb_pieces > w) {
const qulonglong ratio = floor(nb_pieces/(double)w);
std::vector<int> scaled_avail;
scaled_avail.reserve(ceil(nb_pieces/(double)ratio));
for(qulonglong i=0; i<nb_pieces; i+= ratio) {
/*qulonglong j = i;
qulonglong sum = avail[i];
for(j=i+1; j<qMin(i+ratio, nb_pieces); ++j) {
sum += avail[j];
}
scaled_avail.push_back(sum/(qMin(ratio, nb_pieces-i)));*/
// XXX: Do not compute the average to save cpu
scaled_avail.push_back(avail[i]);
}
QPixmap pix = QPixmap(scaled_avail.size(), 1);
//pix.fill();
QPainter painter(&pix);
for(qulonglong i=0; i < scaled_avail.size(); ++i) {
painter.setPen(getPieceColor(scaled_avail[i], average));
painter.drawPoint(i,0);
}
pixmap = pix;
updatePixmap(scaled_avail);
} else {
QPixmap pix = QPixmap(nb_pieces, 1);
//pix.fill();
QPainter painter(&pix);
for(qulonglong i=0; i < nb_pieces; ++i) {
painter.setPen(getPieceColor(avail[i], average));
painter.drawPoint(i,0);
}
pixmap = pix;
updatePixmap(avail);
}
}
update();
}
@ -114,15 +93,24 @@ protected: @@ -114,15 +93,24 @@ protected:
painter.drawPixmap(rect(), pixmap);
}
QColor getPieceColor(int avail, qreal average) {
if(!avail) return Qt::white;
//qDebug("avail: %d/%d", avail, max_avail);
qreal fraction = 100.*average/avail;
if(fraction < 100)
fraction *= 0.8;
else
fraction *= 1.2;
return QColor(Qt::blue).lighter(fraction);
private:
void updatePixmap(const std::vector<int> avail) {
const int max = *std::max_element(avail.begin(), avail.end());
if(max == 0) {
QPixmap pix = QPixmap(1, 1);
pix.fill();
pixmap = pix;
return;
}
QPixmap pix = QPixmap(avail.size(), 1);
//pix.fill();
QPainter painter(&pix);
for(uint i=0; i < avail.size(); ++i) {
const uint rg = 0xff - (0xff * avail[i]/max);
painter.setPen(QColor(rg, rg, 0xff));
painter.drawPoint(i,0);
}
pixmap = pix;
}
};

Loading…
Cancel
Save