Rose
TimeBox.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <utility>
11 #include <atomic>
12 
13 #include "Layout.h"
14 #include "Signals.h"
15 #include "TimerTick.h"
16 #include "LocalTime.h"
17 
18 namespace rose {
19 
24  class TimeBox : public Manager {
25  protected:
26  static constexpr std::string_view HoursMinutesFmt = "%R";
27  static constexpr std::string_view LongSecondsFmt = "%S %Z";
28  static constexpr std::string_view ShortSecondsFmt = "%Z";
29 
30  std::unique_ptr<cpp_local_time::LocalTime> mLocalTimeConvert{};
31 
32  std::shared_ptr<TimerTick> mTimerTick{};
33  bool mDisplaySeconds{true};
34  bool mLocalTime{false};
35  std::string mTimeZone{};
36 
38  const std::time_put<char> &locale_time_put;
39 
40  void initialize();
41 
50  auto put_locale_time(std::stringstream &strm, char fill, const std::tm *tm, const std::string_view &format) {
51  auto fmt = std::string{format};
52  if (fmt.empty()) {
53  throw std::runtime_error("Format must not be empty");
54  } else {
55  const char *fmtbeg = fmt.c_str();
56  const char *fmtend = fmtbeg + fmt.size();
57  return locale_time_put.put(strm, strm, fill, tm, fmtbeg, fmtend);
58  }
59  }
60 
61  void updateTimeDisplay();
62 
63  public:
64  TimeBox() = delete;
65 
66  ~TimeBox() override = default;
67 
68  TimeBox(const TimeBox&) = delete;
69 
70  TimeBox(TimeBox&&) = delete;
71 
72  TimeBox& operator=(const TimeBox&) = delete;
73 
74  TimeBox& operator=(TimeBox&&) = delete;
75 
76  explicit TimeBox(std::shared_ptr<TimerTick> timerTick);
77 
78  TimeBox(std::shared_ptr<TimerTick> timerTick, bool seconds, bool localTime = false) : TimeBox(std::move(timerTick)) {
79  mDisplaySeconds = seconds;
80  mLocalTime = localTime;
81  }
82 
83  TimeBox(std::shared_ptr<TimerTick> timerTick, const char *timeZone, bool seconds = true) : TimeBox(std::move(timerTick)) {
84  mDisplaySeconds = seconds;
85  mTimeZone = timeZone;
86  }
87 
88  TimeBox(std::shared_ptr<TimerTick> timerTick, const std::string &timeZone, bool seconds = true) : TimeBox(std::move(timerTick)) {
89  mDisplaySeconds = seconds;
90  mTimeZone = timeZone;
91  }
92 
93  static constexpr std::string_view id = "TimeBox";
94  std::string_view nodeId() const noexcept override {
95  return id;
96  }
97 
103  void draw(gm::Context &context, const Position<int>& containerPosition) override;
104 
111  Rectangle layout(gm::Context &context, const Rectangle &screenRect) override;
112  };
113 
114  class DateBox : public Manager {
115  protected:
116  static constexpr std::string_view LongDateFormat = "%a %b %d, %Y";
117  static constexpr std::string_view ShortDateFormat = "%a %b %d";
118 
119  std::unique_ptr<cpp_local_time::LocalTime> mLocalTimeConvert{};
120 
121  bool mDisplayYear{true};
122  bool mLocalTime{false};
123  std::string mTimeZone{};
124  std::shared_ptr<TimerTick> mTimerTick{};
125 
126  TickProtocol::slot_type minuteSlot{};
127  const std::time_put<char> &locale_time_put;
128 
129  void initialize();
130 
139  auto put_locale_time(std::stringstream &strm, char fill, const std::tm *tm, const std::string_view &format) {
140  auto fmt = std::string{format};
141  if (fmt.empty()) {
142  throw std::runtime_error("Format must not be empty");
143  } else {
144  const char *fmtbeg = fmt.c_str();
145  const char *fmtend = fmtbeg + fmt.size();
146  return locale_time_put.put(strm, strm, fill, tm, fmtbeg, fmtend);
147  }
148  }
149 
150  void updateDateDisplay();
151 
152  public:
153  DateBox() = delete;
154 
155  ~DateBox() override = default;
156 
157  DateBox(const DateBox&) = delete;
158 
159  DateBox(DateBox&&) = delete;
160 
161  DateBox& operator=(const DateBox&) = delete;
162 
163  DateBox& operator=(DateBox&&) = delete;
164 
165  explicit DateBox(std::shared_ptr<TimerTick> minutes);
166 
167  DateBox(std::shared_ptr<TimerTick> timerTick, bool year, bool localTime = false) : DateBox(std::move(timerTick)) {
168  mDisplayYear = year;
169  mLocalTime = localTime;
170  }
171 
172  DateBox(std::shared_ptr<TimerTick> timerTick, const char *timeZone, bool year = true) : DateBox(std::move(timerTick)) {
173  mDisplayYear = year;
174  mTimeZone = timeZone;
175  }
176 
177  DateBox(std::shared_ptr<TimerTick> timerTick, const std::string &timeZone, bool year = true) : DateBox(std::move(timerTick)) {
178  mDisplayYear = year;
179  mTimeZone = timeZone;
180  }
181 
182  static constexpr std::string_view id = "DateBox";
183  std::string_view nodeId() const noexcept override {
184  return id;
185  }
186 
192  void draw(gm::Context &context, const Position<int>& containerPosition) override;
193 
200  Rectangle layout(gm::Context &context, const Rectangle &screenRect) override;
201  };
202 
203  class TimeDateBox : public Manager {
204  protected:
205  bool mDisplaySecond{};
206  bool mDisplayYear{};
207  bool mLocalTime{};
208  std::string mTimeZone{};
209  std::shared_ptr<TimerTick> mTick{};
210 
211  void initialize();
212 
213  public:
214  TimeDateBox() = delete;
215 
216  ~TimeDateBox() override = default;
217 
218  TimeDateBox(const TimeDateBox&) = delete;
219 
220  TimeDateBox(TimeDateBox&&) = delete;
221 
222  TimeDateBox& operator=(const TimeDateBox&) = delete;
223 
224  TimeDateBox& operator=(TimeDateBox&&) = delete;
225 
226  explicit TimeDateBox(std::shared_ptr<TimerTick> tick);
227 
228  TimeDateBox(std::shared_ptr<TimerTick> timerTick, bool second, bool year, bool localTime = false) : TimeDateBox(std::move(timerTick)) {
229  mDisplayYear = year;
230  mDisplaySecond = second;
231  mLocalTime = localTime;
232  }
233 
234  TimeDateBox(std::shared_ptr<TimerTick> timerTick, const char *timeZone, bool second, bool year = true) : TimeDateBox(std::move(timerTick)) {
235  mDisplayYear = year;
236  mDisplaySecond = second;
237  mTimeZone = timeZone;
238  }
239 
240  TimeDateBox(std::shared_ptr<TimerTick> timerTick, const std::string &timeZone, bool second, bool year = true) : TimeDateBox(std::move(timerTick)) {
241  mDisplayYear = year;
242  mDisplaySecond = second;
243  mTimeZone = timeZone;
244  }
245 
246  static constexpr std::string_view id = "TimeDateBox";
247  std::string_view nodeId() const noexcept override {
248  return id;
249  }
250 
256  void draw(gm::Context &context, const Position<int>& containerPosition) override;
257 
264  Rectangle layout(gm::Context &context, const Rectangle &screenRect) override;
265  };
266 }
267 
auto put_locale_time(std::stringstream &strm, char fill, const std::tm *tm, const std::string_view &format)
A helper function to convert the system time to a localized time.
Definition: TimeBox.h:139
bool mLocalTime
If time zone is empty display local time if true.
Definition: TimeBox.h:34
TickProtocol::slot_type secondSlot
Receive time signals.
Definition: TimeBox.h:37
A Widget which manages contained Widgets.
Definition: Visual.h:697
std::shared_ptr< TimerTick > mTimerTick
The source of time signals.
Definition: TimeBox.h:32
Definition: TimeBox.h:24
bool mDisplaySeconds
Display seconds in time.
Definition: TimeBox.h:33
std::shared_ptr< Slot< Args... > > slot_type
Composed Slot type.
Definition: Signals.h:123
void draw(gm::Context &context, const Position< int > &containerPosition) override
Draw the TimeBox and contents.
Definition: TimeBox.cpp:52
std::string mTimeZone
The time zone if not empty.
Definition: TimeBox.h:35
Rectangle layout(gm::Context &context, const Rectangle &screenRect) override
Layout the TimeBox and contents.
Definition: TimeBox.cpp:56
Context
Definition: GraphicsModel.h:76
auto put_locale_time(std::stringstream &strm, char fill, const std::tm *tm, const std::string_view &format)
A helper function to convert the system time to a localized time.
Definition: TimeBox.h:50
Definition: TimeBox.h:203
A composite of a Position and a Size.
Definition: Types.h:307
Definition: TimeBox.h:114
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
Establish an intra-application signaling protocol.