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.

299 lines
9.0 KiB

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