-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.cpp
More file actions
71 lines (60 loc) · 1.78 KB
/
Copy pathCell.cpp
File metadata and controls
71 lines (60 loc) · 1.78 KB
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
#include "GridPch.h"
#include "Cell.h"
#include "Grid.h"
Cell::Cell(Row& row, size_t column)
: _value("000x000")
, _row(row)
, _index(column)
{
Grid& grid = row.grid();
size_t x = column * grid.columnWidth();
size_t y = row.index() * grid.rowHeight();
QRectF rect((qreal)x, (qreal)y, (qreal)grid.columnWidth(), (qreal)grid.rowHeight());
_rect = grid.scene().addRect(rect);
_rect->setPen(Qt::NoPen);
_rect->setOpacity(1.0);
_text = grid.scene().addSimpleText(_value, grid.font());
changeValue();
}
void Cell::changeValue()
{
QString value(_value);
value[3] = QChar('a' + (rand() % 26));
setValue(value);
setTextColor(rand() % 2 ? Qt::blue : Qt::red);
setBackgroundColor(rand() % 2 ? QColor(244, 250, 202) : QColor(235, 230, 250));
}
void Cell::setValue(const QString& value)
{
_value = value;
_text->setText(_value);
switch (_alignment)
{
case Qt::AlignmentFlag::AlignLeft:
_text->setPos(rect().left(), rect().top() + _ypadding);
break;
case Qt::AlignmentFlag::AlignRight:
{
const QRectF bound = _text->boundingRect();
_text->setPos(rect().right() - bound.width(), rect().top() + _ypadding);
break;
}
case Qt::AlignmentFlag::AlignHCenter:
default:
{
const QRectF bound = _text->boundingRect();
_text->setPos(rect().left() + (rect().width() - bound.width())/2, rect().top() + _ypadding);
break;
}
};
}
void Cell::setTextColor(const QColor& color)
{
QBrush brush = _text->brush();
brush.setColor(color);
_text->setBrush(brush);
}
void Cell::setBackgroundColor(const QColor& color)
{
_rect->setBrush(QBrush(color, Qt::SolidPattern));
}