-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdmilabel.cpp
More file actions
508 lines (464 loc) · 18.5 KB
/
Copy pathdmilabel.cpp
File metadata and controls
508 lines (464 loc) · 18.5 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
#include "dmilabel.h"
#include "qapplication.h"
#include <QDebug>
#include <QApplication>
dmiLabel::dmiLabel(QWidget *parent) : QWidget(parent){
fontFactor = 96 / QApplication::screens().at(0)->logicalDotsPerInch();
QFontDatabase::addApplicationFont(":/fonts/FreeSans.ttf");
QFontDatabase::addApplicationFont(":/fonts/Segment7Standard.otf");
}
void dmiLabel::mousePressEvent(QMouseEvent *event){
if(isEnab){
isPushed = true;
update();
}
(void)event;
}
void dmiLabel::mouseReleaseEvent(QMouseEvent *event){
isPushed = false;
if(isEnab){
emit clicked();
if(isButton && !isDataEntryButton){
click1->play();
}
update();
emit txtBtnClicked(emitText);
}
emit dataEntryBtnClicked(labelText, isEnab);
(void)event;
}
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){
if(click1 == nullptr){
click1 = new QSoundEffect(this);
click1->setVolume(0.5);
click1->setSource(QUrl("qrc:/sounds/click.wav"));
click1->setLoopCount(1);
}
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::setFrameless(){
borderThickness = 0;
}
void dmiLabel::setAsDataEntryLabel(QString text, bool isInputfield, bool isEnabled, bool isValid, bool isApplicable){
setAsDataEntryLabelFg(text, isInputfield, isEnabled, isValid, isApplicable, false);
}
void dmiLabel::setAsDataEntryLabelFg(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);
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);
bgColor = era::darkGrey;
labelTextColorEnab = era::grey;
labelTextColorDisab = era::grey;
borderColorTopAndLeft = era::mediumGrey;
borderColorButtomAndRight = era::mediumGrey;
}
update();
}
void dmiLabel::updateBlinking(){
tickCounter++;
blinkerSuperFast = !blinkerSuperFast;
if (tickCounter % 2 == 0) blinkerFast = !blinkerFast;
if (tickCounter % 4 == 0) blinkerSlow = !blinkerSlow;
if ((blinkFrequency != 0 && fileNameIsSet) || acknowledgeFrameActive)
update();
}
void dmiLabel::updateLabel(){
if(blinkFrequency == 0){
update();
}
}
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::setEnabled(bool enabled){
isEnab = enabled;
updateLabel();
}
void dmiLabel::setIcon(QString filename){
if(filenameIconActive != filename){
if(mimeFile.suffixForFileName(filename) == "svg"){
svgActive.load(filename);
fileNameIsSet = filename != "";
filenameIconActive = filename;
filenameIconInactive = ":/icons/blanc.svg";
svgInactive.load(filenameIconInactive);
}
updateLabel();
}
}
void dmiLabel::setIcon(QString filenameActive, QString filenameInactive){
if((filenameIconActive != filenameActive)||(filenameIconInactive != filenameInactive)){
fileNameIsSet = filenameActive != "";
if(filenameActive == "")
filenameIconActive = ":/icons/blanc.svg";
else
filenameIconActive = filenameActive;
if(filenameInactive == "")
filenameIconInactive = ":/icons/blanc.svg";
else
filenameIconInactive = filenameInactive;
svgActive.load(filenameIconActive);
svgInactive.load(filenameIconInactive);
updateLabel();
}
}
void dmiLabel::setCustomFontFactor(qreal factor){
customFontFactor = factor;
}
void dmiLabel::setCustomFontFactor(qreal factor, Qt::Alignment customAlignment){
setCustomFontFactor(factor);
hasCustomAlignment = true;
alignment = static_cast<int>(customAlignment);
}
void dmiLabel::setCustomFontFactor(qreal factor, Qt::Alignment customAlignment, QFont::Weight style){
textStyle = style;
setCustomFontFactor(factor, customAlignment);
}
void dmiLabel::setText(QString text){
if(labelText != text){
labelText = text;
update();
}
}
void dmiLabel::setText(quint16 value){
QString text = QString::number(value);
if(value == 0) text = "";
if(labelText != text){
labelText = text;
update();
}
}
void dmiLabel::setText(QString text, QColor textColorEnabled, QColor textColorDisabled, QFont::Weight 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){
borderLOpen = openL;
borderROpen = openR;
}
void dmiLabel::setAcklowedgeFrame(bool active){
acknowledgeFrameActive = active;
}
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::setSegmentDigit(quint16 value, bool textVisible, bool fromEtcs){
if(isSegment != textVisible || segmentValue != value){
customFontFactor = 0.6;
segmentValue = value;
isSegment = textVisible;
segmentText = QString::number(value);
segmentText = segmentText.mid(segmentText.length() - segmentPosition,1);
updateLabel();
}
(void)fromEtcs;
}
void dmiLabel::setSegmentText(quint16 value){
isSegment = true;
segmentText = QString::number(value);
update();
}
void dmiLabel::addTextLzbMessage(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::setTextMessageOffset(qint8 offset){
textMessageOffset = offset;
if(textMessageOffset < 0) textMessageOffset = 0;
if(textMessageOffset >= highestTextMessgePosition) textMessageOffset = highestTextMessgePosition - numTextFields + 1;
update();
}
void dmiLabel::paintEvent(QPaintEvent *)
{
if(isVisible){
if(borderThickness > 0) borderThickness = qApp->property("borderThickness").toInt();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// Create a rect, that does not contain the frame
QRect centralAreaLbl( borderThickness * !borderLOpen,
borderThickness,
width() - borderThickness * (!borderROpen + !borderLOpen),
height() - 2 * borderThickness);
// 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 border colors.
// ...But only, when its not pushed, because then we only need the frame.
painter.fillRect(rect(), bgColor);
if(!(isDataEntryButton && isPushed)){ // Regualar frame:
paintFrame(&painter, borderColorTopAndLeft, borderColorButtomAndRight, borderThickness, false);
}
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, borderColorButtomAndRight, borderColorTopAndLeft, borderThickness, true);
}
paintIcon(&painter, centralAreaBtn);
}
else{
if(fileNameIsSet)paintIcon(&painter, centralAreaLbl);
}
if(acknowledgeFrameActive && blinkerSuperFast) paintFrame(&painter, era::yellow, era::veryDarkYellow, borderThickness,false);
if(isTextField) paintTextMessages(&painter, centralAreaLbl);
if(isSegment) paintSegment(&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::paintSegment(QPainter *iconPainter, QRect centralArea){
iconPainter->save();
iconPainter->setPen(era::darkYellow);
iconPainter->setFont(QFont("Segment7",
static_cast<int>(fontFactor * customFontFactor * centralArea.height()),
QFont::Normal,
false));
QRect textRect = iconPainter->boundingRect(centralArea,Qt::AlignLeft,segmentText);
QPointF offset;
if(alignment == Qt::AlignLeft){
offset = QPointF(
static_cast<qreal>(0),
static_cast<qreal>(centralArea.height() - textRect.height()) / 1.3);
}
else if(alignment == Qt::AlignRight){
offset = QPointF(
static_cast<qreal>(centralArea.width() - textRect.width()) / 1.5,
static_cast<qreal>(centralArea.height() - textRect.height()) / 1.3);
}
else{
offset = QPointF(
static_cast<qreal>(centralArea.width() - textRect.width()) / 1.5,
static_cast<qreal>(centralArea.height() - textRect.height()) / 1.3);
}
textRect.translate(offset.toPoint());
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{
if(fileNameIsSet)svgActive.render(iconPainter,centralArea);
}
}
QRect dmiLabel::calcOptimalRect(QRect destArea, QRect iconArea){
if (!isButton || iconArea.isEmpty())
return destArea;
// Fit size of icon (viewBox) proportionaly in destArea
const QSizeF fitted = QSizeF(iconArea.size())
.scaled(destArea.size(), Qt::KeepAspectRatio);
QRect result(QPoint(0,0), fitted.toSize());
result.moveCenter(destArea.center());
return result;
}
void dmiLabel::paintFrame(QPainter *framePainter, QColor bright, QColor dark, int bt, bool isBtn){
// We draw the 3D sunken border
int w = width();
int h = height();
int bO = bt*isBtn; // Offset of widget corner, in case of painting a button
bool lC = !borderLOpen; // We need to take care of labels with open frames
bool rC = !borderROpen;
QPoint brightBorder[6] = {
QPoint( - bO , h - bO ), // Bottom left corner
QPoint(w - bO , h - bO ), // Bottom right corner
QPoint(w - bO , bO ), // Top right corner
QPoint(w - bO - bt*rC, bO + bt), // Shifted top right corner
QPoint(w - bO - bt*rC, h - bO - bt), // Shifted bottom right corner
QPoint( - bO + bt*lC, h - bO - bt) // Shifted bottom left corner
};
QPoint darkBorder[6] = {
QPoint( bO , h - bO ), // Bottom left corner
QPoint( bO , bO ), // Top left corner
QPoint(w - bO , bO ), // Top right corner
QPoint(w - bO - bt*rC, bO + bt), // Shifted top right corner
QPoint( bO + bt*lC, bO + bt), // Shifted lop left corner
QPoint( bO + bt*lC, h - bO - bt) // Shifted bottom left corner
};
framePainter->setPen(Qt::NoPen); // Set no outlin
framePainter->setBrush(bright); // Apply the shadow color
framePainter->drawPolygon(brightBorder, 6); // Draw the frame border at buttom and right
framePainter->setBrush(dark); // Set the brush to dark for the mirrored polygon
framePainter->drawPolygon(darkBorder, 6); // Draw the frame border at top and left
}