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>
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],
73 layers[1],
74 layers[layer_cout - 1],
75 layers[layer_cout - 2] } )
76 {
77 layersToDraw.push_back( i );
78 }
79 }
80 else
81 {
82 layersToDraw = layers;
83 }
84
85 const int step = static_cast<int>( aSize.x / layersToDraw.size() );
86
87 for( size_t i = 0; i < layersToDraw.size(); ++i )
88 {
89 const KIGFX::COLOR4D color = settings.GetColor( layersToDraw[i] );
90 brush.SetColour( color.ToColour() );
91 pen.SetColour( color.ToColour() );
92 iconDC.SetBrush( brush );
93 iconDC.SetPen( pen );
94 iconDC.DrawRectangle( 0, i * step, aSize.x, step );
95 }
96
97 return bitmap;
98}
99
100
101MODEL_ZONES_OVERVIEW_TABLE::MODEL_ZONES_OVERVIEW_TABLE( std::vector<std::shared_ptr<MANAGED_ZONE>> aZones,
102 BOARD* a_pcb, PCB_BASE_FRAME* aPCB_FRAME,
103 wxWindow* a_dialog ) :
104 m_allZones( aZones ),
105 m_filteredZones( std::move( aZones ) ),
106 m_pcb( a_pcb ),
107 m_PCB_FRAME( aPCB_FRAME ),
108 m_dialog( a_dialog ),
109 m_sortByName( true ),
110 m_sortByNet( true )
111{
112 Reset( m_filteredZones.size() );
113}
114
115
117
118
119void MODEL_ZONES_OVERVIEW_TABLE::GetValueByRow( wxVariant& aVariant, unsigned aRow,
120 unsigned aCol ) const
121{
122 if( static_cast<size_t>( aRow ) + 1 > m_filteredZones.size() )
123 return;
124
125 const ZONE& cur = m_filteredZones[aRow]->GetZone();
126
127 switch( aCol )
128 {
129 case NAME:
130 aVariant = cur.GetZoneName();
131 break;
132
133 case NET:
134 aVariant = cur.GetNet()->GetNetname();
135 break;
136
137 case LAYERS:
138 {
139 wxArrayString layers;
141
142 for( PCB_LAYER_ID layer : cur.GetLayerSet().Seq() )
143 layers.Add( m_pcb->GetLayerName( layer ) );
144
145 aVariant << wxDataViewIconText( wxJoin( layers, ',' ),
148 bmSize ) );
149 break;
150 }
151
152 default:
153 break;
154 }
155}
156
157
159{
160 m_sortByName = aEnable;
161}
162
163
165{
166 m_sortByNet = aEnable;
167}
168
169
170bool MODEL_ZONES_OVERVIEW_TABLE::SetValueByRow( const wxVariant& aVariant, unsigned aRow,
171 unsigned aCol )
172{
173 return {};
174}
175
176
178{
179 return m_filteredZones.size();
180}
181
182
183ZONE* MODEL_ZONES_OVERVIEW_TABLE::GetZone( wxDataViewItem const& aItem ) const
184{
185 if( !aItem.IsOk() )
186 return nullptr;
187
188 unsigned int aRow = GetRow( aItem );
189
190 if( aRow + 1 > GetCount() )
191 return nullptr;
192
193 return &m_filteredZones[aRow]->GetZone();
194}
195
196
198{
199 if( !aZone )
200 return {};
201
202 for( size_t i = 0; i < m_filteredZones.size(); i++ )
203 {
204 if( &m_filteredZones[i]->GetZone() == aZone )
205 return GetItem( i );
206 }
207
208 return {};
209}
210
211
212std::optional<unsigned> MODEL_ZONES_OVERVIEW_TABLE::MoveZoneIndex( unsigned aIndex,
213 ZONE_INDEX_MOVEMENT aMovement )
214{
215 switch( aMovement )
216 {
217 case ZONE_INDEX_MOVEMENT::MOVE_UP:
218 if( aIndex >= 1 && GetCount() > 1 )
219 return SwapZonePriority( aIndex, aIndex - 1 );
220
221 break;
222
223 case ZONE_INDEX_MOVEMENT::MOVE_DOWN:
224 if( aIndex + 1 < GetCount() )
225 return SwapZonePriority( aIndex, aIndex + 1 );
226
227 break;
228 }
229
230 return std::optional<unsigned>{};
231}
232
233
234std::optional<unsigned> MODEL_ZONES_OVERVIEW_TABLE::SwapZonePriority( unsigned aDragIndex,
235 unsigned aDropIndex )
236{
237 for( const unsigned i : { aDragIndex, aDropIndex } )
238 {
239 if( !( i < GetCount() ) )
240 return {};
241 }
242
243 if( aDragIndex == aDropIndex )
244 return aDragIndex;
245
246 std::swap( m_filteredZones[aDragIndex]->m_currentPriority,
247 m_filteredZones[aDropIndex]->m_currentPriority );
248 std::swap( m_filteredZones[aDragIndex], m_filteredZones[aDropIndex] );
249
250 for( const unsigned int row : { aDragIndex, aDropIndex } )
251 RowChanged( row );
252
253 return aDropIndex;
254}
255
256
257wxDataViewItem MODEL_ZONES_OVERVIEW_TABLE::ApplyFilter( wxString const& aFilterText,
258 wxDataViewItem aSelection )
259{
260 if( !GetAllZonesCount() )
261 return {};
262
263 wxString lowerFilterText = aFilterText.Strip( wxString::both ).Lower();
264
265 if( lowerFilterText.empty() )
266 return ClearFilter( aSelection );
267
268 ZONE* selected_zone = GetZone( aSelection );
269 m_filteredZones.clear();
270
271 for( const auto& container : m_allZones )
272 {
273 const ZONE zone = container->GetZone();
274
275 if( ( m_sortByName && zone.GetZoneName().Lower().Contains( lowerFilterText ) )
276 || ( m_sortByNet && zone.GetNetname().Lower().Contains( lowerFilterText ) ) )
277 {
278 m_filteredZones.push_back( container );
279 }
280 }
281
283 Reset( GetCount() );
285 return GetItemByZone( selected_zone );
286}
287
288
289wxDataViewItem MODEL_ZONES_OVERVIEW_TABLE::ClearFilter( wxDataViewItem aSelection )
290{
291 if( !GetAllZonesCount() )
292 return {};
293
294 ZONE* zone = GetZone( aSelection );
297 Reset( GetCount() );
299 return GetItemByZone( zone );
300}
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:290
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:574
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 Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:420
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
std::vector< std::shared_ptr< MANAGED_ZONE > > m_filteredZones
ZONE * GetZone(wxDataViewItem const &item) const
std::vector< std::shared_ptr< MANAGED_ZONE > > m_allZones
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
MODEL_ZONES_OVERVIEW_TABLE(std::vector< std::shared_ptr< MANAGED_ZONE > > aZones, BOARD *a_pcb, PCB_BASE_FRAME *aPCB_FRAME, wxWindow *a_dialog)
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:73
const wxString & GetZoneName() const
Definition: zone.h:135
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:60
wxDEFINE_EVENT(EVT_ZONES_OVERVIEW_COUNT_CHANGE, wxCommandEvent)
static wxBitmap MakeBitmapForLayers(LSEQ const &layers, COLOR_SETTINGS const &settings, const wxSize &aSize)
STL namespace.