KiCad PCB EDA Suite
Loading...
Searching...
No Matches
model_zones_overview_table.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 (C) 2023 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>
36
37wxDEFINE_EVENT( EVT_ZONES_OVERVIEW_COUNT_CHANGE, wxCommandEvent );
38
40{
41 std::sort( m_filteredZoneContainers.begin(), m_filteredZoneContainers.end(),
42 []( std::shared_ptr<ZONE_PRIORITY_CONTAINER> const& l,
43 std::shared_ptr<ZONE_PRIORITY_CONTAINER> const& r
44
45 )
46 {
47 return l->GetCurrentPriority() > r->GetCurrentPriority();
48 } );
49}
50
52{
53 wxCommandEvent rowCountChange( EVT_ZONES_OVERVIEW_COUNT_CHANGE );
54 rowCountChange.SetInt( GetCount() );
55 wxPostEvent( m_dialog, rowCountChange );
56}
57
58static wxBitmap MakeBitmapForLayers( LSEQ const& layers, COLOR_SETTINGS const& settings,
59 const wxSize& aSize )
60{
61 wxBitmap bitmap( aSize );
62 wxBrush brush;
63 wxPen pen;
64 wxMemoryDC iconDC;
65
66 iconDC.SelectObject( bitmap );
67 brush.SetStyle( wxBRUSHSTYLE_SOLID );
68 const int layer_cout = layers.size();
69 std::vector<PCB_LAYER_ID> layersToDraw;
70
71 if( layer_cout > 4 )
72 {
73 for( const PCB_LAYER_ID& i :
74 { 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.x / 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 BOARD* a_pcb, PCB_BASE_FRAME* aPCB_FRAME,
100 wxWindow* a_dialog ) :
101 m_allZoneContainers( aZones ),
102 m_filteredZoneContainers( std::move( aZones ) ), m_pcb( a_pcb ), m_PCB_FRAME( aPCB_FRAME ),
103 m_dialog( a_dialog ), m_sortByName( true ), m_sortByNet( true )
104{
106}
107
109
110void MODEL_ZONES_OVERVIEW_TABLE::GetValueByRow( wxVariant& aVariant, unsigned aRow,
111 unsigned aCol ) const
112{
113 if( static_cast<size_t>( aRow ) + 1 > m_filteredZoneContainers.size() )
114 return;
115
116 const ZONE& cur = m_filteredZoneContainers[aRow]->GetZone();
117
118 switch( aCol )
119 {
120 case NAME: aVariant = cur.GetZoneName(); break;
121 case NET: aVariant = cur.GetNet()->GetNetname(); break;
122 case LAYERS:
123 {
124 wxArrayString layers;
125
126 for( PCB_LAYER_ID layer : cur.GetLayerSet().Seq() )
127 layers.Add( m_pcb->GetLayerName( layer ) );
128
129 aVariant << wxDataViewIconText(
130 wxJoin( layers, ',' ),
133 { LAYER_BAR_WIDTH, ZONE_MANAGER_PREFERENCE::LAYER_ICON_SIZE::HEIGHT } ) );
134 }
135 break;
136 default: break;
137 }
138}
139
141{
142 m_sortByName = aEnable;
143}
144
146{
147 m_sortByNet = aEnable;
148}
149
150bool MODEL_ZONES_OVERVIEW_TABLE::SetValueByRow( const wxVariant& aVariant, unsigned aRow,
151 unsigned aCol )
152{
153 WXUNUSED( aVariant )
154 WXUNUSED( aRow )
155 WXUNUSED( aCol )
156 return {};
157}
158
160{
161 return m_filteredZoneContainers.size();
162}
163
164ZONE* MODEL_ZONES_OVERVIEW_TABLE::GetZone( wxDataViewItem const& aItem ) const
165{
166 if( !aItem.IsOk() )
167 return {};
168
169 unsigned int aRow = GetRow( aItem );
170
171 if( aRow + 1 > GetCount() )
172 return {};
173
174 return &m_filteredZoneContainers[aRow]->GetZone();
175}
176
178{
179 if( !aZone )
180 return {};
181
182 for( size_t i = 0; i < m_filteredZoneContainers.size(); i++ )
183 {
184 if( &m_filteredZoneContainers[i]->GetZone() == aZone )
185 return GetItem( i );
186 }
187
188 return {};
189}
190
191
192std::optional<unsigned> MODEL_ZONES_OVERVIEW_TABLE::MoveZoneIndex( unsigned aIndex,
193 ZONE_INDEX_MOVEMENT aMovement )
194{
195 switch( aMovement )
196 {
197 case ZONE_INDEX_MOVEMENT::MOVE_UP:
198 {
199 return aIndex >= 1 && GetCount() > 1 ? SwapZonePriority( aIndex, aIndex - 1 )
200 : std::optional<unsigned>{};
201 }
202 case ZONE_INDEX_MOVEMENT::MOVE_DOWN:
203 {
204 return aIndex + 1 < GetCount() ? SwapZonePriority( aIndex, aIndex + 1 )
205 : std::optional<unsigned>{};
206 }
207 }
208 return {};
209}
210
211std::optional<unsigned> MODEL_ZONES_OVERVIEW_TABLE::SwapZonePriority( unsigned aDragIndex,
212 unsigned aDropIndex )
213{
214 for( const unsigned i : { aDragIndex, aDropIndex } )
215 {
216 if( !( i < GetCount() ) )
217 return {};
218 }
219
220 if( aDragIndex == aDropIndex )
221 return aDragIndex;
222
223 std::swap( m_filteredZoneContainers[aDragIndex]->m_currentPriority,
224 m_filteredZoneContainers[aDropIndex]->m_currentPriority );
225 std::swap( m_filteredZoneContainers[aDragIndex], m_filteredZoneContainers[aDropIndex] );
226
227 for( const unsigned int row : { aDragIndex, aDropIndex } )
228 RowChanged( row );
229
230 return aDropIndex;
231}
232
233
234wxDataViewItem MODEL_ZONES_OVERVIEW_TABLE::ApplyFilter( wxString const& aFilterText,
235 wxDataViewItem aSelection )
236{
237 if( !GetAllZonesCount() )
238 return {};
239
240 wxString lowerFilterText = aFilterText.Strip( wxString::both ).Lower();
241
242 if( lowerFilterText.empty() )
243 return ClearFilter( aSelection );
244
245 ZONE* selected_zone = GetZone( aSelection );
247
248 for( const auto& container : m_allZoneContainers )
249 {
250 const ZONE zone = container->GetZone();
251
252 if( ( m_sortByName && zone.GetZoneName().Lower().Contains( lowerFilterText ) )
253 || ( m_sortByNet && zone.GetNetname().Lower().Contains( lowerFilterText ) ) )
254 {
255 m_filteredZoneContainers.push_back( container );
256 }
257 }
259 Reset( GetCount() );
261 return GetItemByZone( selected_zone );
262}
263
264wxDataViewItem MODEL_ZONES_OVERVIEW_TABLE::ClearFilter( wxDataViewItem aSelection )
265{
266 if( !GetAllZonesCount() )
267 return {};
268
269 ZONE* zone = GetZone( aSelection );
272 Reset( GetCount() );
274 return GetItemByZone( zone );
275}
int color
Definition: DXF_plotter.cpp:58
NETINFO_ITEM * GetNet() const
Return #NET_INFO object for a given item.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:281
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:567
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: layer_ids.h:520
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:418
bool SetValueByRow(const wxVariant &aVariant, unsigned aRow, unsigned aCol) override
std::optional< unsigned > MoveZoneIndex(unsigned aIndex, ZONE_INDEX_MOVEMENT aMovement)
Move selected zone up/down.
~MODEL_ZONES_OVERVIEW_TABLE() override
wxDataViewItem ApplyFilter(wxString const &aFilterText, wxDataViewItem aSelection)
Filter the zones by the filter text.
void GetValueByRow(wxVariant &aVariant, unsigned aRow, unsigned aCol) const override
ZONE * GetZone(wxDataViewItem const &item) const
MODEL_ZONES_OVERVIEW_TABLE(ZONE_PRIORITY_CONTAINER_LIST aZones, BOARD *a_pcb, PCB_BASE_FRAME *aPCB_FRAME, wxWindow *a_dialog)
ZONE_PRIORITY_CONTAINER_LIST m_allZoneContainers
ZONE_PRIORITY_CONTAINER_LIST m_filteredZoneContainers
std::optional< unsigned > SwapZonePriority(unsigned aDragIndex, unsigned aDropIndex)
Swap two zone while drag && drop.
wxDataViewItem GetItemByZone(ZONE *) const
wxDataViewItem ClearFilter(wxDataViewItem aSelection)
Clear up the filter.
unsigned int GetCount() const override
const wxString & GetNetname() const
Definition: netinfo.h:114
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
virtual COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Helper to retrieve the current color settings.
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
const wxString & GetZoneName() const
Definition: zone.h:131
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:129
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)
std::vector< std::shared_ptr< ZONE_PRIORITY_CONTAINER > > ZONE_PRIORITY_CONTAINER_LIST
STL namespace.