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