forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboardWidget.cpp
More file actions
318 lines (287 loc) · 8.95 KB
/
DashboardWidget.cpp
File metadata and controls
318 lines (287 loc) · 8.95 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (c) 2021-2023 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <Misc/ThemeManager.h>
#include <UI/DashboardWidget.h>
#include <UI/Widgets/Bar.h>
#include <UI/Widgets/GPS.h>
#include <UI/Widgets/Plot.h>
#include <UI/Widgets/Gauge.h>
#include <UI/Widgets/Compass.h>
#include <UI/Widgets/FFTPlot.h>
#include <UI/Widgets/LEDPanel.h>
#include <UI/Widgets/DataGroup.h>
#include <UI/Widgets/Gyroscope.h>
#include <UI/Widgets/MultiPlot.h>
#include <UI/Widgets/Accelerometer.h>
/**
* Constructor function
*/
UI::DashboardWidget::DashboardWidget(QQuickItem *parent)
: DeclarativeWidget(parent)
, m_index(-1)
, m_isGpsMap(false)
, m_widgetVisible(false)
, m_isExternalWindow(false)
{
// clang-format off
connect(&UI::Dashboard::instance(), &UI::Dashboard::widgetVisibilityChanged,
this, &UI::DashboardWidget::updateWidgetVisible);
// clang-format on
}
/**
* Delete widget on class destruction
*/
UI::DashboardWidget::~DashboardWidget()
{
if (m_dbWidget)
m_dbWidget->deleteLater();
}
/**
* Returns the global index of the widget (index of the current widget in
* relation to all registered widgets).
*/
int UI::DashboardWidget::widgetIndex() const
{
return m_index;
}
/**
* Returns the relative index of the widget (e.g. index of a bar widget in
* relation to the total number of bar widgets).
*/
int UI::DashboardWidget::relativeIndex() const
{
return UI::Dashboard::instance().relativeIndex(widgetIndex());
}
/**
* Returns @c true if the QML interface should display this widget.
*/
bool UI::DashboardWidget::widgetVisible() const
{
return m_widgetVisible;
}
/**
* Returns the path of the SVG icon to use with this widget
*/
QString UI::DashboardWidget::widgetIcon() const
{
return UI::Dashboard::instance().widgetIcon(widgetIndex());
}
/**
* Returns the appropiate window title for the given widget
*/
QString UI::DashboardWidget::widgetTitle() const
{
if (widgetIndex() >= 0)
{
auto titles = UI::Dashboard::instance().widgetTitles();
if (widgetIndex() < titles.count())
return titles.at(widgetIndex());
}
return tr("Invalid");
}
/**
* If set to @c true, then the widget visibility shall be controlled
* directly by the QML interface.
*
* If set to @c false, then the widget visbility shall be controlled
* by the UI::Dashboard class via the SIGNAL/SLOT system.
*/
bool UI::DashboardWidget::isExternalWindow() const
{
return m_isExternalWindow;
}
/**
* Returns the type of the current widget (e.g. group, plot, bar, gauge, etc...)
*/
UI::Dashboard::WidgetType UI::DashboardWidget::widgetType() const
{
return UI::Dashboard::instance().widgetType(widgetIndex());
}
/**
* Hack: rendering a map is currently very hard on QtWidgets, so we
* must use the QtLocation API (QML only) to display the map. Prevously, we
* used QMapControl to render the satellite map, however, QMapControl does
* not support the current mapping APIs and the project seems to have been
* abandoned :(
*
* The quick and dirty solution is to break the nice abstraction that this
* class provided and feed the GPS data from C++ to QML through this class.
*
* On the QML side, a QML Loader will be activated if (and only if) the
* isGpsMap() function returns true, in turn, the QML loader will display
* the GPS/Map widget and feed it the GPS data. Not nice, not cool, not
* very efficient, but it is a fast solution for a problem that I don't
* have too much time to fix (if I could pay my rent by writting FOSS,
* believe me I would).
*
* Please ping me or make a PR if you find a better solution, or have
* some free time to cleanup this fix.
*/
bool UI::DashboardWidget::isGpsMap() const
{
return m_isGpsMap;
}
/**
* Returns the current GPS altitude indicated by the GPS "parser" widget,
* this function only returns an useful value if @c isGpsMap() is @c true.
*/
qreal UI::DashboardWidget::gpsAltitude() const
{
if (isGpsMap() && m_dbWidget)
return static_cast<Widgets::GPS *>(m_dbWidget)->altitude();
return 0;
}
/**
* Returns the current GPS altitude indicated by the GPS "parser" widget,
* this function only returns an useful value if @c isGpsMap() is @c true.
*/
qreal UI::DashboardWidget::gpsLatitude() const
{
if (isGpsMap() && m_dbWidget)
return static_cast<Widgets::GPS *>(m_dbWidget)->latitude();
return 0;
}
/**
* Returns the current GPS altitude indicated by the GPS "parser" widget,
* this function only returns an useful value if @c isGpsMap() is @c true.
*/
qreal UI::DashboardWidget::gpsLongitude() const
{
if (isGpsMap() && m_dbWidget)
return static_cast<Widgets::GPS *>(m_dbWidget)->longitude();
return 0;
}
/**
* Changes the visibility & enabled status of the widget
*/
void UI::DashboardWidget::setVisible(const bool visible)
{
if (m_dbWidget)
{
m_dbWidget->setEnabled(visible);
update();
}
}
/**
* Selects & configures the appropiate widget for the given @a index
*/
void UI::DashboardWidget::setWidgetIndex(const int index)
{
if (index < UI::Dashboard::instance().totalWidgetCount() && index >= 0)
{
// Update widget index
m_index = index;
// Delete previous widget
if (m_dbWidget)
{
m_dbWidget->deleteLater();
m_dbWidget = nullptr;
}
// Initialize the GPS indicator flag to false by default
m_isGpsMap = false;
// Construct new widget
switch (widgetType())
{
case UI::Dashboard::WidgetType::Group:
m_dbWidget = new Widgets::DataGroup(relativeIndex());
break;
case UI::Dashboard::WidgetType::MultiPlot:
m_dbWidget = new Widgets::MultiPlot(relativeIndex());
break;
case UI::Dashboard::WidgetType::FFT:
m_dbWidget = new Widgets::FFTPlot(relativeIndex());
break;
case UI::Dashboard::WidgetType::Plot:
m_dbWidget = new Widgets::Plot(relativeIndex());
break;
case UI::Dashboard::WidgetType::Bar:
m_dbWidget = new Widgets::Bar(relativeIndex());
break;
case UI::Dashboard::WidgetType::Gauge:
m_dbWidget = new Widgets::Gauge(relativeIndex());
break;
case UI::Dashboard::WidgetType::Compass:
m_dbWidget = new Widgets::Compass(relativeIndex());
break;
case UI::Dashboard::WidgetType::Gyroscope:
m_dbWidget = new Widgets::Gyroscope(relativeIndex());
break;
case UI::Dashboard::WidgetType::Accelerometer:
m_dbWidget = new Widgets::Accelerometer(relativeIndex());
break;
case UI::Dashboard::WidgetType::GPS:
m_isGpsMap = true;
m_dbWidget = new Widgets::GPS(relativeIndex());
break;
case UI::Dashboard::WidgetType::LED:
m_dbWidget = new Widgets::LEDPanel(relativeIndex());
break;
default:
break;
}
// Configure widget
if (m_dbWidget)
{
setWidget(m_dbWidget);
updateWidgetVisible();
connect(m_dbWidget, &Widgets::DashboardWidgetBase::updated, this, [=]() {
if (!isGpsMap())
update();
else
Q_EMIT gpsDataChanged();
});
Q_EMIT widgetIndexChanged();
}
}
}
/**
* Changes the widget visibility controller source.
*
* If set to @c true, then the widget visibility shall be controlled
* directly by the QML interface.
*
* If set to @c false, then the widget visbility shall be controlled
* by the UI::Dashboard class via the SIGNAL/SLOT system.
*/
void UI::DashboardWidget::setIsExternalWindow(const bool isWindow)
{
m_isExternalWindow = isWindow;
Q_EMIT isExternalWindowChanged();
}
/**
* Updates the visibility status of the current widget (this function is called
* automatically by the UI::Dashboard class via signals/slots).
*/
void UI::DashboardWidget::updateWidgetVisible()
{
bool visible = UI::Dashboard::instance().widgetVisible(widgetIndex());
if (widgetVisible() != visible && !isExternalWindow())
{
m_widgetVisible = visible;
if (m_dbWidget)
{
m_dbWidget->setEnabled(visible);
update();
}
Q_EMIT widgetVisibleChanged();
}
}