0.5.1
Loading...
Searching...
No Matches
TimeEdit.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "TimeEdit.hpp"
10
11#include <imgui.h>
12#include <fmt/core.h>
13
16
17namespace NAV
18{
19
21{
22 switch (timeEditFormat)
23 {
25 return "YMDHMS";
27 return "GPS Week/ToW";
29 break;
30 }
31 return "";
32}
33
34namespace gui::widgets
35{
36namespace
37{
38
39/// @brief Shows a ComboBox to select the time edit format
40/// @param[in] label Label to show beside the combo box. This has to be a unique id for ImGui.
41/// @param[in] format Reference to the format to select
42bool ComboTimeEditFormat(const char* label, TimeEditFormat::Format& format)
43{
44 return gui::widgets::EnumCombo(label, format);
45}
46
47} // namespace
48} // namespace gui::widgets
49
50} // namespace NAV
51
52bool NAV::gui::widgets::TimeEdit(const char* str_id, InsTime& insTime, TimeEditFormat& timeEditFormat, float itemWidth, int columns)
53{
54 bool changes = false;
55 bool edited = false;
56
57 ImGui::BeginGroup();
58
59 ImGui::SetNextItemWidth(140.0F);
60 if (ComboTimeEditFormat(fmt::format("##ComboTimeEditFormat {}", str_id).c_str(), timeEditFormat.format))
61 {
62 changes = true;
63 }
64 ImGui::SameLine();
65 ImGui::SetNextItemWidth(80.0F);
66 if (ComboTimeSystem(fmt::format("##ComboTimeSystem {}", str_id).c_str(), timeEditFormat.system))
67 {
68 changes = true;
69 }
70
71 if (timeEditFormat.format == TimeEditFormat::Format::YMDHMS)
72 {
73 auto ymdhms = insTime.toYMDHMS(timeEditFormat.system);
74
75 int year = ymdhms.year;
76 int month = ymdhms.month;
77 int day = ymdhms.day;
78 int hour = ymdhms.hour;
79 int min = ymdhms.min;
80 auto sec = static_cast<double>(ymdhms.sec);
81
82 ImGui::BeginGroup();
83 ImGui::SetNextItemWidth(itemWidth);
84 if (ImGui::InputInt(fmt::format("Year##{}", str_id).c_str(), &year, 0, 0)) { edited = true; }
85 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
86
87 ImGui::SetNextItemWidth(itemWidth);
88 if (ImGui::InputIntL(fmt::format("Month##{}", str_id).c_str(), &month, 1, InsTimeUtil::MONTHS_PER_YEAR, 0, 0)) { edited = true; }
89 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
90
91 ImGui::SetNextItemWidth(itemWidth);
92 if (ImGui::InputIntL(fmt::format("Day##{}", str_id).c_str(), &day, 1, InsTimeUtil::daysInMonth(month, year), 0, 0)) { edited = true; }
93 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
94 ImGui::EndGroup();
95
96 if (columns == 2) { ImGui::SameLine(); }
97 ImGui::BeginGroup();
98 ImGui::SetNextItemWidth(itemWidth);
99 if (ImGui::InputIntL(fmt::format("Hour##{}", str_id).c_str(), &hour, 0, InsTimeUtil::HOURS_PER_DAY - 1, 0, 0)) { edited = true; }
100 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
101
102 ImGui::SetNextItemWidth(itemWidth);
103 if (ImGui::InputIntL(fmt::format("Min##{}", str_id).c_str(), &min, 0, InsTimeUtil::MINUTES_PER_HOUR - 1, 0, 0)) { edited = true; }
104 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
105
106 ImGui::SetNextItemWidth(itemWidth);
107 if (ImGui::InputDoubleL(fmt::format("Sec##{}", str_id).c_str(), &sec, 0, InsTimeUtil::SECONDS_PER_MINUTE - 1e-5, 0, 0, "%.6f")) { edited = true; }
108 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
109 ImGui::EndGroup();
110
111 if (changes || edited)
112 {
113 insTime = InsTime{ static_cast<uint16_t>(year), static_cast<uint16_t>(month), static_cast<uint16_t>(day),
114 static_cast<uint16_t>(hour), static_cast<uint16_t>(min), sec, timeEditFormat.system };
115 }
116 }
117 else // if (timeEditFormat == TimeEditFormat::Format::GPSWeekToW)
118 {
119 auto gpsWeekTow = insTime.toGPSweekTow(timeEditFormat.system);
120
121 int cycle = gpsWeekTow.gpsCycle;
122 int week = gpsWeekTow.gpsWeek;
123 auto tow = static_cast<double>(gpsWeekTow.tow);
124
125 ImGui::SetNextItemWidth(itemWidth);
126 if (ImGui::InputIntL(fmt::format("Cycle##{}", str_id).c_str(), &cycle, 0, std::numeric_limits<int>::max(), 0, 0)) { edited = true; }
127 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
128
129 ImGui::SetNextItemWidth(itemWidth);
130 if (ImGui::InputIntL(fmt::format("Week##{}", str_id).c_str(), &week, 0, InsTimeUtil::WEEKS_PER_GPS_CYCLE - 1, 0, 0)) { edited = true; }
131 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
132
133 ImGui::SetNextItemWidth(itemWidth);
134 if (ImGui::InputDoubleL(fmt::format("ToW [s]##{}", str_id).c_str(), &tow, 0, std::numeric_limits<double>::max(), 0, 0, "%.6f")) { edited = true; }
135 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
136
137 if (changes || edited)
138 {
139 insTime = InsTime{ cycle, week, tow, timeEditFormat.system };
140 }
141 }
142
143 ImGui::EndGroup();
144
145 return changes;
146}
147
148void NAV::gui::widgets::to_json(json& j, const TimeEditFormat& timeEditFormat)
149{
150 j = json{
151 { "format", timeEditFormat.format },
152 { "system", timeEditFormat.system },
153 };
154}
155
156void NAV::gui::widgets::from_json(const json& j, TimeEditFormat& timeEditFormat)
157{
158 j.at("format").get_to(timeEditFormat.format);
159 j.at("system").get_to(timeEditFormat.system);
160}
Combo representing an enumeration.
nlohmann::json json
json namespace
Widget to modify time point values.
The class is responsible for all time-related tasks.
Definition InsTime.hpp:710
constexpr InsTime_GPSweekTow toGPSweekTow(TimeSystem timesys=GPST) const
Converts this time object into a different format.
Definition InsTime.hpp:854
constexpr InsTime_YMDHMS toYMDHMS(TimeSystem timesys=UTC, int digits=-1) const
Converts this time object into a different format.
Definition InsTime.hpp:871
ImGui extensions.
bool InputIntL(const char *label, int *v, int v_min, int v_max, int step, int step_fast, ImGuiInputTextFlags flags)
Shows a value limited InputText GUI element for 'int'.
Definition imgui_ex.cpp:242
bool InputDoubleL(const char *label, double *v, double v_min, double v_max, double step, double step_fast, const char *format, ImGuiInputTextFlags flags)
Shows a value limited InputText GUI element for 'double'.
Definition imgui_ex.cpp:294
constexpr int32_t MINUTES_PER_HOUR
Minutes / Hour.
Definition InsTime.hpp:55
constexpr int32_t daysInMonth(int32_t month, int32_t year)
Returns the number of days in the specified month of the year.
Definition InsTime.hpp:105
constexpr int32_t MONTHS_PER_YEAR
Months / Year.
Definition InsTime.hpp:50
constexpr int32_t HOURS_PER_DAY
Hours / Day.
Definition InsTime.hpp:53
constexpr int32_t WEEKS_PER_GPS_CYCLE
Weeks per GPS cycle.
Definition InsTime.hpp:42
constexpr int32_t SECONDS_PER_MINUTE
Seconds / Minute.
Definition InsTime.hpp:58
bool TimeEdit(const char *str_id, InsTime &insTime, TimeEditFormat &timeEditFormat, float itemWidth=170.0F, int columns=1)
Inputs to edit an InsTime object.
Definition TimeEdit.cpp:52
bool EnumCombo(const char *label, T &enumeration, size_t startIdx=0)
Combo representing an enumeration.
Definition EnumCombo.hpp:30
void from_json(const json &j, DynamicInputPins &obj, Node *node)
Converts the provided json object into a node object.
void to_json(json &j, const DynamicInputPins &obj)
Converts the provided object into json.
const char * to_string(gui::widgets::PositionWithFrame::ReferenceFrame refFrame)
Converts the enum to a string.
bool ComboTimeSystem(const char *label, TimeSystem &timeSystem)
Shows a ComboBox to select the time system.
int32_t gpsCycle
Contains GPS cycle in GPS standard time [GPST].
Definition InsTime.hpp:370
int32_t year
Contains year in Universal Time Coordinated [UTC].
Definition InsTime.hpp:466
Time Edit format and system.
Definition TimeEdit.hpp:29
TimeSystem system
Time System.
Definition TimeEdit.hpp:39
Format
Format to edit the time in.
Definition TimeEdit.hpp:32
@ COUNT
Amount of items in the enum.
Definition TimeEdit.hpp:35
@ YMDHMS
YearMonthDayHourMinuteSecond (UTC)
Definition TimeEdit.hpp:33
@ GPSWeekToW
GPS Week and TimeOfWeek.
Definition TimeEdit.hpp:34