|
|
@ -32,208 +32,199 @@ |
|
|
|
#include <QDebug> |
|
|
|
#include <QDebug> |
|
|
|
#include "pieceavailabilitybar.h" |
|
|
|
#include "pieceavailabilitybar.h" |
|
|
|
|
|
|
|
|
|
|
|
PieceAvailabilityBar::PieceAvailabilityBar(QWidget *parent) : |
|
|
|
|
|
|
|
QWidget(parent) |
|
|
|
PieceAvailabilityBar::PieceAvailabilityBar(QWidget *parent) |
|
|
|
|
|
|
|
: QWidget(parent) |
|
|
|
{ |
|
|
|
{ |
|
|
|
setFixedHeight(BAR_HEIGHT); |
|
|
|
setFixedHeight(BAR_HEIGHT); |
|
|
|
|
|
|
|
|
|
|
|
m_bgColor = 0xffffff; |
|
|
|
m_bgColor = 0xffffff; |
|
|
|
m_borderColor = palette().color(QPalette::Dark).rgb(); |
|
|
|
m_borderColor = palette().color(QPalette::Dark).rgb(); |
|
|
|
m_pieceColor = 0x0000ff; |
|
|
|
m_pieceColor = 0x0000ff; |
|
|
|
|
|
|
|
|
|
|
|
updatePieceColors(); |
|
|
|
updatePieceColors(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QVector<float> PieceAvailabilityBar::intToFloatVector(const QVector<int> &vecin, int reqSize) |
|
|
|
QVector<float> PieceAvailabilityBar::intToFloatVector(const QVector<int> &vecin, int reqSize) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QVector<float> result(reqSize, 0.0); |
|
|
|
QVector<float> result(reqSize, 0.0); |
|
|
|
if (vecin.isEmpty()) return result; |
|
|
|
if (vecin.isEmpty()) return result; |
|
|
|
|
|
|
|
|
|
|
|
const float ratio = vecin.size() / (float)reqSize; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int maxElement = *std::max_element(vecin.begin(), vecin.end()); |
|
|
|
const float ratio = vecin.size() / (float)reqSize; |
|
|
|
|
|
|
|
|
|
|
|
// qMax because in normalization we don't want divide by 0
|
|
|
|
const int maxElement = *std::max_element(vecin.begin(), vecin.end()); |
|
|
|
// if maxElement == 0 check will be disabled please enable this line:
|
|
|
|
|
|
|
|
// const int maxElement = qMax(*std::max_element(avail.begin(), avail.end()), 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (maxElement == 0) |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// simple linear transformation algorithm
|
|
|
|
// qMax because in normalization we don't want divide by 0
|
|
|
|
// for example:
|
|
|
|
// if maxElement == 0 check will be disabled please enable this line:
|
|
|
|
// image.x(0) = pieces.x(0.0 >= x < 1.7)
|
|
|
|
// const int maxElement = qMax(*std::max_element(avail.begin(), avail.end()), 1);
|
|
|
|
// image.x(1) = pieces.x(1.7 >= x < 3.4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < reqSize; ++x) { |
|
|
|
if (maxElement == 0) |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
|
|
|
|
// don't use previously calculated value "ratio" here!!!
|
|
|
|
// simple linear transformation algorithm
|
|
|
|
// float cannot save irrational number like 7/9, if this number will be rounded up by std::ceil
|
|
|
|
// for example:
|
|
|
|
// give you x2 == pieces.size(), and index out of range: pieces[x2]
|
|
|
|
// image.x(0) = pieces.x(0.0 >= x < 1.7)
|
|
|
|
// this code is safe, so keep that in mind when you try optimize more.
|
|
|
|
// image.x(1) = pieces.x(1.7 >= x < 3.4)
|
|
|
|
// tested with size = 3000000ul
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// R - real
|
|
|
|
for (int x = 0; x < reqSize; ++x) { |
|
|
|
const float fromR = (x * vecin.size()) / (float)reqSize; |
|
|
|
// don't use previously calculated value "ratio" here!!!
|
|
|
|
const float toR = ((x + 1) * vecin.size()) / (float)reqSize; |
|
|
|
// float cannot save irrational number like 7/9, if this number will be rounded up by std::ceil
|
|
|
|
|
|
|
|
// give you x2 == pieces.size(), and index out of range: pieces[x2]
|
|
|
|
|
|
|
|
// this code is safe, so keep that in mind when you try optimize more.
|
|
|
|
|
|
|
|
// tested with size = 3000000ul
|
|
|
|
|
|
|
|
|
|
|
|
// C - integer
|
|
|
|
// R - real
|
|
|
|
int fromC = fromR;// std::floor not needed
|
|
|
|
const float fromR = (x * vecin.size()) / (float)reqSize; |
|
|
|
int toC = std::ceil(toR); |
|
|
|
const float toR = ((x + 1) * vecin.size()) / (float)reqSize; |
|
|
|
|
|
|
|
|
|
|
|
// position in pieces table
|
|
|
|
// C - integer
|
|
|
|
// libtorrent::bitfield::m_size is unsigned int(31 bits), so qlonglong is not needed
|
|
|
|
int fromC = fromR;// std::floor not needed
|
|
|
|
// tested with size = 3000000ul
|
|
|
|
int toC = std::ceil(toR); |
|
|
|
int x2 = fromC; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// little speed up for really big pieces table, 10K+ size
|
|
|
|
// position in pieces table
|
|
|
|
const int toCMinusOne = toC - 1; |
|
|
|
// libtorrent::bitfield::m_size is unsigned int(31 bits), so qlonglong is not needed
|
|
|
|
|
|
|
|
// tested with size = 3000000ul
|
|
|
|
|
|
|
|
int x2 = fromC; |
|
|
|
|
|
|
|
|
|
|
|
// value in returned vector
|
|
|
|
// little speed up for really big pieces table, 10K+ size
|
|
|
|
float value = 0; |
|
|
|
const int toCMinusOne = toC - 1; |
|
|
|
|
|
|
|
|
|
|
|
// case when calculated range is (15.2 >= x < 15.7)
|
|
|
|
// value in returned vector
|
|
|
|
if (x2 == toCMinusOne) { |
|
|
|
float value = 0; |
|
|
|
if (vecin[x2]) { |
|
|
|
|
|
|
|
value += (toR - fromR) * vecin[x2]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
++x2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// case when (15.2 >= x < 17.8)
|
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
// subcase (15.2 >= x < 16)
|
|
|
|
|
|
|
|
if (x2 != fromR) { |
|
|
|
|
|
|
|
if (vecin[x2]) { |
|
|
|
|
|
|
|
value += (1.0 - (fromR - fromC)) * vecin[x2]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
++x2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// subcase (16 >= x < 17)
|
|
|
|
// case when calculated range is (15.2 >= x < 15.7)
|
|
|
|
for (; x2 < toCMinusOne; ++x2) { |
|
|
|
if (x2 == toCMinusOne) { |
|
|
|
if (vecin[x2]) { |
|
|
|
if (vecin[x2]) |
|
|
|
value += vecin[x2]; |
|
|
|
value += (toR - fromR) * vecin[x2]; |
|
|
|
|
|
|
|
++x2; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// case when (15.2 >= x < 17.8)
|
|
|
|
|
|
|
|
else { |
|
|
|
// subcase (17 >= x < 17.8)
|
|
|
|
// subcase (15.2 >= x < 16)
|
|
|
|
if (x2 == toCMinusOne) { |
|
|
|
if (x2 != fromR) { |
|
|
|
if (vecin[x2]) { |
|
|
|
if (vecin[x2]) |
|
|
|
value += (1.0 - (toC - toR)) * vecin[x2]; |
|
|
|
value += (1.0 - (fromR - fromC)) * vecin[x2]; |
|
|
|
|
|
|
|
++x2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// subcase (16 >= x < 17)
|
|
|
|
|
|
|
|
for (; x2 < toCMinusOne; ++x2) |
|
|
|
|
|
|
|
if (vecin[x2]) |
|
|
|
|
|
|
|
value += vecin[x2]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// subcase (17 >= x < 17.8)
|
|
|
|
|
|
|
|
if (x2 == toCMinusOne) { |
|
|
|
|
|
|
|
if (vecin[x2]) |
|
|
|
|
|
|
|
value += (1.0 - (toC - toR)) * vecin[x2]; |
|
|
|
|
|
|
|
++x2; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
++x2; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// normalization <0, 1>
|
|
|
|
// normalization <0, 1>
|
|
|
|
value /= ratio * maxElement; |
|
|
|
value /= ratio * maxElement; |
|
|
|
|
|
|
|
|
|
|
|
// float precision sometimes gives > 1, because in not possible to store irrational numbers
|
|
|
|
// float precision sometimes gives > 1, because in not possible to store irrational numbers
|
|
|
|
value = qMin(value, (float)1.0); |
|
|
|
value = qMin(value, (float)1.0); |
|
|
|
|
|
|
|
|
|
|
|
result[x] = value; |
|
|
|
result[x] = value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int PieceAvailabilityBar::mixTwoColors(int &rgb1, int &rgb2, float ratio) |
|
|
|
int PieceAvailabilityBar::mixTwoColors(int &rgb1, int &rgb2, float ratio) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int r1 = qRed(rgb1); |
|
|
|
int r1 = qRed(rgb1); |
|
|
|
int g1 = qGreen(rgb1); |
|
|
|
int g1 = qGreen(rgb1); |
|
|
|
int b1 = qBlue(rgb1); |
|
|
|
int b1 = qBlue(rgb1); |
|
|
|
|
|
|
|
|
|
|
|
int r2 = qRed(rgb2); |
|
|
|
int r2 = qRed(rgb2); |
|
|
|
int g2 = qGreen(rgb2); |
|
|
|
int g2 = qGreen(rgb2); |
|
|
|
int b2 = qBlue(rgb2); |
|
|
|
int b2 = qBlue(rgb2); |
|
|
|
|
|
|
|
|
|
|
|
float ratio_n = 1.0 - ratio; |
|
|
|
float ratio_n = 1.0 - ratio; |
|
|
|
int r = (r1 * ratio_n) + (r2 * ratio); |
|
|
|
int r = (r1 * ratio_n) + (r2 * ratio); |
|
|
|
int g = (g1 * ratio_n) + (g2 * ratio); |
|
|
|
int g = (g1 * ratio_n) + (g2 * ratio); |
|
|
|
int b = (b1 * ratio_n) + (b2 * ratio); |
|
|
|
int b = (b1 * ratio_n) + (b2 * ratio); |
|
|
|
|
|
|
|
|
|
|
|
return qRgb(r, g, b); |
|
|
|
return qRgb(r, g, b); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PieceAvailabilityBar::updateImage() |
|
|
|
void PieceAvailabilityBar::updateImage() |
|
|
|
{ |
|
|
|
{ |
|
|
|
QImage image2(width() - 2, 1, QImage::Format_RGB888); |
|
|
|
QImage image2(width() - 2, 1, QImage::Format_RGB888); |
|
|
|
if (image2.isNull()) { |
|
|
|
if (image2.isNull()) { |
|
|
|
qDebug() << "QImage image2() allocation failed, width():" << width(); |
|
|
|
qDebug() << "QImage image2() allocation failed, width():" << width(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (m_pieces.empty()) { |
|
|
|
if (m_pieces.empty()) { |
|
|
|
image2.fill(0xffffff); |
|
|
|
image2.fill(0xffffff); |
|
|
|
|
|
|
|
m_image = image2; |
|
|
|
|
|
|
|
update(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QVector<float> scaled_pieces = intToFloatVector(m_pieces, image2.width()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// filling image
|
|
|
|
|
|
|
|
for (int x = 0; x < scaled_pieces.size(); ++x) { |
|
|
|
|
|
|
|
float pieces2_val = scaled_pieces.at(x); |
|
|
|
|
|
|
|
image2.setPixel(x, 0, m_pieceColors[pieces2_val * 255]); |
|
|
|
|
|
|
|
} |
|
|
|
m_image = image2; |
|
|
|
m_image = image2; |
|
|
|
update(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QVector<float> scaled_pieces = intToFloatVector(m_pieces, image2.width()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// filling image
|
|
|
|
|
|
|
|
for (int x = 0; x < scaled_pieces.size(); ++x) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
float pieces2_val = scaled_pieces.at(x); |
|
|
|
|
|
|
|
image2.setPixel(x, 0, m_pieceColors[pieces2_val * 255]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m_image = image2; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PieceAvailabilityBar::setAvailability(const QVector<int> &avail) |
|
|
|
void PieceAvailabilityBar::setAvailability(const QVector<int> &avail) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_pieces = avail; |
|
|
|
m_pieces = avail; |
|
|
|
|
|
|
|
|
|
|
|
updateImage(); |
|
|
|
updateImage(); |
|
|
|
update(); |
|
|
|
update(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PieceAvailabilityBar::updatePieceColors() |
|
|
|
void PieceAvailabilityBar::updatePieceColors() |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_pieceColors = QVector<int>(256); |
|
|
|
m_pieceColors = QVector<int>(256); |
|
|
|
for (int i = 0; i < 256; ++i) { |
|
|
|
for (int i = 0; i < 256; ++i) { |
|
|
|
float ratio = (i / 255.0); |
|
|
|
float ratio = (i / 255.0); |
|
|
|
m_pieceColors[i] = mixTwoColors(m_bgColor, m_pieceColor, ratio); |
|
|
|
m_pieceColors[i] = mixTwoColors(m_bgColor, m_pieceColor, ratio); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PieceAvailabilityBar::clear() |
|
|
|
void PieceAvailabilityBar::clear() |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_image = QImage(); |
|
|
|
m_image = QImage(); |
|
|
|
update(); |
|
|
|
update(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PieceAvailabilityBar::paintEvent(QPaintEvent *) |
|
|
|
void PieceAvailabilityBar::paintEvent(QPaintEvent *) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QPainter painter(this); |
|
|
|
QPainter painter(this); |
|
|
|
QRect imageRect(1, 1, width() - 2, height() - 2); |
|
|
|
QRect imageRect(1, 1, width() - 2, height() - 2); |
|
|
|
if (m_image.isNull()) |
|
|
|
if (m_image.isNull()) { |
|
|
|
{ |
|
|
|
painter.setBrush(Qt::white); |
|
|
|
painter.setBrush(Qt::white); |
|
|
|
painter.drawRect(imageRect); |
|
|
|
painter.drawRect(imageRect); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else |
|
|
|
if (m_image.width() != imageRect.width()) |
|
|
|
{ |
|
|
|
updateImage(); |
|
|
|
if (m_image.width() != imageRect.width()) |
|
|
|
painter.drawImage(imageRect, m_image); |
|
|
|
updateImage(); |
|
|
|
} |
|
|
|
painter.drawImage(imageRect, m_image); |
|
|
|
QPainterPath border; |
|
|
|
} |
|
|
|
border.addRect(0, 0, width() - 1, height() - 1); |
|
|
|
QPainterPath border; |
|
|
|
|
|
|
|
border.addRect(0, 0, width() - 1, height() - 1); |
|
|
|
painter.setPen(m_borderColor); |
|
|
|
|
|
|
|
painter.drawPath(border); |
|
|
|
painter.setPen(m_borderColor); |
|
|
|
|
|
|
|
painter.drawPath(border); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void PieceAvailabilityBar::setColors(int background, int border, int available) |
|
|
|
void PieceAvailabilityBar::setColors(int background, int border, int available) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_bgColor = background; |
|
|
|
m_bgColor = background; |
|
|
|
m_borderColor = border; |
|
|
|
m_borderColor = border; |
|
|
|
m_pieceColor = available; |
|
|
|
m_pieceColor = available; |
|
|
|
|
|
|
|
|
|
|
|
updatePieceColors(); |
|
|
|
updatePieceColors(); |
|
|
|
updateImage(); |
|
|
|
updateImage(); |
|
|
|
update(); |
|
|
|
update(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|