0.3.0
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)
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::SetNextItemWidth(itemWidth);
83 if (ImGui::InputInt(fmt::format("Year##{}", str_id).c_str(), &year, 0, 0)) { edited = true; }
84 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
85
86 ImGui::SetNextItemWidth(itemWidth);
87 if (ImGui::InputIntL(fmt::format("Month##{}", str_id).c_str(), &month, 1, InsTimeUtil::MONTHS_PER_YEAR, 0, 0)) { edited = true; }
88 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
89
90 ImGui::SetNextItemWidth(itemWidth);
91 if (ImGui::InputIntL(fmt::format("Day##{}", str_id).c_str(), &day, 1, InsTimeUtil::daysInMonth(month, year), 0, 0)) { edited = true; }
92 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
93
94 ImGui::SetNextItemWidth(itemWidth);
95 if (ImGui::InputIntL(fmt::format("Hour##{}", str_id).c_str(), &hour, 0, InsTimeUtil::HOURS_PER_DAY - 1, 0, 0)) { edited = true; }
96 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
97
98 ImGui::SetNextItemWidth(itemWidth);
99 if (ImGui::InputIntL(fmt::format("Min##{}", str_id).c_str(), &min, 0, InsTimeUtil::MINUTES_PER_HOUR - 1, 0, 0)) { edited = true; }
100 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
101
102 ImGui::SetNextItemWidth(itemWidth);
103 if (ImGui::InputDoubleL(fmt::format("Sec##{}", str_id).c_str(), &sec, 0, InsTimeUtil::SECONDS_PER_MINUTE - 1e-5, 0, 0, "%.6f")) { edited = true; }
104 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
105
106 if (changes || edited)
107 {
108 insTime = InsTime{ static_cast<uint16_t>(year), static_cast<uint16_t>(month), static_cast<uint16_t>(day),
109 static_cast<uint16_t>(hour), static_cast<uint16_t>(min), sec, timeEditFormat.system };
110 }
111 }
112 else // if (timeEditFormat == TimeEditFormat::Format::GPSWeekToW)
113 {
114 auto gpsWeekTow = insTime.toGPSweekTow(timeEditFormat.system);
115
116 int cycle = gpsWeekTow.gpsCycle;
117 int week = gpsWeekTow.gpsWeek;
118 auto tow = static_cast<double>(gpsWeekTow.tow);
119
120 ImGui::SetNextItemWidth(itemWidth);
121 if (ImGui::InputIntL(fmt::format("Cycle##{}", str_id).c_str(), &cycle, 0, std::numeric_limits<int>::max(), 0, 0)) { edited = true; }
122 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
123
124 ImGui::SetNextItemWidth(itemWidth);
125 if (ImGui::InputIntL(fmt::format("Week##{}", str_id).c_str(), &week, 0, InsTimeUtil::WEEKS_PER_GPS_CYCLE - 1, 0, 0)) { edited = true; }
126 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
127
128 ImGui::SetNextItemWidth(itemWidth);
129 if (ImGui::InputDoubleL(fmt::format("ToW [s]##{}", str_id).c_str(), &tow, 0, std::numeric_limits<double>::max(), 0, 0, "%.6f")) { edited = true; }
130 if (ImGui::IsItemDeactivatedAfterEdit()) { changes = true; }
131
132 if (changes || edited)
133 {
134 insTime = InsTime{ cycle, week, tow, timeEditFormat.system };
135 }
136 }
137
138 ImGui::EndGroup();
139
140 return changes;
141}
142
143void NAV::gui::widgets::to_json(json& j, const TimeEditFormat& timeEditFormat)
144{
145 j = json{
146 { "format", timeEditFormat.format },
147 { "system", timeEditFormat.system },
148 };
149}
150
151void NAV::gui::widgets::from_json(const json& j, TimeEditFormat& timeEditFormat)
152{
153 j.at("format").get_to(timeEditFormat.format);
154 j.at("system").get_to(timeEditFormat.system);
155}
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)
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