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 (C) 2023, 2024 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 <kiface_base.h>
32#include <pcb_edit_frame.h>
33#include <pcbnew_settings.h>
34#include <wx/string.h>
35#include <board_commit.h>
37#include <zone.h>
38#include <pad.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"
53#include "zones_container.h"
54#include "pane_zone_viewer.h"
56
57
59 DIALOG_ZONE_MANAGER_BASE( aParent ),
60 m_pcbFrame( aParent ),
61 m_zoneInfo( aZoneInfo ),
62 m_zonesContainer( std::make_unique<ZONES_CONTAINER>( aParent->GetBoard() ) ),
63 m_priorityDragIndex( {} ),
64 m_needZoomGAL( true ),
65 m_isFillingZones( false ),
66 m_zoneFillComplete( false )
67{
68#ifdef __APPLE__
69 m_sizerZoneOP->InsertSpacer( m_sizerZoneOP->GetItemCount(), 5 );
70#endif
71
72 m_btnMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
73 m_btnMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
74
75 m_panelZoneProperties = new PANEL_ZONE_PROPERTIES( this, aParent, *m_zonesContainer );
76 m_sizerProperties->Add( m_panelZoneProperties, 1, wxTOP | wxEXPAND, 5 );
77
78 m_zoneViewer = new PANE_ZONE_VIEWER( this, aParent );
79 m_sizerTop->Add( m_zoneViewer, 1, wxBOTTOM | wxLEFT | wxEXPAND, 10 );
80
81 m_checkRepour->SetValue( ZONE_MANAGER_PREFERENCE::GetRepourOnClose() );
82 //m_zoneViewer->SetId( ZONE_VIEWER );
83
84 for( const auto& [k, v] : MODEL_ZONES_OVERVIEW_TABLE::GetColumnNames() )
85 {
87 m_viewZonesOverview->AppendIconTextColumn( v, k );
88 else
89 m_viewZonesOverview->AppendTextColumn( v, k );
90 }
91
92 m_modelZoneOverviewTable = new MODEL_ZONES_OVERVIEW_TABLE( m_zonesContainer->GetManagedZones(),
93 aParent->GetBoard(), aParent, this );
94 m_viewZonesOverview->AssociateModel( m_modelZoneOverviewTable.get() );
95
96#if wxUSE_DRAG_AND_DROP
97 m_viewZonesOverview->EnableDragSource( wxDF_UNICODETEXT );
98 m_viewZonesOverview->EnableDropTarget( wxDF_UNICODETEXT );
99
100 int id = m_viewZonesOverview->GetId();
101 Bind( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, &DIALOG_ZONE_MANAGER::OnBeginDrag, this, id );
102 Bind( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, &DIALOG_ZONE_MANAGER::OnDropPossible, this, id );
103 Bind( wxEVT_DATAVIEW_ITEM_DROP, &DIALOG_ZONE_MANAGER::OnDrop, this, id );
104#endif // wxUSE_DRAG_AND_DROP
105
106 Bind( EVT_ZONE_NAME_UPDATE, &DIALOG_ZONE_MANAGER::OnZoneNameUpdate, this );
107 Bind( EVT_ZONES_OVERVIEW_COUNT_CHANGE, &DIALOG_ZONE_MANAGER::OnZonesTableRowCountChange, this );
108 Bind( wxEVT_CHECKBOX, &DIALOG_ZONE_MANAGER::OnCheckBoxClicked, this );
109 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
110 Bind( wxEVT_BOOKCTRL_PAGE_CHANGED,
111 [this]( wxNotebookEvent& aEvent )
112 {
113 Layout();
114 },
115 m_zoneViewer->GetId() );
116
117 if( m_modelZoneOverviewTable->GetCount() )
118 SelectZoneTableItem( m_modelZoneOverviewTable->GetItem( 0 ) );
119
120 Layout();
121 m_MainBoxSizer->Fit( this );
122 finishDialogSettings();
123
124 //NOTE - Works on Windows and MacOS , need further handling in IDLE on Ubuntu
125 FitCanvasToScreen();
126}
127
128
130
131
133{
134 if( PANEL_ZONE_GAL* canvas = m_zoneViewer->GetZoneGAL() )
135 canvas->ZoomFitScreen();
136}
137
138
140{
141 bool textCtrlHasFocus = m_filterCtrl->HasFocus();
142 long filterInsertPos = m_filterCtrl->GetInsertionPoint();
143
144 if( aItem.IsOk() )
145 {
146 m_viewZonesOverview->Select( aItem );
147 m_viewZonesOverview->EnsureVisible( aItem );
148 }
149 else
150 {
151 if( m_modelZoneOverviewTable->GetCount() )
152 {
153 wxDataViewItem first_item = m_modelZoneOverviewTable->GetItem( 0 );
154 m_viewZonesOverview->Select( first_item );
155 m_viewZonesOverview->EnsureVisible( first_item );
157 }
158 else
159 {
161 }
162 }
163
164 if( textCtrlHasFocus )
165 {
166 m_filterCtrl->SetFocus();
167 m_filterCtrl->SetInsertionPoint( filterInsertPos );
168 }
169}
170
171
173{
174 aEvent.Skip();
175
176 if( aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
177 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
178}
179
180
181void DIALOG_ZONE_MANAGER::OnTableChar( wxKeyEvent& aEvent )
182{
183 GenericProcessChar( aEvent );
184}
185
186
187void DIALOG_ZONE_MANAGER::OnTableCharHook( wxKeyEvent& aEvent )
188{
189 GenericProcessChar( aEvent );
190}
191
192
193void DIALOG_ZONE_MANAGER::OnIdle( wxIdleEvent& aEvent )
194{
195 WXUNUSED( aEvent )
196 m_viewZonesOverview->SetFocus();
197 Unbind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
198
199 if( !m_needZoomGAL )
200 return;
201
202 m_needZoomGAL = false;
204}
205
206
207void DIALOG_ZONE_MANAGER::onDialogResize( wxSizeEvent& event )
208{
209 event.Skip();
211}
212
213
215{
217 std::list<ZONE_SELECTION_CHANGE_NOTIFIER*>{ m_panelZoneProperties, m_zoneViewer } )
218 {
219 i->OnZoneSelectionChanged( zone );
220 }
221
222 Layout();
223}
224
225
227{
228 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
229}
230
231
233{
234 SelectZoneTableItem( aEvent.GetItem() );
235}
236
237
238void DIALOG_ZONE_MANAGER::SelectZoneTableItem( wxDataViewItem const& aItem )
239{
240 ZONE* zone = m_modelZoneOverviewTable->GetZone( aItem );
241
242 if( !zone )
243 return;
244
246}
247
248
249void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
250{
251 for( ZONE_MANAGEMENT_BASE* zone_management :
252 std::list<ZONE_MANAGEMENT_BASE*>{ m_panelZoneProperties, m_zonesContainer.get() } )
253 {
254 zone_management->OnUserConfirmChange();
255 }
256
257 if( m_zoneInfo )
258 {
259 if( std::shared_ptr<ZONE_SETTINGS> zone = m_panelZoneProperties->GetZoneSettings() )
260 *m_zoneInfo = *zone;
261 }
262
263 aEvt.Skip();
264}
265
266
267void DIALOG_ZONE_MANAGER::OnRepourCheck( wxCommandEvent& aEvent )
268{
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_modelZoneOverviewTable->GetRow( it );
314 const std::optional<unsigned> rtn =
315 m_modelZoneOverviewTable->SwapZonePriority( *m_priorityDragIndex, drop_index );
316
317 if( rtn.has_value() )
318 {
319 const wxDataViewItem item = m_modelZoneOverviewTable->GetItem( *rtn );
320 if( item.IsOk() )
321 m_viewZonesOverview->Select( item );
322 }
323}
324
325#endif // wxUSE_DRAG_AND_DROP
326
327
328void DIALOG_ZONE_MANAGER::OnMoveUpClick( wxCommandEvent& aEvent )
329{
330 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_UP );
331}
332
333
334void DIALOG_ZONE_MANAGER::OnMoveDownClick( wxCommandEvent& aEvent )
335{
336 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_DOWN );
337}
338
339
340void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
341{
343 m_modelZoneOverviewTable->ClearFilter( m_viewZonesOverview->GetSelection() ) );
344 aEvent.Skip();
345}
346
347
348void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
349{
351 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
352 aEvent.Skip();
353}
354
355
357{
359 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
360 aEvent.Skip();
361}
362
363
364void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
365{
367 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
368 aEvent.Skip();
369}
370
371
373{
374 if( m_isFillingZones )
375 return;
376
377 m_isFillingZones = true;
379 m_zonesContainer->FlushZoneSettingsChange();
380 m_zonesContainer->FlushPriorityChange();
381
382 BOARD* board = m_pcbFrame->GetBoard();
383 board->IncrementTimeStamp();
384
385 auto commit = std::make_unique<BOARD_COMMIT>( m_pcbFrame );
386 m_filler = std::make_unique<ZONE_FILLER>( board, commit.get() );
387 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5 );
388 m_filler->SetProgressReporter( reporter.get() );
389
390 // TODO: replace these const_cast calls with a different solution that avoids mutating the
391 // container of the board. This is relatively safe as-is because the original zones list is
392 // swapped back in below, but still should be changed to avoid invalidating the board state
393 // in case this code is refactored to be a non-modal dialog in the future.
394 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetClonedZoneList();
395
396 //NOTE - Nether revert nor commit is needed here , cause the cloned zones are not owned by
397 // the pcb frame.
398 m_zoneFillComplete = m_filler->Fill( board->Zones() );
399 board->BuildConnectivity();
400
402 {
403 gal->RedrawRatsnest();
404 gal->GetView()->UpdateItems();
405 gal->Refresh();
406 int layer = gal->GetLayer();
407 gal->ActivateSelectedZone(
408 m_modelZoneOverviewTable->GetZone( m_viewZonesOverview->GetSelection() ) );
409 gal->OnLayerSelected( layer );
410 }
411
412 //NOTE - But the connectivity need to be rebuild, otherwise if cancelling, it may
413 // segfault.
414 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetOriginalZoneList();
415 board->BuildConnectivity();
416
417 m_isFillingZones = false;
418}
419
420
421void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
422{
423 if( ZONE* zone = m_panelZoneProperties->GetZone(); zone != nullptr )
424 {
425 zone->SetZoneName( aEvent.GetString() );
427 m_modelZoneOverviewTable->GetItemByZone( zone ) ) );
428 }
429}
430
431
433{
434 unsigned count = aEvent.GetInt();
435
437 btn->Enable( count == m_modelZoneOverviewTable->GetAllZonesCount() );
438}
439
440
441void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
442{
443 const wxObject* sender = aEvent.GetEventObject();
444
445 if( aEvent.GetEventObject() == m_checkName )
446 {
447 m_modelZoneOverviewTable->EnableFitterByName( aEvent.IsChecked() );
448 }
449 else if( aEvent.GetEventObject() == m_checkNet )
450 {
451 m_modelZoneOverviewTable->EnableFitterByNet( aEvent.IsChecked() );
452 }
453
454 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
455 {
456 m_modelZoneOverviewTable->ApplyFilter( m_filterCtrl->GetValue(),
457 m_viewZonesOverview->GetSelection() );
458 }
459}
460
461
463{
464 if( !m_viewZonesOverview->HasSelection() )
465 return;
466
467 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
468
469 if( !selectedItem.IsOk() )
470 return;
471
472 const unsigned int selectedRow = m_modelZoneOverviewTable->GetRow( selectedItem );
473 const std::optional<unsigned> new_index =
474 m_modelZoneOverviewTable->MoveZoneIndex( selectedRow, aMove );
475
476 if( new_index.has_value() )
477 {
478 wxDataViewItem new_item = m_modelZoneOverviewTable->GetItem( *new_index );
480 }
481}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
const ZONES & Zones() const
Definition: board.h:335
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:187
void IncrementTimeStamp()
Definition: board.cpp:255
Class DIALOG_ZONE_MANAGER_BASE.
void OnZoneNameUpdate(wxCommandEvent &aEvent)
PANE_ZONE_VIEWER * m_zoneViewer
void OnViewZonesOverviewOnLeftUp(wxMouseEvent &aEvent) override
~DIALOG_ZONE_MANAGER() override
void PostProcessZoneViewSelectionChange(wxDataViewItem const &item)
void GenericProcessChar(wxKeyEvent &event)
PCB_BASE_FRAME * m_pcbFrame
PANEL_ZONE_PROPERTIES * m_panelZoneProperties
void OnMoveDownClick(wxCommandEvent &aEvent) override
std::unique_ptr< ZONE_FILLER > m_filler
void OnRepourCheck(wxCommandEvent &aEvent) override
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)
void OnDataViewCtrlSelectionChanged(wxDataViewEvent &event) override
std::optional< unsigned > m_priorityDragIndex
DIALOG_ZONE_MANAGER(PCB_BASE_FRAME *aParent, ZONE_SETTINGS *aZoneInfo)
void OnTableCharHook(wxKeyEvent &event) override
void OnIdle(wxIdleEvent &aEvent)
void OnCheckBoxClicked(wxCommandEvent &aEvent)
void OnFilterCtrlTextChange(wxCommandEvent &aEvent) override
ZONE_SETTINGS * m_zoneInfo
void SelectZoneTableItem(wxDataViewItem const &aItem)
void OnOk(wxCommandEvent &aEvt) override
void OnFilterCtrlCancel(wxCommandEvent &aEvent) override
wxObjectDataPtr< MODEL_ZONES_OVERVIEW_TABLE > m_modelZoneOverviewTable
void OnFilterCtrlEnter(wxCommandEvent &aEvent) override
void OnZoneSelectionChanged(ZONE *aZone)
void OnZonesTableRowCountChange(wxCommandEvent &aEvent)
std::unique_ptr< ZONES_CONTAINER > m_zonesContainer
void OnFilterCtrlSearch(wxCommandEvent &aEvent) override
static std::map< int, wxString > GetColumnNames()
std::shared_ptr< ZONE_SETTINGS > GetZoneSettings() const
void ActivateSelectedZone(ZONE *new_zone) override
PANEL_ZONE_GAL * GetZoneGAL() const
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
BOARD * GetBoard() const
A bitmap button widget that behaves like a standard dialog button except with an icon.
Classes need post progress after user click OK.
static void SetRefillOnClose(bool aRepour)
Should all the zones be re-poured on dialog close.
Subscriber who is interested in the zone selection change.
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:78
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
#define _(s)
STL namespace.
std::vector< ZONE * > ZONES
Definition: pcb_io_eagle.h:47
BOARD * GetBoard()