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#include "managed_zone.h"
36
37wxDEFINE_EVENT( EVT_ZONES_OVERVIEW_COUNT_CHANGE, wxCommandEvent );
38
40{
41 std::sort( m_filteredZones.begin(), m_filteredZones.end(),
42 []( std::shared_ptr<MANAGED_ZONE> const& l, std::shared_ptr<MANAGED_ZONE> const& r )
43 {
44 return l->GetCurrentPriority() > r->GetCurrentPriority();
45 } );
46}
47
48
50{
51 wxCommandEvent rowCountChange( EVT_ZONES_OVERVIEW_COUNT_CHANGE );
52 rowCountChange.SetInt( GetCount() );
53 wxPostEvent( m_dialog, rowCountChange );
54}
55
56
57static wxBitmap MakeBitmapForLayers( LSEQ const& layers, COLOR_SETTINGS const& settings,
58 const wxSize& aSize )
59{
60 wxBitmap bitmap( aSize );
61 wxBrush brush;
62 wxPen pen;
63 wxMemoryDC iconDC;
64
65 iconDC.SelectObject( bitmap );
66 brush.SetStyle( wxBRUSHSTYLE_SOLID );
67 const int layer_cout = layers.size();
68 std::vector<PCB_LAYER_ID> layersToDraw;
69
70 if( layer_cout > 4 )
71 {
72 for( const PCB_LAYER_ID& i : { layers[0], layers[1], layers[layer_cout - 1], layers[layer_cout - 2] } )
73 layersToDraw.push_back( i );
74 }
75 else
76 {
77 layersToDraw = layers;
78 }
79
80 const int step = static_cast<int>( aSize.x / layersToDraw.size() );
81
82 for( size_t i = 0; i < layersToDraw.size(); ++i )
83 {
84 const KIGFX::COLOR4D color = settings.GetColor( layersToDraw[i] );
85 brush.SetColour( color.ToColour() );
86 pen.SetColour( color.ToColour() );
87 iconDC.SetBrush( brush );
88 iconDC.SetPen( pen );
89 iconDC.DrawRectangle( 0, i * step, aSize.x, step );
90 }
91
92 return bitmap;
93}
94
95
96MODEL_ZONES_OVERVIEW::MODEL_ZONES_OVERVIEW( std::vector<std::shared_ptr<MANAGED_ZONE>> aZones,
97 BOARD* a_pcb, PCB_BASE_FRAME* aPCB_FRAME, wxWindow* a_dialog ) :
98 m_allZones( aZones ),
99 m_filteredZones( std::move( aZones ) ),
100 m_pcb( a_pcb ),
101 m_PCB_FRAME( aPCB_FRAME ),
102 m_dialog( a_dialog ),
103 m_sortByName( true ),
104 m_sortByNet( true )
105{
106 Reset( m_filteredZones.size() );
107}
108
109
110void MODEL_ZONES_OVERVIEW::GetValueByRow( wxVariant& aVariant, unsigned aRow, unsigned aCol ) const
111{
112 if( static_cast<size_t>( aRow ) + 1 > m_filteredZones.size() )
113 return;
114
115 const ZONE& cur = m_filteredZones[aRow]->GetZone();
116
117 switch( aCol )
118 {
119 case NAME:
120 aVariant = cur.GetZoneName();
121 break;
122
123 case NET:
124 aVariant = cur.GetNet()->GetNetname();
125 break;
126
127 case LAYERS:
128 {
129 wxArrayString layers;
131
132 for( PCB_LAYER_ID layer : cur.GetLayerSet().Seq() )
133 layers.Add( m_pcb->GetLayerName( layer ) );
134
135 aVariant << wxDataViewIconText( wxJoin( layers, ',' ), MakeBitmapForLayers( cur.GetLayerSet().UIOrder(),
136 *m_PCB_FRAME->GetColorSettings(),
137 bmSize ) );
138 break;
139 }
140
141 default:
142 break;
143 }
144}
145
146
148{
149 m_sortByName = aEnable;
150}
151
152
154{
155 m_sortByNet = aEnable;
156}
157
158
159bool MODEL_ZONES_OVERVIEW::SetValueByRow( const wxVariant& aVariant, unsigned aRow, unsigned aCol )
160{
161 return {};
162}
163
164
166{
167 return m_filteredZones.size();
168}
169
170
171ZONE* MODEL_ZONES_OVERVIEW::GetZone( wxDataViewItem const& aItem ) const
172{
173 if( !aItem.IsOk() )
174 return nullptr;
175
176 unsigned int aRow = GetRow( aItem );
177
178 if( aRow + 1 > GetCount() )
179 return nullptr;
180
181 return &m_filteredZones[aRow]->GetZone();
182}
183
184
185wxDataViewItem MODEL_ZONES_OVERVIEW::GetItemByZone( ZONE* aZone ) const
186{
187 if( !aZone )
188 return {};
189
190 for( size_t i = 0; i < m_filteredZones.size(); i++ )
191 {
192 if( &m_filteredZones[i]->GetZone() == aZone )
193 return GetItem( i );
194 }
195
196 return {};
197}
198
199
200std::optional<unsigned> MODEL_ZONES_OVERVIEW::MoveZoneIndex( unsigned aIndex, ZONE_INDEX_MOVEMENT aMovement )
201{
202 switch( aMovement )
203 {
205 if( aIndex >= 1 && GetCount() > 1 )
206 return SwapZonePriority( aIndex, aIndex - 1 );
207
208 break;
209
211 if( aIndex + 1 < GetCount() )
212 return SwapZonePriority( aIndex, aIndex + 1 );
213
214 break;
215 }
216
217 return std::optional<unsigned>{};
218}
219
220
221std::optional<unsigned> MODEL_ZONES_OVERVIEW::SwapZonePriority( unsigned aDragIndex, unsigned aDropIndex )
222{
223 for( const unsigned i : { aDragIndex, aDropIndex } )
224 {
225 if( !( i < GetCount() ) )
226 return {};
227 }
228
229 if( aDragIndex == aDropIndex )
230 return aDragIndex;
231
232 std::swap( m_filteredZones[aDragIndex]->m_currentPriority, m_filteredZones[aDropIndex]->m_currentPriority );
233 std::swap( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
234
235 for( const unsigned int row : { aDragIndex, aDropIndex } )
236 RowChanged( row );
237
238 return aDropIndex;
239}
240
241
242wxDataViewItem MODEL_ZONES_OVERVIEW::ApplyFilter( wxString const& aFilterText, wxDataViewItem aSelection )
243{
244 if( !GetAllZonesCount() )
245 return {};
246
247 wxString lowerFilterText = aFilterText.Strip( wxString::both ).Lower();
248
249 if( lowerFilterText.empty() )
250 return ClearFilter( aSelection );
251
252 ZONE* selected_zone = GetZone( aSelection );
253 m_filteredZones.clear();
254
255 for( const auto& container : m_allZones )
256 {
257 const ZONE zone = container->GetZone();
258
259 if( ( m_sortByName && zone.GetZoneName().Lower().Contains( lowerFilterText ) )
260 || ( m_sortByNet && zone.GetNetname().Lower().Contains( lowerFilterText ) ) )
261 {
262 m_filteredZones.push_back( container );
263 }
264 }
265
267 Reset( GetCount() );
269 return GetItemByZone( selected_zone );
270}
271
272
273wxDataViewItem MODEL_ZONES_OVERVIEW::ClearFilter( wxDataViewItem aSelection )
274{
275 if( !GetAllZonesCount() )
276 return {};
277
278 ZONE* zone = GetZone( aSelection );
281 Reset( GetCount() );
283 return GetItemByZone( zone );
284}
int color
NETINFO_ITEM * GetNet() const
Return #NET_INFO object for a given item.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:317
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:104
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.
unsigned int GetAllZonesCount() const
wxDataViewItem ClearFilter(wxDataViewItem aSelection)
Clear up the filter.
ZONE * GetZone(wxDataViewItem const &item) const
MODEL_ZONES_OVERVIEW(std::vector< std::shared_ptr< MANAGED_ZONE > > aZones, BOARD *a_pcb, PCB_BASE_FRAME *aPCB_FRAME, wxWindow *a_dialog)
wxDataViewItem GetItemByZone(ZONE *) const
std::vector< std::shared_ptr< MANAGED_ZONE > > m_allZones
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.
std::vector< std::shared_ptr< MANAGED_ZONE > > m_filteredZones
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: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
wxDEFINE_EVENT(EVT_ZONES_OVERVIEW_COUNT_CHANGE, wxCommandEvent)
static wxBitmap MakeBitmapForLayers(LSEQ const &layers, COLOR_SETTINGS const &settings, const wxSize &aSize)
ZONE_INDEX_MOVEMENT
#define LAYER_BAR_WIDTH
STL namespace.