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.

286 lines
8.8 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 (C) 2020-2022 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. /**
  36. * Provide an abstract interface of a RC_ITEM* list manager.
  37. *
  38. * The details of the actual list architecture are hidden from the caller. Any class that
  39. * implements this interface can then be used by a RC_TREE_MODEL class without it knowing
  40. * the actual architecture of the list.
  41. */
  42. class RC_ITEMS_PROVIDER
  43. {
  44. public:
  45. virtual void SetSeverities( int aSeverities ) = 0;
  46. virtual int GetCount( int aSeverity = -1 ) const = 0;
  47. /**
  48. * Retrieve a RC_ITEM by index.
  49. */
  50. virtual std::shared_ptr<RC_ITEM> GetItem( int aIndex ) const = 0;
  51. /**
  52. * Remove (and optionally deletes) the indexed item from the list.
  53. * @param aDeep If true, the source item should be deleted as well as its entry in the list.
  54. */
  55. virtual void DeleteItem( int aIndex, bool aDeep ) = 0;
  56. virtual ~RC_ITEMS_PROVIDER() { }
  57. };
  58. /**
  59. * A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
  60. *
  61. * RC_ITEMs can have zero, one, or two related EDA_ITEMs.
  62. */
  63. class RC_ITEM
  64. {
  65. public:
  66. typedef std::vector<KIID> KIIDS;
  67. RC_ITEM() :
  68. m_errorCode( 0 ),
  69. m_parent( nullptr )
  70. {
  71. }
  72. RC_ITEM( std::shared_ptr<RC_ITEM> aItem )
  73. {
  74. m_errorCode = aItem->m_errorCode;
  75. m_errorMessage = aItem->m_errorMessage;
  76. m_errorTitle = aItem->m_errorTitle;
  77. m_settingsKey = aItem->m_settingsKey;
  78. m_parent = aItem->m_parent;
  79. m_ids = aItem->m_ids;
  80. }
  81. virtual ~RC_ITEM() { }
  82. void SetErrorMessage( const wxString& aMessage ) { m_errorMessage = aMessage; }
  83. void SetItems( const KIIDS& aIds ) { m_ids = aIds; }
  84. void AddItem( EDA_ITEM* aItem );
  85. void SetItems( const EDA_ITEM* aItem, const EDA_ITEM* bItem = nullptr,
  86. const EDA_ITEM* cItem = nullptr, const EDA_ITEM* dItem = nullptr );
  87. void SetItems( const KIID& aItem, const KIID& bItem = niluuid, const KIID& cItem = niluuid,
  88. const KIID& dItem = niluuid )
  89. {
  90. m_ids.clear();
  91. m_ids.push_back( aItem );
  92. m_ids.push_back( bItem );
  93. m_ids.push_back( cItem );
  94. m_ids.push_back( dItem );
  95. }
  96. virtual KIID GetMainItemID() const { return m_ids.size() > 0 ? m_ids[0] : niluuid; }
  97. virtual KIID GetAuxItemID() const { return m_ids.size() > 1 ? m_ids[1] : niluuid; }
  98. virtual KIID GetAuxItem2ID() const { return m_ids.size() > 2 ? m_ids[2] : niluuid; }
  99. virtual KIID GetAuxItem3ID() const { return m_ids.size() > 3 ? m_ids[3] : niluuid; }
  100. std::vector<KIID> GetIDs() const { return m_ids; }
  101. void SetParent( MARKER_BASE* aMarker ) { m_parent = aMarker; }
  102. MARKER_BASE* GetParent() const { return m_parent; }
  103. /**
  104. * Translate this object into a text string suitable for saving to disk in a report.
  105. *
  106. * @return wxString - the simple multi-line report text.
  107. */
  108. virtual wxString ShowReport( UNITS_PROVIDER* aUnitsProvider, SEVERITY aSeverity,
  109. const std::map<KIID, EDA_ITEM*>& aItemMap ) const;
  110. int GetErrorCode() const { return m_errorCode; }
  111. void SetErrorCode( int aCode ) { m_errorCode = aCode; }
  112. /**
  113. * @return the error message describing the specific details of a RC_ITEM. For instance,
  114. * "Clearance violation (netclass '100ohm' clearance 0.4000mm; actual 0.3200mm)"
  115. */
  116. virtual wxString GetErrorMessage() const;
  117. /**
  118. * @return the error text for the class of error of this RC_ITEM represents. For instance,
  119. * "Clearance violation".
  120. */
  121. wxString GetErrorText() const
  122. {
  123. return wxGetTranslation( m_errorTitle );
  124. }
  125. wxString GetSettingsKey() const
  126. {
  127. return m_settingsKey;
  128. }
  129. virtual wxString GetViolatingRuleDesc() const
  130. {
  131. return wxEmptyString;
  132. }
  133. protected:
  134. int m_errorCode; ///< The error code's numeric value
  135. wxString m_errorMessage; ///< A message describing the details of this specific error
  136. wxString m_errorTitle; ///< The string describing the type of error
  137. wxString m_settingsKey; ///< The key used to describe this type of error in settings
  138. MARKER_BASE* m_parent; ///< The marker this item belongs to, if any
  139. KIIDS m_ids;
  140. };
  141. class RC_TREE_NODE
  142. {
  143. public:
  144. enum NODE_TYPE { MARKER, MAIN_ITEM, AUX_ITEM, AUX_ITEM2, AUX_ITEM3 };
  145. RC_TREE_NODE( RC_TREE_NODE* aParent, std::shared_ptr<RC_ITEM> aRcItem, NODE_TYPE aType ) :
  146. m_Type( aType ),
  147. m_RcItem( aRcItem ),
  148. m_Parent( aParent )
  149. {}
  150. ~RC_TREE_NODE()
  151. {
  152. for( RC_TREE_NODE* child : m_Children )
  153. delete child;
  154. }
  155. NODE_TYPE m_Type;
  156. std::shared_ptr<RC_ITEM> m_RcItem;
  157. RC_TREE_NODE* m_Parent;
  158. std::vector<RC_TREE_NODE*> m_Children;
  159. };
  160. class RC_TREE_MODEL : public wxDataViewModel, public wxEvtHandler
  161. {
  162. public:
  163. static wxDataViewItem ToItem( RC_TREE_NODE const* aNode )
  164. {
  165. return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
  166. }
  167. static RC_TREE_NODE* ToNode( wxDataViewItem aItem )
  168. {
  169. return static_cast<RC_TREE_NODE*>( aItem.GetID() );
  170. }
  171. static KIID ToUUID( wxDataViewItem aItem );
  172. RC_TREE_MODEL( EDA_DRAW_FRAME* aParentFrame, wxDataViewCtrl* aView );
  173. ~RC_TREE_MODEL();
  174. void Update( std::shared_ptr<RC_ITEMS_PROVIDER> aProvider, int aSeverities );
  175. void ExpandAll();
  176. void PrevMarker();
  177. void NextMarker();
  178. void SelectMarker( const MARKER_BASE* aMarker );
  179. void CenterMarker( const MARKER_BASE* aMarker );
  180. bool IsContainer( wxDataViewItem const& aItem ) const override;
  181. wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
  182. unsigned int GetChildren( wxDataViewItem const& aItem,
  183. wxDataViewItemArray& aChildren ) const override;
  184. // Simple, single-text-column model
  185. unsigned int GetColumnCount() const override { return 1; }
  186. wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
  187. bool HasContainerColumns( wxDataViewItem const& aItem ) const override { return true; }
  188. /**
  189. * Called by the wxDataView to fetch an item's value.
  190. */
  191. void GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
  192. unsigned int aCol ) const override;
  193. /**
  194. * Called by the wxDataView to edit an item's content.
  195. */
  196. bool SetValue( wxVariant const& aVariant, wxDataViewItem const& aItem,
  197. unsigned int aCol ) override
  198. {
  199. // Editing not supported
  200. return false;
  201. }
  202. /**
  203. * Called by the wxDataView to fetch an item's formatting. Return true if the
  204. * item has non-default attributes.
  205. */
  206. bool GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
  207. wxDataViewItemAttr& aAttr ) const override;
  208. void ValueChanged( const RC_TREE_NODE* aNode );
  209. void DeleteCurrentItem( bool aDeep );
  210. /**
  211. * Deletes the current item or all items. If all, \a aIncludeExclusions determines
  212. * whether or not exclusions are also deleted.
  213. */
  214. void DeleteItems( bool aCurrentOnly, bool aIncludeExclusions, bool aDeep );
  215. private:
  216. void rebuildModel( std::shared_ptr<RC_ITEMS_PROVIDER> aProvider, int aSeverities );
  217. void onSizeView( wxSizeEvent& aEvent );
  218. EDA_DRAW_FRAME* m_editFrame;
  219. wxDataViewCtrl* m_view;
  220. int m_severities;
  221. std::shared_ptr<RC_ITEMS_PROVIDER> m_rcItemsProvider;
  222. std::vector<RC_TREE_NODE*> m_tree; // I own this
  223. };
  224. #endif // RC_ITEM_H