ccConsole.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. //##########################################################################
  3. //# #
  4. //# CLOUDCOMPARE #
  5. //# #
  6. //# This program is free software; you can redistribute it and/or modify #
  7. //# it under the terms of the GNU General Public License as published by #
  8. //# the Free Software Foundation; version 2 or later of the License. #
  9. //# #
  10. //# This program is distributed in the hope that it will be useful, #
  11. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  12. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  13. //# GNU General Public License for more details. #
  14. //# #
  15. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  16. //# #
  17. //##########################################################################
  18. //qCC_db
  19. #include <ccLog.h>
  20. //Qt
  21. #include <QFile>
  22. #include <QListWidget>
  23. #include <QMutex>
  24. #include <QTimer>
  25. class MainWindow;
  26. class QTextStream;
  27. //! Custom QListWidget to allow for the copy of all selected elements when using CTRL+C
  28. class ccCustomQListWidget : public QListWidget
  29. {
  30. Q_OBJECT
  31. public:
  32. ccCustomQListWidget(QWidget* parent = nullptr);
  33. protected:
  34. void keyPressEvent(QKeyEvent *event) override;
  35. };
  36. //! Console
  37. class ccConsole : public QObject, public ccLog
  38. {
  39. Q_OBJECT
  40. public:
  41. //! Destructor
  42. ~ccConsole() override;
  43. //! Inits console (and optionaly associates it with a text output widget)
  44. /** WARNING: in release mode, no message will be output if no 'textDisplay'
  45. widget is defined. Moreover, error messages will only appear in a
  46. (blocking) QMessageBox if a 'parentWidget' widget is defined.
  47. In debug mode, all message are sent to system console (with 'printf').
  48. \param textDisplay text output widget (optional)
  49. \param parentWidget parent widget (optional)
  50. \param parentWindow parent window (if any - optional)
  51. \param redirectToStdOut whether to redirect log messages to the system std::out output or not (optional)
  52. **/
  53. static void Init( QListWidget* textDisplay = nullptr,
  54. QWidget* parentWidget = nullptr,
  55. MainWindow* parentWindow = nullptr,
  56. bool redirectToStdOut = false);
  57. //! Returns the (unique) static instance
  58. /** \param autoInit automatically initialize the console instance (with no widget!) if not done already
  59. **/
  60. static ccConsole* TheInstance(bool autoInit = true);
  61. //! Releases unique instance
  62. static void ReleaseInstance(bool flush = true);
  63. //! Sets the console refresh time
  64. /** \param cycle_ms Refresh cycle (ms) - must be strictly positive
  65. **/
  66. static void SetRefreshCycle(int cycle_ms = 1000);
  67. //! Sets auto-refresh state
  68. void setAutoRefresh(bool state);
  69. //! Whether auto-refresh is in progress
  70. bool autoRefresh() const;
  71. //! Sets log file
  72. bool setLogFile(const QString& filename);
  73. //! Whether to show Qt messages (qDebug / qWarning / etc.) in Console
  74. static void EnableQtMessages(bool state);
  75. //! Returns whether to show Qt messages (qDebug / qWarning / etc.) in Console or not
  76. static bool QtMessagesEnabled() { return s_showQtMessagesInConsole; }
  77. //! Returns the parent widget (if any)
  78. inline QWidget* parentWidget() { return m_parentWidget; }
  79. public:
  80. //! Refreshes console (display all messages still in queue)
  81. void refresh();
  82. protected:
  83. //! Default constructor
  84. /** Constructor is protected to avoid using this object as a non static class.
  85. **/
  86. ccConsole();
  87. //inherited from ccLog
  88. void logMessage(const QString& message, int level) override;
  89. //! Associated text display widget
  90. QListWidget* m_textDisplay;
  91. //! Parent widget
  92. QWidget* m_parentWidget;
  93. //! Parent window (if any)
  94. MainWindow* m_parentWindow;
  95. //! Mutex for concurrent thread access to console
  96. QMutex m_mutex;
  97. //! Queue element type (message + color)
  98. using ConsoleItemType = QPair<QString, int>;
  99. //! Queue for incoming messages
  100. QVector<ConsoleItemType> m_queue;
  101. //! Timer for auto-refresh
  102. QTimer m_timer;
  103. //! Log file
  104. QFile m_logFile;
  105. //! Log file stream
  106. QTextStream* m_logStream;
  107. //! Whether to show Qt messages (qDebug / qWarning / etc.) in Console
  108. static bool s_showQtMessagesInConsole;
  109. static bool s_redirectToStdOut;
  110. };