KiCad PCB EDA Suite
drc_provider.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) 2018 KiCad Developers, see change_log.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 
25 #ifndef DRC_PROVIDER__H
26 #define DRC_PROVIDER__H
27 
28 #include <class_board.h>
29 #include <class_marker_pcb.h>
30 #include <pcb_base_frame.h>
31 #include <drc/drc.h>
32 #include <widgets/ui_common.h>
33 #include <functional>
34 
35 
42 {
43 public:
47  using MARKER_HANDLER = std::function<void( MARKER_PCB* )>;
48 
55  virtual bool RunDRC( EDA_UNITS aUnits, BOARD& aBoard ) = 0;
56 
57  virtual ~DRC_TEST_PROVIDER() {}
58 
59 protected:
60  DRC_TEST_PROVIDER( MARKER_HANDLER aMarkerHandler ) :
61  m_marker_handler( std::move( aMarkerHandler ) )
62  {
63  }
64 
68  void HandleMarker( MARKER_PCB* aMarker ) const
69  {
70  m_marker_handler( aMarker );
71  }
72 
73 private:
76 };
77 
78 
85 {
86 private:
88 
90  std::vector<MARKER_PCB*> m_filteredMarkers;
91 
92 public:
94  m_board( aBoard ),
95  m_severities( 0 )
96  {
97  }
98 
99  void SetSeverities( int aSeverities ) override
100  {
101  m_severities = aSeverities;
102 
104 
105  m_filteredMarkers.clear();
106 
107  for( MARKER_PCB* marker : m_board->Markers() )
108  {
109  int markerSeverity;
110 
111  if( marker->IsExcluded() )
112  markerSeverity = RPT_SEVERITY_EXCLUSION;
113  else
114  markerSeverity = bds.GetSeverity( marker->GetRCItem()->GetErrorCode() );
115 
116  if( markerSeverity & m_severities )
117  m_filteredMarkers.push_back( marker );
118  }
119  }
120 
121  int GetCount( int aSeverity = -1 ) override
122  {
123  if( aSeverity < 0 )
124  return m_filteredMarkers.size();
125 
127 
128  int count = 0;
129 
130  for( MARKER_PCB* marker : m_board->Markers() )
131  {
132  int markerSeverity;
133 
134  if( marker->IsExcluded() )
135  markerSeverity = RPT_SEVERITY_EXCLUSION;
136  else
137  markerSeverity = bds.GetSeverity( marker->GetRCItem()->GetErrorCode() );
138 
139  if( markerSeverity == aSeverity )
140  count++;
141  }
142 
143  return count;
144  }
145 
146  DRC_ITEM* GetItem( int aIndex ) override
147  {
148  MARKER_PCB* marker = m_filteredMarkers[ aIndex ];
149 
150  return marker ? static_cast<DRC_ITEM*>( marker->GetRCItem() ) : nullptr;
151  }
152 
153  void DeleteItem( int aIndex, bool aDeep ) override
154  {
155  MARKER_PCB* marker = m_filteredMarkers[ aIndex ];
156  m_filteredMarkers.erase( m_filteredMarkers.begin() + aIndex );
157 
158  if( aDeep )
159  m_board->Delete( marker );
160  }
161 
162  void DeleteAllItems() override
163  {
165  m_filteredMarkers.clear();
166  }
167 };
168 
169 
177 {
179  std::vector<DRC_ITEM*>* m_sourceVector; // owns its DRC_ITEMs
180 
182  std::vector<DRC_ITEM*> m_filteredVector; // does not own its DRC_ITEMs
183 
184 public:
185 
186  VECTOR_DRC_ITEMS_PROVIDER( PCB_BASE_FRAME* aFrame, std::vector<DRC_ITEM*>* aList ) :
187  m_frame( aFrame ),
188  m_sourceVector( aList ),
189  m_severities( 0 )
190  {
191  }
192 
193  void SetSeverities( int aSeverities ) override
194  {
195  m_severities = aSeverities;
196 
198 
199  m_filteredVector.clear();
200 
201  if( m_sourceVector )
202  {
203  for( DRC_ITEM* item : *m_sourceVector )
204  {
205  if( bds.GetSeverity( item->GetErrorCode() ) & aSeverities )
206  m_filteredVector.push_back( item );
207  }
208  }
209  }
210 
211  int GetCount( int aSeverity = -1 ) override
212  {
213  if( aSeverity < 0 )
214  return m_filteredVector.size();
215 
216  int count = 0;
218 
219  if( m_sourceVector )
220  {
221  for( DRC_ITEM* item : *m_sourceVector )
222  {
223  if( bds.GetSeverity( item->GetErrorCode() ) == aSeverity )
224  count++;
225  }
226  }
227 
228  return count;
229  }
230 
231  DRC_ITEM* GetItem( int aIndex ) override
232  {
233  return (m_filteredVector)[aIndex];
234  }
235 
236  void DeleteItem( int aIndex, bool aDeep ) override
237  {
238  DRC_ITEM* item = m_filteredVector[aIndex];
239  m_filteredVector.erase( m_filteredVector.begin() + aIndex );
240 
241  if( aDeep )
242  {
243  for( size_t i = 0; i < m_sourceVector->size(); ++i )
244  {
245  if( m_sourceVector->at( i ) == item )
246  {
247  delete item;
248  m_sourceVector->erase( m_sourceVector->begin() + i );
249  break;
250  }
251  }
252  }
253  }
254 
255  void DeleteAllItems() override
256  {
257  if( m_sourceVector )
258  {
259  for( DRC_ITEM* item : *m_sourceVector )
260  delete item;
261 
262  m_sourceVector->clear();
263  }
264 
265  m_filteredVector.clear(); // no ownership of DRC_ITEM pointers
266  }
267 };
268 
269 
274 {
275  // TODO: for now this is just a vector, but we need to map it to some board-level
276  // data-structure so that deleting/excluding things can do a deep delete/exclusion
277  // which will be reflected in the ratsnest....
278 public:
279  RATSNEST_DRC_ITEMS_PROVIDER( PCB_BASE_FRAME* aFrame, std::vector<DRC_ITEM*>* aList ) :
280  VECTOR_DRC_ITEMS_PROVIDER( aFrame, aList )
281  { }
282 };
283 
284 
285 #endif // DRC_PROVIDER__H
EDA_UNITS
Definition: common.h:198
void SetSeverities(int aSeverities) override
Definition: drc_provider.h:193
RATSNEST_DRC_ITEMS_PROVIDER.
Definition: drc_provider.h:273
void DeleteAllItems() override
Function DeleteAllItems removes and deletes all the items in the list.
Definition: drc_provider.h:162
MARKER_HANDLER m_marker_handler
The handler for any generated markers.
Definition: drc_provider.h:75
virtual bool RunDRC(EDA_UNITS aUnits, BOARD &aBoard)=0
Runs this provider against the given PCB with configured options (if any).
RC_ITEM * GetRCItem()
Function GetReporter returns the DRC_ITEM held within this MARKER so that its interface may be used.
Definition: marker_base.h:117
std::vector< DRC_ITEM * > m_filteredVector
Definition: drc_provider.h:182
BOARD_DRC_ITEMS_PROVIDER(BOARD *aBoard)
Definition: drc_provider.h:93
MARKERS & Markers()
Definition: class_board.h:285
DRC_ITEM * GetItem(int aIndex) override
Function GetItem retrieves a RC_ITEM by index.
Definition: drc_provider.h:146
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:551
Provide an abstract interface of a RC_ITEM* list manager.
Definition: rc_item.h:42
Classes used in Pcbnew, CvPcb and GerbView.
RATSNEST_DRC_ITEMS_PROVIDER(PCB_BASE_FRAME *aFrame, std::vector< DRC_ITEM * > *aList)
Definition: drc_provider.h:279
int GetCount(int aSeverity=-1) override
Definition: drc_provider.h:211
void DeleteItem(int aIndex, bool aDeep) override
Function DeleteItem removes (and optionally deletes) the indexed item from the list.
Definition: drc_provider.h:236
std::vector< MARKER_PCB * > m_filteredMarkers
Definition: drc_provider.h:90
Markers used to show a drc problem on boards.
VECTOR_DRC_ITEMS_PROVIDER(PCB_BASE_FRAME *aFrame, std::vector< DRC_ITEM * > *aList)
Definition: drc_provider.h:186
void DeleteMARKERs()
Function DeleteMARKERs deletes ALL MARKERS from the board.
BOARD_DRC_ITEMS_PROVIDER is an implementation of the RC_ITEMS_PROVIDER interface which uses a BOARD i...
Definition: drc_provider.h:84
void DeleteItem(int aIndex, bool aDeep) override
Function DeleteItem removes (and optionally deletes) the indexed item from the list.
Definition: drc_provider.h:153
DRC_ITEM * GetItem(int aIndex) override
Function GetItem retrieves a RC_ITEM by index.
Definition: drc_provider.h:231
void HandleMarker(MARKER_PCB *aMarker) const
Pass a given marker to the marker handler.
Definition: drc_provider.h:68
Functions to provide common constants and other functions to assist in making a consistent UI.
virtual ~DRC_TEST_PROVIDER()
Definition: drc_provider.h:57
void SetSeverities(int aSeverities) override
Definition: drc_provider.h:99
void DeleteAllItems() override
Function DeleteAllItems removes and deletes all the items in the list.
Definition: drc_provider.h:255
DRC_TEST_PROVIDER is a base class that represents a DRC "provider" which runs some DRC functions over...
Definition: drc_provider.h:41
std::function< void(MARKER_PCB *)> MARKER_HANDLER
A callable that can handle a single generated PCB_MARKER.
Definition: drc_provider.h:47
DRC_TEST_PROVIDER(MARKER_HANDLER aMarkerHandler)
Definition: drc_provider.h:60
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
std::vector< DRC_ITEM * > * m_sourceVector
Definition: drc_provider.h:179
PCB_BASE_FRAME * m_frame
Definition: drc_provider.h:178
virtual void Delete(BOARD_ITEM *aItem)
Removes an item from the container and deletes it.
BOARD * GetBoard() const
int GetSeverity(int aDRCErrorCode)
VECTOR_DRC_ITEMS_PROVIDER is an implementation of the interface named DRC_ITEMS_PROVIDER which uses a...
Definition: drc_provider.h:176
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
int GetCount(int aSeverity=-1) override
Definition: drc_provider.h:121
BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.