KiCad PCB EDA Suite
Loading...
Searching...
No Matches
model_zones_overview.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) 2023 Ethan Chien <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <board.h>
22#include <netinfo.h>
23#include <vector>
24#include <wx/string.h>
26#include <wx/dcmemory.h>
27#include <pcb_edit_frame.h>
28#include <wx/variant.h>
30
31
32wxDEFINE_EVENT( EVT_ZONES_OVERVIEW_COUNT_CHANGE, wxCommandEvent );
33
34
36{
37 std::sort( m_filteredZones.begin(), m_filteredZones.end(),
38 [&]( ZONE* const& l, ZONE* const& r )
39 {
40 // Reordering only updates the bag, the clones keep their priority until apply
41 unsigned lPriority = m_zoneSettingsBag.GetZonePriority( l );
42 unsigned rPriority = m_zoneSettingsBag.GetZonePriority( r );
43
44 if( lPriority != rPriority )
45 return lPriority > rPriority;
46
47 return l->HigherPriority( r );
48 } );
49}
50
51
53{
54 wxCommandEvent rowCountChange( EVT_ZONES_OVERVIEW_COUNT_CHANGE );
55 rowCountChange.SetInt( GetCount() );
56 wxPostEvent( m_parent, rowCountChange );
57}
58
59
60static wxBitmap MakeBitmapForLayers( LSEQ const& layers, COLOR_SETTINGS const& settings, const wxSize& aSize )
61{
62 wxBitmap bitmap( aSize );
63 wxBrush brush;
64 wxPen pen;
65 wxMemoryDC iconDC;
66
67 iconDC.SelectObject( bitmap );
68 brush.SetStyle( wxBRUSHSTYLE_SOLID );
69 const int layer_cout = layers.size();
70 std::vector<PCB_LAYER_ID> layersToDraw;
71
72 if( layer_cout > 4 )
73 {
74 for( const PCB_LAYER_ID& i : { layers[0], layers[1], layers[layer_cout - 1], layers[layer_cout - 2] } )
75 layersToDraw.push_back( i );
76 }
77 else
78 {
79 layersToDraw = layers;
80 }
81
82 const int step = static_cast<int>( aSize.y / layersToDraw.size() );
83
84 for( size_t i = 0; i < layersToDraw.size(); ++i )
85 {
86 const KIGFX::COLOR4D color = settings.GetColor( layersToDraw[i] );
87 brush.SetColour( color.ToColour() );
88 pen.SetColour( color.ToColour() );
89 iconDC.SetBrush( brush );
90 iconDC.SetPen( pen );
91 iconDC.DrawRectangle( 0, i * step, aSize.x, step );
92 }
93
94 return bitmap;
95}
96
97
99 ZONE_SETTINGS_BAG& aZoneSettingsBag ) :
100 m_parent( aParent ),
101 m_frame( aFrame ),
102 m_zoneSettingsBag( aZoneSettingsBag ),
103 m_sortByName( true ),
104 m_sortByNet( true ),
106{
107 m_filteredZones = m_zoneSettingsBag.GetClonedZoneList();
108 Reset( m_filteredZones.size() );
109}
110
111
112void MODEL_ZONES_OVERVIEW::GetValueByRow( wxVariant& aVariant, unsigned aRow, unsigned aCol ) const
113{
114 if( static_cast<size_t>( aRow ) + 1 > m_filteredZones.size() )
115 return;
116
117 const ZONE& cur = *m_filteredZones[aRow];
118
119 switch( aCol )
120 {
121 case NAME:
122 aVariant = cur.GetZoneName();
123 break;
124
125 case NET:
126 aVariant = cur.GetNet()->GetNetname();
127 break;
128
129 case LAYERS:
130 {
131 wxArrayString layers;
132
133 for( PCB_LAYER_ID layer : cur.GetLayerSet().Seq() )
134 layers.Add( m_frame->GetBoard()->GetLayerName( layer ) );
135
136 aVariant << wxDataViewIconText( wxJoin( layers, ',' ),
137 MakeBitmapForLayers( cur.GetLayerSet().UIOrder(), *m_frame->GetColorSettings(),
138 wxSize( LAYER_BAR_WIDTH, LAYER_BAR_HEIGHT ) ) );
139 break;
140 }
141
142 default:
143 break;
144 }
145}
146
147
149{
150 m_sortByName = aEnable;
151}
152
153
155{
156 m_sortByNet = aEnable;
157}
158
159
164
165
166bool MODEL_ZONES_OVERVIEW::SetValueByRow( const wxVariant& aVariant, unsigned aRow, unsigned aCol )
167{
168 return {};
169}
170
171
173{
174 return m_filteredZones.size();
175}
176
177
178ZONE* MODEL_ZONES_OVERVIEW::GetZone( wxDataViewItem const& aItem ) const
179{
180 if( !aItem.IsOk() )
181 return nullptr;
182
183 unsigned int aRow = GetRow( aItem );
184
185 if( aRow + 1 > GetCount() )
186 return nullptr;
187
188 return m_filteredZones[aRow];
189}
190
191
192wxDataViewItem MODEL_ZONES_OVERVIEW::GetItemByZone( ZONE* aZone ) const
193{
194 if( !aZone )
195 return {};
196
197 for( size_t i = 0; i < m_filteredZones.size(); i++ )
198 {
199 if( m_filteredZones[i] == aZone )
200 return GetItem( i );
201 }
202
203 return {};
204}
205
206
207std::optional<unsigned> MODEL_ZONES_OVERVIEW::MoveZoneIndex( unsigned aIndex, ZONE_INDEX_MOVEMENT aMovement )
208{
209 switch( aMovement )
210 {
212 if( aIndex >= 1 && GetCount() > 1 )
213 return SwapZonePriority( aIndex, aIndex - 1 );
214
215 break;
216
218 if( aIndex + 1 < GetCount() )
219 return SwapZonePriority( aIndex, aIndex + 1 );
220
221 break;
222
224 if( aIndex >= 1 && GetCount() > 1 )
225 {
226 unsigned cur = aIndex;
227
228 while( cur > 0 )
229 {
230 SwapZonePriority( cur, cur - 1 );
231 --cur;
232 }
233
234 return 0u;
235 }
236
237 break;
238
240 if( aIndex + 1 < GetCount() )
241 {
242 unsigned cur = aIndex;
243 const unsigned last = GetCount() - 1;
244
245 while( cur < last )
246 {
247 SwapZonePriority( cur, cur + 1 );
248 ++cur;
249 }
250
251 return last;
252 }
253
254 break;
255 }
256
257 return std::optional<unsigned>{};
258}
259
260
261std::optional<unsigned> MODEL_ZONES_OVERVIEW::SwapZonePriority( unsigned aDragIndex, unsigned aDropIndex )
262{
263 for( const unsigned i : { aDragIndex, aDropIndex } )
264 {
265 if( !( i < GetCount() ) )
266 return {};
267 }
268
269 if( aDragIndex == aDropIndex )
270 return aDragIndex;
271
272 m_zoneSettingsBag.SwapPriority( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
273 std::swap( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
274
275 for( const unsigned int row : { aDragIndex, aDropIndex } )
276 RowChanged( row );
277
278 return aDropIndex;
279}
280
281
282wxDataViewItem MODEL_ZONES_OVERVIEW::ApplyFilter( wxString const& aFilterText, wxDataViewItem aSelection )
283{
284 if( m_zoneSettingsBag.GetClonedZoneList().empty() )
285 return {};
286
287 wxString lowerFilterText = aFilterText.Strip( wxString::both ).Lower();
288
289 if( lowerFilterText.empty() )
290 return ClearFilter( aSelection );
291
292 ZONE* selected_zone = GetZone( aSelection );
293 m_filteredZones.clear();
294
295 for( ZONE* zone : m_zoneSettingsBag.GetClonedZoneList() )
296 {
297 if( m_layerFilter != UNDEFINED_LAYER && !zone->GetLayerSet().Contains( m_layerFilter ) )
298 continue;
299
300 if( ( m_sortByName && zone->GetZoneName().Lower().Contains( lowerFilterText ) )
301 || ( m_sortByNet && zone->GetNetname().Lower().Contains( lowerFilterText ) ) )
302 {
303 m_filteredZones.push_back( zone );
304 }
305 }
306
308 Reset( GetCount() );
310 return GetItemByZone( selected_zone );
311}
312
313
314wxDataViewItem MODEL_ZONES_OVERVIEW::ClearFilter( wxDataViewItem aSelection )
315{
316 if( m_zoneSettingsBag.GetClonedZoneList().empty() )
317 return {};
318
319 ZONE* zone = GetZone( aSelection );
320 m_filteredZones.clear();
321
322 for( ZONE* z : m_zoneSettingsBag.GetClonedZoneList() )
323 {
324 if( m_layerFilter == UNDEFINED_LAYER || z->GetLayerSet().Contains( m_layerFilter ) )
325 m_filteredZones.push_back( z );
326 }
327
329 Reset( GetCount() );
331 return GetItemByZone( zone );
332}
NETINFO_ITEM * GetNet() const
Return NETINFO_ITEM object for a given item.
Color settings are a bit different than most of the settings objects in that there can be more than o...
COLOR4D GetColor(int aLayer) const
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
wxColour ToColour() const
Definition color4d.cpp:221
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
LSEQ UIOrder() const
Return the copper, technical and user layers in the order shown in layer widget.
Definition lset.cpp:739
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:309
bool SetValueByRow(const wxVariant &aVariant, unsigned aRow, unsigned aCol) override
void EnableFitterByNet(bool aEnable)
void GetValueByRow(wxVariant &aVariant, unsigned aRow, unsigned aCol) const override
std::optional< unsigned > SwapZonePriority(unsigned aDragIndex, unsigned aDropIndex)
Swap two zone while drag && drop.
MODEL_ZONES_OVERVIEW(wxWindow *aParent, PCB_BASE_FRAME *aFrame, ZONE_SETTINGS_BAG &aZoneSettingsBag)
wxDataViewItem ClearFilter(wxDataViewItem aSelection)
Clear up the filter.
void SetLayerFilter(PCB_LAYER_ID aLayer)
ZONE * GetZone(wxDataViewItem const &item) const
wxDataViewItem GetItemByZone(ZONE *) const
std::vector< ZONE * > m_filteredZones
std::optional< unsigned > MoveZoneIndex(unsigned aIndex, ZONE_INDEX_MOVEMENT aMovement)
Move selected zone up/down.
void EnableFitterByName(bool aEnable)
unsigned int GetCount() const override
wxDataViewItem ApplyFilter(wxString const &aFilterText, wxDataViewItem aSelection)
Filter the zones by the filter text.
ZONE_SETTINGS_BAG & m_zoneSettingsBag
const wxString & GetNetname() const
Definition netinfo.h:110
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
const wxString & GetZoneName() const
Definition zone.h:160
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:133
void Reset() override
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
wxDEFINE_EVENT(EVT_ZONES_OVERVIEW_COUNT_CHANGE, wxCommandEvent)
static wxBitmap MakeBitmapForLayers(LSEQ const &layers, COLOR_SETTINGS const &settings, const wxSize &aSize)
#define LAYER_BAR_HEIGHT
ZONE_INDEX_MOVEMENT
#define LAYER_BAR_WIDTH