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.

315 lines
9.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef RC_ITEM_H
  24. #define RC_ITEM_H
  25. #include <wx/dataview.h>
  26. #include <units_provider.h>
  27. #include <kiid.h>
  28. #include <reporter.h>
  29. #include <math/vector2d.h>
  30. class MARKER_BASE;
  31. class EDA_BASE_FRAME;
  32. class RC_ITEM;
  33. class EDA_ITEM;
  34. class EDA_DRAW_FRAME;
  35. namespace RC_JSON
  36. {
  37. struct VIOLATION;
  38. }
  39. /**
  40. * Provide an abstract interface of a RC_ITEM* list manager.
  41. *
  42. * The details of the actual list architecture are hidden from the caller. Any class that
  43. * implements this interface can then be used by a RC_TREE_MODEL class without it knowing
  44. * the actual architecture of the list.
  45. */
  46. class RC_ITEMS_PROVIDER
  47. {
  48. public:
  49. virtual void SetSeverities( int aSeverities ) = 0;
  50. virtual int GetCount( int aSeverity = -1 ) const = 0;
  51. /**
  52. * Retrieve a RC_ITEM by index.
  53. */
  54. virtual std::shared_ptr<RC_ITEM> GetItem( int aIndex ) const = 0;
  55. /**
  56. * Remove (and optionally deletes) the indexed item from the list.
  57. * @param aDeep If true, the source item should be deleted as well as its entry in the list.
  58. */
  59. virtual void DeleteItem( int aIndex, bool aDeep ) = 0;
  60. virtual ~RC_ITEMS_PROVIDER() { }
  61. };
  62. /**
  63. * A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
  64. *
  65. * RC_ITEMs can have zero, one, or two related EDA_ITEMs.
  66. */
  67. class RC_ITEM
  68. {
  69. public:
  70. typedef std::vector<KIID> KIIDS;
  71. RC_ITEM() :
  72. m_errorCode( 0 ),
  73. m_parent( nullptr )
  74. {
  75. }
  76. RC_ITEM( const std::shared_ptr<RC_ITEM>& aItem )
  77. {
  78. m_errorCode = aItem->m_errorCode;
  79. m_errorMessage = aItem->m_errorMessage;
  80. m_errorTitle = aItem->m_errorTitle;
  81. m_settingsKey = aItem->m_settingsKey;
  82. m_parent = aItem->m_parent;
  83. m_ids = aItem->m_ids;
  84. }
  85. virtual ~RC_ITEM() { }
  86. void SetErrorMessage( const wxString& aMessage ) { m_errorMessage = aMessage; }
  87. void SetItems( const KIIDS& aIds ) { m_ids = aIds; }
  88. void AddItem( EDA_ITEM* aItem );
  89. void SetItems( const EDA_ITEM* aItem, const EDA_ITEM* bItem = nullptr,
  90. const EDA_ITEM* cItem = nullptr, const EDA_ITEM* dItem = nullptr );
  91. void SetItems( const KIID& aItem, const KIID& bItem = niluuid, const KIID& cItem = niluuid,
  92. const KIID& dItem = niluuid )
  93. {
  94. m_ids.clear();
  95. m_ids.push_back( aItem );
  96. m_ids.push_back( bItem );
  97. m_ids.push_back( cItem );
  98. m_ids.push_back( dItem );
  99. }
  100. virtual KIID GetMainItemID() const { return m_ids.size() > 0 ? m_ids[0] : niluuid; }
  101. virtual KIID GetAuxItemID() const { return m_ids.size() > 1 ? m_ids[1] : niluuid; }
  102. virtual KIID GetAuxItem2ID() const { return m_ids.size() > 2 ? m_ids[2] : niluuid; }
  103. virtual KIID GetAuxItem3ID() const { return m_ids.size() > 3 ? m_ids[3] : niluuid; }
  104. std::vector<KIID> GetIDs() const { return m_ids; }
  105. void SetParent( MARKER_BASE* aMarker ) { m_parent = aMarker; }
  106. MARKER_BASE* GetParent() const { return m_parent; }
  107. /**
  108. * Translate this object into a text string suitable for saving to disk in a report.
  109. *
  110. * @return wxString - the simple multi-line report text.
  111. */
  112. virtual wxString ShowReport( UNITS_PROVIDER* aUnitsProvider, SEVERITY aSeverity,
  113. const std::map<KIID, EDA_ITEM*>& aItemMap ) const;
  114. /**
  115. * Translate this object into an RC_JSON::VIOLATION object
  116. *
  117. * @param aViolation is the violation to be populated by info from this item
  118. * @param aUnitsProvider is the units provider that will be used to output coordinates
  119. * @param aSeverity is the severity of this item
  120. * @param aItemMap is a map allowing the lookup of items from KIIDs
  121. *
  122. * @return None
  123. */
  124. virtual void GetJsonViolation( RC_JSON::VIOLATION& aViolation, UNITS_PROVIDER* aUnitsProvider,
  125. SEVERITY aSeverity,
  126. const std::map<KIID, EDA_ITEM*>& aItemMap ) const;
  127. int GetErrorCode() const { return m_errorCode; }
  128. void SetErrorCode( int aCode ) { m_errorCode = aCode; }
  129. /**
  130. * @return the error message describing the specific details of a RC_ITEM. For instance,
  131. * "Clearance violation (netclass '100ohm' clearance 0.4000mm; actual 0.3200mm)"
  132. */
  133. virtual wxString GetErrorMessage() const;
  134. /**
  135. * @return the error text for the class of error of this RC_ITEM represents. For instance,
  136. * "Clearance violation".
  137. */
  138. wxString GetErrorText() const
  139. {
  140. return wxGetTranslation( m_errorTitle );
  141. }
  142. wxString GetSettingsKey() const
  143. {
  144. return m_settingsKey;
  145. }
  146. virtual wxString GetViolatingRuleDesc() const
  147. {
  148. return wxEmptyString;
  149. }
  150. protected:
  151. static wxString getSeverityString( SEVERITY aSeverity );
  152. int m_errorCode; ///< The error code's numeric value
  153. wxString m_errorMessage; ///< A message describing the details of this specific error
  154. wxString m_errorTitle; ///< The string describing the type of error
  155. wxString m_settingsKey; ///< The key used to describe this type of error in settings
  156. MARKER_BASE* m_parent; ///< The marker this item belongs to, if any
  157. KIIDS m_ids;
  158. };
  159. class RC_TREE_NODE
  160. {
  161. public:
  162. enum NODE_TYPE
  163. {
  164. MARKER,
  165. MAIN_ITEM,
  166. AUX_ITEM,
  167. AUX_ITEM2,
  168. AUX_ITEM3,
  169. COMMENT
  170. };
  171. RC_TREE_NODE( RC_TREE_NODE* aParent, const std::shared_ptr<RC_ITEM>& aRcItem,
  172. NODE_TYPE aType ) :
  173. m_Type( aType ),
  174. m_RcItem( aRcItem ),
  175. m_Parent( aParent )
  176. {}
  177. ~RC_TREE_NODE()
  178. {
  179. for( RC_TREE_NODE* child : m_Children )
  180. delete child;
  181. }
  182. NODE_TYPE m_Type;
  183. std::shared_ptr<RC_ITEM> m_RcItem;
  184. RC_TREE_NODE* m_Parent;
  185. std::vector<RC_TREE_NODE*> m_Children;
  186. };
  187. class RC_TREE_MODEL : public wxDataViewModel, public wxEvtHandler
  188. {
  189. public:
  190. static wxDataViewItem ToItem( RC_TREE_NODE const* aNode )
  191. {
  192. return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
  193. }
  194. static RC_TREE_NODE* ToNode( wxDataViewItem aItem )
  195. {
  196. return static_cast<RC_TREE_NODE*>( aItem.GetID() );
  197. }
  198. static KIID ToUUID( wxDataViewItem aItem );
  199. RC_TREE_MODEL( EDA_DRAW_FRAME* aParentFrame, wxDataViewCtrl* aView );
  200. ~RC_TREE_MODEL();
  201. void Update( std::shared_ptr<RC_ITEMS_PROVIDER> aProvider, int aSeverities );
  202. void ExpandAll();
  203. void PrevMarker();
  204. void NextMarker();
  205. void SelectMarker( const MARKER_BASE* aMarker );
  206. void CenterMarker( const MARKER_BASE* aMarker );
  207. bool IsContainer( wxDataViewItem const& aItem ) const override;
  208. wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
  209. unsigned int GetChildren( wxDataViewItem const& aItem,
  210. wxDataViewItemArray& aChildren ) const override;
  211. // Simple, single-text-column model
  212. unsigned int GetColumnCount() const override { return 1; }
  213. wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
  214. bool HasContainerColumns( wxDataViewItem const& aItem ) const override { return true; }
  215. /**
  216. * Called by the wxDataView to fetch an item's value.
  217. */
  218. void GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
  219. unsigned int aCol ) const override;
  220. /**
  221. * Called by the wxDataView to edit an item's content.
  222. */
  223. bool SetValue( wxVariant const& aVariant, wxDataViewItem const& aItem,
  224. unsigned int aCol ) override
  225. {
  226. // Editing not supported
  227. return false;
  228. }
  229. /**
  230. * Called by the wxDataView to fetch an item's formatting. Return true if the
  231. * item has non-default attributes.
  232. */
  233. bool GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
  234. wxDataViewItemAttr& aAttr ) const override;
  235. void ValueChanged( RC_TREE_NODE* aNode );
  236. void DeleteCurrentItem( bool aDeep );
  237. /**
  238. * Delete the current item or all items.
  239. *
  240. * If all, \a aIncludeExclusions determines whether or not exclusions are also deleted.
  241. */
  242. void DeleteItems( bool aCurrentOnly, bool aIncludeExclusions, bool aDeep );
  243. protected:
  244. void rebuildModel( std::shared_ptr<RC_ITEMS_PROVIDER> aProvider, int aSeverities );
  245. EDA_DRAW_FRAME* m_editFrame;
  246. wxDataViewCtrl* m_view;
  247. int m_severities;
  248. std::shared_ptr<RC_ITEMS_PROVIDER> m_rcItemsProvider;
  249. std::vector<RC_TREE_NODE*> m_tree; // I own this
  250. };
  251. #endif // RC_ITEM_H