-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstrumentcluster.cpp
More file actions
478 lines (359 loc) · 11.8 KB
/
Copy pathinstrumentcluster.cpp
File metadata and controls
478 lines (359 loc) · 11.8 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// SPDX-License-Inentifier: Apache-2.0 and MIT
/*
* HiPNUC CHManager Instrument Clusters used to make
* the Attitude Indicator widget and Compass widget.
*
* Copyright (C) 2018 Marek M. Cel, Dave Culp
* Copyright (C) 2016-2020 HiPNUC All rights reserved.
*
*/
#include "instrumentcluster.h"
#include <math.h>
#include <QTimer>
/**
* @brief CHInstrumentWidget::CHInstrumentWidget - Constructs a flight
* instrument with a title and a parent.
* @param title
* @param parent
*/
InstrumentWidget::InstrumentWidget(const QString &title, QWidget *parent)
: QWidget(parent), m_adi(0), layout(0) {
QGraphicsView *graphics = nullptr;
this->resize(400, 300);
int result =
QString::compare(title, "AttitudeIndicator", Qt::CaseInsensitive);
this->result = result;
if (!result) {
graphics = new AttitudeIndicator(this);
m_adi = (AttitudeIndicator *)graphics;
} else {
graphics = new Compass(this);
m_hsi = (Compass *)graphics;
}
graphics->setObjectName(title);
graphics->setEnabled(false);
graphics->setGeometry(QRect(70, 40, 51, 31));
graphics->setFrameShape(QFrame::NoFrame);
graphics->setFrameShadow(QFrame::Plain);
graphics->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
graphics->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
graphics->setInteractive(false);
layout = new InstrumentLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(graphics);
setLayout(layout);
}
InstrumentWidget::~InstrumentWidget() {}
void InstrumentWidget::setData(float yaw, float roll, float pitch) {
if (result)
m_hsi->setYaw(yaw);
else
m_adi->setData(roll, pitch);
}
void InstrumentWidget::updateView(float yaw, float roll, float pitch) {
this->setData(yaw, roll, pitch);
}
void InstrumentWidget::transaction() {}// timer->start(100); }
/**
* @brief CHAttitudeIndicator::CHAttitudeIndicator
* @param parent
*/
AttitudeIndicator::AttitudeIndicator(QWidget *parent)
: QGraphicsView(parent), m_scene(nullptr), m_itemBack(nullptr),
m_itemFace(nullptr), m_itemRing(nullptr), m_itemCase(nullptr),
m_roll(0.0f), m_pitch(0.0f), m_faceDeltaX_new(0.0f),
m_faceDeltaX_old(0.0f), m_faceDeltaY_new(0.0f), m_faceDeltaY_old(0.0f),
m_scaleX(1.0f), m_scaleY(1.0f), m_originalHeight(240),
m_originalWidth(240), m_originalPixPerDeg(1.7f),
m_originalAdiCtr(120.0f, 120.0f), m_backZ(-30), m_faceZ(-20),
m_ringZ(-10), m_caseZ(10) {
reset();
m_scene = new QGraphicsScene(this);
setScene(m_scene);
m_scene->clear();
init();
}
AttitudeIndicator::~AttitudeIndicator() {
if (m_scene != nullptr) {
m_scene->clear();
delete m_scene;
m_scene = nullptr;
}
reset();
}
void AttitudeIndicator::reinit() {
if (m_scene) {
m_scene->clear();
init();
}
}
void AttitudeIndicator::update() {
updateView();
m_faceDeltaX_old = m_faceDeltaX_new;
m_faceDeltaY_old = m_faceDeltaY_new;
}
void AttitudeIndicator::setData(float roll, float pitch) {
m_roll = roll;
m_pitch = pitch;
if (m_roll < -180.0f)
m_roll = -180.0f;
if (m_roll > 180.0f)
m_roll = 180.0f;
if (m_pitch < -25.0f)
m_pitch = -25.0f;
if (m_pitch > 25.0f)
m_pitch = 25.0f;
update();
}
void AttitudeIndicator::resizeEvent(QResizeEvent *event) {
QGraphicsView::resizeEvent(event);
reinit();
}
void AttitudeIndicator::init() {
m_scaleX = (float)width() / (float)m_originalWidth;
m_scaleY = (float)height() / (float)m_originalHeight;
reset();
m_itemBack = new QGraphicsSvgItem(":/svg/compass/adi/adi_back.svg");
m_itemBack->setCacheMode(QGraphicsItem::NoCache);
m_itemBack->setZValue(m_backZ);
m_itemBack->setTransform(QTransform::fromScale(m_scaleX, m_scaleY), true);
m_itemBack->setTransformOriginPoint(m_originalAdiCtr);
m_scene->addItem(m_itemBack);
m_itemFace = new QGraphicsSvgItem(":/svg/compass/adi/adi_face.svg");
m_itemFace->setCacheMode(QGraphicsItem::NoCache);
m_itemFace->setZValue(m_faceZ);
m_itemFace->setTransform(QTransform::fromScale(m_scaleX, m_scaleY), true);
m_itemFace->setTransformOriginPoint(m_originalAdiCtr);
m_scene->addItem(m_itemFace);
m_itemRing = new QGraphicsSvgItem(":/svg/compass/adi/adi_ring.svg");
m_itemRing->setCacheMode(QGraphicsItem::NoCache);
m_itemRing->setZValue(m_ringZ);
m_itemRing->setTransform(QTransform::fromScale(m_scaleX, m_scaleY), true);
m_itemRing->setTransformOriginPoint(m_originalAdiCtr);
m_scene->addItem(m_itemRing);
m_itemCase = new QGraphicsSvgItem(":/svg/compass/adi/adi_case.svg");
m_itemCase->setCacheMode(QGraphicsItem::NoCache);
m_itemCase->setZValue(m_caseZ);
m_itemCase->setTransform(QTransform::fromScale(m_scaleX, m_scaleY), true);
m_scene->addItem(m_itemCase);
centerOn(width() / 2.0f, height() / 2.0f);
updateView();
}
void AttitudeIndicator::reset() {
m_itemBack = nullptr;
m_itemFace = nullptr;
m_itemRing = nullptr;
m_itemCase = nullptr;
m_roll = 0.0f;
m_pitch = 0.0f;
m_faceDeltaX_new = 0.0f;
m_faceDeltaX_old = 0.0f;
m_faceDeltaY_new = 0.0f;
m_faceDeltaY_old = 0.0f;
}
void AttitudeIndicator::updateView() {
m_scaleX = (float)width() / (float)m_originalWidth;
m_scaleY = (float)height() / (float)m_originalHeight;
m_itemBack->setRotation(-m_roll);
m_itemFace->setRotation(-m_roll);
m_itemRing->setRotation(-m_roll);
float roll_rad = M_PI * m_roll / 180.0;
float delta = m_originalPixPerDeg * m_pitch;
m_faceDeltaX_new = m_scaleX * delta * sin(roll_rad);
m_faceDeltaY_new = m_scaleY * delta * cos(roll_rad);
m_itemFace->moveBy(m_faceDeltaX_new - m_faceDeltaX_old,
m_faceDeltaY_new - m_faceDeltaY_old);
m_scene->update();
}
Compass::Compass(QWidget *parent)
: QGraphicsView(parent), m_scene(nullptr), m_itemFace(nullptr),
m_itemCase(nullptr), m_yaw(0.0f), m_scaleX(1.0f), m_scaleY(1.0f),
m_originalHeight(240), m_originalWidth(240),
m_originalHsiCtr(120.0f, 120.0f), m_faceZ(-20), m_caseZ(10) {
reset();
m_scene = new QGraphicsScene(this);
setScene(m_scene);
m_scene->clear();
init();
}
Compass::~Compass() {
if (m_scene != nullptr) {
m_scene->clear();
delete m_scene;
m_scene = nullptr;
}
reset();
}
void Compass::reinit() {
if (m_scene) {
m_scene->clear();
init();
}
}
void Compass::update() { updateView(); }
void Compass::setYaw(float yaw) {
m_yaw = yaw;
update();
}
void Compass::resizeEvent(QResizeEvent *event) {
QGraphicsView::resizeEvent(event);
reinit();
}
void Compass::init() {
m_scaleX = (float)width() / (float)m_originalWidth;
m_scaleY = (float)height() / (float)m_originalHeight;
reset();
m_itemFace = new QGraphicsSvgItem(":/svg/compass/hsi/hsi_face.svg");
m_itemFace->setCacheMode(QGraphicsItem::NoCache);
m_itemFace->setZValue(m_faceZ);
m_itemFace->setTransform(QTransform::fromScale(m_scaleX, m_scaleY), true);
m_itemFace->setTransformOriginPoint(m_originalHsiCtr);
m_scene->addItem(m_itemFace);
m_itemCase = new QGraphicsSvgItem(":/svg/compass/hsi/hsi_case.svg");
m_itemCase->setCacheMode(QGraphicsItem::NoCache);
m_itemCase->setZValue(m_caseZ);
m_itemCase->setTransform(QTransform::fromScale(m_scaleX, m_scaleY), true);
m_scene->addItem(m_itemCase);
centerOn(width() / 2.0f, height() / 2.0f);
updateView();
}
void Compass::reset() {
m_itemFace = nullptr;
m_itemCase = nullptr;
m_yaw = 0.0f;
}
void Compass::updateView() {
m_itemFace->setRotation(-m_yaw);
m_scene->update();
}
/**
* @brief CHInstrumentLayout::CHInstrumentLayout
* @param parent
* @param spacing
*/
InstrumentLayout::InstrumentLayout(QWidget *parent, int spacing)
: QLayout(parent) {
init(spacing);
}
InstrumentLayout::InstrumentLayout(int spacing) : QLayout(0) {
init(spacing);
}
InstrumentLayout::~InstrumentLayout() {
if (m_item)
delete m_item;
m_item = 0;
if (m_rectLast)
delete m_rectLast;
m_rectLast = 0;
if (m_geometry)
delete m_geometry;
m_geometry = 0;
}
void InstrumentLayout::addItem(QLayoutItem *item) {
if (!hasItem())
replaceItem(item);
}
void InstrumentLayout::addWidget(QWidget *widget) {
if (!hasItem())
replaceItem(new QWidgetItem(widget));
}
int InstrumentLayout::count() const {
if (hasItem())
return 1;
return 0;
}
Qt::Orientations InstrumentLayout::expandingDirections() const {
return (Qt::Horizontal | Qt::Vertical);
}
QRect InstrumentLayout::geometry() { return (QRect)(*m_geometry); }
bool InstrumentLayout::hasHeightForWidth() const { return false; }
bool InstrumentLayout::hasItem() const { return (m_item != 0); }
QLayoutItem *InstrumentLayout::itemAt(int index) const {
if (index == 0) {
if (hasItem())
return m_item;
}
return 0;
}
QSize InstrumentLayout::minimumSize() const { return m_item->minimumSize(); }
QLayoutItem *InstrumentLayout::replaceItem(QLayoutItem *item) {
QLayoutItem *tempItem = 0;
if (hasItem())
tempItem = m_item;
m_item = item;
setGeometry(*m_geometry);
return tempItem;
}
void InstrumentLayout::setGeometry(const QRect &rect) {
if (!hasItem() || areRectsEqual(*m_rectLast, rect))
return;
setRectLast(rect);
QSize properSize = calculateProperSize(rect.size());
QPoint centerPoint = calculateCenterPnt(rect.size(), properSize);
m_item->setGeometry(QRect(centerPoint, properSize));
QRect *tempRect = m_geometry;
m_geometry = new QRect(centerPoint, properSize);
delete tempRect;
QLayout::setGeometry(*m_geometry);
}
QSize InstrumentLayout::sizeHint() const { return m_item->minimumSize(); }
QLayoutItem *InstrumentLayout::take() {
QLayoutItem *tempItem = 0;
if (hasItem()) {
tempItem = m_item;
m_item = 0;
}
return tempItem;
}
QLayoutItem *InstrumentLayout::takeAt(int index) {
if (index == 0)
return take();
return 0;
}
bool InstrumentLayout::areRectsEqual(const QRect &rect_1,
const QRect &rect_2) const {
bool result = false;
if ((rect_1.x() == rect_2.x()) && (rect_1.y() == rect_2.y()) &&
(rect_1.height() == rect_2.height()) &&
(rect_1.width() == rect_2.width())) {
result = true;
}
return result;
}
QPoint InstrumentLayout::calculateCenterPnt(QSize fromSize,
QSize itemSize) const {
QPoint centerPoint;
if ((fromSize.width() - fromSize.width() / 2.0 - itemSize.width() / 2.0) >
0.0) {
centerPoint.setX(fromSize.width() - fromSize.width() / 2.0 -
itemSize.width() / 2.0);
}
if ((fromSize.height() - fromSize.height() / 2.0 -
itemSize.height() / 2.0) > 0.0) {
centerPoint.setY(fromSize.height() - fromSize.height() / 2.0 -
itemSize.height() / 2.0);
}
return centerPoint;
}
QSize InstrumentLayout::calculateProperSize(QSize fromSize) const {
QSize resultSize;
if (fromSize.height() < fromSize.width()) {
resultSize.setHeight(fromSize.height() - margin());
resultSize.setWidth(fromSize.height() - margin());
} else {
resultSize.setHeight(fromSize.width() - margin());
resultSize.setWidth(fromSize.width() - margin());
}
return resultSize;
}
void InstrumentLayout::init(int spacing) {
m_item = 0;
m_rectLast = new QRect(0, 0, 0, 0);
m_geometry = new QRect(0, 0, 0, 0);
setSpacing(spacing);
}
void InstrumentLayout::setRectLast(const QRect &rect) {
QRect *tempRect = m_rectLast;
m_rectLast = new QRect(rect.topLeft(), rect.size());
delete tempRect;
}