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_needZoomGAL( true ),
56 m_isFillingZones( false ),
57 m_zoneFillComplete( false )
58{
59#ifdef __APPLE__
60 m_sizerZoneOP->InsertSpacer( m_sizerZoneOP->GetItemCount(), 5 );
61#endif
62
63 m_btnMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
64 m_btnMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
65
66 m_panelZoneProperties = new PANEL_ZONE_PROPERTIES( m_zonePanel, aParent, m_zoneSettingsBag );
67 m_sizerProperties->Add( m_panelZoneProperties, 0, wxEXPAND, 5 );
68
69 m_zonePreviewNotebook = new ZONE_PREVIEW_NOTEBOOK( m_zonePanel, aParent );
70 m_sizerPreview->Add( m_zonePreviewNotebook, 1, wxALL | wxEXPAND, 5 );
71
72 for( const auto& [k, v] : MODEL_ZONES_OVERVIEW::GetColumnNames() )
73 {
75 m_viewZonesOverview->AppendIconTextColumn( v, k, wxDATAVIEW_CELL_INERT, 140 );
76 else
77 m_viewZonesOverview->AppendTextColumn( v, k, wxDATAVIEW_CELL_INERT, 160 );
78 }
79
80 m_modelZonesOverview = new MODEL_ZONES_OVERVIEW( this, m_pcbFrame, m_zoneSettingsBag );
81 m_viewZonesOverview->AssociateModel( m_modelZonesOverview.get() );
82
83#if wxUSE_DRAG_AND_DROP
84 m_viewZonesOverview->EnableDragSource( wxDF_UNICODETEXT );
85 m_viewZonesOverview->EnableDropTarget( wxDF_UNICODETEXT );
86
87 int id = m_viewZonesOverview->GetId();
88 Bind( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, &DIALOG_ZONE_MANAGER::OnBeginDrag, this, id );
89 Bind( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, &DIALOG_ZONE_MANAGER::OnDropPossible, this, id );
90 Bind( wxEVT_DATAVIEW_ITEM_DROP, &DIALOG_ZONE_MANAGER::OnDrop, this, id );
91#endif // wxUSE_DRAG_AND_DROP
92
93 Bind( EVT_ZONE_NAME_UPDATE, &DIALOG_ZONE_MANAGER::OnZoneNameUpdate, this );
94 Bind( EVT_ZONE_NET_UPDATE, &DIALOG_ZONE_MANAGER::OnZoneNetUpdate, this );
95 Bind( EVT_ZONES_OVERVIEW_COUNT_CHANGE, &DIALOG_ZONE_MANAGER::OnZonesTableRowCountChange, this );
96 Bind( wxEVT_CHECKBOX, &DIALOG_ZONE_MANAGER::OnCheckBoxClicked, this );
97 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
98 Bind( wxEVT_BOOKCTRL_PAGE_CHANGED,
99 [this]( wxNotebookEvent& aEvent )
100 {
101 Layout();
102 },
103 m_zonePreviewNotebook->GetId() );
104
105 Layout();
106 m_MainBoxSizer->Fit( this );
107 finishDialogSettings();
108
109 //NOTE - Works on Windows and MacOS , need further handling in IDLE on Ubuntu
110 FitCanvasToScreen();
111}
112
113
115
116
118{
119 m_zonePreviewNotebook->FitCanvasToScreen();
120}
121
122
124{
125 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(), m_viewZonesOverview->GetSelection() );
126
127 if( m_modelZonesOverview->GetCount() )
129
130 return true;
131}
132
133
134void DIALOG_ZONE_MANAGER::PostProcessZoneViewSelChange( wxDataViewItem const& aItem )
135{
136 bool textCtrlHasFocus = m_filterCtrl->HasFocus();
137 long filterInsertPos = m_filterCtrl->GetInsertionPoint();
138
139 if( aItem.IsOk() )
140 {
141 m_viewZonesOverview->Select( aItem );
142 m_viewZonesOverview->EnsureVisible( aItem );
143 }
144 else
145 {
146 if( m_modelZonesOverview->GetCount() )
147 {
148 wxDataViewItem first_item = m_modelZonesOverview->GetItem( 0 );
149 m_viewZonesOverview->Select( first_item );
150 m_viewZonesOverview->EnsureVisible( first_item );
151 m_zonePreviewNotebook->OnZoneSelectionChanged( m_modelZonesOverview->GetZone( first_item ) );
152 }
153 else
154 {
155 m_zonePreviewNotebook->OnZoneSelectionChanged( nullptr );
156 }
157 }
158
159 if( textCtrlHasFocus )
160 {
161 m_filterCtrl->SetFocus();
162 m_filterCtrl->SetInsertionPoint( filterInsertPos );
163 }
164}
165
166
168{
169 aEvent.Skip();
170
171 if( aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
172 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
173}
174
175
176void DIALOG_ZONE_MANAGER::OnTableChar( wxKeyEvent& aEvent )
177{
178 GenericProcessChar( aEvent );
179}
180
181
182void DIALOG_ZONE_MANAGER::OnTableCharHook( wxKeyEvent& aEvent )
183{
184 GenericProcessChar( aEvent );
185}
186
187
188void DIALOG_ZONE_MANAGER::OnIdle( wxIdleEvent& aEvent )
189{
190 WXUNUSED( aEvent )
191 m_viewZonesOverview->SetFocus();
192 Unbind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
193
194 if( !m_needZoomGAL )
195 return;
196
197 m_needZoomGAL = false;
199}
200
201
202void DIALOG_ZONE_MANAGER::onDialogResize( wxSizeEvent& event )
203{
204 event.Skip();
206}
207
208
210{
211 wxWindowUpdateLocker updateLock( this );
212
213 m_panelZoneProperties->SetZone( zone );
214 m_zonePreviewNotebook->OnZoneSelectionChanged( zone );
215
216 Layout();
217}
218
219
221{
222 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
223}
224
225
227{
228 SelectZoneTableItem( aEvent.GetItem() );
229}
230
231
232void DIALOG_ZONE_MANAGER::SelectZoneTableItem( wxDataViewItem const& aItem )
233{
234 ZONE* zone = m_modelZonesOverview->GetZone( aItem );
235
236 if( !zone )
237 return;
238
240}
241
242
243void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
244{
245 m_panelZoneProperties->TransferZoneSettingsFromWindow();
246
247 m_zoneSettingsBag.UpdateClonedZones();
248
249 for( const auto& [ zone, zoneClone ] : m_zoneSettingsBag.GetZonesCloneMap() )
250 {
251 std::map<PCB_LAYER_ID, std::shared_ptr<SHAPE_POLY_SET>> filled_zone_to_restore;
252 ZONE* internal_zone = zone; // Duplicate the zone pointer to allow capture on older MacOS (13)
253
254 zone->GetLayerSet().RunOnLayers(
255 [&]( PCB_LAYER_ID layer )
256 {
257 std::shared_ptr<SHAPE_POLY_SET> fill = internal_zone->GetFilledPolysList( layer );
258
259 if( fill )
260 filled_zone_to_restore[layer] = fill;
261 } );
262
263 *zone = *zoneClone;
264
265 for( const auto& [ layer, fill ] : filled_zone_to_restore )
266 zone->SetFilledPolysList( layer, *fill );
267 }
268
269 aEvt.Skip();
270}
271
272
273#if wxUSE_DRAG_AND_DROP
274
275void DIALOG_ZONE_MANAGER::OnBeginDrag( wxDataViewEvent& aEvent )
276{
277 wxTextDataObject* obj = new wxTextDataObject;
278 obj->SetText( "42" ); //FIXME - Workaround for drop on GTK
279 aEvent.SetDataObject( obj );
280 aEvent.SetDragFlags( wxDrag_AllowMove );
281 const wxDataViewItem it = aEvent.GetItem();
282
283 if( it.IsOk() )
285}
286
287
288void DIALOG_ZONE_MANAGER::OnDropPossible( wxDataViewEvent& aEvent )
289{
290 aEvent.SetDropEffect( wxDragMove ); // check 'move' drop effect
291}
292
293
294void DIALOG_ZONE_MANAGER::OnDrop( wxDataViewEvent& aEvent )
295{
296 if( aEvent.GetDataFormat() != wxDF_UNICODETEXT )
297 {
298 aEvent.Veto();
299 return;
300 }
301
302 if( !m_priorityDragIndex.has_value() )
303 return;
304
305 const wxDataViewItem it = aEvent.GetItem();
306
307 if( !it.IsOk() )
308 {
309 aEvent.Veto();
310 return;
311 }
312
313 unsigned int drop_index = m_modelZonesOverview->GetRow( it );
314 const std::optional<unsigned> rtn = m_modelZonesOverview->SwapZonePriority( *m_priorityDragIndex, drop_index );
315
316 if( rtn.has_value() )
317 {
318 const wxDataViewItem item = m_modelZonesOverview->GetItem( *rtn );
319
320 if( item.IsOk() )
321 m_viewZonesOverview->Select( item );
322 }
323}
324
325#endif // wxUSE_DRAG_AND_DROP
326
327
332
333
338
339
340void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
341{
343 aEvent.Skip();
344}
345
346
347void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
348{
349 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
350 m_viewZonesOverview->GetSelection() ) );
351 aEvent.Skip();
352}
353
354
356{
357 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
358 m_viewZonesOverview->GetSelection() ) );
359 aEvent.Skip();
360}
361
362
363void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
364{
365 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
366 m_viewZonesOverview->GetSelection() ) );
367 aEvent.Skip();
368}
369
370
372{
373 if( m_isFillingZones )
374 return;
375
376 m_isFillingZones = true;
377 m_panelZoneProperties->TransferZoneSettingsFromWindow();
378 m_zoneSettingsBag.UpdateClonedZones();
379
380 BOARD* board = m_pcbFrame->GetBoard();
381 board->IncrementTimeStamp();
382
383 auto commit = std::make_unique<BOARD_COMMIT>( m_pcbFrame );
384 m_filler = std::make_unique<ZONE_FILLER>( board, commit.get() );
385 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5, PR_CAN_ABORT );
386 m_filler->SetProgressReporter( reporter.get() );
387
388 // TODO: replace these const_cast calls with a different solution that avoids mutating the
389 // container of the board. This is relatively safe as-is because the original zones list is
390 // swapped back in below, but still should be changed to avoid invalidating the board state
391 // in case this code is refactored to be a non-modal dialog in the future.
392 const_cast<ZONES&>( board->Zones() ) = m_zoneSettingsBag.GetClonedZoneList();
393
394 //NOTE - Nether revert nor commit is needed here , cause the cloned zones are not owned by
395 // the pcb frame.
396 m_zoneFillComplete = m_filler->Fill( board->Zones() );
397 board->BuildConnectivity();
398
399 m_zonePreviewNotebook->OnZoneSelectionChanged( m_panelZoneProperties->GetZone() );
400
401 //NOTE - The connectivity MUST be rebuilt to remove stale pointers to cloned zones in case of
402 // a cancel.
403 const_cast<ZONES&>( board->Zones() ) = m_zoneSettingsBag.GetOriginalZoneList();
404 board->BuildConnectivity();
405
406 m_isFillingZones = false;
407}
408
409
410void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
411{
412 if( ZONE* zone = m_panelZoneProperties->GetZone() )
413 m_modelZonesOverview->RowChanged( m_modelZonesOverview->GetRow( m_modelZonesOverview->GetItemByZone( zone ) ) );
414}
415
416
417void DIALOG_ZONE_MANAGER::OnZoneNetUpdate( wxCommandEvent& aEvent )
418{
419 if( ZONE* zone = m_panelZoneProperties->GetZone() )
420 m_modelZonesOverview->RowChanged( m_modelZonesOverview->GetRow( m_modelZonesOverview->GetItemByZone( zone ) ) );
421}
422
423
425{
426 unsigned count = aEvent.GetInt();
427
429 btn->Enable( count == m_zoneSettingsBag.GetClonedZoneList().size() );
430}
431
432
433void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
434{
435 const wxObject* sender = aEvent.GetEventObject();
436
437 if( aEvent.GetEventObject() == m_checkName )
438 m_modelZonesOverview->EnableFitterByName( aEvent.IsChecked() );
439 else if( aEvent.GetEventObject() == m_checkNet )
440 m_modelZonesOverview->EnableFitterByNet( aEvent.IsChecked() );
441
442 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
443 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(), m_viewZonesOverview->GetSelection() );
444}
445
446
448{
449 if( !m_viewZonesOverview->HasSelection() )
450 return;
451
452 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
453
454 if( !selectedItem.IsOk() )
455 return;
456
457 const unsigned int selectedRow = m_modelZonesOverview->GetRow( selectedItem );
458 const std::optional<unsigned> new_index = m_modelZonesOverview->MoveZoneIndex( selectedRow, aMove );
459
460 if( new_index.has_value() )
461 {
462 wxDataViewItem new_item = m_modelZonesOverview->GetItem( *new_index );
464 }
465}
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
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 OnTableChar(wxKeyEvent &event) override
void onDialogResize(wxSizeEvent &event) override
void OnMoveUpClick(wxCommandEvent &aEvent) override
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
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 OnZoneSelectionChanged(ZONE *aZone)
DIALOG_ZONE_MANAGER(PCB_BASE_FRAME *aParent)
void OnZonesTableRowCountChange(wxCommandEvent &aEvent)
void OnFilterCtrlSearch(wxCommandEvent &aEvent) override
void RunOnLayers(const std::function< void(PCB_LAYER_ID)> &aFunction) const
Execute a function on each layer of the LSET.
Definition lset.h:252
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:74
const std::shared_ptr< SHAPE_POLY_SET > & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:596
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
ZONE_INDEX_MOVEMENT
std::vector< ZONE * > ZONES
BOARD * GetBoard()
#define PR_CAN_ABORT