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_btnMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
63 m_btnMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
64
65 m_panelZoneProperties = new PANEL_ZONE_PROPERTIES( m_zonePanel, aParent, m_zoneSettingsBag );
66 m_sizerProperties->Add( m_panelZoneProperties, 0, wxEXPAND, 5 );
67
68 m_zonePreviewNotebook = new ZONE_PREVIEW_NOTEBOOK( m_zonePanel, aParent );
69 m_sizerPreview->Add( m_zonePreviewNotebook, 1, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 5 );
70
71 for( const auto& [k, v] : MODEL_ZONES_OVERVIEW::GetColumnNames() )
72 {
74 m_viewZonesOverview->AppendIconTextColumn( v, k, wxDATAVIEW_CELL_INERT, 140 );
75 else
76 m_viewZonesOverview->AppendTextColumn( v, k, wxDATAVIEW_CELL_INERT, 160 );
77 }
78
79 m_modelZonesOverview = new MODEL_ZONES_OVERVIEW( this, m_pcbFrame, m_zoneSettingsBag );
80 m_viewZonesOverview->AssociateModel( m_modelZonesOverview.get() );
81 m_viewZonesOverview->SetLayoutDirection( wxLayout_LeftToRight );
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
110
112
113
115{
116 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(), m_viewZonesOverview->GetSelection() );
117
118 if( m_modelZonesOverview->GetCount() )
120
121 return true;
122}
123
124
125void DIALOG_ZONE_MANAGER::PostProcessZoneViewSelChange( wxDataViewItem const& aItem )
126{
127 bool textCtrlHasFocus = m_filterCtrl->HasFocus();
128 long filterInsertPos = m_filterCtrl->GetInsertionPoint();
129
130 if( aItem.IsOk() )
131 {
132 m_viewZonesOverview->Select( aItem );
133 m_viewZonesOverview->EnsureVisible( aItem );
134 }
135 else
136 {
137 if( m_modelZonesOverview->GetCount() )
138 {
139 wxDataViewItem first_item = m_modelZonesOverview->GetItem( 0 );
140 m_viewZonesOverview->Select( first_item );
141 m_viewZonesOverview->EnsureVisible( first_item );
142 m_zonePreviewNotebook->OnZoneSelectionChanged( m_modelZonesOverview->GetZone( first_item ) );
143 }
144 else
145 {
146 m_zonePreviewNotebook->OnZoneSelectionChanged( nullptr );
147 }
148 }
149
150 if( textCtrlHasFocus )
151 {
152 m_filterCtrl->SetFocus();
153 m_filterCtrl->SetInsertionPoint( filterInsertPos );
154 }
155}
156
157
159{
160 aEvent.Skip();
161
162 if( aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
163 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
164}
165
166
167void DIALOG_ZONE_MANAGER::OnTableChar( wxKeyEvent& aEvent )
168{
169 GenericProcessChar( aEvent );
170}
171
172
173void DIALOG_ZONE_MANAGER::OnTableCharHook( wxKeyEvent& aEvent )
174{
175 GenericProcessChar( aEvent );
176}
177
178
179void DIALOG_ZONE_MANAGER::OnIdle( wxIdleEvent& aEvent )
180{
181 WXUNUSED( aEvent )
182 m_viewZonesOverview->SetFocus();
183 Unbind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
184}
185
186
187void DIALOG_ZONE_MANAGER::onDialogResize( wxSizeEvent& event )
188{
189 event.Skip();
190}
191
192
194{
195 wxWindowUpdateLocker updateLock( this );
196
197 m_panelZoneProperties->SetZone( zone );
198 m_zonePreviewNotebook->OnZoneSelectionChanged( zone );
199
200 Layout();
201}
202
203
205{
206 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
207}
208
209
211{
212 SelectZoneTableItem( aEvent.GetItem() );
213}
214
215
216void DIALOG_ZONE_MANAGER::SelectZoneTableItem( wxDataViewItem const& aItem )
217{
218 ZONE* zone = m_modelZonesOverview->GetZone( aItem );
219
220 if( !zone )
221 return;
222
224}
225
226
227void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
228{
229 m_panelZoneProperties->TransferZoneSettingsFromWindow();
230
231 m_zoneSettingsBag.UpdateClonedZones();
232
233 for( const auto& [ zone, zoneClone ] : m_zoneSettingsBag.GetZonesCloneMap() )
234 {
235 std::map<PCB_LAYER_ID, std::shared_ptr<SHAPE_POLY_SET>> filled_zone_to_restore;
236 ZONE* internal_zone = zone; // Duplicate the zone pointer to allow capture on older MacOS (13)
237
238 zone->GetLayerSet().RunOnLayers(
239 [&]( PCB_LAYER_ID layer )
240 {
241 std::shared_ptr<SHAPE_POLY_SET> fill = internal_zone->GetFilledPolysList( layer );
242
243 if( fill )
244 filled_zone_to_restore[layer] = fill;
245 } );
246
247 *zone = *zoneClone;
248
249 for( const auto& [ layer, fill ] : filled_zone_to_restore )
250 zone->SetFilledPolysList( layer, *fill );
251 }
252
253 aEvt.Skip();
254}
255
256
257#if wxUSE_DRAG_AND_DROP
258
259void DIALOG_ZONE_MANAGER::OnBeginDrag( wxDataViewEvent& aEvent )
260{
261 wxTextDataObject* obj = new wxTextDataObject;
262 obj->SetText( "42" ); //FIXME - Workaround for drop on GTK
263 aEvent.SetDataObject( obj );
264 aEvent.SetDragFlags( wxDrag_AllowMove );
265 const wxDataViewItem it = aEvent.GetItem();
266
267 if( it.IsOk() )
269}
270
271
272void DIALOG_ZONE_MANAGER::OnDropPossible( wxDataViewEvent& aEvent )
273{
274 aEvent.SetDropEffect( wxDragMove ); // check 'move' drop effect
275}
276
277
278void DIALOG_ZONE_MANAGER::OnDrop( wxDataViewEvent& aEvent )
279{
280 if( aEvent.GetDataFormat() != wxDF_UNICODETEXT )
281 {
282 aEvent.Veto();
283 return;
284 }
285
286 if( !m_priorityDragIndex.has_value() )
287 return;
288
289 const wxDataViewItem it = aEvent.GetItem();
290
291 if( !it.IsOk() )
292 {
293 aEvent.Veto();
294 return;
295 }
296
297 unsigned int drop_index = m_modelZonesOverview->GetRow( it );
298 const std::optional<unsigned> rtn = m_modelZonesOverview->SwapZonePriority( *m_priorityDragIndex, drop_index );
299
300 if( rtn.has_value() )
301 {
302 const wxDataViewItem item = m_modelZonesOverview->GetItem( *rtn );
303
304 if( item.IsOk() )
305 m_viewZonesOverview->Select( item );
306 }
307}
308
309#endif // wxUSE_DRAG_AND_DROP
310
311
316
317
322
323
324void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
325{
327 aEvent.Skip();
328}
329
330
331void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
332{
333 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
334 m_viewZonesOverview->GetSelection() ) );
335 aEvent.Skip();
336}
337
338
340{
341 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
342 m_viewZonesOverview->GetSelection() ) );
343 aEvent.Skip();
344}
345
346
347void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
348{
349 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
350 m_viewZonesOverview->GetSelection() ) );
351 aEvent.Skip();
352}
353
354
356{
357 if( m_isFillingZones )
358 return;
359
360 m_isFillingZones = true;
361
362 if( !m_panelZoneProperties->TransferZoneSettingsFromWindow() )
363 {
364 m_isFillingZones = false;
365 return;
366 }
367
368 m_zoneSettingsBag.UpdateClonedZones();
369
370 BOARD* board = m_pcbFrame->GetBoard();
371 board->IncrementTimeStamp();
372
373 // Save the original zones before swapping so we can restore them later
374 ZONES originalZones = board->Zones();
375
376 // Do not use a commit here since we're operating on cloned zones that are not owned by the
377 // board. Using a commit would create undo entries pointing to the clones, which would cause
378 // corruption when the commit is destroyed.
379 m_filler = std::make_unique<ZONE_FILLER>( board, nullptr );
380 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5, PR_CAN_ABORT );
381 m_filler->SetProgressReporter( reporter.get() );
382
383 // TODO: replace these const_cast calls with a different solution that avoids mutating the
384 // container of the board. This is relatively safe as-is because the original zones list is
385 // swapped back in below, but still should be changed to avoid invalidating the board state
386 // in case this code is refactored to be a non-modal dialog in the future.
387 const_cast<ZONES&>( board->Zones() ) = m_zoneSettingsBag.GetClonedZoneList();
388
389 m_zoneFillComplete = m_filler->Fill( board->Zones() );
390 board->BuildConnectivity();
391
392 m_zonePreviewNotebook->OnZoneSelectionChanged( m_panelZoneProperties->GetZone() );
393
394 // Restore the original zones. The connectivity MUST be rebuilt to remove stale pointers to
395 // cloned zones in case of a cancel.
396 const_cast<ZONES&>( board->Zones() ) = originalZones;
397 board->BuildConnectivity();
398
399 m_isFillingZones = false;
400}
401
402
403void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
404{
405 if( ZONE* zone = m_panelZoneProperties->GetZone() )
406 m_modelZonesOverview->RowChanged( m_modelZonesOverview->GetRow( m_modelZonesOverview->GetItemByZone( zone ) ) );
407}
408
409
410void DIALOG_ZONE_MANAGER::OnZoneNetUpdate( wxCommandEvent& aEvent )
411{
412 if( ZONE* zone = m_panelZoneProperties->GetZone() )
413 m_modelZonesOverview->RowChanged( m_modelZonesOverview->GetRow( m_modelZonesOverview->GetItemByZone( zone ) ) );
414}
415
416
418{
419 unsigned count = aEvent.GetInt();
420
422 btn->Enable( count == m_zoneSettingsBag.GetClonedZoneList().size() );
423}
424
425
426void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
427{
428 const wxObject* sender = aEvent.GetEventObject();
429
430 if( aEvent.GetEventObject() == m_checkName )
431 m_modelZonesOverview->EnableFitterByName( aEvent.IsChecked() );
432 else if( aEvent.GetEventObject() == m_checkNet )
433 m_modelZonesOverview->EnableFitterByNet( aEvent.IsChecked() );
434
435 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
436 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(), m_viewZonesOverview->GetSelection() );
437}
438
439
441{
442 if( !m_viewZonesOverview->HasSelection() )
443 return;
444
445 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
446
447 if( !selectedItem.IsOk() )
448 return;
449
450 const unsigned int selectedRow = m_modelZonesOverview->GetRow( selectedItem );
451 const std::optional<unsigned> new_index = m_modelZonesOverview->MoveZoneIndex( selectedRow, aMove );
452
453 if( new_index.has_value() )
454 {
455 wxDataViewItem new_item = m_modelZonesOverview->GetItem( *new_index );
457 }
458}
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:192
void IncrementTimeStamp()
Definition board.cpp:260
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
std::shared_ptr< SHAPE_POLY_SET > GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:607
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:137
#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