table_cell.cpp
3.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <escher/table_cell.h>
#include <escher/palette.h>
TableCell::TableCell(Layout layout) :
HighlightCell(),
m_layout(layout)
{
}
View * TableCell::labelView() const {
return nullptr;
}
View * TableCell::accessoryView() const {
return nullptr;
}
View * TableCell::subAccessoryView() const {
return nullptr;
}
int TableCell::numberOfSubviews() const {
return (labelView() != nullptr) + (accessoryView()!= nullptr) + (subAccessoryView()!= nullptr);
}
View * TableCell::subviewAtIndex(int index) {
if (index == 0) {
return labelView();
}
if (index == 1) {
return accessoryView();
}
return subAccessoryView();
}
void TableCell::layoutSubviews() {
KDCoordinate width = bounds().width();
KDCoordinate height = bounds().height();
View * label = labelView();
if (label) {
KDSize labelSize = label->minimalSizeForOptimalDisplay();
switch (m_layout) {
case Layout::Vertical:
label->setFrame(KDRect(k_separatorThickness+k_labelMargin, k_separatorThickness+k_labelTopMargin, width-2*k_separatorThickness-k_labelMargin, labelSize.height()));
break;
default:
label->setFrame(KDRect(k_separatorThickness+k_labelMargin, k_separatorThickness, labelSize.width(), height - 2*k_separatorThickness));
break;
}
}
View * accessory = accessoryView();
if (accessory) {
KDSize accessorySize = accessory->minimalSizeForOptimalDisplay();
/* Handle textfield that has no defined width (as their width evolves with
* the length of edited text */
if (accessorySize.width() == 0 && m_layout != Layout::Vertical) {
accessorySize = KDSize(width - 2*k_separatorThickness, accessorySize.height());
if (label) {
KDSize labelSize = label->minimalSizeForOptimalDisplay();
accessorySize = KDSize(width - 2*k_separatorThickness - labelSize.width()-k_labelMargin-k_accessoryMargin, accessorySize.height());
}
}
switch (m_layout) {
case Layout::Vertical:
accessory->setFrame(KDRect(k_separatorThickness+k_accessoryMargin, height-k_separatorThickness-accessorySize.height()-k_accessoryBottomMargin,
width-2*k_separatorThickness - k_accessoryMargin, accessorySize.height()));
break;
default:
accessory->setFrame(KDRect(width - accessorySize.width() - k_separatorThickness-k_accessoryMargin, k_separatorThickness,
accessorySize.width(), height-2*k_separatorThickness));
break;
}
}
View * subAccessory = subAccessoryView();
if (subAccessory && accessory) {
KDSize accessorySize = accessory->minimalSizeForOptimalDisplay();
KDSize subAccessorySize = subAccessory->minimalSizeForOptimalDisplay();
subAccessory->setFrame(KDRect(width-k_separatorThickness-k_accessoryMargin-accessorySize.width()-subAccessorySize.width(), k_separatorThickness,
subAccessorySize.width(), height-2*k_separatorThickness));
}
}
void TableCell::drawRect(KDContext * ctx, KDRect rect) const {
KDCoordinate width = bounds().width();
KDCoordinate height = bounds().height();
KDColor backgroundColor = isHighlighted() ? Palette::Select : KDColorWhite;
ctx->fillRect(KDRect(k_separatorThickness, k_separatorThickness, width-2*k_separatorThickness, height-k_separatorThickness), backgroundColor);
// Draw rectangle around cell
ctx->fillRect(KDRect(0, 0, width, k_separatorThickness), Palette::GreyBright);
ctx->fillRect(KDRect(0, k_separatorThickness, k_separatorThickness, height-k_separatorThickness), Palette::GreyBright);
ctx->fillRect(KDRect(width-k_separatorThickness, k_separatorThickness, k_separatorThickness, height-k_separatorThickness), Palette::GreyBright);
ctx->fillRect(KDRect(0, height-k_separatorThickness, width, k_separatorThickness), Palette::GreyBright);
}