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 <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{
216 Freeze();
217
219 std::list<ZONE_SELECTION_CHANGE_NOTIFIER*>{ m_panelZoneProperties, m_zoneViewer } )
220 {
221 i->OnZoneSelectionChanged( zone );
222 }
223
224 Layout();
225 Thaw();
226}
227
228
230{
231 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
232}
233
234
236{
237 SelectZoneTableItem( aEvent.GetItem() );
238}
239
240
241void DIALOG_ZONE_MANAGER::SelectZoneTableItem( wxDataViewItem const& aItem )
242{
243 ZONE* zone = m_modelZoneOverviewTable->GetZone( aItem );
244
245 if( !zone )
246 return;
247
249}
250
251
252void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
253{
254 for( ZONE_MANAGEMENT_BASE* zone_management :
255 std::list<ZONE_MANAGEMENT_BASE*>{ m_panelZoneProperties, m_zonesContainer.get() } )
256 {
257 zone_management->OnUserConfirmChange();
258 }
259
260 if( m_zoneInfo )
261 {
262 if( std::shared_ptr<ZONE_SETTINGS> zone = m_panelZoneProperties->GetZoneSettings() )
263 *m_zoneInfo = *zone;
264 }
265
266 aEvt.Skip();
267}
268
269
270void DIALOG_ZONE_MANAGER::OnRepourCheck( wxCommandEvent& aEvent )
271{
273}
274
275
276#if wxUSE_DRAG_AND_DROP
277
278void DIALOG_ZONE_MANAGER::OnBeginDrag( wxDataViewEvent& aEvent )
279{
280 wxTextDataObject* obj = new wxTextDataObject;
281 obj->SetText( "42" ); //FIXME - Workaround for drop on GTK
282 aEvent.SetDataObject( obj );
283 aEvent.SetDragFlags( wxDrag_AllowMove );
284 const wxDataViewItem it = aEvent.GetItem();
285
286 if( it.IsOk() )
288}
289
290
291void DIALOG_ZONE_MANAGER::OnDropPossible( wxDataViewEvent& aEvent )
292{
293 aEvent.SetDropEffect( wxDragMove ); // check 'move' drop effect
294}
295
296
297void DIALOG_ZONE_MANAGER::OnDrop( wxDataViewEvent& aEvent )
298{
299 if( aEvent.GetDataFormat() != wxDF_UNICODETEXT )
300 {
301 aEvent.Veto();
302 return;
303 }
304
305 if( !m_priorityDragIndex.has_value() )
306 return;
307
308 const wxDataViewItem it = aEvent.GetItem();
309
310 if( !it.IsOk() )
311 {
312 aEvent.Veto();
313 return;
314 }
315
316 unsigned int drop_index = m_modelZoneOverviewTable->GetRow( it );
317 const std::optional<unsigned> rtn =
318 m_modelZoneOverviewTable->SwapZonePriority( *m_priorityDragIndex, drop_index );
319
320 if( rtn.has_value() )
321 {
322 const wxDataViewItem item = m_modelZoneOverviewTable->GetItem( *rtn );
323 if( item.IsOk() )
324 m_viewZonesOverview->Select( item );
325 }
326}
327
328#endif // wxUSE_DRAG_AND_DROP
329
330
331void DIALOG_ZONE_MANAGER::OnMoveUpClick( wxCommandEvent& aEvent )
332{
333 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_UP );
334}
335
336
337void DIALOG_ZONE_MANAGER::OnMoveDownClick( wxCommandEvent& aEvent )
338{
339 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_DOWN );
340}
341
342
343void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
344{
346 m_modelZoneOverviewTable->ClearFilter( m_viewZonesOverview->GetSelection() ) );
347 aEvent.Skip();
348}
349
350
351void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
352{
354 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
355 aEvent.Skip();
356}
357
358
360{
362 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
363 aEvent.Skip();
364}
365
366
367void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
368{
370 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
371 aEvent.Skip();
372}
373
374
376{
377 if( m_isFillingZones )
378 return;
379
380 m_isFillingZones = true;
382 m_zonesContainer->FlushZoneSettingsChange();
383 m_zonesContainer->FlushPriorityChange();
384
385 BOARD* board = m_pcbFrame->GetBoard();
386 board->IncrementTimeStamp();
387
388 auto commit = std::make_unique<BOARD_COMMIT>( m_pcbFrame );
389 m_filler = std::make_unique<ZONE_FILLER>( board, commit.get() );
390 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5 );
391 m_filler->SetProgressReporter( reporter.get() );
392
393 // TODO: replace these const_cast calls with a different solution that avoids mutating the
394 // container of the board. This is relatively safe as-is because the original zones list is
395 // swapped back in below, but still should be changed to avoid invalidating the board state
396 // in case this code is refactored to be a non-modal dialog in the future.
397 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetClonedZoneList();
398
399 //NOTE - Nether revert nor commit is needed here , cause the cloned zones are not owned by
400 // the pcb frame.
401 m_zoneFillComplete = m_filler->Fill( board->Zones() );
402 board->BuildConnectivity();
403
405 {
406 gal->RedrawRatsnest();
407 gal->GetView()->UpdateItems();
408 gal->Refresh();
409 int layer = gal->GetLayer();
410
411 // rebuild the currently displayed zone and refresh display
412 ZONE* curr_zone = gal->GetZone();
413 gal->ActivateSelectedZone( curr_zone );
414
415 gal->OnLayerSelected( layer );
416 }
417
418 //NOTE - But the connectivity need to be rebuild, otherwise if cancelling, it may
419 // segfault.
420 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetOriginalZoneList();
421 board->BuildConnectivity();
422
423 m_isFillingZones = false;
424}
425
426
427void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
428{
429 if( ZONE* zone = m_panelZoneProperties->GetZone(); zone != nullptr )
430 {
431 zone->SetZoneName( aEvent.GetString() );
433 m_modelZoneOverviewTable->GetItemByZone( zone ) ) );
434 }
435}
436
437
439{
440 unsigned count = aEvent.GetInt();
441
443 btn->Enable( count == m_modelZoneOverviewTable->GetAllZonesCount() );
444}
445
446
447void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
448{
449 const wxObject* sender = aEvent.GetEventObject();
450
451 if( aEvent.GetEventObject() == m_checkName )
452 {
453 m_modelZoneOverviewTable->EnableFitterByName( aEvent.IsChecked() );
454 }
455 else if( aEvent.GetEventObject() == m_checkNet )
456 {
457 m_modelZoneOverviewTable->EnableFitterByNet( aEvent.IsChecked() );
458 }
459
460 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
461 {
462 m_modelZoneOverviewTable->ApplyFilter( m_filterCtrl->GetValue(),
463 m_viewZonesOverview->GetSelection() );
464 }
465}
466
467
469{
470 if( !m_viewZonesOverview->HasSelection() )
471 return;
472
473 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
474
475 if( !selectedItem.IsOk() )
476 return;
477
478 const unsigned int selectedRow = m_modelZoneOverviewTable->GetRow( selectedItem );
479 const std::optional<unsigned> new_index =
480 m_modelZoneOverviewTable->MoveZoneIndex( selectedRow, aMove );
481
482 if( new_index.has_value() )
483 {
484 wxDataViewItem new_item = m_modelZoneOverviewTable->GetItem( *new_index );
486 }
487}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:295
const ZONES & Zones() const
Definition: board.h:340
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:190
void IncrementTimeStamp()
Definition: board.cpp:258
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()