KiCad PCB EDA Suite
Loading...
Searching...
No Matches
diff_tree_grouping.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 The 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 3
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/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 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
26
27#include <wx/intl.h>
28
29#include <map>
30
31
32namespace KICAD_DIFF
33{
35{
36 switch( aKind )
37 {
38 case CHANGE_KIND::ADDED: return _( "Added" );
39 case CHANGE_KIND::REMOVED: return _( "Removed" );
40 case CHANGE_KIND::MODIFIED: return _( "Modified" );
41 case CHANGE_KIND::COLLISION: return _( "Collision" );
42 case CHANGE_KIND::DUPLICATE_UUID: return _( "Duplicate ID" );
43 }
44
45 return wxEmptyString;
46}
47
48
49bool ChangeMatchesSearchFilter( const ITEM_CHANGE& aChange, const wxString& aLowercaseFilter )
50{
51 if( aLowercaseFilter.IsEmpty() )
52 return true;
53
54 if( aChange.typeName.Lower().Contains( aLowercaseFilter )
55 || aChange.typeLabel.Lower().Contains( aLowercaseFilter ) )
56 {
57 return true;
58 }
59
60 if( aChange.refdes.has_value() && aChange.refdes->Lower().Contains( aLowercaseFilter ) )
61 {
62 return true;
63 }
64
65 return false;
66}
67
68
69std::vector<CHANGE_TREE_GROUP> BuildChangeTreeGroups( const DOCUMENT_DIFF& aDiff, const wxString& aSearchFilter,
70 const std::array<bool, CATEGORY_COUNT>& aVisibleCategories )
71{
72 std::vector<CHANGE_TREE_GROUP> out;
73
74 // Bucket changes by kind. The recursive walk flattens nested children
75 // so a footprint-pad edit shows up as its own bucket entry even when
76 // the parent footprint is also being shown.
77 std::map<CHANGE_KIND, std::vector<CHANGE_TREE_ENTRY>> byKind;
78 std::map<std::pair<CHANGE_KIND, wxString>, const ITEM_CHANGE*> netEntries;
79
80 // A library item lists its element edits in its own property list, and the canvas draws items by name
81 const DOC_KIND docKind = DocKindFromDocType( aDiff.docType );
82 const bool listChildren = docKind != DOC_KIND::SYM_LIB && docKind != DOC_KIND::FP_LIB;
83
84 auto collect = [&byKind, &netEntries, listChildren]( auto& aSelf, const ITEM_CHANGE& aC ) -> void
85 {
86 if( IsRoutingNetChange( aC ) )
87 {
88 netEntries.try_emplace( std::make_pair( aC.kind, *aC.refdes ), &aC );
89 }
90 else
91 {
92 byKind[aC.kind].push_back( { &aC, ChangeDisplayLabel( aC ) } );
93 }
94
95 if( listChildren )
96 {
97 for( const ITEM_CHANGE& child : aC.children )
98 aSelf( aSelf, child );
99 }
100 };
101
102 for( const ITEM_CHANGE& c : aDiff.changes )
103 collect( collect, c );
104
105 for( const auto& [key, change] : netEntries )
106 {
107 const auto& [kind, netName] = key;
108 byKind[kind].push_back( { change, wxS( "NET [" ) + netName + wxS( "]" ) } );
109 }
110
111 const wxString lowerFilter = aSearchFilter.Lower();
112
113 for( const auto& [kind, items] : byKind )
114 {
115 const CATEGORY cat = CategoryFor( kind );
116
117 if( !aVisibleCategories[static_cast<std::size_t>( cat )] )
118 continue;
119
120 std::vector<CHANGE_TREE_ENTRY> visibleEntries;
121 visibleEntries.reserve( items.size() );
122
123 for( const CHANGE_TREE_ENTRY& item : items )
124 {
125 if( !ChangeMatchesSearchFilter( *item.change, lowerFilter )
126 && !item.itemLabel.Lower().Contains( lowerFilter ) )
127 {
128 continue;
129 }
130
131 visibleEntries.push_back( item );
132 }
133
134 if( visibleEntries.empty() )
135 continue;
136
137 wxString groupLabel;
138
139 if( lowerFilter.IsEmpty() || visibleEntries.size() == items.size() )
140 groupLabel = wxString::Format( wxS( "%s (%zu)" ), ChangeKindLabel( kind ), visibleEntries.size() );
141 else
142 groupLabel = wxString::Format( wxS( "%s (%zu/%zu)" ), ChangeKindLabel( kind ), visibleEntries.size(),
143 items.size() );
144
145 out.push_back( CHANGE_TREE_GROUP{ kind, std::move( groupLabel ), std::move( visibleEntries ) } );
146 }
147
148 return out;
149}
150
151} // namespace KICAD_DIFF
#define _(s)
CHANGE_KIND
Coarse classification of a single item-level change between two documents.
wxString ChangeDisplayLabel(const ITEM_CHANGE &aChange)
User-facing item label used consistently by scene tooltips and change tree entries.
bool ChangeMatchesSearchFilter(const ITEM_CHANGE &aChange, const wxString &aLowercaseFilter)
Predicate: does this item change match the active search filter?
std::vector< CHANGE_TREE_GROUP > BuildChangeTreeGroups(const DOCUMENT_DIFF &aDiff, const wxString &aSearchFilter, const std::array< bool, CATEGORY_COUNT > &aVisibleCategories)
Group the changes in a DOCUMENT_DIFF by kind, apply category and search filters, and return the resul...
wxString ChangeKindLabel(CHANGE_KIND aKind)
Human-readable label for a CHANGE_KIND (e.g.
DOC_KIND
Document type a diff/merge entry point should route to, derived from a file path's extension.
bool IsRoutingNetChange(const ITEM_CHANGE &aChange)
Presentation predicate for PCB routing changes that should be displayed as one net-level entry/shape.
CATEGORY
Visual category each ITEM_CHANGE belongs to in the scene.
Definition diff_scene.h:52
DOC_KIND DocKindFromDocType(const wxString &aDocType)
Map a DOCUMENT_DIFF::docType tag written by the differs ("kicad_pcb", "kicad_sch",...
CATEGORY CategoryFor(CHANGE_KIND aKind)
Map a CHANGE_KIND to the visual category it belongs to.
One row in a grouped change tree — an ITEM_CHANGE plus its display label (typeName + optional refdes ...
One bucket in a grouped change tree — a CHANGE_KIND, a human- readable group label,...
The full set of changes between two parsed documents of one type.
std::vector< ITEM_CHANGE > changes
One change record on a single item.
std::optional< wxString > refdes
std::vector< ITEM_CHANGE > children