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.

250 lines
6.5 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. #include <widgets/ui_common.h>
  28. /**
  29. * @file reporter.h
  30. * @author Wayne Stambaugh
  31. * @note A special thanks to Dick Hollenbeck who came up with the idea that inspired
  32. * me to write this.
  33. */
  34. class wxTextCtrl;
  35. class wxHtmlListbox;
  36. class WX_HTML_REPORT_PANEL;
  37. /**
  38. * REPORTER
  39. * is a pure virtual class used to derive REPORTER objects from.
  40. *
  41. * The purpose of the REPORTER object is to offer a way for a procedural function
  42. * to report multiple errors without having to:
  43. * <ul>
  44. * <li> know too much about the caller's UI, i.e. wx. </li>
  45. * <li> stop after the first error </li>
  46. * </ul>
  47. * the reporter has 4 severity levels (flags) tagging the messages:
  48. * - information
  49. * - warning
  50. * - error
  51. * - action (i.e. indication of changes - add component, change footprint, etc. )
  52. * They are indicators for the message formatting and displaying code,
  53. * filtering is not made here.
  54. */
  55. class REPORTER {
  56. public:
  57. /**
  58. * Location where the message is to be reported.
  59. * LOC_HEAD messages are printed before all others (typically intro messages)
  60. * LOC_BODY messages are printed in the middle
  61. * LOC_TAIL messages are printed after all others (typically status messages)
  62. */
  63. enum LOCATION {
  64. LOC_HEAD = 0,
  65. LOC_BODY,
  66. LOC_TAIL
  67. };
  68. /**
  69. * Function Report
  70. * is a pure virtual function to override in the derived object.
  71. *
  72. * @param aText is the string to report.
  73. * @param aSeverity is an indicator ( RPT_UNDEFINED, RPT_INFO, RPT_WARNING,
  74. * RPT_ERROR, RPT_ACTION ) used to filter and format messages
  75. */
  76. virtual REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) = 0;
  77. /**
  78. * Function ReportTail
  79. * Places the report at the end of the list, for objects that support report ordering
  80. */
  81. virtual REPORTER& ReportTail( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED )
  82. {
  83. return Report( aText, aSeverity );
  84. }
  85. /**
  86. * Function ReportHead
  87. * Places the report at the beginning of the list for objects that support ordering
  88. */
  89. virtual REPORTER& ReportHead( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED )
  90. {
  91. return Report( aText, aSeverity );
  92. }
  93. REPORTER& Report( const char* aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED );
  94. REPORTER& operator <<( const wxString& aText ) { return Report( aText ); }
  95. REPORTER& operator <<( const wxChar* aText ) { return Report( wxString( aText ) ); }
  96. REPORTER& operator <<( wxChar aChar ) { return Report( wxString( aChar ) ); }
  97. REPORTER& operator <<( const char* aText ) { return Report( aText ); }
  98. /**
  99. * Function HasMessage
  100. * Returns true if the reporter client is non-empty.
  101. */
  102. virtual bool HasMessage() const = 0;
  103. virtual ~REPORTER()
  104. {
  105. }
  106. };
  107. /**
  108. * WX_TEXT_CTRL_REPORTER
  109. * is wrapper for reporting to a wxTextCtrl object.
  110. */
  111. class WX_TEXT_CTRL_REPORTER : public REPORTER
  112. {
  113. wxTextCtrl* m_textCtrl;
  114. public:
  115. WX_TEXT_CTRL_REPORTER( wxTextCtrl* aTextCtrl ) :
  116. REPORTER(),
  117. m_textCtrl( aTextCtrl )
  118. {
  119. }
  120. virtual ~WX_TEXT_CTRL_REPORTER()
  121. {
  122. }
  123. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  124. bool HasMessage() const override;
  125. };
  126. /**
  127. * WX_STRING_REPORTER
  128. * is a wrapper for reporting to a wxString object.
  129. */
  130. class WX_STRING_REPORTER : public REPORTER
  131. {
  132. wxString* m_string;
  133. public:
  134. WX_STRING_REPORTER( wxString* aString ) :
  135. REPORTER(),
  136. m_string( aString )
  137. {
  138. }
  139. virtual ~WX_STRING_REPORTER()
  140. {
  141. }
  142. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  143. bool HasMessage() const override;
  144. };
  145. /**
  146. * WX_HTML_PANEL_REPORTER
  147. * is a wrapper for reporting to a wx HTML window
  148. */
  149. class WX_HTML_PANEL_REPORTER : public REPORTER
  150. {
  151. WX_HTML_REPORT_PANEL* m_panel;
  152. public:
  153. WX_HTML_PANEL_REPORTER( WX_HTML_REPORT_PANEL* aPanel ) :
  154. REPORTER(),
  155. m_panel( aPanel )
  156. {
  157. }
  158. virtual ~WX_HTML_PANEL_REPORTER()
  159. {
  160. }
  161. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  162. REPORTER& ReportTail( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  163. REPORTER& ReportHead( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  164. bool HasMessage() const override;
  165. };
  166. /**
  167. * NULL_REPORTER
  168. *
  169. * A singleton reporter that reports to nowhere. Used as to simplify code by
  170. * avoiding the reportee to check for a non-NULL reporter object.
  171. */
  172. class NULL_REPORTER : public REPORTER
  173. {
  174. public:
  175. NULL_REPORTER()
  176. {
  177. }
  178. virtual ~NULL_REPORTER()
  179. {
  180. }
  181. static REPORTER& GetInstance();
  182. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  183. bool HasMessage() const override { return false; }
  184. };
  185. /**
  186. * STDOUT_REPORTER
  187. *
  188. * Debug type reporter, forwarding messages to std::cout.
  189. */
  190. class STDOUT_REPORTER : public REPORTER
  191. {
  192. public:
  193. STDOUT_REPORTER()
  194. {
  195. }
  196. virtual ~STDOUT_REPORTER()
  197. {
  198. }
  199. static REPORTER& GetInstance();
  200. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  201. bool HasMessage() const override { return false; }
  202. };
  203. #endif // _REPORTER_H_