5#include <QtCore/QTimer>
6#include <QtCore/QDateTime>
13const QString kUnreadStart = QStringLiteral(
"<#U>");
14const QString kUnreadEnd = QStringLiteral(
"</#U>");
17QByteArray statusTextBytes(
const mavlink_statustext_t& statusText,
bool& includesNullTerminator)
19 constexpr int textLength = MAVLINK_MSG_STATUSTEXT_FIELD_TEXT_LEN;
20 const char*
const textEnd =
static_cast<const char*
>(memchr(statusText.text,
'\0', textLength));
21 includesNullTerminator = (textEnd !=
nullptr);
22 return QByteArray(statusText.text, includesNullTerminator ? (textEnd - statusText.text) : textLength);
26void appendMissingChunk(QList<std::optional<QByteArray>>& chunks)
28 chunks.resize(chunks.size() + 1);
34 : m_compId(componentid)
35 , m_severity(severity)
44 case MAV_SEVERITY_EMERGENCY:
45 case MAV_SEVERITY_ALERT:
46 case MAV_SEVERITY_CRITICAL:
47 case MAV_SEVERITY_ERROR:
58 case MAV_SEVERITY_WARNING:
59 case MAV_SEVERITY_NOTICE:
69 , m_chunkedStatusTextTimer(new QTimer(this))
73 m_chunkedStatusTextTimer->setSingleShot(
true);
74 m_chunkedStatusTextTimer->setInterval(1000);
75 (void) connect(m_chunkedStatusTextTimer, &QTimer::timeout,
this, &StatusTextHandler::_chunkedStatusTextTimeout);
90 mavlink_statustext_t statusText;
91 mavlink_msg_statustext_decode(&message, &statusText);
93 bool includesNullTerminator;
94 return QString::fromUtf8(statusTextBytes(statusText, includesNullTerminator));
97QString StatusTextHandler::_displayText(
const StatusText& message)
100 (void) text.replace(kUnreadStart, message.
isRead() ? QString() : QStringLiteral(
"<b>"));
101 (void) text.replace(kUnreadEnd, message.
isRead() ? QString() : QStringLiteral(
"</b>"));
109 (void) result.prepend(_displayText(*message));
117 const bool hadMessages = !m_messages.isEmpty();
119 qDeleteAll(m_messages);
122 _resetUnreadSeverityCounts();
124 if (m_unreadMessageCount != 0) {
125 m_unreadMessageCount = 0;
137 if (m_unreadMessageCount == 0) {
145 m_unreadMessageCount = 0;
149 _resetUnreadSeverityCounts();
154void StatusTextHandler::_resetUnreadSeverityCounts()
156 if (m_unreadCriticalMessageCount != 0) {
157 m_unreadCriticalMessageCount = 0;
161 if (m_unreadWarningMessageCount != 0) {
162 m_unreadWarningMessageCount = 0;
169 QString htmlText(text);
171 (void) htmlText.replace(
"\n",
"<br/>");
174 if (!description.isEmpty()) {
175 QString htmlDescription(description);
176 (void) htmlDescription.replace(
"\n",
"<br/>");
177 (void) htmlText.append(QStringLiteral(
"<br/><small><small>"));
178 (void) htmlText.append(htmlDescription);
179 (void) htmlText.append(QStringLiteral(
"</small></small>"));
182 if (m_activeComponent == MAV_COMPONENT::MAV_COMPONENT_ENUM_END) {
183 m_activeComponent = compId;
186 if (compId != m_activeComponent) {
196 case MAV_SEVERITY_EMERGENCY:
197 case MAV_SEVERITY_ALERT:
198 case MAV_SEVERITY_CRITICAL:
199 case MAV_SEVERITY_ERROR:
200 style = QStringLiteral(
"<#E>");
203 case MAV_SEVERITY_NOTICE:
204 case MAV_SEVERITY_WARNING:
205 style = QStringLiteral(
"<#I>");
209 style = QStringLiteral(
"<#N>");
213 QString severityText;
215 case MAV_SEVERITY_EMERGENCY:
216 severityText = tr(
"EMERGENCY");
219 case MAV_SEVERITY_ALERT:
220 severityText = tr(
"ALERT");
223 case MAV_SEVERITY_CRITICAL:
224 severityText = tr(
"Critical");
227 case MAV_SEVERITY_ERROR:
228 severityText = tr(
"Error");
231 case MAV_SEVERITY_WARNING:
232 severityText = tr(
"Warning");
235 case MAV_SEVERITY_NOTICE:
236 severityText = tr(
"Notice");
239 case MAV_SEVERITY_INFO:
240 severityText = tr(
"Info");
243 case MAV_SEVERITY_DEBUG:
244 severityText = tr(
"Debug");
248 qCWarning(StatusTextHandlerLog) << Q_FUNC_INFO <<
"Invalid MAV_SEVERITY";
254 compString = QString(
"COMP:%1").arg(compId);
257 const QString dateString = QDateTime::currentDateTime().toString(
"hh:mm:ss.zzz");
259 const QString formatText =
260 QString(
"<font style=\"%1\">%2[%3 %4] %5: %6%7</font><br/>")
261 .arg(style, kUnreadStart, dateString, compString, severityText, htmlText, kUnreadEnd);
266 (void) m_messages.append(message);
268 if (m_unreadMessageCount++ == 0) {
276 m_unreadCriticalMessageCount++;
280 m_unreadWarningMessageCount++;
287 if (message.msgid != MAVLINK_MSG_ID_STATUSTEXT) {
291 _handleStatusText(message);
296 mavlink_statustext_t statustext;
297 mavlink_msg_statustext_decode(&message, &statustext);
299 bool includesNullTerminator;
300 const QByteArray chunkBytes = statusTextBytes(statustext, includesNullTerminator);
302 const MAV_COMPONENT compId =
static_cast<MAV_COMPONENT
>(message.compid);
303 if (m_chunkedStatusTextInfoMap.contains(compId) && (m_chunkedStatusTextInfoMap.value(compId).chunkId != statustext.id)) {
305 appendMissingChunk(m_chunkedStatusTextInfoMap[compId].rgMessageChunks);
306 _chunkedStatusTextCompleted(compId);
309 if (statustext.id == 0) {
311 ChunkedStatusTextInfo_t chunkedInfo;
312 chunkedInfo.chunkId = 0;
313 chunkedInfo.severity =
static_cast<MAV_SEVERITY
>(statustext.severity);
314 chunkedInfo.rgMessageChunks.append(chunkBytes);
315 (void) m_chunkedStatusTextInfoMap.insert(compId, chunkedInfo);
317 if (!m_chunkedStatusTextInfoMap.contains(compId)) {
319 ChunkedStatusTextInfo_t chunkedInfo;
320 chunkedInfo.chunkId = statustext.id;
321 chunkedInfo.severity =
static_cast<MAV_SEVERITY
>(statustext.severity);
322 (void) m_chunkedStatusTextInfoMap.insert(compId, chunkedInfo);
325 QList<std::optional<QByteArray>>& chunks = m_chunkedStatusTextInfoMap[compId].rgMessageChunks;
326 if (statustext.chunk_seq >= chunks.size()) {
328 chunks.resize(statustext.chunk_seq + 1);
330 chunks[statustext.chunk_seq] = chunkBytes;
332 m_chunkedStatusTextTimer->start();
335 if ((statustext.id == 0) || includesNullTerminator) {
336 m_chunkedStatusTextTimer->stop();
337 _chunkedStatusTextCompleted(compId);
341void StatusTextHandler::_chunkedStatusTextTimeout()
343 for (
auto compId : m_chunkedStatusTextInfoMap.keys()) {
344 auto& chunkedInfo = m_chunkedStatusTextInfoMap[compId];
345 appendMissingChunk(chunkedInfo.rgMessageChunks);
346 _chunkedStatusTextCompleted(compId);
350void StatusTextHandler::_chunkedStatusTextCompleted(MAV_COMPONENT compId)
352 const ChunkedStatusTextInfo_t& chunkedInfo = m_chunkedStatusTextInfoMap.value(compId);
353 const MAV_SEVERITY severity = chunkedInfo.severity;
357 QByteArray pendingBytes;
358 for (
const std::optional<QByteArray>& chunk : std::as_const(chunkedInfo.rgMessageChunks)) {
359 if (chunk.has_value()) {
360 pendingBytes.append(*chunk);
362 (void) messageText.append(QString::fromUtf8(pendingBytes));
363 pendingBytes.clear();
364 (void) messageText.append(tr(
" ... ",
"Indicates missing chunk from chunked STATUS_TEXT"));
367 (void) messageText.append(QString::fromUtf8(pendingBytes));
369 (void) m_chunkedStatusTextInfoMap.remove(compId);
struct __mavlink_message mavlink_message_t
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void unreadMessageCountChanged()
StatusTextHandler(QObject *parent=nullptr)
void handleHTMLEscapedTextMessage(MAV_COMPONENT componentid, MAV_SEVERITY severity, const QString &text, const QString &description)
const QList< StatusText * > & messages() const
void formattedMessagesChanged()
void hasUnreadMessagesChanged()
void newErrorMessage(QString message)
static QString getMessageText(const mavlink_message_t &message)
void mavlinkMessageReceived(const mavlink_message_t &message)
void textMessageReceived(MAV_COMPONENT componentid, MAV_SEVERITY severity, QString text, QString description)
void newFormattedMessage(QString message)
QString formattedMessages() const
Unread messages are wrapped in so they render bold.
void unreadWarningMessageCountChanged()
void unreadCriticalMessageCountChanged()
StatusText(MAV_COMPONENT componentid, MAV_SEVERITY severity, const QString &text)
bool severityIsWarning() const
bool severityIsError() const
void setFormatedText(const QString &formatedText)
QString getFormattedText() const