-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablepainter.cpp
83 lines (67 loc) · 1.5 KB
/
tablepainter.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
#include "tablepainter.h"
TablePainter::TablePainter()
{
}
TablePainter::TablePainter(QRectF rect)
{
_rect = rect;
}
void TablePainter::draw(QPainter *painter) const
{
_applyFlex();
for (auto row : qAsConst(_rows))
{
row->draw(painter);
}
}
void TablePainter::addRow(TableRow *row)
{
_rows.append(row);
}
void TablePainter::removeRow(unsigned int rowIndex)
{
_rows.removeAt(rowIndex);
}
qreal TablePainter::flex() const
{
return _flex;
}
void TablePainter::setFlex(qreal newFlex)
{
_flex = newFlex;
}
const QRectF &TablePainter::rect() const
{
return _rect;
}
void TablePainter::setRect(const QRectF &newRect) const
{
_rect = QRectF(newRect);
}
void TablePainter::_applyFlex() const
{
qreal totalFixedFlexValue = 0;
unsigned int fixedFlexCount = 0;
for (const auto &row : qAsConst(_rows))
{
if (!row->autoFlex())
{
totalFixedFlexValue += row->flex();
fixedFlexCount++;
}
}
qreal remainingFlexPerCell = (1 - totalFixedFlexValue) / (_rows.size() - fixedFlexCount);
qreal currentY = 0;
for (auto i = 0; i < _rows.size(); i++)
{
auto row = _rows[i];
if (row->autoFlex())
{
row->setFlex(remainingFlexPerCell);
}
auto rowHeight = _rect.height() * row->flex();
auto newRect = QRectF(_rect.topLeft() + QPointF(0, currentY), QSizeF(_rect.width(), rowHeight));
row->setRect(newRect);
currentY += rowHeight;
}
}