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.

312 lines
7.8 KiB

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