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 )
102{
103 m_filteredZones = m_zoneSettingsBag.GetClonedZoneList();
104 Reset( m_filteredZones.size() );
105}
106
107
108void MODEL_ZONES_OVERVIEW::GetValueByRow( wxVariant& aVariant, unsigned aRow, unsigned aCol ) const
109{
110 if( static_cast<size_t>( aRow ) + 1 > m_filteredZones.size() )
111 return;
112
113 const ZONE& cur = *m_filteredZones[aRow];
114
115 switch( aCol )
116 {
117 case NAME:
118 aVariant = cur.GetZoneName();
119 break;
120
121 case NET:
122 aVariant = cur.GetNet()->GetNetname();
123 break;
124
125 case LAYERS:
126 {
127 wxArrayString layers;
128
129 for( PCB_LAYER_ID layer : cur.GetLayerSet().Seq() )
130 layers.Add( m_frame->GetBoard()->GetLayerName( layer ) );
131
132 aVariant << wxDataViewIconText( wxJoin( layers, ',' ),
133 MakeBitmapForLayers( cur.GetLayerSet().UIOrder(), *m_frame->GetColorSettings(),
134 wxSize( LAYER_BAR_WIDTH, LAYER_BAR_HEIGHT ) ) );
135 break;
136 }
137
138 default:
139 break;
140 }
141}
142
143
145{
146 m_sortByName = aEnable;
147}
148
149
151{
152 m_sortByNet = aEnable;
153}
154
155
156bool MODEL_ZONES_OVERVIEW::SetValueByRow( const wxVariant& aVariant, unsigned aRow, unsigned aCol )
157{
158 return {};
159}
160
161
163{
164 return m_filteredZones.size();
165}
166
167
168ZONE* MODEL_ZONES_OVERVIEW::GetZone( wxDataViewItem const& aItem ) const
169{
170 if( !aItem.IsOk() )
171 return nullptr;
172
173 unsigned int aRow = GetRow( aItem );
174
175 if( aRow + 1 > GetCount() )
176 return nullptr;
177
178 return m_filteredZones[aRow];
179}
180
181
182wxDataViewItem MODEL_ZONES_OVERVIEW::GetItemByZone( ZONE* aZone ) const
183{
184 if( !aZone )
185 return {};
186
187 for( size_t i = 0; i < m_filteredZones.size(); i++ )
188 {
189 if( m_filteredZones[i] == aZone )
190 return GetItem( i );
191 }
192
193 return {};
194}
195
196
197std::optional<unsigned> MODEL_ZONES_OVERVIEW::MoveZoneIndex( unsigned aIndex, ZONE_INDEX_MOVEMENT aMovement )
198{
199 switch( aMovement )
200 {
202 if( aIndex >= 1 && GetCount() > 1 )
203 return SwapZonePriority( aIndex, aIndex - 1 );
204
205 break;
206
208 if( aIndex + 1 < GetCount() )
209 return SwapZonePriority( aIndex, aIndex + 1 );
210
211 break;
212 }
213
214 return std::optional<unsigned>{};
215}
216
217
218std::optional<unsigned> MODEL_ZONES_OVERVIEW::SwapZonePriority( unsigned aDragIndex, unsigned aDropIndex )
219{
220 for( const unsigned i : { aDragIndex, aDropIndex } )
221 {
222 if( !( i < GetCount() ) )
223 return {};
224 }
225
226 if( aDragIndex == aDropIndex )
227 return aDragIndex;
228
229 m_zoneSettingsBag.SwapPriority( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
230 std::swap( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
231
232 for( const unsigned int row : { aDragIndex, aDropIndex } )
233 RowChanged( row );
234
235 return aDropIndex;
236}
237
238
239wxDataViewItem MODEL_ZONES_OVERVIEW::ApplyFilter( wxString const& aFilterText, wxDataViewItem aSelection )
240{
241 if( m_zoneSettingsBag.GetClonedZoneList().empty() )
242 return {};
243
244 wxString lowerFilterText = aFilterText.Strip( wxString::both ).Lower();
245
246 if( lowerFilterText.empty() )
247 return ClearFilter( aSelection );
248
249 ZONE* selected_zone = GetZone( aSelection );
250 m_filteredZones.clear();
251
252 for( ZONE* zone : m_zoneSettingsBag.GetClonedZoneList() )
253 {
254 if( ( m_sortByName && zone->GetZoneName().Lower().Contains( lowerFilterText ) )
255 || ( m_sortByNet && zone->GetNetname().Lower().Contains( lowerFilterText ) ) )
256 {
257 m_filteredZones.push_back( zone );
258 }
259 }
260
262 Reset( GetCount() );
264 return GetItemByZone( selected_zone );
265}
266
267
268wxDataViewItem MODEL_ZONES_OVERVIEW::ClearFilter( wxDataViewItem aSelection )
269{
270 if( m_zoneSettingsBag.GetClonedZoneList().empty() )
271 return {};
272
273 ZONE* zone = GetZone( aSelection );
274 m_filteredZones = m_zoneSettingsBag.GetClonedZoneList();
276 Reset( GetCount() );
278 return GetItemByZone( zone );
279}
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:220
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:726
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:296
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.
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:74
const wxString & GetZoneName() const
Definition zone.h:159
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
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