multiple_data_view.cpp
3.17 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "multiple_data_view.h"
#include <assert.h>
using namespace Shared;
namespace Statistics {
void MultipleDataView::reload() {
layoutSubviews();
for (int i = 0; i < Store::k_numberOfSeries; i++) {
dataViewAtIndex(i)->reload();
}
}
int MultipleDataView::indexOfSubviewAtSeries(int series) {
int displayedSubviewIndex = 0;
for (int i = 0; i < Store::k_numberOfSeries; i++) {
if (!m_store->seriesIsEmpty(i)) {
if (i == series) {
return displayedSubviewIndex;
}
displayedSubviewIndex++;
} else if (i == series) {
return -1;
}
}
assert(false);
return -1;
}
void MultipleDataView::selectDataView(int index) {
changeDataViewSelection(index, true);
}
void MultipleDataView::deselectDataView(int index) {
changeDataViewSelection(index, false);
}
int MultipleDataView::numberOfSubviews() const {
int result = m_store->numberOfNonEmptySeries();
assert(result <= Store::k_numberOfSeries);
return result + 1; // +1 for the banner view
}
View * MultipleDataView::subviewAtIndex(int index) {
if (index == MultipleDataView::numberOfSubviews() -1) {
return editableBannerView();
}
int seriesIndex = 0;
int nonEmptySeriesIndex = -1;
while (seriesIndex < Store::k_numberOfSeries) {
if (!m_store->seriesIsEmpty(seriesIndex)) {
nonEmptySeriesIndex++;
if (nonEmptySeriesIndex == index) {
return dataViewAtIndex(seriesIndex);
}
}
seriesIndex++;
}
assert(false);
return nullptr;
}
void MultipleDataView::layoutSubviews() {
// We need to set the banner width first, so its height can be computed
editableBannerView()->setFrame(KDRect(0, 0, bounds().width(), 0));
layoutDataSubviews();
layoutBanner();
}
void MultipleDataView::layoutDataSubviews() {
int numberDataSubviews = m_store->numberOfNonEmptySeries();
assert(numberDataSubviews > 0);
KDCoordinate bannerHeight = bannerFrame().height();
KDCoordinate subviewHeight = (bounds().height() - bannerHeight)/numberDataSubviews;
int displayedSubviewIndex = 0;
for (int i = 0; i < Store::k_numberOfSeries; i++) {
if (!m_store->seriesIsEmpty(i)) {
CurveView * dataView = dataViewAtIndex(i);
KDRect frame = KDRect(0, displayedSubviewIndex*subviewHeight, bounds().width(), subviewHeight);
dataView->setFrame(frame);
displayedSubviewIndex++;
}
}
}
void MultipleDataView::changeDataViewSelection(int index, bool select) {
dataViewAtIndex(index)->selectMainView(select);
dataViewAtIndex(index)->reload();
}
KDRect MultipleDataView::bannerFrame() const {
KDCoordinate bannerHeight = bannerView()->minimalSizeForOptimalDisplay().height();
KDRect frame = KDRect(0, bounds().height() - bannerHeight, bounds().width(), bannerHeight);
return frame;
}
void MultipleDataView::layoutBanner() {
KDCoordinate bannerHeight = bannerFrame().height();
if (m_displayBanner) {
editableBannerView()->setFrame(bannerFrame());
} else {
KDRect frame = KDRect(0, bounds().height() - bannerHeight, bounds().width(), 0);
editableBannerView()->setFrame(frame);
}
}
void MultipleDataView::drawRect(KDContext * ctx, KDRect rect) const {
if (!m_displayBanner) {
ctx->fillRect(bannerFrame(), KDColorWhite);
}
}
}