KiCad PCB EDA Suite
rc_item.h
Go to the documentation of this file.
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 
24 #ifndef RC_ITEM_H
25 #define RC_ITEM_H
26 
27 #include <wx/dataview.h>
28 #include <eda_item.h>
29 #include <reporter.h>
30 
31 class MARKER_BASE;
32 class EDA_BASE_FRAME;
33 class RC_ITEM;
34 
35 
44 {
45 public:
46  virtual void SetSeverities( int aSeverities ) = 0;
47 
48  virtual int GetCount( int aSeverity = -1 ) = 0;
49 
53  virtual std::shared_ptr<RC_ITEM> GetItem( int aIndex ) = 0;
54 
59  virtual void DeleteItem( int aIndex, bool aDeep ) = 0;
60 
61  virtual void DeleteAllItems( bool aIncludeExclusions, bool aDeep ) = 0;
62 
63  virtual ~RC_ITEMS_PROVIDER() { }
64 };
65 
66 
72 class RC_ITEM
73 {
74 public:
75  typedef std::vector<KIID> KIIDS;
76 
77 protected:
79  wxString m_errorMessage;
80  wxString m_errorTitle;
81  wxString m_settingsKey;
83 
85 
86 public:
87  RC_ITEM() :
88  m_errorCode( 0 ),
89  m_parent( nullptr )
90  {
91  }
92 
93  RC_ITEM( std::shared_ptr<RC_ITEM> aItem )
94  {
95  m_errorCode = aItem->m_errorCode;
96  m_errorMessage = aItem->m_errorMessage;
97  m_errorTitle = aItem->m_errorTitle;
98  m_settingsKey = aItem->m_settingsKey;
99  m_parent = aItem->m_parent;
100  m_ids = aItem->m_ids;
101  }
102 
103  virtual ~RC_ITEM() { }
104 
105  void SetErrorMessage( const wxString& aMessage ) { m_errorMessage = aMessage; }
106 
107  void SetItems( const KIIDS& aIds )
108  {
109  m_ids = aIds;
110  }
111 
112  void AddItem( EDA_ITEM* aItem )
113  {
114  m_ids.push_back( aItem->m_Uuid );
115  }
116 
117  void SetItems( const EDA_ITEM* aItem, const EDA_ITEM* bItem = nullptr,
118  const EDA_ITEM* cItem = nullptr, const EDA_ITEM* dItem = nullptr )
119  {
120  m_ids.clear();
121 
122  m_ids.push_back( aItem->m_Uuid );
123 
124  if( bItem )
125  m_ids.push_back( bItem->m_Uuid );
126 
127  if( cItem )
128  m_ids.push_back( cItem->m_Uuid );
129 
130  if( dItem )
131  m_ids.push_back( dItem->m_Uuid );
132  }
133 
134  void SetItems( const KIID& aItem, const KIID& bItem = niluuid, const KIID& cItem = niluuid,
135  const KIID& dItem = niluuid )
136  {
137  m_ids.clear();
138 
139  m_ids.push_back( aItem );
140  m_ids.push_back( bItem );
141  m_ids.push_back( cItem );
142  m_ids.push_back( dItem );
143  }
144 
145  KIID GetMainItemID() const { return m_ids.size() > 0 ? m_ids[0] : niluuid; }
146  KIID GetAuxItemID() const { return m_ids.size() > 1 ? m_ids[1] : niluuid;; }
147  KIID GetAuxItem2ID() const { return m_ids.size() > 2 ? m_ids[2] : niluuid;; }
148  KIID GetAuxItem3ID() const { return m_ids.size() > 3 ? m_ids[3] : niluuid;; }
149 
150  void SetParent( MARKER_BASE* aMarker ) { m_parent = aMarker; }
151  MARKER_BASE* GetParent() const { return m_parent; }
152 
153 
159  virtual wxString ShowReport( EDA_UNITS aUnits, SEVERITY aSeverity,
160  const std::map<KIID, EDA_ITEM*>& aItemMap ) const;
161 
162  int GetErrorCode() const { return m_errorCode; }
163  void SetErrorCode( int aCode ) { m_errorCode = aCode; }
164 
168  virtual wxString GetErrorMessage() const;
169 
170  wxString GetErrorText() const
171  {
172  return wxGetTranslation( m_errorTitle );
173  }
174 
175  wxString GetSettingsKey() const
176  {
177  return m_settingsKey;
178  }
179 
183  static wxString ShowCoord( EDA_UNITS aUnits, const wxPoint& aPos );
184 };
185 
186 
188 {
189 public:
191 
192  RC_TREE_NODE( RC_TREE_NODE* aParent, std::shared_ptr<RC_ITEM> aRcItem, NODE_TYPE aType ) :
193  m_Type( aType ),
194  m_RcItem( aRcItem ),
195  m_Parent( aParent )
196  {}
197 
199  {
200  for( RC_TREE_NODE* child : m_Children )
201  delete child;
202  }
203 
205  std::shared_ptr<RC_ITEM> m_RcItem;
206 
208  std::vector<RC_TREE_NODE*> m_Children;
209 };
210 
211 
212 class RC_TREE_MODEL : public wxDataViewModel, wxEvtHandler
213 {
214 public:
215  static wxDataViewItem ToItem( RC_TREE_NODE const* aNode )
216  {
217  return wxDataViewItem( const_cast<void*>( static_cast<void const*>( aNode ) ) );
218  }
219 
220  static RC_TREE_NODE* ToNode( wxDataViewItem aItem )
221  {
222  return static_cast<RC_TREE_NODE*>( aItem.GetID() );
223  }
224 
225  static KIID ToUUID( wxDataViewItem aItem );
226 
227 
228 public:
229  RC_TREE_MODEL( EDA_DRAW_FRAME* aParentFrame, wxDataViewCtrl* aView );
230 
231  ~RC_TREE_MODEL();
232 
233  void SetProvider( RC_ITEMS_PROVIDER* aProvider );
234  void SetSeverities( int aSeverities );
235 
236  int GetDRCItemCount() const { return m_tree.size(); }
237 
238  void ExpandAll();
239 
240  bool IsContainer( wxDataViewItem const& aItem ) const override;
241 
242  wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override;
243 
244  unsigned int GetChildren( wxDataViewItem const& aItem,
245  wxDataViewItemArray& aChildren ) const override;
246 
247  // Simple, single-text-column model
248  unsigned int GetColumnCount() const override { return 1; }
249  wxString GetColumnType( unsigned int aCol ) const override { return "string"; }
250  bool HasContainerColumns( wxDataViewItem const& aItem ) const override { return true; }
251 
255  void GetValue( wxVariant& aVariant,
256  wxDataViewItem const& aItem,
257  unsigned int aCol ) const override;
258 
262  bool SetValue( wxVariant const& aVariant,
263  wxDataViewItem const& aItem,
264  unsigned int aCol ) override
265  {
266  // Editing not supported
267  return false;
268  }
269 
274  bool GetAttr( wxDataViewItem const& aItem,
275  unsigned int aCol,
276  wxDataViewItemAttr& aAttr ) const override;
277 
278  void ValueChanged( RC_TREE_NODE* aNode );
279 
280  void DeleteCurrentItem( bool aDeep );
281 
286  void DeleteItems( bool aCurrentOnly, bool aIncludeExclusions, bool aDeep );
287 
288 private:
289  void rebuildModel( RC_ITEMS_PROVIDER* aProvider, int aSeverities );
290  void onSizeView( wxSizeEvent& aEvent );
291 
292 private:
294  wxDataViewCtrl* m_view;
296  RC_ITEMS_PROVIDER* m_rcItemsProvider; // I own this, but not its contents
297 
298  std::vector<RC_TREE_NODE*> m_tree; // I own this
299 };
300 
301 #endif // RC_ITEM_H
void DeleteItems(bool aCurrentOnly, bool aIncludeExclusions, bool aDeep)
Deletes the current item or all items.
Definition: rc_item.cpp:425
unsigned int GetColumnCount() const override
Definition: rc_item.h:248
KIIDS m_ids
Definition: rc_item.h:84
virtual void DeleteAllItems(bool aIncludeExclusions, bool aDeep)=0
KIID GetAuxItem2ID() const
Definition: rc_item.h:147
KIID niluuid(0)
static wxDataViewItem ToItem(RC_TREE_NODE const *aNode)
Definition: rc_item.h:215
void SetProvider(RC_ITEMS_PROVIDER *aProvider)
Definition: rc_item.cpp:258
wxString m_settingsKey
The key used to describe this type of error in settings.
Definition: rc_item.h:81
SEVERITY
Definition: ui_common.h:76
RC_ITEM()
Definition: rc_item.h:87
A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
Definition: rc_item.h:72
bool GetAttr(wxDataViewItem const &aItem, unsigned int aCol, wxDataViewItemAttr &aAttr) const override
Called by the wxDataView to fetch an item's formatting.
Definition: rc_item.cpp:369
RC_TREE_NODE * m_Parent
Definition: rc_item.h:207
virtual int GetCount(int aSeverity=-1)=0
std::vector< KIID > KIIDS
Definition: rc_item.h:75
static KIID ToUUID(wxDataViewItem aItem)
Definition: rc_item.cpp:119
wxDataViewCtrl * m_view
Definition: rc_item.h:294
bool HasContainerColumns(wxDataViewItem const &aItem) const override
Definition: rc_item.h:250
void SetParent(MARKER_BASE *aMarker)
Definition: rc_item.h:150
The base class for create windows for drawing purpose.
bool SetValue(wxVariant const &aVariant, wxDataViewItem const &aItem, unsigned int aCol) override
Called by the wxDataView to edit an item's content.
Definition: rc_item.h:262
virtual std::shared_ptr< RC_ITEM > GetItem(int aIndex)=0
Retrieve a RC_ITEM by index.
wxString GetColumnType(unsigned int aCol) const override
Definition: rc_item.h:249
void DeleteCurrentItem(bool aDeep)
Definition: rc_item.cpp:419
void SetSeverities(int aSeverities)
Definition: rc_item.cpp:264
Provide an abstract interface of a RC_ITEM* list manager.
Definition: rc_item.h:43
void GetValue(wxVariant &aVariant, wxDataViewItem const &aItem, unsigned int aCol) const override
Called by the wxDataView to fetch an item's value.
Definition: rc_item.cpp:308
wxString GetErrorText() const
Definition: rc_item.h:170
std::vector< RC_TREE_NODE * > m_Children
Definition: rc_item.h:208
RC_ITEMS_PROVIDER * m_rcItemsProvider
Definition: rc_item.h:296
Definition: kiid.h:44
int GetDRCItemCount() const
Definition: rc_item.h:236
std::shared_ptr< RC_ITEM > m_RcItem
Definition: rc_item.h:205
int m_errorCode
The error code's numeric value.
Definition: rc_item.h:78
void ExpandAll()
Definition: rc_item.cpp:270
void rebuildModel(RC_ITEMS_PROVIDER *aProvider, int aSeverities)
Definition: rc_item.cpp:169
static RC_TREE_NODE * ToNode(wxDataViewItem aItem)
Definition: rc_item.h:220
static wxString ShowCoord(EDA_UNITS aUnits, const wxPoint &aPos)
Format a coordinate or position to text.
Definition: rc_item.cpp:46
KIID GetAuxItemID() const
Definition: rc_item.h:146
RC_TREE_MODEL(EDA_DRAW_FRAME *aParentFrame, wxDataViewCtrl *aView)
Definition: rc_item.cpp:148
int GetErrorCode() const
Definition: rc_item.h:162
virtual ~RC_ITEM()
Definition: rc_item.h:103
EDA_DRAW_FRAME * m_editFrame
Definition: rc_item.h:293
wxString m_errorMessage
A message describing the details of this specific error.
Definition: rc_item.h:79
void onSizeView(wxSizeEvent &aEvent)
Definition: rc_item.cpp:499
virtual void SetSeverities(int aSeverities)=0
unsigned int GetChildren(wxDataViewItem const &aItem, wxDataViewItemArray &aChildren) const override
Definition: rc_item.cpp:292
void AddItem(EDA_ITEM *aItem)
Definition: rc_item.h:112
const KIID m_Uuid
Definition: eda_item.h:151
NODE_TYPE m_Type
Definition: rc_item.h:204
EDA_UNITS
Definition: eda_units.h:38
RC_TREE_NODE(RC_TREE_NODE *aParent, std::shared_ptr< RC_ITEM > aRcItem, NODE_TYPE aType)
Definition: rc_item.h:192
std::vector< RC_TREE_NODE * > m_tree
Definition: rc_item.h:298
virtual ~RC_ITEMS_PROVIDER()
Definition: rc_item.h:63
virtual wxString ShowReport(EDA_UNITS aUnits, SEVERITY aSeverity, const std::map< KIID, EDA_ITEM * > &aItemMap) const
Translate this object into a text string suitable for saving to disk in a report.
Definition: rc_item.cpp:54
MARKER_BASE * m_parent
The marker this item belongs to, if any.
Definition: rc_item.h:82
int m_severities
Definition: rc_item.h:295
void SetItems(const KIIDS &aIds)
Definition: rc_item.h:107
The base frame for deriving all KiCad main window classes.
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: eda_item.h:148
wxString GetSettingsKey() const
Definition: rc_item.h:175
void SetErrorMessage(const wxString &aMessage)
Definition: rc_item.h:105
KIID GetMainItemID() const
Definition: rc_item.h:145
MARKER_BASE * GetParent() const
Definition: rc_item.h:151
~RC_TREE_NODE()
Definition: rc_item.h:198
RC_ITEM(std::shared_ptr< RC_ITEM > aItem)
Definition: rc_item.h:93
void SetItems(const KIID &aItem, const KIID &bItem=niluuid, const KIID &cItem=niluuid, const KIID &dItem=niluuid)
Definition: rc_item.h:134
bool IsContainer(wxDataViewItem const &aItem) const override
Definition: rc_item.cpp:277
void SetItems(const EDA_ITEM *aItem, const EDA_ITEM *bItem=nullptr, const EDA_ITEM *cItem=nullptr, const EDA_ITEM *dItem=nullptr)
Definition: rc_item.h:117
KIID GetAuxItem3ID() const
Definition: rc_item.h:148
void SetErrorCode(int aCode)
Definition: rc_item.h:163
wxDataViewItem GetParent(wxDataViewItem const &aItem) const override
Definition: rc_item.cpp:286
virtual wxString GetErrorMessage() const
Return the error message of a RC_ITEM.
Definition: rc_item.cpp:37
virtual void DeleteItem(int aIndex, bool aDeep)=0
Remove (and optionally deletes) the indexed item from the list.
void ValueChanged(RC_TREE_NODE *aNode)
Definition: rc_item.cpp:402
wxString m_errorTitle
The string describing the type of error.
Definition: rc_item.h:80