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.

243 lines
6.7 KiB

13 years ago
13 years ago
13 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
  5. * Copyright (C) 1992-2013 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef _REPORTER_H_
  25. #define _REPORTER_H_
  26. #include <wx/string.h>
  27. /**
  28. * @file reporter.h
  29. * @author Wayne Stambaugh
  30. * @note A special thanks to Dick Hollenbeck who came up with the idea that inspired
  31. * me to write this.
  32. */
  33. class wxTextCtrl;
  34. class wxHtmlListbox;
  35. class WX_HTML_REPORT_PANEL;
  36. /**
  37. * Class REPORTER
  38. * is a pure virtual class used to derive REPORTER objects from.
  39. *
  40. * The purpose of the REPORTER object is to offer a way for a procedural function
  41. * to report multiple errors without having to:
  42. * <ul>
  43. * <li> know too much about the caller's UI, i.e. wx. </li>
  44. * <li> stop after the first error </li>
  45. * </ul>
  46. * the reporter has 4 severity levels (flags) tagging the messages:
  47. * - information
  48. * - warning
  49. * - error
  50. * - action (i.e. indication of changes - add component, change footprint, etc. )
  51. * They are indicators for the message formatting and displaying code,
  52. * filtering is not made here.
  53. */
  54. class REPORTER {
  55. public:
  56. /**
  57. * Severity of the reported messages.
  58. * Undefined are default status messages
  59. * Info are processing messages for which no action is taken
  60. * Action messages are items that modify the file(s) as expected
  61. * Warning messages are items that might be problematic but don't prevent
  62. * the process from completing
  63. * Error messages are items that prevent the process from completing
  64. */
  65. //
  66. enum SEVERITY {
  67. RPT_UNDEFINED = 0x0,
  68. RPT_INFO = 0x1,
  69. RPT_ACTION = 0x2,
  70. RPT_WARNING = 0x4,
  71. RPT_ERROR = 0x8
  72. };
  73. /**
  74. * Location where the message is to be reported.
  75. * LOC_HEAD messages are printed before all others (typically intro messages)
  76. * LOC_BODY messages are printed in the middle
  77. * LOC_TAIL messages are printed after all others (typically status messages)
  78. */
  79. enum LOCATION {
  80. LOC_HEAD = 0,
  81. LOC_BODY,
  82. LOC_TAIL
  83. };
  84. /**
  85. * Function Report
  86. * is a pure virtual function to override in the derived object.
  87. *
  88. * @param aText is the string to report.
  89. * @param aSeverity is an indicator ( RPT_UNDEFINED, RPT_INFO, RPT_WARNING,
  90. * RPT_ERROR, RPT_ACTION ) used to filter and format messages
  91. */
  92. virtual REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) = 0;
  93. /**
  94. * Function ReportTail
  95. * Places the report at the end of the list, for objects that support report ordering
  96. */
  97. virtual REPORTER& ReportTail( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED )
  98. {
  99. return Report( aText, aSeverity );
  100. }
  101. /**
  102. * Function ReportHead
  103. * Places the report at the beginning of the list for objects that support ordering
  104. */
  105. virtual REPORTER& ReportHead( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED )
  106. {
  107. return Report( aText, aSeverity );
  108. }
  109. REPORTER& Report( const char* aText, SEVERITY aSeverity = RPT_UNDEFINED );
  110. REPORTER& operator <<( const wxString& aText ) { return Report( aText ); }
  111. REPORTER& operator <<( const wxChar* aText ) { return Report( wxString( aText ) ); }
  112. REPORTER& operator <<( wxChar aChar ) { return Report( wxString( aChar ) ); }
  113. REPORTER& operator <<( const char* aText ) { return Report( aText ); }
  114. /**
  115. * Function HasMessage
  116. * Returns true if the reporter client is non-empty.
  117. */
  118. virtual bool HasMessage() const = 0;
  119. };
  120. /**
  121. * Class WX_TEXT_CTRL_REPORTER
  122. * is wrapper for reporting to a wxTextCtrl object.
  123. */
  124. class WX_TEXT_CTRL_REPORTER : public REPORTER
  125. {
  126. wxTextCtrl* m_textCtrl;
  127. public:
  128. WX_TEXT_CTRL_REPORTER( wxTextCtrl* aTextCtrl ) :
  129. REPORTER(),
  130. m_textCtrl( aTextCtrl )
  131. {
  132. }
  133. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  134. bool HasMessage() const override;
  135. };
  136. /**
  137. * Class WX_STRING_REPORTER
  138. * is a wrapper for reporting to a wxString object.
  139. */
  140. class WX_STRING_REPORTER : public REPORTER
  141. {
  142. wxString* m_string;
  143. public:
  144. WX_STRING_REPORTER( wxString* aString ) :
  145. REPORTER(),
  146. m_string( aString )
  147. {
  148. }
  149. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  150. bool HasMessage() const override;
  151. };
  152. /**
  153. * Class WX_HTML_PANEL_REPORTER
  154. * is a wrapper for reporting to a wx HTML window
  155. */
  156. class WX_HTML_PANEL_REPORTER : public REPORTER
  157. {
  158. WX_HTML_REPORT_PANEL* m_panel;
  159. public:
  160. WX_HTML_PANEL_REPORTER( WX_HTML_REPORT_PANEL* aPanel ) :
  161. REPORTER(),
  162. m_panel( aPanel )
  163. {
  164. }
  165. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  166. REPORTER& ReportTail( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  167. REPORTER& ReportHead( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  168. bool HasMessage() const override;
  169. };
  170. /**
  171. * Class NULL_REPORTER
  172. *
  173. * A singleton reporter that reports to nowhere. Used as to simplify code by
  174. * avoiding the reportee to check for a non-NULL reporter object.
  175. */
  176. class NULL_REPORTER : public REPORTER
  177. {
  178. public:
  179. NULL_REPORTER()
  180. {
  181. }
  182. static REPORTER& GetInstance();
  183. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  184. bool HasMessage() const override { return false; }
  185. };
  186. /**
  187. * Class STDOUT_REPORTER
  188. *
  189. * Debug type reporter, forwarding messages to std::cout.
  190. */
  191. class STDOUT_REPORTER : public REPORTER
  192. {
  193. public:
  194. STDOUT_REPORTER()
  195. {
  196. }
  197. static REPORTER& GetInstance();
  198. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  199. bool HasMessage() const override { return false; }
  200. };
  201. #endif // _REPORTER_H_