KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_zone_manager.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 <memory>
26#include <optional>
27#include <wx/dataview.h>
28#include <wx/debug.h>
29#include <wx/event.h>
30#include <wx/gdicmn.h>
31#include <wx/wupdlock.h>
32#include <pcb_edit_frame.h>
33#include <wx/string.h>
34#include <board_commit.h>
37#include <zone.h>
38#include <zone_settings_bag.h>
39#include <board.h>
40#include <bitmaps.h>
41#include <string_utils.h>
42#include <zone_filler.h>
43
47#include "dialog_zone_manager.h"
48
49
51 DIALOG_ZONE_MANAGER_BASE( aParent ),
52 m_pcbFrame( aParent ),
53 m_zoneSettingsBag( aParent->GetBoard() ),
55 m_isFillingZones( false ),
56 m_zoneFillComplete( false )
57{
58#ifdef __APPLE__
59 m_sizerZoneOP->InsertSpacer( m_sizerZoneOP->GetItemCount(), 5 );
60#endif
61
62 m_btnMoveTop->SetBitmap( KiBitmapBundle( BITMAPS::small_top ) );
63 m_btnMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
64 m_btnMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
65 m_btnMoveBottom->SetBitmap( KiBitmapBundle( BITMAPS::small_bottom ) );
66
67 m_panelZoneProperties = new PANEL_ZONE_PROPERTIES( m_zonePanel, aParent, m_zoneSettingsBag );
68 m_sizerProperties->Add( m_panelZoneProperties, 0, wxEXPAND, 5 );
69
70 m_zonePreviewNotebook = new ZONE_PREVIEW_NOTEBOOK( m_zonePanel, aParent );
71 m_sizerPreview->Add( m_zonePreviewNotebook, 1, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 5 );
72
73 for( const auto& [k, v] : MODEL_ZONES_OVERVIEW::GetColumnNames() )
74 {
76 m_viewZonesOverview->AppendIconTextColumn( v, k, wxDATAVIEW_CELL_INERT, 140 );
77 else
78 m_viewZonesOverview->AppendTextColumn( v, k, wxDATAVIEW_CELL_INERT, 160 );
79 }
80
81 m_modelZonesOverview = new MODEL_ZONES_OVERVIEW( this, m_pcbFrame, m_zoneSettingsBag );
82 m_viewZonesOverview->AssociateModel( m_modelZonesOverview.get() );
83 m_viewZonesOverview->SetLayoutDirection( wxLayout_LeftToRight );
84
85#if wxUSE_DRAG_AND_DROP
86 m_viewZonesOverview->EnableDragSource( wxDF_UNICODETEXT );
87 m_viewZonesOverview->EnableDropTarget( wxDF_UNICODETEXT );
88
89 int id = m_viewZonesOverview->GetId();
90 Bind( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, &DIALOG_ZONE_MANAGER::OnBeginDrag, this, id );
91 Bind( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, &DIALOG_ZONE_MANAGER::OnDropPossible, this, id );
92 Bind( wxEVT_DATAVIEW_ITEM_DROP, &DIALOG_ZONE_MANAGER::OnDrop, this, id );
93#endif // wxUSE_DRAG_AND_DROP
94
95 Bind( EVT_ZONE_NAME_UPDATE, &DIALOG_ZONE_MANAGER::OnZoneNameUpdate, this );
96 Bind( EVT_ZONE_NET_UPDATE, &DIALOG_ZONE_MANAGER::OnZoneNetUpdate, this );
97 Bind( EVT_ZONES_OVERVIEW_COUNT_CHANGE, &DIALOG_ZONE_MANAGER::OnZonesTableRowCountChange, this );
98 Bind( wxEVT_CHECKBOX, &DIALOG_ZONE_MANAGER::OnCheckBoxClicked, this );
99 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
100 Bind( wxEVT_CHAR_HOOK, &DIALOG_ZONE_MANAGER::OnDialogCharHook, this );
101 Bind( wxEVT_BOOKCTRL_PAGE_CHANGED,
102 [this]( wxNotebookEvent& aEvent )
103 {
104 Layout();
105 },
106 m_zonePreviewNotebook->GetId() );
107
108 Layout();
109 m_MainBoxSizer->Fit( this );
110 finishDialogSettings();
111}
112
113
115
116
118{
119 m_layerFilter->Clear();
120 m_layerFilter->Append( _( "All Layers" ) );
121
122 LSET usedLayers;
123 BOARD* board = m_pcbFrame->GetBoard();
124
125 for( ZONE* zone : m_zoneSettingsBag.GetClonedZoneList() )
126 usedLayers |= zone->GetLayerSet();
127
128 for( PCB_LAYER_ID layer : usedLayers.Seq() )
129 {
130 m_layerFilter->Append( board->GetLayerName( layer ),
131 reinterpret_cast<void*>( static_cast<intptr_t>( layer ) ) );
132 }
133
134 m_modelZonesOverview->SetLayerFilter( UNDEFINED_LAYER );
135 m_layerFilter->SetSelection( 0 );
136
137 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(), m_viewZonesOverview->GetSelection() );
138
139 if( m_modelZonesOverview->GetCount() )
141
142 return true;
143}
144
145
146void DIALOG_ZONE_MANAGER::PostProcessZoneViewSelChange( wxDataViewItem const& aItem )
147{
148 bool textCtrlHasFocus = m_filterCtrl->HasFocus();
149 long filterInsertPos = m_filterCtrl->GetInsertionPoint();
150
151 if( aItem.IsOk() )
152 {
153 m_viewZonesOverview->Select( aItem );
154 m_viewZonesOverview->EnsureVisible( aItem );
155 SelectZoneTableItem( aItem );
156 }
157 else
158 {
159 if( m_modelZonesOverview->GetCount() )
160 {
161 wxDataViewItem first_item = m_modelZonesOverview->GetItem( 0 );
162 m_viewZonesOverview->Select( first_item );
163 m_viewZonesOverview->EnsureVisible( first_item );
164 m_zonePreviewNotebook->OnZoneSelectionChanged( m_modelZonesOverview->GetZone( first_item ) );
165 }
166 else
167 {
168 m_zonePreviewNotebook->OnZoneSelectionChanged( nullptr );
169 }
170 }
171
172 if( textCtrlHasFocus )
173 {
174 m_filterCtrl->SetFocus();
175 m_filterCtrl->SetInsertionPoint( filterInsertPos );
176 }
177}
178
179
181{
182 aEvent.Skip();
183}
184
185
187{
188 if( aEvent.GetKeyCode() == WXK_UP )
189 {
191 }
192 else if( aEvent.GetKeyCode() == WXK_DOWN )
193 {
195 }
196 else
197 {
198 aEvent.Skip();
199 }
200}
201
202
204{
205 unsigned count = m_modelZonesOverview->GetCount();
206
207 if( count == 0 )
208 return;
209
210 wxDataViewItem current = m_viewZonesOverview->GetSelection();
211 unsigned currentRow = 0;
212
213 if( current.IsOk() )
214 currentRow = m_modelZonesOverview->GetRow( current );
215
216 int newRow = (int) currentRow + aDirection;
217 newRow = std::max( 0, std::min( newRow, (int) count - 1 ) );
218
219 if( !current.IsOk() || (unsigned) newRow != currentRow )
220 PostProcessZoneViewSelChange( m_modelZonesOverview->GetItem( (unsigned) newRow ) );
221}
222
223
224void DIALOG_ZONE_MANAGER::OnTableChar( wxKeyEvent& aEvent )
225{
226 GenericProcessChar( aEvent );
227}
228
229
230void DIALOG_ZONE_MANAGER::OnTableCharHook( wxKeyEvent& aEvent )
231{
232 GenericProcessChar( aEvent );
233}
234
235
236void DIALOG_ZONE_MANAGER::OnIdle( wxIdleEvent& aEvent )
237{
238 WXUNUSED( aEvent )
239 m_viewZonesOverview->SetFocus();
240 Unbind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
241}
242
243
244void DIALOG_ZONE_MANAGER::onDialogResize( wxSizeEvent& event )
245{
246 event.Skip();
247}
248
249
251{
252 wxWindowUpdateLocker updateLock( this );
253
254 m_panelZoneProperties->SetZone( zone );
255 m_zonePreviewNotebook->OnZoneSelectionChanged( zone );
256
257 Layout();
258}
259
260
262{
263 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
264}
265
266
268{
269 SelectZoneTableItem( aEvent.GetItem() );
270}
271
272
273void DIALOG_ZONE_MANAGER::SelectZoneTableItem( wxDataViewItem const& aItem )
274{
275 ZONE* zone = m_modelZonesOverview->GetZone( aItem );
276
277 if( !zone )
278 return;
279
281}
282
283
284void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
285{
286 m_panelZoneProperties->TransferZoneSettingsFromWindow();
287
288 m_zoneSettingsBag.UpdateClonedZones();
289
290 for( const auto& [ zone, zoneClone ] : m_zoneSettingsBag.GetZonesCloneMap() )
291 {
292 std::map<PCB_LAYER_ID, std::shared_ptr<SHAPE_POLY_SET>> filled_zone_to_restore;
293 ZONE* internal_zone = zone; // Duplicate the zone pointer to allow capture on older MacOS (13)
294
295 zone->GetLayerSet().RunOnLayers(
296 [&]( PCB_LAYER_ID layer )
297 {
298 std::shared_ptr<SHAPE_POLY_SET> fill = internal_zone->GetFilledPolysList( layer );
299
300 if( fill )
301 filled_zone_to_restore[layer] = fill;
302 } );
303
304 *zone = *zoneClone;
305
306 for( const auto& [ layer, fill ] : filled_zone_to_restore )
307 zone->SetFilledPolysList( layer, *fill );
308 }
309
310 aEvt.Skip();
311}
312
313
314#if wxUSE_DRAG_AND_DROP
315
316void DIALOG_ZONE_MANAGER::OnBeginDrag( wxDataViewEvent& aEvent )
317{
318 wxTextDataObject* obj = new wxTextDataObject;
319 obj->SetText( "42" ); //FIXME - Workaround for drop on GTK
320 aEvent.SetDataObject( obj );
321 aEvent.SetDragFlags( wxDrag_AllowMove );
322 const wxDataViewItem it = aEvent.GetItem();
323
324 if( it.IsOk() )
326}
327
328
329void DIALOG_ZONE_MANAGER::OnDropPossible( wxDataViewEvent& aEvent )
330{
331 aEvent.SetDropEffect( wxDragMove ); // check 'move' drop effect
332}
333
334
335void DIALOG_ZONE_MANAGER::OnDrop( wxDataViewEvent& aEvent )
336{
337 if( aEvent.GetDataFormat() != wxDF_UNICODETEXT )
338 {
339 aEvent.Veto();
340 return;
341 }
342
343 if( !m_priorityDragIndex.has_value() )
344 return;
345
346 const wxDataViewItem it = aEvent.GetItem();
347
348 if( !it.IsOk() )
349 {
350 aEvent.Veto();
351 return;
352 }
353
354 unsigned int drop_index = m_modelZonesOverview->GetRow( it );
355 const std::optional<unsigned> rtn = m_modelZonesOverview->SwapZonePriority( *m_priorityDragIndex, drop_index );
356
357 if( rtn.has_value() )
358 {
359 const wxDataViewItem item = m_modelZonesOverview->GetItem( *rtn );
360
361 if( item.IsOk() )
362 m_viewZonesOverview->Select( item );
363 }
364}
365
366#endif // wxUSE_DRAG_AND_DROP
367
368
373
374
379
380
385
386
391
392
393void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
394{
396 aEvent.Skip();
397}
398
399
400void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
401{
402 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
403 m_viewZonesOverview->GetSelection() ) );
404 aEvent.Skip();
405}
406
407
409{
410 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
411 m_viewZonesOverview->GetSelection() ) );
412 aEvent.Skip();
413}
414
415
416void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
417{
418 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
419 m_viewZonesOverview->GetSelection() ) );
420 aEvent.Skip();
421}
422
423
424void DIALOG_ZONE_MANAGER::OnLayerFilterChanged( wxCommandEvent& aEvent )
425{
426 int sel = m_layerFilter->GetSelection();
427
428 if( sel <= 0 )
429 {
430 m_modelZonesOverview->SetLayerFilter( UNDEFINED_LAYER );
431 }
432 else
433 {
434 void* data = m_layerFilter->GetClientData( sel );
435 PCB_LAYER_ID layer = static_cast<PCB_LAYER_ID>( reinterpret_cast<intptr_t>( data ) );
436 m_modelZonesOverview->SetLayerFilter( layer );
437 }
438
440 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(),
441 m_viewZonesOverview->GetSelection() ) );
442}
443
444
446{
447 if( m_isFillingZones )
448 return;
449
450 m_isFillingZones = true;
451
452 if( !m_panelZoneProperties->TransferZoneSettingsFromWindow() )
453 {
454 m_isFillingZones = false;
455 return;
456 }
457
458 m_zoneSettingsBag.UpdateClonedZones();
459
460 BOARD* board = m_pcbFrame->GetBoard();
461 board->IncrementTimeStamp();
462
463 // Save the original zones before swapping so we can restore them later
464 ZONES originalZones = board->Zones();
465
466 // Do not use a commit here since we're operating on cloned zones that are not owned by the
467 // board. Using a commit would create undo entries pointing to the clones, which would cause
468 // corruption when the commit is destroyed.
469 m_filler = std::make_unique<ZONE_FILLER>( board, nullptr );
470 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5, PR_CAN_ABORT );
471 m_filler->SetProgressReporter( reporter.get() );
472
473 // TODO: replace these const_cast calls with a different solution that avoids mutating the
474 // container of the board. This is relatively safe as-is because the original zones list is
475 // swapped back in below, but still should be changed to avoid invalidating the board state
476 // in case this code is refactored to be a non-modal dialog in the future.
477 const_cast<ZONES&>( board->Zones() ) = m_zoneSettingsBag.GetClonedZoneList();
478
479 m_zoneFillComplete = m_filler->Fill( board->Zones() );
480 board->BuildConnectivity();
481
482 m_zonePreviewNotebook->OnZoneSelectionChanged( m_panelZoneProperties->GetZone() );
483
484 // Restore the original zones. The connectivity MUST be rebuilt to remove stale pointers to
485 // cloned zones in case of a cancel.
486 const_cast<ZONES&>( board->Zones() ) = originalZones;
487 board->BuildConnectivity();
488
489 m_isFillingZones = false;
490}
491
492
493void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
494{
495 if( ZONE* zone = m_panelZoneProperties->GetZone() )
496 m_modelZonesOverview->RowChanged( m_modelZonesOverview->GetRow( m_modelZonesOverview->GetItemByZone( zone ) ) );
497}
498
499
500void DIALOG_ZONE_MANAGER::OnZoneNetUpdate( wxCommandEvent& aEvent )
501{
502 if( ZONE* zone = m_panelZoneProperties->GetZone() )
503 m_modelZonesOverview->RowChanged( m_modelZonesOverview->GetRow( m_modelZonesOverview->GetItemByZone( zone ) ) );
504}
505
506
508{
509 unsigned count = aEvent.GetInt();
510
512 btn->Enable( count > 1 );
513}
514
515
516void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
517{
518 const wxObject* sender = aEvent.GetEventObject();
519
520 if( aEvent.GetEventObject() == m_checkName )
521 m_modelZonesOverview->EnableFitterByName( aEvent.IsChecked() );
522 else if( aEvent.GetEventObject() == m_checkNet )
523 m_modelZonesOverview->EnableFitterByNet( aEvent.IsChecked() );
524
525 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
526 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(), m_viewZonesOverview->GetSelection() );
527}
528
529
531{
532 if( !m_viewZonesOverview->HasSelection() )
533 return;
534
535 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
536
537 if( !selectedItem.IsOk() )
538 return;
539
540 const unsigned int selectedRow = m_modelZonesOverview->GetRow( selectedItem );
541 const std::optional<unsigned> new_index = m_modelZonesOverview->MoveZoneIndex( selectedRow, aMove );
542
543 if( new_index.has_value() )
544 {
545 wxDataViewItem new_item = m_modelZonesOverview->GetItem( *new_index );
547 }
548}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
const ZONES & Zones() const
Definition board.h:367
bool BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
Definition board.cpp:191
void IncrementTimeStamp()
Definition board.cpp:259
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition board.cpp:729
DIALOG_ZONE_MANAGER_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Zone Manager"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void OnZoneNameUpdate(wxCommandEvent &aEvent)
void OnViewZonesOverviewOnLeftUp(wxMouseEvent &aEvent) override
~DIALOG_ZONE_MANAGER() override
void GenericProcessChar(wxKeyEvent &event)
PCB_BASE_FRAME * m_pcbFrame
wxObjectDataPtr< MODEL_ZONES_OVERVIEW > m_modelZonesOverview
PANEL_ZONE_PROPERTIES * m_panelZoneProperties
void OnMoveDownClick(wxCommandEvent &aEvent) override
void PostProcessZoneViewSelChange(wxDataViewItem const &aItem)
std::unique_ptr< ZONE_FILLER > m_filler
void OnMoveTopClick(wxCommandEvent &aEvent) override
void OnTableChar(wxKeyEvent &event) override
void onDialogResize(wxSizeEvent &event) override
void OnMoveUpClick(wxCommandEvent &aEvent) override
void OnDialogCharHook(wxKeyEvent &aEvent)
void OnUpdateDisplayedZonesClick(wxCommandEvent &aEvent) override
void MoveSelectedZonePriority(ZONE_INDEX_MOVEMENT aMove)
ZONE_SETTINGS_BAG m_zoneSettingsBag
void OnDataViewCtrlSelectionChanged(wxDataViewEvent &event) override
std::optional< unsigned > m_priorityDragIndex
void OnTableCharHook(wxKeyEvent &event) override
void OnIdle(wxIdleEvent &aEvent)
void OnZoneNetUpdate(wxCommandEvent &aEvent)
void OnCheckBoxClicked(wxCommandEvent &aEvent)
void OnFilterCtrlTextChange(wxCommandEvent &aEvent) override
void NavigateZoneSelection(int aDirection)
bool TransferDataToWindow() override
ZONE_PREVIEW_NOTEBOOK * m_zonePreviewNotebook
void SelectZoneTableItem(wxDataViewItem const &aItem)
void OnOk(wxCommandEvent &aEvt) override
void OnFilterCtrlCancel(wxCommandEvent &aEvent) override
void OnFilterCtrlEnter(wxCommandEvent &aEvent) override
void OnMoveBottomClick(wxCommandEvent &aEvent) override
void OnZoneSelectionChanged(ZONE *aZone)
DIALOG_ZONE_MANAGER(PCB_BASE_FRAME *aParent)
void OnZonesTableRowCountChange(wxCommandEvent &aEvent)
void OnLayerFilterChanged(wxCommandEvent &aEvent) override
void OnFilterCtrlSearch(wxCommandEvent &aEvent) override
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
void RunOnLayers(const std::function< void(PCB_LAYER_ID)> &aFunction) const
Execute a function on each layer of the LSET.
Definition lset.h:263
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:313
static std::map< int, wxString > GetColumnNames()
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
A bitmap button widget that behaves like a standard dialog button except with an icon.
Handle a list of polygons defining a copper zone.
Definition zone.h:73
std::shared_ptr< SHAPE_POLY_SET > GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:606
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:136
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ UNDEFINED_LAYER
Definition layer_ids.h:61
ZONE_INDEX_MOVEMENT
std::vector< ZONE * > ZONES
BOARD * GetBoard()
#define PR_CAN_ABORT