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.

290 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 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 <kiid.h>
  27. #include <reporter.h>
  28. #include <math/vector2d.h>
  29. class MARKER_BASE;
  30. class EDA_BASE_FRAME;
  31. class RC_ITEM;
  32. class EDA_ITEM;
  33. class EDA_DRAW_FRAME;
  34. /**
  35. * Provide an abstract interface of a RC_ITEM* list manager.
  36. *
  37. * The details of the actual list architecture are hidden from the caller. Any class that
  38. * implements this interface can then be used by a RC_TREE_MODEL class without it knowing
  39. * the actual architecture of the list.
  40. */
  41. class RC_ITEMS_PROVIDER
  42. {
  43. public:
  44. virtual void SetSeverities( int aSeverities ) = 0;
  45. virtual int GetCount( int aSeverity = -1 ) const = 0;
  46. /**
  47. * Retrieve a RC_ITEM by index.
  48. */
  49. virtual std::shared_ptr<RC_ITEM> GetItem( int aIndex ) const = 0;
  50. /**
  51. * Remove (and optionally deletes) the indexed item from the list.
  52. * @param aDeep If true, the source item should be deleted as well as its entry in the list.
  53. */
  54. virtual void DeleteItem( int aIndex, bool aDeep ) = 0;
  55. virtual void DeleteAllItems( bool aIncludeExclusions, 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( EDA_UNITS aUnits, 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 of a RC_ITEM.
  114. */
  115. virtual wxString GetErrorMessage() const;
  116. wxString GetErrorText() const
  117. {
  118. return wxGetTranslation( m_errorTitle );
  119. }
  120. wxString GetSettingsKey() const
  121. {
  122. return m_settingsKey;
  123. }
  124. virtual wxString GetViolatingRuleDesc() const
  125. {
  126. return wxEmptyString;
  127. }
  128. /**
  129. * Format a coordinate or position to text.
  130. */
  131. static wxString ShowCoord( EDA_UNITS aUnits, const VECTOR2I& aPos );
  132. protected:
  133. int m_errorCode; ///< The error code's numeric value
  134. wxString m_errorMessage; ///< A message describing the details of this specific error
  135. wxString m_errorTitle; ///< The string describing the type of error
  136. wxString m_settingsKey; ///< The key used to describe this type of error in settings
  137. MARKER_BASE* m_parent; ///< The marker this item belongs to, if any
  138. KIIDS m_ids;
  139. };
  140. class RC_TREE_NODE
  141. {
  142. public:
  143. enum NODE_TYPE { MARKER, MAIN_ITEM, AUX_ITEM, AUX_ITEM2, AUX_ITEM3 };
  144. RC_TREE_NODE( RC_TREE_NODE* aParent, std::shared_ptr<RC_ITEM> aRcItem, NODE_TYPE aType ) :
  145. m_Type( aType ),
  146. m_RcItem( aRcItem ),
  147. m_Parent( aParent )
  148. {}
  149. ~RC_TREE_NODE()
  150. {
  151. for( RC_TREE_NODE* child : m_Children )
  152. delete child;
  153. }
  154. NODE_TYPE m_Type;
  155. std::shared_ptr<RC_ITEM> m_RcItem;
  156. RC_TREE_NODE* m_Parent;
  157. std::vector<RC_TREE_NODE*> m_Children;
  158. };
  159. class RC_TREE_MODEL : public wxDataViewModel, public wxEvtHandler
  160. {
  161. public:
  162. static wxDataViewItem ToItem( RC_TREE_NODE const* aNode )
  163. {
  164. return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
  165. }
  166. static RC_TREE_NODE* ToNode( wxDataViewItem aItem )
  167. {
  168. return static_cast<RC_TREE_NODE*>( aItem.GetID() );
  169. }
  170. static KIID ToUUID( wxDataViewItem aItem );
  171. RC_TREE_MODEL( EDA_DRAW_FRAME* aParentFrame, wxDataViewCtrl* aView );
  172. ~RC_TREE_MODEL();
  173. void SetProvider( RC_ITEMS_PROVIDER* aProvider );
  174. void SetSeverities( int aSeverities );
  175. int GetDRCItemCount() const { return m_tree.size(); }
  176. void ExpandAll();
  177. void PrevMarker();
  178. void NextMarker();
  179. void SelectMarker( const MARKER_BASE* aMarker );
  180. void CenterMarker( const MARKER_BASE* aMarker );
  181. bool IsContainer( wxDataViewItem const& aItem ) const override;
  182. wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
  183. unsigned int GetChildren( wxDataViewItem const& aItem,
  184. wxDataViewItemArray& aChildren ) const override;
  185. // Simple, single-text-column model
  186. unsigned int GetColumnCount() const override { return 1; }
  187. wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
  188. bool HasContainerColumns( wxDataViewItem const& aItem ) const override { return true; }
  189. /**
  190. * Called by the wxDataView to fetch an item's value.
  191. */
  192. void GetValue( wxVariant& aVariant, wxDataViewItem const& aItem,
  193. unsigned int aCol ) const override;
  194. /**
  195. * Called by the wxDataView to edit an item's content.
  196. */
  197. bool SetValue( wxVariant const& aVariant, wxDataViewItem const& aItem,
  198. unsigned int aCol ) override
  199. {
  200. // Editing not supported
  201. return false;
  202. }
  203. /**
  204. * Called by the wxDataView to fetch an item's formatting. Return true if the
  205. * item has non-default attributes.
  206. */
  207. bool GetAttr( wxDataViewItem const& aItem, unsigned int aCol,
  208. wxDataViewItemAttr& aAttr ) const override;
  209. void ValueChanged( const RC_TREE_NODE* aNode );
  210. void DeleteCurrentItem( bool aDeep );
  211. /**
  212. * Deletes the current item or all items. If all, \a aIncludeExclusions determines
  213. * whether or not exclusions are also deleted.
  214. */
  215. void DeleteItems( bool aCurrentOnly, bool aIncludeExclusions, bool aDeep );
  216. private:
  217. void rebuildModel( RC_ITEMS_PROVIDER* aProvider, int aSeverities );
  218. void onSizeView( wxSizeEvent& aEvent );
  219. EDA_DRAW_FRAME* m_editFrame;
  220. wxDataViewCtrl* m_view;
  221. int m_severities;
  222. RC_ITEMS_PROVIDER* m_rcItemsProvider; // I own this, but not its contents
  223. std::vector<RC_TREE_NODE*> m_tree; // I own this
  224. };
  225. #endif // RC_ITEM_H