-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridWidget.cpp
149 lines (127 loc) · 3.64 KB
/
GridWidget.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <QtDebug>
#include "GridWidget.h"
GridWidget::GridWidget(QWidget *parent)
: QWidget(parent), mousePressed(false)
{
xCells=10;
yCells=10;
this->setMouseTracking(true);
fgColor.setNamedColor("skyblue");
}
GridWidget::~GridWidget()
{
}
void GridWidget::paintEvent(QPaintEvent *)
{
bool allNew = false;
StateListNode *sn;
QPainter gwPainter(this);
QPen gridPen;
gridPen.setWidth(1);
gridPen.setStyle(Qt::SolidLine);
gridPen.setColor(fgColor);
if (fieldPixmap.size() != size())
{
allNew = true;
fieldPixmap = QPixmap(size());
}
QPainter painter(&fieldPixmap);
// Jetzt die Felder zeichnen
xCells = mGridData->getXCells();
yCells = mGridData->getYCells();
painter.setWindow(0,0,xCells,yCells);
if (allNew)
mGridData->SetFirstCell(Field::ITER_ALL, 0);
else
mGridData->SetFirstCell(Field::ITER_PARTIAL, 0);
while (!mGridData->LastCell ())
{
sn = mStates->getState(mGridData->GetCellState());
int x = mGridData->getXIndex();
int y = mGridData->getYIndex();
switch (sn->how)
{
case 1: /* kleines Bildchen */
break;
case 2: /* als Zeichen anzeigen */
break;
case 3: /* die Zustandsnr. anzeigen */
break;
default: /* farbiges Rechteck */
painter.fillRect(x,y,1,1,sn->color);
break;
}
mGridData->NextCell();
}
bool drawGrid = (this->width() > (5*xCells)) && (this->height() > (5*yCells));
if (drawGrid)
{
qreal sx = 1.0 * width() / xCells;
qreal sy = 1.0 * height() / yCells;
painter.setWindow(0,0,width(),height());
painter.setPen(gridPen);
QLineF l1(0.0, 0.0, 0.0, 1.0 * height());
for (int c=0; c <= xCells; ++c)
{
painter.drawLine(l1);
l1.translate(sx, 0.0);
}
QLineF l2(0.0, 0.0, 1.0*width(), 0.0);
for (int r=0; r <= yCells; ++r)
{
painter.drawLine(l2);
l2.translate(0.0, sy);
}
}
// zum Schluss das Werk anzeigen
gwPainter.drawPixmap(0,0,fieldPixmap);
}
void GridWidget::mousePressEvent(QMouseEvent *mouseEvent)
{
int nx = mouseEvent->x()*xCells/this->width();
int ny = mouseEvent->y()*yCells/this->height();
// qDebug() << "press mouse " << nx << ", " << ny;
mGridData->ClearMask();
mGridData->SetCellState(nx,ny,getNextState(nx,ny));
mGridData->MarkCellMask(nx, ny, Field::CELL_CHANGED);
update();
mousePressed = true;
}
void GridWidget::mouseMoveEvent(QMouseEvent *mouseEvent)
{
int nx = mouseEvent->x()*xCells/this->width();
int ny = mouseEvent->y()*yCells/this->height();
qDebug() << "move mouse " << nx << ", " << ny;
if (mousePressed && Field::CELL_CHANGED != mGridData->GetCellMask(nx,ny))
{
mGridData->SetCellState(nx,ny,getNextState(nx,ny));
mGridData->MarkCellMask(nx, ny, Field::CELL_CHANGED);
update();
}
}
void GridWidget::mouseReleaseEvent(QMouseEvent *mouseEvent)
{
//int nx = mouseEvent->x()*xCells/this->width();
//int ny = mouseEvent->y()*yCells/this->height();
// qDebug() << "release mouse " << nx << ", " << ny;
mGridData->ClearMask();
mousePressed = false;
}
void GridWidget::setForegroundColor (QColor &aColor)
{
this->fgColor = aColor;
}
void GridWidget::setGridData(Grid *aGrid)
{
mGridData = aGrid;
}
int GridWidget::getNextState(int nx, int ny) const
{
int nc = mStates->getMouseState();
if ( nc < 0)
{
nc = mGridData->GetCellState(nx, ny);
nc = (nc+1)%mStates->size();
}
return nc;
}