-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatePanel.cpp
More file actions
113 lines (108 loc) · 3.32 KB
/
Copy pathDatePanel.cpp
File metadata and controls
113 lines (108 loc) · 3.32 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
/*
* This file is part of the Eclipse2024 project. It is subject to the GPLv3
* license terms in the LICENSE file found in the top-level directory of this
* distribution and at
* https://github.com/jjackowski/eclipse2024/blob/master/LICENSE.
* No part of the Eclipse2024 project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*
* Copyright (C) 2024 Jeff Jackowski
*/
#include "DatePanel.hpp"
#include <duds/time/planetary/Planetary.hpp>
#include <duds/ui/graphics/BppFontPool.hpp>
#include <duds/hardware/devices/clocks/LinuxClock.hpp>
#include "DisplayStuff.hpp"
extern duds::ui::graphics::BppFontPool FontPool;
// ordered by size-step
static const std::string fontNames[] = {
//"Date-medium",
//"Date-small",
"Date-medium",
"Date-small",
};
DatePanel::DatePanel(Token) : nextDay(duds::time::interstellar::SecondTime::min()) {
// configure a date facet for making the date string
boost::gregorian::date_facet *dateform =
new boost::gregorian::date_facet("%a %b %e, %Y");
oss.imbue(std::locale(oss.getloc(), dateform));
}
void DatePanel::updateTime(const duds::data::Measurement::TimeSample &ts) {
// using timestamp here is a shortcut, but should work fine
if (ts.value >= nextDay) {
// get the new day in computer's set time zone
std::time_t tt = duds::time::planetary::earth->timeUtc(
ts.value
);
std::tm ltime;
localtime_r(&tt, <ime);
date = boost::gregorian::date_from_tm(ltime);
// record time zone
DisplayStuff::tzone = ltime.tm_gmtoff;
// compute and store the time of the next day
++ltime.tm_mday;
ltime.tm_sec = ltime.tm_min = ltime.tm_hour = 0;
tt = mktime(<ime);
// time zone may have changed
std::tm ntime;
localtime_r(&tt, &ntime);
duds::time::planetary::earth->time(
nextDay,
boost::posix_time::from_time_t(tt) +
// for time zone change
boost::posix_time::seconds(ltime.tm_gmtoff - ntime.tm_gmtoff)
);
// clear rendered date images
for (duds::ui::graphics::BppImageSptr &out : outputs) {
out.reset();
}
// clear date strings
dateStr.clear();
}
}
const duds::ui::graphics::BppImage *DatePanel::render(
duds::ui::graphics::ImageLocation &offset,
duds::ui::graphics::ImageDimensions &dim,
duds::ui::graphics::PanelMargins &margin,
int sizeStep
) {
// find widest date that fits
for (int i = 0; i < 2; ++i) {
if (!outputs[i]) {
if (dateStr.empty()) {
// produce date string
oss << date << std::ends;
dateStr = oss.str();
if (dateStr.back() == 0) {
dateStr.pop_back();
}
oss.clear();
oss.seekp(0);
}
// render image
duds::ui::graphics::BppFontSptr font = FontPool.getFont(fontNames[i]);
if (!font) continue; // font really should be there
// render the date
outputs[i] = font->render(dateStr);
// length without year
duds::ui::graphics::ImageDimensions dim = font->lineDimensions(
dateStr.substr(0, dateStr.size() - 6)
);
widths[i] = dim.w;
}
// width check with year
if (outputs[i]->dimensions().w <= dim.w) {
dim = outputs[sizeStep]->dimensions();
return outputs[i].get();
}
// width check without year
if (widths[i] <= dim.w) {
dim = outputs[sizeStep]->dimensions();
dim.w = widths[i];
return outputs[i].get();
}
}
// if execution gets here, panel layout is misconfigured
return nullptr;
}