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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <board.h>
26#include <netinfo.h>
27#include <vector>
28#include <wx/string.h>
30#include <wx/dcmemory.h>
31#include <pcb_edit_frame.h>
32#include <wx/variant.h>
34
35
36wxDEFINE_EVENT( EVT_ZONES_OVERVIEW_COUNT_CHANGE, wxCommandEvent );
37
38
40{
41 std::sort( m_filteredZones.begin(), m_filteredZones.end(),
42 [&]( ZONE* const& l, ZONE* const& r )
43 {
44 return m_zoneSettingsBag.GetZonePriority( l ) > m_zoneSettingsBag.GetZonePriority( r );
45 } );
46}
47
48
50{
51 wxCommandEvent rowCountChange( EVT_ZONES_OVERVIEW_COUNT_CHANGE );
52 rowCountChange.SetInt( GetCount() );
53 wxPostEvent( m_parent, rowCountChange );
54}
55
56
57static wxBitmap MakeBitmapForLayers( LSEQ const& layers, COLOR_SETTINGS const& settings, const wxSize& aSize )
58{
59 wxBitmap bitmap( aSize );
60 wxBrush brush;
61 wxPen pen;
62 wxMemoryDC iconDC;
63
64 iconDC.SelectObject( bitmap );
65 brush.SetStyle( wxBRUSHSTYLE_SOLID );
66 const int layer_cout = layers.size();
67 std::vector<PCB_LAYER_ID> layersToDraw;
68
69 if( layer_cout > 4 )
70 {
71 for( const PCB_LAYER_ID& i : { layers[0], layers[1], layers[layer_cout - 1], layers[layer_cout - 2] } )
72 layersToDraw.push_back( i );
73 }
74 else
75 {
76 layersToDraw = layers;
77 }
78
79 const int step = static_cast<int>( aSize.y / layersToDraw.size() );
80
81 for( size_t i = 0; i < layersToDraw.size(); ++i )
82 {
83 const KIGFX::COLOR4D color = settings.GetColor( layersToDraw[i] );
84 brush.SetColour( color.ToColour() );
85 pen.SetColour( color.ToColour() );
86 iconDC.SetBrush( brush );
87 iconDC.SetPen( pen );
88 iconDC.DrawRectangle( 0, i * step, aSize.x, step );
89 }
90
91 return bitmap;
92}
93
94
96 ZONE_SETTINGS_BAG& aZoneSettingsBag ) :
97 m_parent( aParent ),
98 m_frame( aFrame ),
99 m_zoneSettingsBag( aZoneSettingsBag ),
100 m_sortByName( true ),
101 m_sortByNet( true ),
103{
104 m_filteredZones = m_zoneSettingsBag.GetClonedZoneList();
105 Reset( m_filteredZones.size() );
106}
107
108
109void MODEL_ZONES_OVERVIEW::GetValueByRow( wxVariant& aVariant, unsigned aRow, unsigned aCol ) const
110{
111 if( static_cast<size_t>( aRow ) + 1 > m_filteredZones.size() )
112 return;
113
114 const ZONE& cur = *m_filteredZones[aRow];
115
116 switch( aCol )
117 {
118 case NAME:
119 aVariant = cur.GetZoneName();
120 break;
121
122 case NET:
123 aVariant = cur.GetNet()->GetNetname();
124 break;
125
126 case LAYERS:
127 {
128 wxArrayString layers;
129
130 for( PCB_LAYER_ID layer : cur.GetLayerSet().Seq() )
131 layers.Add( m_frame->GetBoard()->GetLayerName( layer ) );
132
133 aVariant << wxDataViewIconText( wxJoin( layers, ',' ),
134 MakeBitmapForLayers( cur.GetLayerSet().UIOrder(), *m_frame->GetColorSettings(),
135 wxSize( LAYER_BAR_WIDTH, LAYER_BAR_HEIGHT ) ) );
136 break;
137 }
138
139 default:
140 break;
141 }
142}
143
144
146{
147 m_sortByName = aEnable;
148}
149
150
152{
153 m_sortByNet = aEnable;
154}
155
156
161
162
163bool MODEL_ZONES_OVERVIEW::SetValueByRow( const wxVariant& aVariant, unsigned aRow, unsigned aCol )
164{
165 return {};
166}
167
168
170{
171 return m_filteredZones.size();
172}
173
174
175ZONE* MODEL_ZONES_OVERVIEW::GetZone( wxDataViewItem const& aItem ) const
176{
177 if( !aItem.IsOk() )
178 return nullptr;
179
180 unsigned int aRow = GetRow( aItem );
181
182 if( aRow + 1 > GetCount() )
183 return nullptr;
184
185 return m_filteredZones[aRow];
186}
187
188
189wxDataViewItem MODEL_ZONES_OVERVIEW::GetItemByZone( ZONE* aZone ) const
190{
191 if( !aZone )
192 return {};
193
194 for( size_t i = 0; i < m_filteredZones.size(); i++ )
195 {
196 if( m_filteredZones[i] == aZone )
197 return GetItem( i );
198 }
199
200 return {};
201}
202
203
204std::optional<unsigned> MODEL_ZONES_OVERVIEW::MoveZoneIndex( unsigned aIndex, ZONE_INDEX_MOVEMENT aMovement )
205{
206 switch( aMovement )
207 {
209 if( aIndex >= 1 && GetCount() > 1 )
210 return SwapZonePriority( aIndex, aIndex - 1 );
211
212 break;
213
215 if( aIndex + 1 < GetCount() )
216 return SwapZonePriority( aIndex, aIndex + 1 );
217
218 break;
219
221 if( aIndex >= 1 && GetCount() > 1 )
222 {
223 unsigned cur = aIndex;
224
225 while( cur > 0 )
226 {
227 SwapZonePriority( cur, cur - 1 );
228 --cur;
229 }
230
231 return 0u;
232 }
233
234 break;
235
237 if( aIndex + 1 < GetCount() )
238 {
239 unsigned cur = aIndex;
240 const unsigned last = GetCount() - 1;
241
242 while( cur < last )
243 {
244 SwapZonePriority( cur, cur + 1 );
245 ++cur;
246 }
247
248 return last;
249 }
250
251 break;
252 }
253
254 return std::optional<unsigned>{};
255}
256
257
258std::optional<unsigned> MODEL_ZONES_OVERVIEW::SwapZonePriority( unsigned aDragIndex, unsigned aDropIndex )
259{
260 for( const unsigned i : { aDragIndex, aDropIndex } )
261 {
262 if( !( i < GetCount() ) )
263 return {};
264 }
265
266 if( aDragIndex == aDropIndex )
267 return aDragIndex;
268
269 m_zoneSettingsBag.SwapPriority( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
270 std::swap( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
271
272 for( const unsigned int row : { aDragIndex, aDropIndex } )
273 RowChanged( row );
274
275 return aDropIndex;
276}
277
278
279wxDataViewItem MODEL_ZONES_OVERVIEW::ApplyFilter( wxString const& aFilterText, wxDataViewItem aSelection )
280{
281 if( m_zoneSettingsBag.GetClonedZoneList().empty() )
282 return {};
283
284 wxString lowerFilterText = aFilterText.Strip( wxString::both ).Lower();
285
286 if( lowerFilterText.empty() )
287 return ClearFilter( aSelection );
288
289 ZONE* selected_zone = GetZone( aSelection );
290 m_filteredZones.clear();
291
292 for( ZONE* zone : m_zoneSettingsBag.GetClonedZoneList() )
293 {
294 if( m_layerFilter != UNDEFINED_LAYER && !zone->GetLayerSet().Contains( m_layerFilter ) )
295 continue;
296
297 if( ( m_sortByName && zone->GetZoneName().Lower().Contains( lowerFilterText ) )
298 || ( m_sortByNet && zone->GetNetname().Lower().Contains( lowerFilterText ) ) )
299 {
300 m_filteredZones.push_back( zone );
301 }
302 }
303
305 Reset( GetCount() );
307 return GetItemByZone( selected_zone );
308}
309
310
311wxDataViewItem MODEL_ZONES_OVERVIEW::ClearFilter( wxDataViewItem aSelection )
312{
313 if( m_zoneSettingsBag.GetClonedZoneList().empty() )
314 return {};
315
316 ZONE* zone = GetZone( aSelection );
317 m_filteredZones.clear();
318
319 for( ZONE* z : m_zoneSettingsBag.GetClonedZoneList() )
320 {
321 if( m_layerFilter == UNDEFINED_LAYER || z->GetLayerSet().Contains( m_layerFilter ) )
322 m_filteredZones.push_back( z );
323 }
324
326 Reset( GetCount() );
328 return GetItemByZone( zone );
329}
NETINFO_ITEM * GetNet() const
Return #NET_INFO 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:105
wxColour ToColour() const
Definition color4d.cpp:225
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:743
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:313
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:112
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
Handle a list of polygons defining a copper zone.
Definition zone.h:73
const wxString & GetZoneName() const
Definition zone.h:163
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:136
void Reset() override
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ UNDEFINED_LAYER
Definition layer_ids.h:61
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