-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesfieldwidgetupdaterthread.cpp
66 lines (56 loc) · 2.78 KB
/
minesfieldwidgetupdaterthread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "minesfieldwidgetupdaterthread.h"
MinesFieldWidgetUpdaterThread::MinesFieldWidgetUpdaterThread(MinesFieldWidget* widget, QObject* parent)
: QThread(parent)
, minesFieldWidget(widget)
{
}
void MinesFieldWidgetUpdaterThread::run()
{
QPixmap lpixmap = QPixmap(minesFieldWidget->viewport.width, minesFieldWidget->viewport.height);
lpixmap.fill(minesFieldWidget->settings.backgroundColor);
QPainter painter(&lpixmap);
int stepX = minesFieldWidget->cellSize.width() + minesFieldWidget->borderSize.width();
int stepY = minesFieldWidget->cellSize.height() + minesFieldWidget->borderSize.height();
// horizontal lines
if (minesFieldWidget->borderSize.height() > 0) {
painter.setPen(QPen(minesFieldWidget->settings.borderColor,
minesFieldWidget->borderSize.height()));
for (unsigned y = minesFieldWidget->borderSize.height() / 2;
y <= minesFieldWidget->viewport.height; y += stepY)
painter.drawLine(0, y, minesFieldWidget->viewport.width, y);
}
// vertical lines
if (minesFieldWidget->borderSize.width() > 0) {
painter.setPen(QPen(minesFieldWidget->settings.borderColor,
minesFieldWidget->borderSize.width()));
for (unsigned x = minesFieldWidget->borderSize.width() / 2;
x <= minesFieldWidget->viewport.width; x += stepX)
painter.drawLine(x, 0, x, minesFieldWidget->viewport.height);
}
for (unsigned y = minesFieldWidget->viewport.start_row;
y < minesFieldWidget->viewport.start_row + minesFieldWidget->viewport.rows && y < minesFieldWidget->field->getRows();
y++) {
for (unsigned x = minesFieldWidget->viewport.start_col;
x < minesFieldWidget->viewport.start_col + minesFieldWidget->viewport.cols && x < minesFieldWidget->field->getCols();
x++) {
unsigned i = minesFieldWidget->field->getCellIndex(Point(x, y));
if (!minesFieldWidget->field->isCellIndexValid(i))
continue;
auto& cells = minesFieldWidget->field->getCells();
const Cell& cell = cells.at(i);
int minesAroundCell = cell.minesAround();
QColor color = minesFieldWidget->getCellColor(cell.cellState(), minesAroundCell);
painter.fillRect((x - minesFieldWidget->viewport.start_col) * stepX + minesFieldWidget->borderSize.width(),
(y - minesFieldWidget->viewport.start_row) * stepY + minesFieldWidget->borderSize.height(),
minesFieldWidget->cellSize.width(),
minesFieldWidget->cellSize.height(),
color);
}
}
// atomic copy
minesFieldWidget->updatingMutex.lock();
minesFieldWidget->pixmap = lpixmap;
minesFieldWidget->updatingMutex.unlock();
static int n = 1;
n++;
}