Repository navigation
Expand file tree
/
Copy pathetcstextwidget.cpp
More file actions
167 lines (144 loc) · 6.43 KB
/
Copy pathetcstextwidget.cpp
File metadata and controls
167 lines (144 loc) · 6.43 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
#include "etcstextwidget.h"
#include <QPaintEvent>
#include <QFontMetrics>
#include <QMouseEvent>
EtcsTextWidget::EtcsTextWidget(QWidget *parent) : QWidget(parent), borderThickness(2) {
// We generate some test messages for initial display
//generateTestMessages();
}
void EtcsTextWidget::generateTestMessages() {
// We clear the message list and add some sample messages
messageList.clear();
messageList.append({1, QDateTime::currentDateTime(), 0, "INFO", "Short test message", "", 0});
messageList.append({2, QDateTime::currentDateTime(), 0, "WARN", "A slightly longer message that should wrap across multiple lines to test word wrapping.", "", 0});
messageList.append({3, QDateTime::currentDateTime(), 0, "ERROR", "This is a very long test message that will definitely wrap over multiple lines to check vertical alignment.", "", 0});
messageList.append({4, QDateTime::currentDateTime(), 0, "ALERT", "ACK message", "", 1});
update();
}
void EtcsTextWidget::setEtcsTextMessages(const QList<EtcsTextMessage>& messages) {
// We update the message list and reset scrolling
messageList = messages;
scrollIndex = 0;
update();
}
void EtcsTextWidget::scrollUp() {
// We scroll up if we are not already at the top
if (scrollIndex > 0) {
scrollIndex--;
update();
emit enableScrollDown(true);
}
if (scrollIndex == 0) {
emit enableScrollUp(false);
}
}
void EtcsTextWidget::scrollDown() {
// We scroll down if there are more messages below
if (scrollIndex < messageList.size() - 1) {
scrollIndex++;
update();
emit enableScrollUp(true);
}
if (scrollIndex >= messageList.size() - 1) {
emit enableScrollDown(false);
}
}
int EtcsTextWidget::calculateTextHeight() const {
// We calculate the proportional text height based on widget height
return static_cast<int>(width()/13.09);
}
void EtcsTextWidget::paintEvent(QPaintEvent *event) {
int borderThickness = qApp->property("borderThickness").toInt();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// We fill the background with the predefined dark blue color
painter.fillRect(rect(), era::darkBlue);
int lineHeight = calculateTextHeight();
int timeFontSize = static_cast<int>(lineHeight * 0.6); // We make the time font smaller
int textFontSize = static_cast<int>(lineHeight * 0.8); // We make the message text slightly bigger
QFont timeFont("FreeSans");
timeFont.setPixelSize(timeFontSize);
QFont textFont("FreeSans");
textFont.setPixelSize(textFontSize);
painter.setPen(era::white);
QList<EtcsTextMessage> displayMessages;
bool ackFound = false;
for (int i = 0; i < messageList.size(); ++i) {
const EtcsTextMessage& msg = messageList.at(i);
if (msg.ackNeeded) {
displayMessages.append(msg);
ackFound = true;
break;
}
}
if (ackFound) {
emit enableScrollUp(false);
emit enableScrollDown(false);
} else {
displayMessages = messageList;
emit enableScrollUp(scrollIndex > 0);
emit enableScrollDown(scrollIndex < messageList.size() - 1);
}
int y = 0;
for (int i = scrollIndex; i < displayMessages.size() && y < height(); ++i) {
const EtcsTextMessage& msg = displayMessages[i];
QString timeString = msg.timeStamp.toString("hh:mm");
int timeWidth = static_cast<int>(width() * 0.15);
int textWidth = static_cast<int>(width() - timeWidth - 10); // We allocate more space for text
int xOffset = timeWidth + 5;
painter.setFont(textFont);
QFontMetrics textMetrics(textFont);
QRect textBoundingRect = textMetrics.boundingRect(0, 0, textWidth, 0, Qt::TextWordWrap, msg.msgText);
int textHeight = textBoundingRect.height();
// We adjust the time position to align it better with the first text line
int timeY = y + (textMetrics.ascent() / 3);
QRect timeRect(3, timeY, timeWidth, timeFontSize);
QRect textRect(xOffset, y, textWidth, textHeight);
// We draw the time and text messages
painter.setFont(timeFont);
painter.drawText(timeRect, Qt::AlignCenter, timeString);
painter.setFont(textFont);
painter.drawText(textRect, Qt::TextWordWrap | Qt::AlignVCenter, msg.msgText);
y += textHeight;
}
if (blinking && !displayMessages.isEmpty() && displayMessages.first().ackNeeded) {// We draw the blinking border if required
paintFrame(&painter, era::yellow, era::veryDarkYellow, borderThickness * 2);
}
else{ // Otherwise the regular frame.
paintFrame(&painter, era::shadow, era::black, borderThickness);
}
(void)event; // To stop compiler warnings be because of unused stuff.
}
void EtcsTextWidget::updateBlinking() {
// We toggle the blinking state and refresh the display
blinking = !blinking;
update();
}
void EtcsTextWidget::mouseReleaseEvent(QMouseEvent *event) {
// We emit a click signal when the widget is clicked
if(!messageList.isEmpty())
emit sendAckEtcsTextMessage(messageList.at(scrollIndex).msgID);
(void)event;
}
void EtcsTextWidget::paintFrame(QPainter *framePainter, QColor bright, QColor dark, int bt){
// We draw the 3D sunken border
int w = width();
int h = height();
QPoint brightBorder[6] = {
QPoint(0, h), // Bottom left corner
QPoint(w, h), // Bottom right corner
QPoint(w, 0), // Top right corner
QPoint(w - bt, bt), // Shifted top right corner
QPoint(w - bt, h - bt), //Shifted bottom right corner
QPoint(bt, h - 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 original polygon
QPoint brightBorderMirrored[6]; // Create a mirrored version of the polygon
for (int i = 0; i < 6; i++) { // (diagonal reflection)
brightBorderMirrored[i] = QPoint(w - brightBorder[i].x(), h - brightBorder[i].y());
}
framePainter->setBrush(dark); // Set the brush to black for the mirrored polygon
framePainter->drawPolygon(brightBorderMirrored, 6); // Draw the mirrored polygon
}