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 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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::WFlags 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. void MessageWindow::customEvent(QEvent* event)
  50. {
  51. if (static_cast<MessageWindow::EventType>(event->type()) == MessageWindow::MessageEventType)
  52. msgTextEdit.append(dynamic_cast<MessageEvent* >(event)->msg);
  53. }
  54. void MessageWindow::postMsgEvent(QtMsgType type, const char* msg)
  55. {
  56. QString qmsg = MessageWindow::QtMsgToQString(type, msg);
  57. switch (type) {
  58. case QtDebugMsg:
  59. break;
  60. case QtWarningMsg:
  61. qmsg.prepend(QLatin1String("<FONT color=\"#FF0000\">"));
  62. qmsg.append(QLatin1String("</FONT>"));
  63. break;
  64. case QtCriticalMsg:
  65. if (QMessageBox::critical(this, QLatin1String("Critical Error"), qmsg,
  66. QMessageBox::Ignore,
  67. QMessageBox::Abort,
  68. QMessageBox::NoButton) == QMessageBox::Abort)
  69. abort(); // core dump
  70. qmsg.prepend(QLatin1String("<B><FONT color=\"#FF0000\">"));
  71. qmsg.append(QLatin1String("</FONT></B>"));
  72. break;
  73. case QtFatalMsg:
  74. QMessageBox::critical(this, QLatin1String("Fatal Error"), qmsg, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
  75. abort(); // deliberately core dump
  76. }
  77. //it's impossible to change GUI directly from thread other than the main thread
  78. //so post message encapsulated by MessageEvent to the main thread's event queue
  79. QCoreApplication::postEvent(this, new MessageEvent(qmsg));
  80. }
  81. MessageEvent::MessageEvent(QString & msg):
  82. QEvent(static_cast<QEvent::Type>(MessageWindow::MessageEventType))
  83. {
  84. this->msg = msg;
  85. }