forked from nonesense84/QDmi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmilabel.cpp
More file actions
570 lines (524 loc) · 20.6 KB
/
Copy pathdmilabel.cpp
File metadata and controls
570 lines (524 loc) · 20.6 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "dmilabel.h"
#include <QDebug>
#include <QtSvg>
dmiLabel::dmiLabel(QWidget *parent) : QWidget(parent){
fontFactor = 96 / QApplication::screens().at(0)->logicalDotsPerInch();
setTargetDistance(100,false);
QFontDatabase::addApplicationFont(":/fonts/FreeSans.ttf");
QFontDatabase::addApplicationFont(":/fonts/Segment7Standard.otf");
}
void dmiLabel::setDpi(qreal dpi){
}
void dmiLabel::attenuationRoutine(){
bool updateNedded = false;
qreal attenuation = 0.2;
if(targetDistanceGraph < targetDistanceDest){
targetDistanceGraph =targetDistanceDest;
updateNedded = true;
}
if(targetDistanceGraph > targetDistanceDest){
targetDistanceGraph = targetDistanceGraph - (targetDistanceGraph - targetDistanceDest) * attenuation;
if((targetDistanceGraph - targetDistanceDest) < 0.25){
targetDistanceGraph = targetDistanceDest;
attenuationTimer->stop();
}
updateNedded = true;
}
if(updateNedded)update();
}
void dmiLabel::mousePressEvent(QMouseEvent *event){
if(isEnab){
isPushed = true;
update();
}
}
void dmiLabel::mouseReleaseEvent(QMouseEvent *event){
isPushed = false;
if(isEnab){
emit clicked(true);
if(isButton){
QMediaPlayer *click =new QMediaPlayer; // workaround: QSound::play crash on some android devices
click->setMedia(QUrl("qrc:/sounds/click.wav"));
click->setVolume(50);
click->play();
}
update();
emit txtBtnClicked(emitText);
}
emit dataEntryBtnClicked(labelText, isEnab);
}
void dmiLabel::setAsButton(QString text){
setAsButton(true, text, text);
}
void dmiLabel::setAsButton(bool enabled, QString text){
setAsButton(enabled, text, text);
}
void dmiLabel::setAsButton(bool enabled, QString text, QString textToEmit){
bool needUpdate = false;
if(isEnab != enabled || labelText != text)needUpdate = true;
isButton = true;
isEnab = enabled;
labelText = text;
emitText = textToEmit;
textStyle = QFont::Light;
if(needUpdate)update();
}
void dmiLabel::setAsDataEntryLabel(QString text, bool isInputfield, bool isEnabled, bool isValid, bool isApplicable, bool fullGrid){
if(!isApplicable){
labelText = "";
bgColor = era::darkBlue;
borderColorTopAndLeft = era::darkBlue;
borderColorButtomAndRight = era::darkBlue;
isDataEntryButton = false;
isButton = true;
update();
return;
}
textStyle = QFont::Light;
isEnab = isEnabled;
labelText = text;
emitText = text;
isDataEntryButton = true;
if(isInputfield){
if(!hasCustomAlignment)alignment = Qt::AlignLeft;
isButton = true;
setUnclosedFrame(!fullGrid && !isEnabled,false,false,false);
if(isEnabled){
bgColor = era::mediumGrey;
labelTextColorEnab = era::black;
borderColorTopAndLeft = era::darkGrey;
borderColorButtomAndRight = era::white;
}
else{
bgColor = era::darkGrey;
borderColorTopAndLeft = era::mediumGrey;
borderColorButtomAndRight = era::mediumGrey;
if(isValid){
labelTextColorDisab = era::white;
}
else{
labelTextColorDisab = era::grey;
}
}
}
else{
if(!hasCustomAlignment)alignment = Qt::AlignRight;
setUnclosedFrame(false,true,false,false);
bgColor = era::darkGrey;
labelTextColorEnab = era::grey;
labelTextColorDisab = era::grey;
borderColorTopAndLeft = era::mediumGrey;
borderColorButtomAndRight = era::mediumGrey;
}
update();
}
void dmiLabel::setTargetDistance(quint16 distance, bool visible){
if(!visible) distance = 0;
targetDistanceDest = distance;
targetDistance = distance;
targetDistanceVisible = visible;
if(targetDistance > distanceScale){
targetDistanceGraph = distanceScale;
attenuationTimer->stop();
}
else{
attenuationTimer->start();
}
update();
}
void dmiLabel::setIsDistanceScale(){
connect(attenuationTimer, SIGNAL(timeout()),this,SLOT(attenuationRoutine()));
attenuationTimer->setInterval(80);
attenuationTimer->start();
isTargetDistance = true;
if(useEraStyle){
fileForDistanceScale = ":/icons/targetDistEra1000m.svg";
distanceScale = 1000;
}
else{
fileForDistanceScale = ":/icons/targetDistDb4000m.svg";
distanceScale = 4000;
}
}
void dmiLabel::setEraUse(bool useEra){
useEraStyle = useEra;
if(isTargetDistance) setIsDistanceScale();
updateLabel();
}
void dmiLabel::updateBlinking(){
blinkerFast = !blinkerFast;
if(blinkerFast) blinkerSlow = !blinkerSlow;
if((blinkFrequency != 0 && fileNameIsSet)){
update();
}
}
void dmiLabel::updateLabel(){
if(blinkFrequency == 0){
update();
}
}
void dmiLabel::setBorderThickness(int thickness){
borderThickness = thickness;
}
void dmiLabel::setVisib(bool visible){
isVisible = visible;
updateLabel();
}
void dmiLabel::setWorking(bool enabled, quint8 blinkingFreq, bool inverse){
if((enabled != isEnab) || (blinkingFreq != blinkFrequency) || (inverse != isInvert)){
isEnab = enabled;
blinkFrequency = blinkingFreq;
isInvert = inverse;
updateLabel();
}
}
void dmiLabel::setIcon(QString filename){
if(filenameIconActive != filename){
if(mimeFile.suffixForFileName(filename) == "svg"){
svgActive.load(filename);
fileNameIsSet = filename != "";
}
filenameIconActive = filename;
updateLabel();
}
}
void dmiLabel::setIcon(QString filenameActive, QString filenameInactive){
if((filenameIconActive != filenameActive)||(filenameIconInactive != filenameInactive)){
svgActive.load(filenameActive);
svgInactive.load(filenameInactive);
fileNameIsSet = filenameActive != "";
filenameIconActive = filenameActive;
filenameIconInactive = filenameInactive;
updateLabel();
}
}
void dmiLabel::setCustomFontFactor(qreal factor){
customFontFactor = factor;
}
void dmiLabel::setCustomFontFactor(qreal factor, Qt::Alignment customAlignment){
customFontFactor = factor;
hasCustomAlignment = true;
alignment = static_cast<int>(customAlignment);
}
void dmiLabel::setText(QString text){
if(labelText != text){
labelText = text;
update();
}
}
void dmiLabel::setText(quint16 value){
QString text = QString::number(value);
if(labelText != text){
labelText = text;
update();
}
}
void dmiLabel::setText(QString text, QColor textColorEnabled, QColor textColorDisabled, quint8 boldOrThin){
if((labelText != text)||(labelTextColorEnab != textColorEnabled)||(labelTextColorDisab != textColorDisabled)||(textStyle != boldOrThin)){
textStyle = boldOrThin; //Normal (Not bold)
labelText = text;
labelTextColorEnab = textColorEnabled;
labelTextColorDisab = textColorDisabled;
update();
}
}
void dmiLabel::setText(QString text, QColor textColor, QColor backgroundColor){
if((labelText != text)||(labelTextColorEnab != textColor)||(bgColor != backgroundColor)){
labelText = text;
labelTextColorEnab = textColor;
bgColor = backgroundColor;
update();
}
}
void dmiLabel::setUnclosedFrame(bool openL, bool openR, bool openU, bool openD){
borderLClosed = !openL;
borderRClosed = !openR;
borderTClosed = !openU;
borderBClosed = !openD;
}
void dmiLabel::setTextFieldUsing(quint8 numFields){
isTextField = true;
numTextFields = numFields;
if(!hasCustomAlignment)alignment = Qt::AlignLeft;
}
void dmiLabel::setTextFieldUsing(quint8 numFields, quint8 customAlignment){
isTextField = true;
numTextFields = numFields;
hasCustomAlignment = true;
alignment = customAlignment;
}
void dmiLabel::setSegmentDigitToUse(quint8 position){
segmentPosition = position;
}
void dmiLabel::setSegmentText(quint16 value, bool textVisible){
if(isSegment != textVisible || segmentValue != value){
segmentValue = value;
isSegment = textVisible;
segmentText = QString::number(value);
segmentText = segmentText.mid(segmentText.length() - segmentPosition,1);
updateLabel();
}
}
void dmiLabel::addTextMessage(QString text, QColor textColor, QColor bgColor, quint8 msgId){
if(!isTextField)setTextFieldUsing(1);
for(quint8 i=0; i<10; i++){
if(messageIds[i] == msgId){
return; // Message allready in use
}
}
for(quint8 i=0; i<10; i++){
if(messageIds[i] == 255){
messageTexts[i] = text;
messageBackQolors[i] = bgColor;
messageTextColors[i] = textColor;
messageIds[i] = msgId;
update();
highestTextMessgePosition = i;
//qDebug() << "highestTextMessgePosition: " + QString::number(highestTextMessgePosition);
if(i >= numTextFields)emit messaesOutOfView(true);
return;
}
}
}
void dmiLabel::removeTextMessage(quint8 msgId){
bool needUpdate = false;
bool foundMessaesOutOfView = false;
for(quint8 i=0; i<10; i++){
if(messageIds[i] == msgId){
messageTexts[i] = "";
messageBackQolors[i] = era::darkBlue;
messageTextColors[i] = era::darkBlue;
messageIds[i] = 255;
needUpdate = true;
}
if(messageIds[i] != 255 && i > numTextFields){
foundMessaesOutOfView = true;
highestTextMessgePosition = i;
if(textMessageOffset >= highestTextMessgePosition) textMessageOffset = highestTextMessgePosition - numTextFields + 1;
//qDebug() << "highestTextMessgePosition: " + QString::number(highestTextMessgePosition);
}
}
if(needUpdate)update();
emit messaesOutOfView(foundMessaesOutOfView);
}
void dmiLabel::shiftTextMessageOffset(qint8 shift){
textMessageOffset = textMessageOffset + shift;
if(textMessageOffset < 0) textMessageOffset = 0;
if(textMessageOffset >= highestTextMessgePosition) textMessageOffset = highestTextMessgePosition - numTextFields + 1;
update();
//qDebug() << "textMessageOffset: " + QString::number(textMessageOffset);
}
void dmiLabel::paintEvent(QPaintEvent *)
{
if(isVisible){
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRect centralAreaLbl( borderThickness * borderLClosed,
borderThickness * borderTClosed,
width() - borderThickness * (borderRClosed + borderLClosed),
height() - borderThickness * (borderBClosed + borderTClosed));
// First we have to draw the frame of the label.
// In case of a data entry button, there is no frame arround,
// so the buttom will be drawn by the frame itself, and without a frame if its pushed.
// If it is a regular button, we have to draw the button INSIDE the label with swaped worder colors.
// ...But only, when its not pushed, because the we only need the frame.
if(isDataEntryButton && isPushed){ // Pushed data enrtry button. Entire label without a frame:
paintFrame(&painter, centralAreaLbl, bgColor, bgColor, 0);
}
else{ // Regualar frame:
paintFrame(&painter, centralAreaLbl, borderColorButtomAndRight, borderColorTopAndLeft, 0);
}
if(isButton && !isDataEntryButton){ // Regular button inside a frame, thats allready painted
QRect centralAreaBtn(2*borderThickness, 2 * borderThickness, width() - 4 * borderThickness, height() - 4 * borderThickness);
if(!isPushed){ // Regular bushed button inside a frame, thats allready painted
paintFrame(&painter, centralAreaBtn, borderColorTopAndLeft, borderColorButtomAndRight, borderThickness);
}
paintIcon(&painter, centralAreaBtn);
}
if(isTextField){
paintTextMessages(&painter, centralAreaLbl);
}
if(isSegment){
paintSegment(&painter, centralAreaLbl);
}
if(isTargetDistance && targetDistanceVisible){
paintDistance(&painter, centralAreaLbl);
}
if(!isTargetDistance && !isSegment && !isButton){
paintIcon(&painter, centralAreaLbl);
}
if(labelText != ""){
paintText(&painter, centralAreaLbl);
}
}
}
void dmiLabel::paintText(QPainter *iconPainter, QRect centralArea){
int lineHeight = centralArea.height();
int lineWidth = centralArea.width();
QRect field(centralArea.x(),centralArea.y(),lineWidth, lineHeight);
if(isEnab){
iconPainter->setPen(labelTextColorEnab);
}
else{
iconPainter->setPen(labelTextColorDisab);
}
iconPainter->setFont(QFont("FreeSans",
static_cast<int>(fontFactor * customFontFactor * lineHeight),
textStyle,
false));
QRectF textRect = iconPainter->boundingRect(field,alignment,labelText);
if(alignment == Qt::AlignLeft){
textRect.moveLeft(5*borderThickness);
textRect.translate(0, field.height()/2 - textRect.height()/2);
}
if(alignment == Qt::AlignRight){
textRect.translate(0, field.height()/2 - textRect.height()/2);
}
iconPainter->drawText(textRect,labelText);
}
void dmiLabel::paintDistance(QPainter *iconPainter, QRect centralArea){
iconPainter->save();
svgActive.load(fileForDistanceScale);
svgActive.render(iconPainter,centralArea);
iconPainter->scale(static_cast<qreal>(width()) / 58.0, static_cast<qreal>(height()) / 221.0);
QRect digitalDistPosition(8,5,48,18);
if(useEraStyle){
iconPainter->setBrush(era::grey);
iconPainter->setPen(era::grey);
iconPainter->setFont(QFont("FreeSans",
static_cast<int>(12 * fontFactor),
QFont::Normal,
false));
}
else{
iconPainter->setBrush(era::darkYellow);
iconPainter->setPen(era::darkYellow);
iconPainter->setFont(QFont("Segment7",
static_cast<int>(12 * fontFactor),
QFont::Normal,
false));
}
QRect textRect = iconPainter->boundingRect(digitalDistPosition,Qt::AlignRight,QString::number(targetDistance));
digitalDistPosition.setLeft(45 - textRect.width());
//quint16 targetDistanceAnalog = targetDistance;
qreal tem = 0.0;
if(useEraStyle){
iconPainter->drawText(digitalDistPosition,QString::number(ceil((targetDistance / 10)) * 10));
if(targetDistanceGraph < 1000)
tem = -2.50582750582749E-10 * qPow(targetDistanceGraph,4)
+7.07459207459205E-07 * qPow(targetDistanceGraph,3)
-0.000815792540793 * qPow(targetDistanceGraph,2)
+0.545314685314686 * targetDistanceGraph
+0.300699300699198;
else
tem = 187;
QRectF distBar(30,217,10,-tem);
iconPainter->drawRect(distBar);
}
else{
if(targetDistance > 4000)iconPainter->drawText(digitalDistPosition,QString::number(ceil((targetDistance + 100)/ 200) * 200));
if(targetDistance <= 100)
tem = 0.19135135136 * targetDistanceGraph + 1.58882185807825E-15;
if(targetDistanceGraph > 100 && targetDistanceGraph <= 1000)
tem = 0.09567567568 * targetDistanceGraph + 9.56756756800004;
if(targetDistanceGraph > 1000)
tem = 0.02391891892 * targetDistanceGraph + 81.3243243279999;
QRectF distBar(42,213,10,-tem);
iconPainter->drawRect(distBar);
}
iconPainter->restore();
}
void dmiLabel::paintSegment(QPainter *iconPainter, QRect centralArea){
iconPainter->save();
iconPainter->setPen(era::darkYellow);
iconPainter->setFont(QFont("Segment7",
static_cast<int>(fontFactor * 0.6 * centralArea.height()),
QFont::Normal,
false));
QRect textRect = iconPainter->boundingRect(centralArea,Qt::AlignLeft,segmentText);
textRect.translate(centralArea.width()/1.5 - textRect.width()/1.5,
(centralArea.height()/1.3 - textRect.height()/1.3));
iconPainter->drawText(textRect,segmentText);
iconPainter->restore();
}
void dmiLabel::paintTextMessages(QPainter *iconPainter, QRect centralArea){
int lineHeight = centralArea.height()/numTextFields;
QRect field(centralArea.x(),centralArea.y(),centralArea.width(), lineHeight);
iconPainter->save();
QFont font = QFont("FreeSans");
font.setBold(true);
font.setPointSizeF(fontFactor * 0.44 * lineHeight);
iconPainter->setFont(font);
for(quint8 i=0; i < numTextFields; i++){
if(i == numTextFields - 1){
field.setHeight(centralArea.height() - i * lineHeight);
}
iconPainter->setPen(Qt::NoPen);
iconPainter->setBrush(messageBackQolors[i + textMessageOffset]);
iconPainter->drawRect(field);
iconPainter->setPen(messageTextColors[i + textMessageOffset]);
QRect textRect = iconPainter->boundingRect(field,Qt::AlignLeft,messageTexts[i + textMessageOffset]);
textRect.moveLeft(3*borderThickness);
if(alignment == Qt::AlignLeft)textRect.translate(0, field.height()/2 - textRect.height()/2);
if(alignment == Qt::AlignRight)textRect.translate(field.width()-textRect.width(), field.height()/2 - textRect.height()/2);
if(alignment == Qt::AlignCenter)textRect.translate(field.width()/2-textRect.width()/2, field.height()/2 - textRect.height()/2);
iconPainter->drawText(textRect,messageTexts[i + textMessageOffset]);
field.translate(0,lineHeight);
}
iconPainter->restore();
}
void dmiLabel::paintIcon(QPainter *iconPainter, QRect centralArea){
centralArea = calcOptimalRect(centralArea,svgActive.viewBox());
if(((blinkFrequency == 1) && blinkerSlow && !isInvert) ||
((blinkFrequency == 1) && !blinkerSlow && isInvert) ||
((blinkFrequency == 2) && blinkerFast && !isInvert) ||
((blinkFrequency == 2) && !blinkerFast && isInvert) ||
!isEnab){
svgInactive.render(iconPainter, centralArea);
}
else{
svgActive.render(iconPainter,centralArea);
}
}
QRect dmiLabel::calcOptimalRect(QRect destArea, QRect iconArea){
if(iconArea.width()>0 && isButton){
qreal relDest = static_cast<qreal>(destArea.width()) / static_cast<qreal>(destArea.height()); // >1 Breiter als hoch. Je größer, desto Breiter.
qreal relIcon = static_cast<qreal>(iconArea.width()) / static_cast<qreal>(iconArea.height());
qreal destRatio = static_cast<qreal>(destArea.height()) / static_cast<qreal>(destArea.width());
if(relDest > relIcon){ // Wenn das Widget breiter als das Icon
int originWidth = destArea.width();
destArea.setWidth(static_cast<int>(static_cast<qreal>(destArea.width())*destRatio));
destArea.moveLeft(originWidth/2 -destArea.width()/2 + destArea.left());
}
if(relDest < relIcon){ // Wenn das Widget schmaler als das Icon
int originHeight = destArea.height();
destArea.setHeight(static_cast<int>(static_cast<qreal>(destArea.height())/destRatio));
destArea.moveTop(originHeight/2 -destArea.height()/2 + destArea.top());
}
}
return destArea;
}
void dmiLabel::paintFrame(QPainter *framePainter,QRect centralArea, QColor colorLefUp, QColor colorRightDown, int offset){
int halfHeight = height()/2;
int halfWidth = width()/2;
QRect darkBorder(0 + offset, 0 + offset, width() - 2 * offset, height() - 2 * offset);
QPoint brightBorder[5];
brightBorder[0] = QPoint(width() - offset, 0 + offset);
brightBorder[1] = QPoint(width() - offset, height() - offset);
brightBorder[2] = QPoint(0 + offset, height() - offset);
if(halfWidth > halfHeight){
brightBorder[3] = QPoint(0 + halfHeight + offset, halfHeight - offset);
brightBorder[4] = QPoint( width() - halfHeight - offset, halfHeight + offset);
}
else{
brightBorder[3] = QPoint(halfWidth + offset, height() - halfWidth - offset);
brightBorder[4] = QPoint(halfWidth - offset, halfWidth + offset);
}
framePainter->setPen(Qt::NoPen);
framePainter->setBrush(colorLefUp);
framePainter->drawRect(darkBorder);
framePainter->setBrush(colorRightDown);
framePainter->drawPolygon(brightBorder, 5);
framePainter->setBrush(bgColor);
framePainter->drawRect(centralArea);
}