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->GetZonesPriorityContainers(),
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( wxEVT_BUTTON, &DIALOG_ZONE_MANAGER::OnOk, this, wxID_OK );
107 Bind( EVT_ZONE_NAME_UPDATE, &DIALOG_ZONE_MANAGER::OnZoneNameUpdate, this );
108 Bind( EVT_ZONES_OVERVIEW_COUNT_CHANGE, &DIALOG_ZONE_MANAGER::OnZonesTableRowCountChange, this );
109 Bind( wxEVT_CHECKBOX, &DIALOG_ZONE_MANAGER::OnCheckBoxClicked, this );
110 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
111 Bind( wxEVT_BOOKCTRL_PAGE_CHANGED,
112 [this]( wxNotebookEvent& aEvent )
113 {
114 Layout();
115 },
116 m_zoneViewer->GetId() );
117
118 if( m_modelZoneOverviewTable->GetCount() )
119 SelectZoneTableItem( m_modelZoneOverviewTable->GetItem( 0 ) );
120
121 Layout();
122 m_MainBoxSizer->Fit( this );
123 finishDialogSettings();
124
125 //NOTE - Works on Windows and MacOS , need further handling in IDLE on Ubuntu
126 FitCanvasToScreen();
127}
128
129
131
132
134{
135 if( PANEL_ZONE_GAL* canvas = m_zoneViewer->GetZoneGAL() )
136 canvas->ZoomFitScreen();
137}
138
139
141{
142 bool textCtrlHasFocus = m_filterCtrl->HasFocus();
143 long filterInsertPos = m_filterCtrl->GetInsertionPoint();
144
145 if( aItem.IsOk() )
146 {
147 m_viewZonesOverview->Select( aItem );
148 m_viewZonesOverview->EnsureVisible( aItem );
149 }
150 else
151 {
152 if( m_modelZoneOverviewTable->GetCount() )
153 {
154 wxDataViewItem first_item = m_modelZoneOverviewTable->GetItem( 0 );
155 m_viewZonesOverview->Select( first_item );
156 m_viewZonesOverview->EnsureVisible( first_item );
158 }
159 else
160 {
162 }
163 }
164
165 if( textCtrlHasFocus )
166 {
167 m_filterCtrl->SetFocus();
168 m_filterCtrl->SetInsertionPoint( filterInsertPos );
169 }
170}
171
172
174{
175 aEvent.Skip();
176
177 if( aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
178 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
179}
180
181
182void DIALOG_ZONE_MANAGER::OnTableChar( wxKeyEvent& aEvent )
183{
184 GenericProcessChar( aEvent );
185}
186
187
188void DIALOG_ZONE_MANAGER::OnTableCharHook( wxKeyEvent& aEvent )
189{
190 GenericProcessChar( aEvent );
191}
192
193
194void DIALOG_ZONE_MANAGER::OnIdle( wxIdleEvent& aEvent )
195{
196 WXUNUSED( aEvent )
197 m_viewZonesOverview->SetFocus();
198 Unbind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
199
200 if( !m_needZoomGAL )
201 return;
202
203 m_needZoomGAL = false;
205}
206
207
208void DIALOG_ZONE_MANAGER::onDialogResize( wxSizeEvent& event )
209{
210 event.Skip();
212}
213
214
216{
218 std::list<ZONE_SELECTION_CHANGE_NOTIFIER*>{ m_panelZoneProperties, m_zoneViewer } )
219 {
220 i->OnZoneSelectionChanged( zone );
221 }
222
223 Layout();
224}
225
226
228{
229 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIdle, this );
230}
231
232
234{
235 SelectZoneTableItem( aEvent.GetItem() );
236}
237
238
239void DIALOG_ZONE_MANAGER::SelectZoneTableItem( wxDataViewItem const& aItem )
240{
241 ZONE* zone = m_modelZoneOverviewTable->GetZone( aItem );
242
243 if( !zone )
244 return;
245
247}
248
249
250void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
251{
252 for( ZONE_MANAGEMENT_BASE* zone_management :
253 std::list<ZONE_MANAGEMENT_BASE*>{ m_panelZoneProperties, m_zonesContainer.get() } )
254 {
255 zone_management->OnUserConfirmChange();
256 }
257
258 if( m_zoneInfo )
259 {
260 if( std::shared_ptr<ZONE_SETTINGS> zone = m_panelZoneProperties->GetZoneSettings() )
261 *m_zoneInfo = *zone;
262 }
263
264 aEvt.Skip();
265}
266
267
268void DIALOG_ZONE_MANAGER::OnRepourCheck( wxCommandEvent& aEvent )
269{
271}
272
273
274#if wxUSE_DRAG_AND_DROP
275
276void DIALOG_ZONE_MANAGER::OnBeginDrag( wxDataViewEvent& aEvent )
277{
278 wxTextDataObject* obj = new wxTextDataObject;
279 obj->SetText( "42" ); //FIXME - Workaround for drop on GTK
280 aEvent.SetDataObject( obj );
281 aEvent.SetDragFlags( wxDrag_AllowMove );
282 const wxDataViewItem it = aEvent.GetItem();
283
284 if( it.IsOk() )
286}
287
288
289void DIALOG_ZONE_MANAGER::OnDropPossible( wxDataViewEvent& aEvent )
290{
291 aEvent.SetDropEffect( wxDragMove ); // check 'move' drop effect
292}
293
294
295void DIALOG_ZONE_MANAGER::OnDrop( wxDataViewEvent& aEvent )
296{
297 if( aEvent.GetDataFormat() != wxDF_UNICODETEXT )
298 {
299 aEvent.Veto();
300 return;
301 }
302
303 if( !m_priorityDragIndex.has_value() )
304 return;
305
306 const wxDataViewItem it = aEvent.GetItem();
307
308 if( !it.IsOk() )
309 {
310 aEvent.Veto();
311 return;
312 }
313
314 unsigned int drop_index = m_modelZoneOverviewTable->GetRow( it );
315 const std::optional<unsigned> rtn =
316 m_modelZoneOverviewTable->SwapZonePriority( *m_priorityDragIndex, drop_index );
317
318 if( rtn.has_value() )
319 {
320 const wxDataViewItem item = m_modelZoneOverviewTable->GetItem( *rtn );
321 if( item.IsOk() )
322 m_viewZonesOverview->Select( item );
323 }
324}
325
326#endif // wxUSE_DRAG_AND_DROP
327
328
329void DIALOG_ZONE_MANAGER::OnMoveUpClick( wxCommandEvent& aEvent )
330{
331 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_UP );
332}
333
334
335void DIALOG_ZONE_MANAGER::OnMoveDownClick( wxCommandEvent& aEvent )
336{
337 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_DOWN );
338}
339
340
341void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
342{
344 m_modelZoneOverviewTable->ClearFilter( m_viewZonesOverview->GetSelection() ) );
345 aEvent.Skip();
346}
347
348
349void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
350{
352 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
353 aEvent.Skip();
354}
355
356
358{
360 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
361 aEvent.Skip();
362}
363
364
365void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
366{
368 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
369 aEvent.Skip();
370}
371
372
374{
375 if( m_isFillingZones )
376 return;
377
378 m_isFillingZones = true;
380 m_zonesContainer->FlushZoneSettingsChange();
381 m_zonesContainer->FlushPriorityChange();
382
383 BOARD* board = m_pcbFrame->GetBoard();
384 board->IncrementTimeStamp();
385
386 auto commit = std::make_unique<BOARD_COMMIT>( m_pcbFrame );
387 m_filler = std::make_unique<ZONE_FILLER>( board, commit.get() );
388 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5 );
389 m_filler->SetProgressReporter( reporter.get() );
390
391 // TODO: replace these const_cast calls with a different solution that avoids mutating the
392 // container of the board. This is relatively safe as-is because the original zones list is
393 // swapped back in below, but still should be changed to avoid invalidating the board state
394 // in case this code is refactored to be a non-modal dialog in the future.
395 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetClonedZoneList();
396
397 //NOTE - Nether revert nor commit is needed here , cause the cloned zones are not owned by
398 // the pcb frame.
399 m_zoneFillComplete = m_filler->Fill( board->Zones() );
400 board->BuildConnectivity();
401
403 {
404 gal->RedrawRatsnest();
405 gal->GetView()->UpdateItems();
406 gal->Refresh();
407 int layer = gal->GetLayer();
408 gal->ActivateSelectedZone(
409 m_modelZoneOverviewTable->GetZone( m_viewZonesOverview->GetSelection() ) );
410 gal->OnLayerSelected( layer );
411 }
412
413 //NOTE - But the connectivity need to be rebuild, otherwise if cancelling, it may
414 // segfault.
415 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetOriginalZoneList();
416 board->BuildConnectivity();
417
418 m_isFillingZones = false;
419}
420
421
422void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
423{
424 if( ZONE* zone = m_panelZoneProperties->GetZone(); zone != nullptr )
425 {
426 zone->SetZoneName( aEvent.GetString() );
428 m_modelZoneOverviewTable->GetItemByZone( zone ) ) );
429 }
430}
431
432
434{
435 unsigned count = aEvent.GetInt();
436
438 btn->Enable( count == m_modelZoneOverviewTable->GetAllZonesCount() );
439}
440
441
442void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
443{
444 const wxObject* sender = aEvent.GetEventObject();
445
446 if( aEvent.GetEventObject() == m_checkName )
447 {
448 m_modelZoneOverviewTable->EnableFitterByName( aEvent.IsChecked() );
449 }
450 else if( aEvent.GetEventObject() == m_checkNet )
451 {
452 m_modelZoneOverviewTable->EnableFitterByNet( aEvent.IsChecked() );
453 }
454
455 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
456 {
457 m_modelZoneOverviewTable->ApplyFilter( m_filterCtrl->GetValue(),
458 m_viewZonesOverview->GetSelection() );
459 }
460}
461
462
464{
465 if( !m_viewZonesOverview->HasSelection() )
466 return;
467
468 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
469
470 if( !selectedItem.IsOk() )
471 return;
472
473 const unsigned int selectedRow = m_modelZoneOverviewTable->GetRow( selectedItem );
474 const std::optional<unsigned> new_index =
475 m_modelZoneOverviewTable->MoveZoneIndex( selectedRow, aMove );
476
477 if( new_index.has_value() )
478 {
479 wxDataViewItem new_item = m_modelZoneOverviewTable->GetItem( *new_index );
481 }
482}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:288
const ZONES & Zones() const
Definition: board.h:333
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:184
void IncrementTimeStamp()
Definition: board.cpp:253
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 OnOk(wxCommandEvent &aEvt)
void OnCheckBoxClicked(wxCommandEvent &aEvent)
void OnFilterCtrlTextChange(wxCommandEvent &aEvent) override
ZONE_SETTINGS * m_zoneInfo
void SelectZoneTableItem(wxDataViewItem const &aItem)
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:72
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()