* @brief Displays the transmit function output or an error message. */
| 351 | * @brief Displays the transmit function output or an error message. |
| 352 | */ |
| 353 | void DataModel::TransmitTestDialog::displayOutput(const QByteArray& result, const QString& errorMsg) |
| 354 | { |
| 355 | auto* commonFonts = &Misc::CommonFonts::instance(); |
| 356 | |
| 357 | if (!errorMsg.isEmpty()) { |
| 358 | m_rawOutput->setFont(commonFonts->uiFont()); |
| 359 | m_rawOutput->setPlainText(errorMsg); |
| 360 | m_hexOutput->clear(); |
| 361 | m_byteCountLabel->clear(); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | if (result.isEmpty()) { |
| 366 | m_rawOutput->setFont(commonFonts->uiFont()); |
| 367 | m_rawOutput->setPlainText(tr("(empty) No data returned")); |
| 368 | m_hexOutput->clear(); |
| 369 | m_byteCountLabel->setText(tr("0 bytes")); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | QString rawStr; |
| 374 | for (int i = 0; i < result.size(); ++i) { |
| 375 | unsigned char ch = static_cast<unsigned char>(result.at(i)); |
| 376 | switch (ch) { |
| 377 | case '\r': |
| 378 | rawStr += QStringLiteral("\\r"); |
| 379 | continue; |
| 380 | case '\n': |
| 381 | rawStr += QStringLiteral("\\n"); |
| 382 | continue; |
| 383 | case '\t': |
| 384 | rawStr += QStringLiteral("\\t"); |
| 385 | continue; |
| 386 | default: |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | if (ch < 0x20 || ch >= 0x7F) |
| 391 | rawStr += QStringLiteral("\\x%1").arg(ch, 2, 16, QLatin1Char('0')); |
| 392 | else |
| 393 | rawStr += QChar(ch); |
| 394 | } |
| 395 | |
| 396 | QString hexStr; |
| 397 | for (int i = 0; i < result.size(); ++i) { |
| 398 | if (i > 0) |
| 399 | hexStr += ' '; |
| 400 | |
| 401 | hexStr += |
| 402 | QStringLiteral("%1").arg(static_cast<unsigned char>(result.at(i)), 2, 16, QLatin1Char('0')); |
| 403 | } |
| 404 | |
| 405 | m_rawOutput->setFont(commonFonts->monoFont()); |
| 406 | m_rawOutput->setPlainText(rawStr); |
| 407 | m_hexOutput->setPlainText(hexStr.toUpper()); |
| 408 | m_byteCountLabel->setText(tr("%1 byte(s)").arg(result.size())); |
| 409 | } |