Browse Source

Performance improvement for piece availability and downloaded pieces widgets

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
67973515d5
  1. 21
      src/downloadedpiecesbar.h
  2. 20
      src/pieceavailabilitybar.h

21
src/downloadedpiecesbar.h

@ -63,28 +63,31 @@ public:
const qulonglong nb_pieces = pieces.size(); const qulonglong nb_pieces = pieces.size();
// Reduce the number of pieces before creating the pixmap // Reduce the number of pieces before creating the pixmap
// otherwise it can crash when there are too many pieces // otherwise it can crash when there are too many pieces
if(nb_pieces > (uint)width()) { const uint w = width();
const int ratio = floor(nb_pieces/(double)width()); if(nb_pieces > w) {
std::vector<bool> scaled_pieces; const uint ratio = floor(nb_pieces/(double)w);
std::vector<bool> scaled_downloading; bitfield scaled_pieces(ceil(nb_pieces/(double)ratio), false);
bitfield scaled_downloading(ceil(nb_pieces/(double)ratio), false);
uint scaled_index = 0;
for(qulonglong i=0; i<nb_pieces; i+= ratio) { for(qulonglong i=0; i<nb_pieces; i+= ratio) {
bool have = true; bool have = true;
for(qulonglong j=i; j<qMin(i+ratio, nb_pieces); ++j) { for(qulonglong j=i; j<qMin(i+ratio, nb_pieces); ++j) {
if(!pieces[i]) { have = false; break; } if(!pieces[i]) { have = false; break; }
} }
scaled_pieces.push_back(have);
if(have) { if(have) {
scaled_downloading.push_back(false); scaled_pieces.set_bit(scaled_index);
} else { } else {
bool downloading = false; bool downloading = false;
for(qulonglong j=i; j<qMin(i+ratio, nb_pieces); ++j) { for(qulonglong j=i; j<qMin(i+ratio, nb_pieces); ++j) {
if(downloading_pieces[i]) { downloading = true; break; } if(downloading_pieces[i]) { downloading = true; break; }
} }
scaled_downloading.push_back(downloading); if(downloading)
scaled_downloading.set_bit(scaled_index);
} }
++scaled_index;
} }
QPixmap pix = QPixmap(scaled_pieces.size(), 1); QPixmap pix = QPixmap(scaled_pieces.size(), 1);
pix.fill(); //pix.fill();
QPainter painter(&pix); QPainter painter(&pix);
for(uint i=0; i<scaled_pieces.size(); ++i) { for(uint i=0; i<scaled_pieces.size(); ++i) {
if(scaled_pieces[i]) { if(scaled_pieces[i]) {
@ -101,7 +104,7 @@ public:
pixmap = pix; pixmap = pix;
} else { } else {
QPixmap pix = QPixmap(pieces.size(), 1); QPixmap pix = QPixmap(pieces.size(), 1);
pix.fill(); //pix.fill();
QPainter painter(&pix); QPainter painter(&pix);
for(uint i=0; i<pieces.size(); ++i) { for(uint i=0; i<pieces.size(); ++i) {
if(pieces[i]) { if(pieces[i]) {

20
src/pieceavailabilitybar.h

@ -60,23 +60,25 @@ public:
pixmap = pix; pixmap = pix;
} else { } else {
// Look for maximum value // Look for maximum value
const int nb_pieces = avail.size(); const qulonglong nb_pieces = avail.size();
average = std::accumulate(avail.begin(), avail.end(), 0)/(double)nb_pieces; average = std::accumulate(avail.begin(), avail.end(), 0)/(double)nb_pieces;
// Reduce the number of pieces before creating the pixmap // Reduce the number of pieces before creating the pixmap
// otherwise it can crash when there are too many pieces // otherwise it can crash when there are too many pieces
if(nb_pieces > width()) { const uint w = width();
const int ratio = floor(nb_pieces/(double)width()); if(nb_pieces > w) {
const qulonglong ratio = floor(nb_pieces/(double)w);
std::vector<int> scaled_avail; std::vector<int> scaled_avail;
for(int i=0; i<nb_pieces; i+= ratio) { scaled_avail.reserve(ceil(nb_pieces/(double)ratio));
int j = i; for(qulonglong i=0; i<nb_pieces; i+= ratio) {
int sum = avail[i]; qulonglong j = i;
qulonglong sum = avail[i];
for(j=i+1; j<qMin(i+ratio, nb_pieces); ++j) { for(j=i+1; j<qMin(i+ratio, nb_pieces); ++j) {
sum += avail[j]; sum += avail[j];
} }
scaled_avail.push_back(sum/(qMin(ratio, nb_pieces-i))); scaled_avail.push_back(sum/(qMin(ratio, nb_pieces-i)));
} }
QPixmap pix = QPixmap(scaled_avail.size(), 1); QPixmap pix = QPixmap(scaled_avail.size(), 1);
pix.fill(); //pix.fill();
QPainter painter(&pix); QPainter painter(&pix);
for(qulonglong i=0; i < scaled_avail.size(); ++i) { for(qulonglong i=0; i < scaled_avail.size(); ++i) {
painter.setPen(getPieceColor(scaled_avail[i], average)); painter.setPen(getPieceColor(scaled_avail[i], average));
@ -85,9 +87,9 @@ public:
pixmap = pix; pixmap = pix;
} else { } else {
QPixmap pix = QPixmap(nb_pieces, 1); QPixmap pix = QPixmap(nb_pieces, 1);
pix.fill(); //pix.fill();
QPainter painter(&pix); QPainter painter(&pix);
for(int i=0; i < nb_pieces; ++i) { for(qulonglong i=0; i < nb_pieces; ++i) {
painter.setPen(getPieceColor(avail[i], average)); painter.setPen(getPieceColor(avail[i], average));
painter.drawPoint(i,0); painter.drawPoint(i,0);
} }

Loading…
Cancel
Save