NDEVR
API Documentation
ProgressWidgetDemo.h
1#pragma once
2#include "Widgets/Headers/QCustomTabWidget.h"
3#include "Widgets/Headers/ProgressWidget.h"
4#include "Widgets/Headers/GenericOptionsWidgets.h"
5#include "Widgets/Headers/Button.h"
6#include "Widgets/Headers/QTTools.h"
7#include "Base/Headers/Translator.h"
8#include <QTimer>
9#include <QLayout>
10namespace NDEVR
11{
16 {
17 public:
21 {
22 static GenericOptionGroup* group = nullptr;
23 if (group == nullptr)
24 {
25 group = new GenericOptionGroup(_t("Default Options"));
26 group->addOption(GenericOption("text", _t("Text"), String()));
27 GenericOption thickness("thickness", _t("Thickness"), 0.1);
28 thickness.value_bounds[MIN] = 0.0;
29 thickness.value_bounds[MAX] = 0.9;
30 group->addOption(thickness);
31
32 GenericOption tick_count("tick_count", _t("Tick Count"), 10U);
33 tick_count.value_bounds[MIN] = 1U;
34 tick_count.value_bounds[MAX] = 50U;
35 group->addOption(tick_count);
36
37 GenericOption preview_interval("preview_interval", _t("Preview Inverval"), 5.0);
38 preview_interval.value_bounds[MIN] = 0.5;
39 preview_interval.value_bounds[MIN] = 1000.0;
40 group->addOption(preview_interval);
41 GenericOption icon_id("icon_id", _t("Icon"), String());
42 icon_id.allow_custom_option = true;
43 icon_id.available_options.add({ _t("Blank"), String()});
44 icon_id.available_options.add({ _t("Battery"), String("battery") });
45 icon_id.available_options.add({ _t("NDEVR"), String("ndevr")});
46 icon_id.available_options.add({ _t("Warning"), String("Images/warning.png") });
47 icon_id.available_options.add({ _t("Compass Arrow"), String("Images/compass_arrow_markings.png") });
48 icon_id.available_options.add({ _t("Wi-Fi"), String("Images/wifi.png") });
49 icon_id.value_bounds[MAX] = 1.0;
50 group->addOption(icon_id);
51 group->addOption(GenericOption("horizontal", _t("Horizontal"), false));
52 }
53 return *group;
54 }
55
58 static Button* OptionsButton(QWidget* parent)
59 {
60 Button* options_button = new Button(_t("Default Options"), "dock_menu", [](Button* b)
61 {
62 static GenericOptionsGroup* s_group = nullptr;
63 s_group = new GenericOptionsGroup(options());
64 s_group->layout()->setContentsMargins(5,5,5,5);
65 QObject::connect(s_group, &GenericOptionsGroup::edited, s_group, []
66 {
67 options() = s_group->group();
68 });
69 PopupInfo info(b);
70 info.closable = true;
71 QTTools::ShowDialog(s_group, info);
72 }, parent);
73 options_button->setButtonState(Button::e_small);
74 return options_button;
75 }
76
78 static void Setup(QCustomTabWidget* dialog)
79 {
80 ProgressWidget* widget = new ProgressWidget();
81 widget->setProgressColors({
82 RGBColor(255, 0 , 0)//red
83 , RGBColor(255, 127, 0)
84 , RGBColor(255, 255, 0)
85 , RGBColor(0, 255, 0)
86 , RGBColor(0, 0, 255)
87 , RGBColor(75, 0, 130)
88 , RGBColor(148, 0, 211)
89 , RGBColor(255, 0 , 0) });
90
91 Button* options_button = OptionsButton(dialog);
92
93 QTimer* timer = new QTimer();
94 timer->setInterval(50);
95 timer->setSingleShot(false);
96 QObject::connect(timer, &QTimer::timeout, widget, [widget, options_button, dialog]
97 {
98 UpdateWidgetDemo(widget);
99 AdjustButtonPosition(options_button, dialog);
100 });
101 timer->start();
102 dialog->addWidget(widget);
103 }
104
107 static void AdjustButtonPosition(Button* preview_button, QCustomTabWidget* dialog)
108 {
109 //move button to always be at bottom right
110 QSize dialog_size = dialog->size();
111 QSize button_size = preview_button->size();
112 QPoint location;
113 location.setX(dialog_size.width() - button_size.width());
114 location.setY(dialog_size.height() - button_size.height());
115 preview_button->move(location);
116 preview_button->raise();
117 }
118
120 static void UpdateWidgetDemo(ProgressWidget* widget)
121 {
122 //animation progress variables
123 uint04 preview_period = cast<uint04>(1000 * options().getValue<fltp04>("preview_interval"));
124 uint08 millis = Time::SystemTime().getMilliseconds() % (4 * preview_period);
125 fltp04 percent = cast<fltp04>(millis % preview_period) / cast<fltp04>(preview_period);
126 //variables that modify appearance
127 String icon_id = options().getValue<String>("icon_id");
128 String text = options().getValue<String>("text");
129 uint04 tick_count = options().getValue<fltp04>("tick_count");
130 fltp04 thickness = options().getValue<fltp04>("thickness");
131 bool is_horizontal = options().getValue<bool>("horizontal");
132 switch (millis / preview_period)
133 {
134 case 0:
135 {
136 if (text.size() == 0)
137 {
138 if (percent > 0.1)
139 text += "Automatically";
140 if (percent > 0.2)
141 text += " Adjust";
142 if (percent > 0.3)
143 text += " Text";
144 if (percent > 0.4)
145 text += " to";
146 if (percent > 0.5)
147 text += " fit";
148 if (percent > 0.6)
149 text += " in";
150 if (percent > 0.7)
151 text += " dial";
152 }
153 percent = Constant<fltp04>::Invalid;
154 } break;
155 case 1:
156 if(text.size() == 0)
157 text = "Set Ticks\n%p%";
158 if (percent > 0.5)
159 tick_count = cast<uint04>((1.0f - percent) * 50) + 5U;
160 else
161 tick_count = cast<uint04>(percent * 50) + 5U;
162 break;
163 case 2:
164 if (text.size() == 0)
165 text = "Thickness\n%p%";
166 if (percent > 0.5)
167 thickness = 0.9f - 0.8f * percent;
168 else
169 thickness = 0.8f * percent + 0.1f;
170 break;
171 case 3:
172 if (text.size() == 0)
173 text = "Custom Icons";
174 if (percent < 0.2)
175 icon_id = "wifi";
176 else if (percent < 0.4)
177 icon_id = "Images/compass_arrow_markings.png";
178 else if (percent < 0.6)
179 icon_id = "cross_hair";
180 else if (percent < 0.8)
181 icon_id = "error";
182 else
183 icon_id = "ndevr";
184 break;
185 }
186 widget->setFormat(text);
188 widget->setPercent(percent);
189 widget->setIconID(icon_id);
190 widget->setHorizontal(is_horizontal);
191 widget->setTickCount(tick_count);
192 }
193 };
194}
A core widget that allows the user to click one of many button types.
Definition Button.h:68
void setButtonState(ButtonState state)
Sets the visual state/style of the button.
@ e_small
Small button with icon only.
Definition Button.h:80
Stores a groups of GenericOptions that can be used to group them.
t_type getValue(const TranslatedString &name) const
Retrieves the value of an option converted to the requested type, looked up by translated name.
void addOption(const TranslatedString &name, const t_type &value, bool is_editable=true)
Adds a new option to this group with the given name and typed value.
Creates a clean formatted dialog for any number of GenericOptions within a GenericOptionGroup Each op...
const GenericOptionGroup & group()
Returns the current GenericOptionGroup.
void edited()
Emitted when any option in the group has been edited.
Provides a demonstration of the ProgressWidget with animated previews and configurable options.
static Button * OptionsButton(QWidget *parent)
Creates a button that opens a popup with the demo options editor.
static void Setup(QCustomTabWidget *dialog)
Sets up the progress widget demo inside a tab widget with animation timer.
static void UpdateWidgetDemo(ProgressWidget *widget)
Updates the progress widget with animated values based on current time and options.
static GenericOptionGroup & options()
Returns the shared default options group for the progress widget demo.
static void AdjustButtonPosition(Button *preview_button, QCustomTabWidget *dialog)
Repositions the options button to the bottom-right corner of the dialog.
Displays the progress, either horizontally as a bar or as a round spin dial.
void setTickCount(uint04 tick_count)
Sets the number of tick marks drawn around the circular dial.
virtual void setPercent(fltp08 percent) override
Sets the progress percentage directly without animation.
void setProgressThickness(fltp04 thickness)
Sets the thickness of the progress arc or bar relative to the widget size.
void setIconID(const String &image_id, bool foreground=true)
Sets the icon displayed within the progress widget.
void setHorizontal(bool is_horizontal)
Sets whether the progress is displayed as a horizontal bar or a circular dial.
void setFormat(const StringView &format)
Sets the raw format string displayed inside the progress bar when progress is indeterminate.
void setProgressColors(const Buffer< RGBColor > &colors)
Sets multiple progress colors that will be evenly spaced across the progress range.
A tab widget provides a tab area and a "page area" that is used to display pages related to each tab.
uint04 addWidget(QWidget *child) override
Adds a widget as a new tab page.
static bool ShowDialog(QWidget *widget, PopupInfo info, PopupInfo end_location=PopupInfo())
Shows a dialog widget with animation and positioning specified by PopupInfo.
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
The core String class for the NDEVR API.
Definition String.h:95
static Time SystemTime()
Retrieves the current system time which is a combination of std::chrono::steady_clock to ensure smoot...
uint08 getMilliseconds() const
Gets the timestamp in milliseconds since 1970.
Definition Time.h:275
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
@ thickness
The thickness value applied to geometry rendering.
Definition Geometry.h:58
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Stores a generic option of any type as well as some information about how the user might interact wit...
Bounds< 1, fltp08 > value_bounds
The numeric bounds constraining valid values for this option.
Buffer< std::pair< TranslatedString, StringAllocatingView > > available_options
The set of predefined choices available for this option.
bool allow_custom_option
Whether the user may enter a custom value outside the predefined choices.
Class which is used to pass arguments and requests for creating a popup dialog or widget.
Definition PopupInfo.h:16