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