Browse Source

Merge pull request #3879 from Chocobo1/avail_bar_crash

Fix potential crash when memory allocation failed. Closes #3877.
adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
8a905bed5d
  1. 37
      src/gui/properties/pieceavailabilitybar.cpp
  2. 5
      src/gui/properties/pieceavailabilitybar.h

37
src/gui/properties/pieceavailabilitybar.cpp

@ -29,10 +29,12 @@ @@ -29,10 +29,12 @@
*/
#include <cmath>
#include <QDebug>
#include "pieceavailabilitybar.h"
PieceAvailabilityBar::PieceAvailabilityBar(QWidget *parent) :
QWidget(parent)
PieceAvailabilityBar::PieceAvailabilityBar(QWidget *parent)
: QWidget(parent)
{
setFixedHeight(BAR_HEIGHT);
@ -65,7 +67,6 @@ QVector<float> PieceAvailabilityBar::intToFloatVector(const QVector<int> &vecin, @@ -65,7 +67,6 @@ QVector<float> PieceAvailabilityBar::intToFloatVector(const QVector<int> &vecin,
// image.x(1) = pieces.x(1.7 >= x < 3.4)
for (int x = 0; x < reqSize; ++x) {
// don't use previously calculated value "ratio" here!!!
// 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]
@ -93,33 +94,28 @@ QVector<float> PieceAvailabilityBar::intToFloatVector(const QVector<int> &vecin, @@ -93,33 +94,28 @@ QVector<float> PieceAvailabilityBar::intToFloatVector(const QVector<int> &vecin,
// case when calculated range is (15.2 >= x < 15.7)
if (x2 == toCMinusOne) {
if (vecin[x2]) {
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]) {
if (vecin[x2])
value += (1.0 - (fromR - fromC)) * vecin[x2];
}
++x2;
}
// subcase (16 >= x < 17)
for (; x2 < toCMinusOne; ++x2) {
if (vecin[x2]) {
for (; x2 < toCMinusOne; ++x2)
if (vecin[x2])
value += vecin[x2];
}
}
// subcase (17 >= x < 17.8)
if (x2 == toCMinusOne) {
if (vecin[x2]) {
if (vecin[x2])
value += (1.0 - (toC - toR)) * vecin[x2];
}
++x2;
}
}
@ -156,8 +152,11 @@ int PieceAvailabilityBar::mixTwoColors(int &rgb1, int &rgb2, float ratio) @@ -156,8 +152,11 @@ int PieceAvailabilityBar::mixTwoColors(int &rgb1, int &rgb2, float ratio)
void PieceAvailabilityBar::updateImage()
{
// qDebug() << "updateImageAv";
QImage image2(width() - 2, 1, QImage::Format_RGB888);
if (image2.isNull()) {
qDebug() << "QImage image2() allocation failed, width():" << width();
return;
}
if (m_pieces.empty()) {
image2.fill(0xffffff);
@ -169,8 +168,7 @@ void PieceAvailabilityBar::updateImage() @@ -169,8 +168,7 @@ void PieceAvailabilityBar::updateImage()
QVector<float> scaled_pieces = intToFloatVector(m_pieces, image2.width());
// filling image
for (int x = 0; x < scaled_pieces.size(); ++x)
{
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]);
}
@ -204,13 +202,11 @@ void PieceAvailabilityBar::paintEvent(QPaintEvent *) @@ -204,13 +202,11 @@ void PieceAvailabilityBar::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QRect imageRect(1, 1, width() - 2, height() - 2);
if (m_image.isNull())
{
if (m_image.isNull()) {
painter.setBrush(Qt::white);
painter.drawRect(imageRect);
}
else
{
else {
if (m_image.width() != imageRect.width())
updateImage();
painter.drawImage(imageRect, m_image);
@ -232,4 +228,3 @@ void PieceAvailabilityBar::setColors(int background, int border, int available) @@ -232,4 +228,3 @@ void PieceAvailabilityBar::setColors(int background, int border, int available)
updateImage();
update();
}

5
src/gui/properties/pieceavailabilitybar.h

@ -37,7 +37,9 @@ @@ -37,7 +37,9 @@
#define BAR_HEIGHT 18
class PieceAvailabilityBar: public QWidget {
class PieceAvailabilityBar: public QWidget
{
Q_OBJECT
Q_DISABLE_COPY(PieceAvailabilityBar)
@ -78,7 +80,6 @@ public: @@ -78,7 +80,6 @@ public:
protected:
void paintEvent(QPaintEvent *);
};
#endif // PIECEAVAILABILITYBAR_H

Loading…
Cancel
Save