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.

307 lines
7.7 KiB

13 years ago
13 years ago
8 months 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@gmail.com>
  5. * Copyright The KiCad Developers, see AUTHORS.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 <memory>
  27. #include <eda_units.h>
  28. #include <widgets/report_severity.h>
  29. #include <kicommon.h>
  30. /**
  31. * @file reporter.h
  32. * @author Wayne Stambaugh
  33. * @note A special thanks to Dick Hollenbeck who came up with the idea that inspired
  34. * me to write this.
  35. * @warning Do not add any dependencies to wxWidgets (or any other third party UI library )
  36. * to the REPORTER object. All wxWidgets objects should be defined by pointer or
  37. * reference and forward declared so that using reporters in low level KiCad objects
  38. * will not require pulling in wxWidgets to building them.
  39. */
  40. class wxString;
  41. class wxStatusBar;
  42. class wxTextCtrl;
  43. class WX_HTML_REPORT_PANEL;
  44. class WX_INFOBAR;
  45. /**
  46. * A pure virtual class used to derive REPORTER objects from.
  47. *
  48. * The purpose of the REPORTER object is to offer a way for a procedural function
  49. * to report multiple errors without having to:
  50. * <ul>
  51. * <li> know too much about the caller's UI, i.e. wx. </li>
  52. * <li> stop after the first error </li>
  53. * </ul>
  54. * the reporter has 4 severity levels (flags) tagging the messages:
  55. * - information
  56. * - warning
  57. * - error
  58. * - action (i.e. indication of changes - add component, change footprint, etc. )
  59. *
  60. * They are indicators for the message formatting and displaying code,
  61. * filtering is not made here.
  62. */
  63. class KICOMMON_API REPORTER
  64. {
  65. public:
  66. /**
  67. * Location where the message is to be reported.
  68. * LOC_HEAD messages are printed before all others (typically intro messages)
  69. * LOC_BODY messages are printed in the middle
  70. * LOC_TAIL messages are printed after all others (typically status messages)
  71. */
  72. enum LOCATION {
  73. LOC_HEAD = 0,
  74. LOC_BODY,
  75. LOC_TAIL
  76. };
  77. /**
  78. * Report a string with a given severity.
  79. *
  80. * @param aText is the string to report.
  81. * @param aSeverity is an indicator ( RPT_UNDEFINED, RPT_INFO, RPT_WARNING, RPT_ERROR,
  82. * RPT_ACTION ) used to filter and format messages
  83. */
  84. virtual REPORTER& Report( const wxString& aText,
  85. SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) = 0;
  86. /**
  87. * Places the report at the end of the list, for objects that support report ordering
  88. */
  89. virtual REPORTER& ReportTail( const wxString& aText,
  90. SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED )
  91. {
  92. return Report( aText, aSeverity );
  93. }
  94. /**
  95. * Places the report at the beginning of the list for objects that support ordering.
  96. */
  97. virtual REPORTER& ReportHead( const wxString& aText,
  98. SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED )
  99. {
  100. return Report( aText, aSeverity );
  101. }
  102. REPORTER& Report( const char* aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED );
  103. REPORTER& operator <<( const wxString& aText ) { return Report( aText ); }
  104. /**
  105. * Returns true if the reporter client is non-empty.
  106. */
  107. virtual bool HasMessage() const = 0;
  108. /**
  109. * Returns true if the reporter has one or more messages matching the specified
  110. * severity mask.
  111. */
  112. virtual bool HasMessageOfSeverity( int aSeverityMask ) const;
  113. virtual EDA_UNITS GetUnits() const
  114. {
  115. return EDA_UNITS::MM;
  116. }
  117. virtual ~REPORTER()
  118. {
  119. }
  120. };
  121. /**
  122. * A wrapper for reporting to a wxTextCtrl object.
  123. */
  124. class KICOMMON_API WX_TEXT_CTRL_REPORTER : public REPORTER
  125. {
  126. public:
  127. WX_TEXT_CTRL_REPORTER( wxTextCtrl* aTextCtrl ) :
  128. REPORTER(),
  129. m_textCtrl( aTextCtrl )
  130. {
  131. }
  132. virtual ~WX_TEXT_CTRL_REPORTER()
  133. {
  134. }
  135. REPORTER& Report( const wxString& aText,
  136. SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  137. bool HasMessage() const override;
  138. private:
  139. wxTextCtrl* m_textCtrl;
  140. };
  141. /**
  142. * A wrapper for reporting to a wxString object.
  143. */
  144. class KICOMMON_API WX_STRING_REPORTER : public REPORTER
  145. {
  146. public:
  147. WX_STRING_REPORTER() :
  148. REPORTER(),
  149. m_severityMask( 0 )
  150. {
  151. }
  152. virtual ~WX_STRING_REPORTER()
  153. {
  154. }
  155. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  156. bool HasMessage() const override;
  157. bool HasMessageOfSeverity( int aSeverityMask ) const override;
  158. const wxString& GetMessages() const;
  159. void Clear();
  160. private:
  161. wxString m_string;
  162. int m_severityMask;
  163. };
  164. /**
  165. * A singleton reporter that reports to nowhere.
  166. *
  167. * Used as to simplify code by avoiding the reportee to check for a non-NULL reporter object.
  168. */
  169. class KICOMMON_API NULL_REPORTER : public REPORTER
  170. {
  171. public:
  172. NULL_REPORTER()
  173. {
  174. }
  175. virtual ~NULL_REPORTER()
  176. {
  177. }
  178. static REPORTER& GetInstance();
  179. REPORTER& Report( const wxString& aText,
  180. SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  181. bool HasMessage() const override { return false; }
  182. };
  183. /**
  184. * Reporter forwarding messages to stdout or stderr as appropriate
  185. */
  186. class KICOMMON_API CLI_REPORTER : public REPORTER
  187. {
  188. public:
  189. CLI_REPORTER()
  190. {
  191. }
  192. virtual ~CLI_REPORTER()
  193. {
  194. }
  195. static REPORTER& GetInstance();
  196. REPORTER& Report( const wxString& aMsg, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  197. bool HasMessage() const override { return false; }
  198. };
  199. /**
  200. * Debug type reporter, forwarding messages to std::cout.
  201. */
  202. class KICOMMON_API STDOUT_REPORTER : public REPORTER
  203. {
  204. public:
  205. STDOUT_REPORTER()
  206. {
  207. }
  208. virtual ~STDOUT_REPORTER()
  209. {
  210. }
  211. static REPORTER& GetInstance();
  212. REPORTER& Report( const wxString& aMsg, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  213. bool HasMessage() const override { return false; }
  214. };
  215. class KICOMMON_API WXLOG_REPORTER : public REPORTER
  216. {
  217. public:
  218. WXLOG_REPORTER()
  219. {
  220. }
  221. virtual ~WXLOG_REPORTER()
  222. {
  223. }
  224. static REPORTER& GetInstance();
  225. REPORTER& Report( const wxString& aMsg, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  226. bool HasMessage() const override { return false; }
  227. };
  228. /**
  229. * A wrapper for reporting to a specific text location in a statusbar.
  230. */
  231. class KICOMMON_API STATUSBAR_REPORTER : public REPORTER
  232. {
  233. public:
  234. STATUSBAR_REPORTER( wxStatusBar* aStatusBar, int aPosition = 0 )
  235. : REPORTER(),
  236. m_statusBar( aStatusBar ),
  237. m_position( aPosition )
  238. {
  239. }
  240. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
  241. bool HasMessage() const override;
  242. private:
  243. wxStatusBar* m_statusBar;
  244. int m_position;
  245. };
  246. #endif // _REPORTER_H_