Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageWindow.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @file MessageWindow.cpp
  3. * @brief MessageWindow Implementation.
  4. * @see MessageWindow.h
  5. * @author Micha? Policht
  6. */
  7. #include <stdio.h>
  8. #include "MessageWindow.h"
  9. #include <QMessageBox>
  10. #include <QCoreApplication>
  11. #include <QMutexLocker>
  12. const char *MessageWindow::WINDOW_TITLE = "Message Window";
  13. MessageWindow *MessageWindow::MsgHandler = NULL;
  14. MessageWindow::MessageWindow(QWidget *parent, Qt::WindowFlags flags)
  15. : QDockWidget(parent, flags),
  16. msgTextEdit(this)
  17. {
  18. setWindowTitle(tr(WINDOW_TITLE));
  19. msgTextEdit.setReadOnly(true);
  20. setWidget(&msgTextEdit);
  21. MessageWindow::MsgHandler = this;
  22. }
  23. //static
  24. QString MessageWindow::QtMsgToQString(QtMsgType type, const char *msg)
  25. {
  26. switch (type) {
  27. case QtDebugMsg:
  28. return QLatin1String("Debug: ")+QLatin1String(msg);
  29. case QtWarningMsg:
  30. return QLatin1String("Warning: ")+QLatin1String(msg);
  31. case QtCriticalMsg:
  32. return QLatin1String("Critical: ")+QLatin1String(msg);
  33. case QtFatalMsg:
  34. return QLatin1String("Fatal: ")+QLatin1String(msg);
  35. default:
  36. return QLatin1String("Unrecognized message type: ")+QLatin1String(msg);
  37. }
  38. }
  39. //static
  40. void MessageWindow::AppendMsgWrapper(QtMsgType type, const char *msg)
  41. {
  42. static QMutex mutex;
  43. QMutexLocker locker(&mutex);
  44. if (MessageWindow::MsgHandler != NULL)
  45. return MessageWindow::MsgHandler->postMsgEvent(type, msg);
  46. else
  47. fprintf(stderr, "%s", MessageWindow::QtMsgToQString(type, msg).toLatin1().data());
  48. }
  49. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  50. void MessageWindow::AppendMsgWrapper(QtMsgType type, const QMessageLogContext & /*context*/, const QString &msg)
  51. {
  52. AppendMsgWrapper(type, msg.toLatin1().data());
  53. }
  54. #endif
  55. void MessageWindow::customEvent(QEvent *event)
  56. {
  57. if (static_cast<MessageWindow::EventType>(event->type()) == MessageWindow::MessageEventType)
  58. msgTextEdit.append(dynamic_cast<MessageEvent *>(event)->msg);
  59. }
  60. void MessageWindow::postMsgEvent(QtMsgType type, const char *msg)
  61. {
  62. QString qmsg = MessageWindow::QtMsgToQString(type, msg);
  63. switch (type) {
  64. case QtDebugMsg:
  65. break;
  66. case QtWarningMsg:
  67. qmsg.prepend(QLatin1String("<FONT color=\"#FF0000\">"));
  68. qmsg.append(QLatin1String("</FONT>"));
  69. break;
  70. case QtCriticalMsg:
  71. if (QMessageBox::critical(this, QLatin1String("Critical Error"), qmsg,
  72. QMessageBox::Ignore,
  73. QMessageBox::Abort,
  74. QMessageBox::NoButton) == QMessageBox::Abort)
  75. abort(); // core dump
  76. qmsg.prepend(QLatin1String("<B><FONT color=\"#FF0000\">"));
  77. qmsg.append(QLatin1String("</FONT></B>"));
  78. break;
  79. case QtFatalMsg:
  80. QMessageBox::critical(this, QLatin1String("Fatal Error"), qmsg, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
  81. abort(); // deliberately core dump
  82. }
  83. //it's impossible to change GUI directly from thread other than the main thread
  84. //so post message encapsulated by MessageEvent to the main thread's event queue
  85. QCoreApplication::postEvent(this, new MessageEvent(qmsg));
  86. }
  87. MessageEvent::MessageEvent(QString &msg):
  88. QEvent(static_cast<QEvent::Type>(MessageWindow::MessageEventType))
  89. {
  90. this->msg = msg;
  91. }