KiCad PCB EDA Suite
rc_item.cpp
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 
25 #include <wx/wupdlock.h>
26 #include <wx/dataview.h>
27 #include <widgets/ui_common.h>
28 #include <marker_base.h>
29 #include <eda_draw_frame.h>
30 #include <rc_item.h>
31 #include <base_units.h>
32 
33 #define WX_DATAVIEW_WINDOW_PADDING 6
34 
35 
36 wxString RC_ITEM::GetErrorMessage() const
37 {
38  if( m_errorMessage.IsEmpty() )
39  return GetErrorText( m_errorCode );
40  else
41  return m_errorMessage;
42 }
43 
44 
45 wxString RC_ITEM::ShowCoord( EDA_UNITS aUnits, const wxPoint& aPos )
46 {
47  return wxString::Format( "@(%s, %s)",
48  MessageTextFromValue( aUnits, aPos.x ),
49  MessageTextFromValue( aUnits, aPos.y ) );
50 }
51 
52 
53 wxString RC_ITEM::ShowReport( EDA_UNITS aUnits, const std::map<KIID, EDA_ITEM*>& aItemMap ) const
54 {
55  EDA_ITEM* mainItem = nullptr;
56  EDA_ITEM* auxItem = nullptr;
57 
58  if( m_mainItemUuid != niluuid )
59  mainItem = aItemMap.at( m_mainItemUuid );
60 
61  if( m_auxItemUuid != niluuid )
62  auxItem = aItemMap.at( m_auxItemUuid );
63 
64  if( mainItem && auxItem )
65  {
66  return wxString::Format( wxT( "ErrType(%d): %s\n %s: %s\n %s: %s\n" ),
67  GetErrorCode(),
69  ShowCoord( aUnits, mainItem->GetPosition() ),
70  mainItem->GetSelectMenuText( aUnits ),
71  ShowCoord( aUnits, auxItem->GetPosition() ),
72  auxItem->GetSelectMenuText( aUnits ) );
73  }
74  else if( mainItem )
75  {
76  return wxString::Format( wxT( "ErrType(%d): %s\n %s: %s\n" ),
77  GetErrorCode(),
79  ShowCoord( aUnits, mainItem->GetPosition() ),
80  mainItem->GetSelectMenuText( aUnits ) );
81  }
82  else
83  {
84  return wxString::Format( wxT( "ErrType(%d): %s\n" ),
85  GetErrorCode(),
86  GetErrorMessage() );
87  }
88 }
89 
90 
91 KIID RC_TREE_MODEL::ToUUID( wxDataViewItem aItem )
92 {
93  const RC_TREE_NODE* node = RC_TREE_MODEL::ToNode( aItem );
94 
95  if( node )
96  {
97  const RC_ITEM* rc_item = node->m_RcItem;
98 
99  switch( node->m_Type )
100  {
102  // rc_item->GetParent() can be null, if the parent is not existing
103  // when a RC item has no corresponding ERC/DRC marker
104  if( rc_item->GetParent() )
105  return rc_item->GetParent()->GetUUID();
106 
107  break;
108 
109  case RC_TREE_NODE::MAIN_ITEM: return rc_item->GetMainItemID();
110  case RC_TREE_NODE::AUX_ITEM: return rc_item->GetAuxItemID();
111  }
112  }
113 
114  return niluuid;
115 }
116 
117 
118 RC_TREE_MODEL::RC_TREE_MODEL( EDA_DRAW_FRAME* aParentFrame, wxDataViewCtrl* aView ) :
119  m_editFrame( aParentFrame ),
120  m_view( aView ),
121  m_severities( 0 ),
122  m_rcItemsProvider( nullptr )
123 {
124  m_view->GetMainWindow()->Connect( wxEVT_SIZE,
125  wxSizeEventHandler( RC_TREE_MODEL::onSizeView ),
126  NULL, this );
127 }
128 
129 
131 {
132  delete m_rcItemsProvider;
133 
134  for( RC_TREE_NODE* topLevelNode : m_tree )
135  delete topLevelNode;
136 }
137 
138 
139 void RC_TREE_MODEL::rebuildModel( RC_ITEMS_PROVIDER* aProvider, int aSeverities )
140 {
141  wxWindowUpdateLocker updateLock( m_view );
142 
143  // Even with the updateLock, wxWidgets sometimes ties its knickers in
144  // a knot when trying to run a wxdataview_selection_changed_callback()
145  // on a row that has been deleted.
146  if( m_view )
147  m_view->UnselectAll();
148 
149  if( aProvider != m_rcItemsProvider )
150  {
151  delete m_rcItemsProvider;
152  m_rcItemsProvider = aProvider;
153  }
154 
155  if( aSeverities != m_severities )
156  m_severities = aSeverities;
157 
158  if( m_rcItemsProvider )
160 
161  m_tree.clear();
162 
163  for( int i = 0; m_rcItemsProvider && i < m_rcItemsProvider->GetCount(); ++i )
164  {
165  RC_ITEM* drcItem = m_rcItemsProvider->GetItem( i );
166 
167  m_tree.push_back( new RC_TREE_NODE( nullptr, drcItem, RC_TREE_NODE::MARKER ) );
168  RC_TREE_NODE* n = m_tree.back();
169 
170  if( drcItem->GetMainItemID() != niluuid )
171  n->m_Children.push_back( new RC_TREE_NODE( n, drcItem, RC_TREE_NODE::MAIN_ITEM ) );
172 
173  if( drcItem->GetAuxItemID() != niluuid )
174  n->m_Children.push_back( new RC_TREE_NODE( n, drcItem, RC_TREE_NODE::AUX_ITEM ) );
175  }
176 
177  // Must be called after a significant change of items to force the
178  // wxDataViewModel to reread all of them, repopulating itself entirely.
179  Cleared();
180 
181 #ifdef __WXGTK__
182  // The fastest method to update wxDataViewCtrl is to rebuild from
183  // scratch by calling Cleared(). Linux requires to reassociate model to
184  // display data, but Windows will create multiple associations.
185  // On MacOS, this crashes kicad. See https://gitlab.com/kicad/code/kicad/issues/3666
186  // and https://gitlab.com/kicad/code/kicad/issues/3653
187  m_view->AssociateModel( this );
188 #endif
189 
190  m_view->ClearColumns();
191  int width = m_view->GetMainWindow()->GetRect().GetWidth() - WX_DATAVIEW_WINDOW_PADDING;
192  m_view->AppendTextColumn( wxEmptyString, 0, wxDATAVIEW_CELL_INERT, width );
193 
194  ExpandAll();
195 }
196 
197 
199 {
200  rebuildModel( aProvider, m_severities );
201 }
202 
203 
204 void RC_TREE_MODEL::SetSeverities( int aSeverities )
205 {
206  rebuildModel( m_rcItemsProvider, aSeverities );
207 }
208 
209 
211 {
212  for( RC_TREE_NODE* topLevelNode : m_tree )
213  m_view->Expand( ToItem( topLevelNode ) );
214 }
215 
216 
217 bool RC_TREE_MODEL::IsContainer( wxDataViewItem const& aItem ) const
218 {
219  if( ToNode( aItem ) == nullptr ) // must be tree root...
220  return true;
221  else
222  return ToNode( aItem )->m_Type == RC_TREE_NODE::MARKER;
223 }
224 
225 
226 wxDataViewItem RC_TREE_MODEL::GetParent( wxDataViewItem const& aItem ) const
227 {
228  return ToItem( ToNode( aItem)->m_Parent );
229 }
230 
231 
232 unsigned int RC_TREE_MODEL::GetChildren( wxDataViewItem const& aItem,
233  wxDataViewItemArray& aChildren ) const
234 {
235  const RC_TREE_NODE* node = ToNode( aItem );
236  const std::vector<RC_TREE_NODE*>& children = node ? node->m_Children : m_tree;
237 
238  for( const RC_TREE_NODE* child: children )
239  aChildren.push_back( ToItem( child ) );
240 
241  return children.size();
242 }
243 
244 
248 void RC_TREE_MODEL::GetValue( wxVariant& aVariant,
249  wxDataViewItem const& aItem,
250  unsigned int aCol ) const
251 {
252  const RC_TREE_NODE* node = ToNode( aItem );
253  const RC_ITEM* rcItem = node->m_RcItem;
254 
255  switch( node->m_Type )
256  {
258  {
259  bool excluded = rcItem->GetParent() && rcItem->GetParent()->IsExcluded();
260  bool error = m_editFrame->GetSeverity( rcItem->GetErrorCode() ) == RPT_SEVERITY_ERROR;
261  wxString prefix = wxString::Format( wxT( "%s%s" ),
262  excluded ? _( "Excluded " ) : wxString( "" ),
263  error ? _( "Error: " ) : _( "Warning: " ) );
264 
265  aVariant = prefix + rcItem->GetErrorMessage();
266  }
267  break;
268 
270  {
271  EDA_ITEM* item = m_editFrame->GetItem( rcItem->GetMainItemID() );
272 
273  aVariant = item->GetSelectMenuText( m_editFrame->GetUserUnits() );
274  }
275  break;
276 
278  {
279  EDA_ITEM* item = m_editFrame->GetItem( rcItem->GetAuxItemID() );
280 
281  aVariant = item->GetSelectMenuText( m_editFrame->GetUserUnits() );
282  }
283  break;
284  }
285 }
286 
287 
292 bool RC_TREE_MODEL::GetAttr( wxDataViewItem const& aItem,
293  unsigned int aCol,
294  wxDataViewItemAttr& aAttr ) const
295 {
296  const RC_TREE_NODE* node = ToNode( aItem );
297  wxASSERT( node );
298 
299  bool ret = false;
300  bool heading = node->m_Type == RC_TREE_NODE::MARKER;
301 
302  if( heading )
303  {
304  aAttr.SetBold( true );
305  ret = true;
306  }
307 
308  if( node->m_RcItem->GetParent() && node->m_RcItem->GetParent()->IsExcluded() )
309  {
310  wxColour textColour = wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOXTEXT );
311 
312  if( KIGFX::COLOR4D( textColour ).GetBrightness() > 0.5 )
313  aAttr.SetColour( textColour.ChangeLightness( heading ? 30 : 35 ) );
314  else
315  aAttr.SetColour( textColour.ChangeLightness( heading ? 170 : 165 ) );
316 
317  aAttr.SetItalic( true ); // Strikethrough would be better, if wxWidgets supported it
318  ret = true;
319  }
320 
321  return ret;
322 }
323 
324 
326 {
327  if( aNode->m_Type == RC_TREE_NODE::MAIN_ITEM || aNode->m_Type == RC_TREE_NODE::AUX_ITEM )
328  {
329  ValueChanged( aNode->m_Parent );
330  }
331 
332  if( aNode->m_Type == RC_TREE_NODE::MARKER )
333  {
334  wxDataViewModel::ValueChanged( ToItem( aNode ), 0 );
335 
336  for( auto & child : aNode->m_Children )
337  wxDataViewModel::ValueChanged( ToItem( child ), 0 );
338  }
339 }
340 
341 
343 {
344  RC_TREE_NODE* tree_node = ToNode( m_view->GetCurrentItem() );
345  const RC_ITEM* drc_item = tree_node ? tree_node->m_RcItem : nullptr;
346 
347  if( !drc_item )
348  {
349  wxBell();
350  return;
351  }
352 
353  for( int i = 0; i < m_rcItemsProvider->GetCount(); ++i )
354  {
355  if( m_rcItemsProvider->GetItem( i ) == drc_item )
356  {
357  wxDataViewItem markerItem = ToItem( m_tree[i] );
358  wxDataViewItemArray childItems;
359  wxDataViewItem parentItem = ToItem( m_tree[i]->m_Parent );
360 
361  for( RC_TREE_NODE* child : m_tree[i]->m_Children )
362  {
363  childItems.push_back( ToItem( child ) );
364  delete child;
365  }
366 
367  m_tree[i]->m_Children.clear();
368  ItemsDeleted( markerItem, childItems );
369 
370  delete m_tree[i];
371  m_tree.erase( m_tree.begin() + i );
372  ItemDeleted( parentItem, markerItem );
373 
374  m_rcItemsProvider->DeleteItem( i, aDeep );
375  break;
376  }
377  }
378 }
379 
380 
382 {
383  if( m_rcItemsProvider )
384  {
386 
387  m_tree.clear();
388  Cleared();
389  }
390 }
391 
392 
393 void RC_TREE_MODEL::onSizeView( wxSizeEvent& aEvent )
394 {
395  int width = m_view->GetMainWindow()->GetRect().GetWidth() - WX_DATAVIEW_WINDOW_PADDING;
396 
397  if( m_view->GetColumnCount() > 0 )
398  m_view->GetColumn( 0 )->SetWidth( width );
399 
400  // Pass size event to other widgets
401  aEvent.Skip();
402 }
EDA_UNITS
Definition: common.h:196
virtual EDA_ITEM * GetItem(const KIID &aId)
Fetch an item by KIID.
static wxDataViewItem ToItem(RC_TREE_NODE const *aNode)
Definition: rc_item.h:189
void SetProvider(RC_ITEMS_PROVIDER *aProvider)
Definition: rc_item.cpp:198
virtual void DeleteAllItems()=0
Function DeleteAllItems removes and deletes all the items in the list.
Implementation of conversion functions that require both schematic and board internal units.
RC_ITEM is a holder for a DRC (in Pcbnew) or ERC (in Eeschema) error item.
Definition: rc_item.h:77
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:292
RC_TREE_NODE * m_Parent
Definition: rc_item.h:181
KIID m_auxItemUuid
Definition: rc_item.h:84
virtual int GetCount(int aSeverity=-1)=0
static KIID ToUUID(wxDataViewItem aItem)
Definition: rc_item.cpp:91
wxDataViewCtrl * m_view
Definition: rc_item.h:263
#define WX_DATAVIEW_WINDOW_PADDING
Definition: rc_item.cpp:33
The base class for create windows for drawing purpose.
virtual RC_ITEM * GetItem(int aIndex)=0
Function GetItem retrieves a RC_ITEM by index.
void DeleteCurrentItem(bool aDeep)
Definition: rc_item.cpp:342
void SetSeverities(int aSeverities)
Definition: rc_item.cpp:204
Provide an abstract interface of a RC_ITEM* list manager.
Definition: rc_item.h:42
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:248
std::vector< RC_TREE_NODE * > m_Children
Definition: rc_item.h:182
virtual const wxPoint GetPosition() const
Definition: base_struct.h:344
RC_ITEMS_PROVIDER * m_rcItemsProvider
Definition: rc_item.h:265
Definition: common.h:68
virtual wxString ShowReport(EDA_UNITS aUnits, const std::map< KIID, EDA_ITEM * > &aItemMap) const
Function ShowReport translates this object into a text string suitable for saving to disk in a report...
Definition: rc_item.cpp:53
#define NULL
int m_errorCode
Definition: rc_item.h:80
void ExpandAll()
Definition: rc_item.cpp:210
void rebuildModel(RC_ITEMS_PROVIDER *aProvider, int aSeverities)
Definition: rc_item.cpp:139
static RC_TREE_NODE * ToNode(wxDataViewItem aItem)
Definition: rc_item.h:194
static wxString ShowCoord(EDA_UNITS aUnits, const wxPoint &aPos)
Function ShowCoord formats a coordinate or position to text.
Definition: rc_item.cpp:45
KIID niluuid(0)
KIID GetAuxItemID() const
Definition: rc_item.h:124
RC_TREE_MODEL(EDA_DRAW_FRAME *aParentFrame, wxDataViewCtrl *aView)
Definition: rc_item.cpp:118
int GetErrorCode() const
Definition: rc_item.h:138
Functions to provide common constants and other functions to assist in making a consistent UI.
EDA_DRAW_FRAME * m_editFrame
Definition: rc_item.h:262
wxString m_errorMessage
Definition: rc_item.h:81
bool IsExcluded() const
Definition: marker_base.h:108
void onSizeView(wxSizeEvent &aEvent)
Definition: rc_item.cpp:393
virtual int GetSeverity(int aErrorCode) const
virtual void SetSeverities(int aSeverities)=0
unsigned int GetChildren(wxDataViewItem const &aItem, wxDataViewItemArray &aChildren) const override
Definition: rc_item.cpp:232
NODE_TYPE m_Type
Definition: rc_item.h:178
void DeleteAllItems()
Definition: rc_item.cpp:381
virtual wxString GetErrorText(int aCode=-1, bool aTranslate=true) const =0
Function GetErrorText returns the string form of a RC error code.
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aUseMils, EDA_DATA_TYPE aType)
Definition: base_units.cpp:124
std::vector< RC_TREE_NODE * > m_tree
Definition: rc_item.h:267
RC_ITEM * m_RcItem
Definition: rc_item.h:179
int m_severities
Definition: rc_item.h:264
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
virtual wxString GetSelectMenuText(EDA_UNITS aUnits) const
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:163
KIID m_mainItemUuid
Definition: rc_item.h:83
KIID GetMainItemID() const
Definition: rc_item.h:123
MARKER_BASE * GetParent() const
Definition: rc_item.h:127
bool IsContainer(wxDataViewItem const &aItem) const override
Definition: rc_item.cpp:217
virtual const KIID GetUUID() const =0
wxDataViewItem GetParent(wxDataViewItem const &aItem) const override
Definition: rc_item.cpp:226
virtual wxString GetErrorMessage() const
Function GetErrorMessage returns the error message of a RC_ITEM.
Definition: rc_item.cpp:36
virtual void DeleteItem(int aIndex, bool aDeep)=0
Function DeleteItem removes (and optionally deletes) the indexed item from the list.
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void ValueChanged(RC_TREE_NODE *aNode)
Definition: rc_item.cpp:325
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40