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 <kiface_base.h>
33#include <pcb_edit_frame.h>
34#include <pcbnew_settings.h>
35#include <wx/string.h>
36#include <board_commit.h>
38#include <zone.h>
39#include <pad.h>
40#include <board.h>
41#include <bitmaps.h>
42#include <string_utils.h>
43#include <zone_filler.h>
44
48#include "dialog_zone_manager.h"
54#include "zones_container.h"
55#include "pane_zone_viewer.h"
57
58
60 DIALOG_ZONE_MANAGER_BASE( aParent ),
61 m_pcbFrame( aParent ),
62 m_zoneInfo( aZoneInfo ),
63 m_zonesContainer( std::make_unique<ZONES_CONTAINER>( aParent->GetBoard() ) ),
64 m_priorityDragIndex( {} ),
65 m_needZoomGAL( true ),
66 m_isFillingZones( false ),
67 m_zoneFillComplete( false )
68{
69#ifdef __APPLE__
70 m_sizerZoneOP->InsertSpacer( m_sizerZoneOP->GetItemCount(), 5 );
71#endif
72
73 m_btnMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
74 m_btnMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
75
76 m_panelZoneProperties = new PANEL_ZONE_PROPERTIES( this, aParent, *m_zonesContainer );
77 m_sizerProperties->Add( m_panelZoneProperties, 1, wxTOP | wxEXPAND, 5 );
78
79 m_zoneViewer = new PANE_ZONE_VIEWER( this, aParent );
80 m_sizerTop->Add( m_zoneViewer, 1, wxBOTTOM | wxLEFT | wxRIGHT | wxEXPAND, 5 );
81
82 m_checkRepour->SetValue( ZONE_MANAGER_PREFERENCE::GetRepourOnClose() );
83 //m_zoneViewer->SetId( ZONE_VIEWER );
84
85 for( const auto& [k, v] : MODEL_ZONES_OVERVIEW::GetColumnNames() )
86 {
88 m_viewZonesOverview->AppendIconTextColumn( v, k );
89 else
90 m_viewZonesOverview->AppendTextColumn( v, k );
91 }
92
93 m_modelZonesOverview = new MODEL_ZONES_OVERVIEW( m_zonesContainer->GetManagedZones(), aParent->GetBoard(),
94 aParent, this );
95 m_viewZonesOverview->AssociateModel( m_modelZonesOverview.get() );
96
97#if wxUSE_DRAG_AND_DROP
98 m_viewZonesOverview->EnableDragSource( wxDF_UNICODETEXT );
99 m_viewZonesOverview->EnableDropTarget( wxDF_UNICODETEXT );
100
101 int id = m_viewZonesOverview->GetId();
102 Bind( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, &DIALOG_ZONE_MANAGER::OnBeginDrag, this, id );
103 Bind( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, &DIALOG_ZONE_MANAGER::OnDropPossible, this, id );
104 Bind( wxEVT_DATAVIEW_ITEM_DROP, &DIALOG_ZONE_MANAGER::OnDrop, this, id );
105#endif // wxUSE_DRAG_AND_DROP
106
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_modelZonesOverview->GetCount() )
119 SelectZoneTableItem( m_modelZonesOverview->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
140void DIALOG_ZONE_MANAGER::PostProcessZoneViewSelChange( wxDataViewItem const& aItem )
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_modelZonesOverview->GetCount() )
153 {
154 wxDataViewItem first_item = m_modelZonesOverview->GetItem( 0 );
155 m_viewZonesOverview->Select( first_item );
156 m_viewZonesOverview->EnsureVisible( first_item );
157 m_zoneViewer->ActivateSelectedZone( m_modelZonesOverview->GetZone( 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{
217 wxWindowUpdateLocker updateLock( this );
218
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_modelZonesOverview->GetZone( aItem );
241
242 if( !zone )
243 return;
244
246}
247
248
249void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
250{
252 m_zonesContainer->OnUserConfirmChange();
253
254 if( m_zoneInfo )
255 {
256 if( std::shared_ptr<ZONE_SETTINGS> zone = m_panelZoneProperties->GetZoneSettings() )
257 m_zoneInfo->CopyFrom( *zone, false );
258 }
259
260 aEvt.Skip();
261}
262
263
264void DIALOG_ZONE_MANAGER::OnRepourCheck( wxCommandEvent& aEvent )
265{
267}
268
269
270#if wxUSE_DRAG_AND_DROP
271
272void DIALOG_ZONE_MANAGER::OnBeginDrag( wxDataViewEvent& aEvent )
273{
274 wxTextDataObject* obj = new wxTextDataObject;
275 obj->SetText( "42" ); //FIXME - Workaround for drop on GTK
276 aEvent.SetDataObject( obj );
277 aEvent.SetDragFlags( wxDrag_AllowMove );
278 const wxDataViewItem it = aEvent.GetItem();
279
280 if( it.IsOk() )
282}
283
284
285void DIALOG_ZONE_MANAGER::OnDropPossible( wxDataViewEvent& aEvent )
286{
287 aEvent.SetDropEffect( wxDragMove ); // check 'move' drop effect
288}
289
290
291void DIALOG_ZONE_MANAGER::OnDrop( wxDataViewEvent& aEvent )
292{
293 if( aEvent.GetDataFormat() != wxDF_UNICODETEXT )
294 {
295 aEvent.Veto();
296 return;
297 }
298
299 if( !m_priorityDragIndex.has_value() )
300 return;
301
302 const wxDataViewItem it = aEvent.GetItem();
303
304 if( !it.IsOk() )
305 {
306 aEvent.Veto();
307 return;
308 }
309
310 unsigned int drop_index = m_modelZonesOverview->GetRow( it );
311 const std::optional<unsigned> rtn = m_modelZonesOverview->SwapZonePriority( *m_priorityDragIndex, drop_index );
312
313 if( rtn.has_value() )
314 {
315 const wxDataViewItem item = m_modelZonesOverview->GetItem( *rtn );
316
317 if( item.IsOk() )
318 m_viewZonesOverview->Select( item );
319 }
320}
321
322#endif // wxUSE_DRAG_AND_DROP
323
324
325void DIALOG_ZONE_MANAGER::OnMoveUpClick( wxCommandEvent& aEvent )
326{
327 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_UP );
328}
329
330
331void DIALOG_ZONE_MANAGER::OnMoveDownClick( wxCommandEvent& aEvent )
332{
333 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_DOWN );
334}
335
336
337void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
338{
340 aEvent.Skip();
341}
342
343
344void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
345{
346 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
347 m_viewZonesOverview->GetSelection() ) );
348 aEvent.Skip();
349}
350
351
353{
354 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
355 m_viewZonesOverview->GetSelection() ) );
356 aEvent.Skip();
357}
358
359
360void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
361{
362 PostProcessZoneViewSelChange( m_modelZonesOverview->ApplyFilter( aEvent.GetString(),
363 m_viewZonesOverview->GetSelection() ) );
364 aEvent.Skip();
365}
366
367
369{
370 if( m_isFillingZones )
371 return;
372
373 m_isFillingZones = true;
375 m_zonesContainer->FlushZoneSettingsChange();
376 m_zonesContainer->FlushPriorityChange();
377
378 BOARD* board = m_pcbFrame->GetBoard();
379 board->IncrementTimeStamp();
380
381 auto commit = std::make_unique<BOARD_COMMIT>( m_pcbFrame );
382 m_filler = std::make_unique<ZONE_FILLER>( board, commit.get() );
383 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5, PR_CAN_ABORT );
384 m_filler->SetProgressReporter( reporter.get() );
385
386 // TODO: replace these const_cast calls with a different solution that avoids mutating the
387 // container of the board. This is relatively safe as-is because the original zones list is
388 // swapped back in below, but still should be changed to avoid invalidating the board state
389 // in case this code is refactored to be a non-modal dialog in the future.
390 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetClonedZoneList();
391
392 //NOTE - Nether revert nor commit is needed here , cause the cloned zones are not owned by
393 // the pcb frame.
394 m_zoneFillComplete = m_filler->Fill( board->Zones() );
395 board->BuildConnectivity();
396
398 {
399 gal->RedrawRatsnest();
400 gal->GetView()->UpdateItems();
401 gal->Refresh();
402 int layer = gal->GetLayer();
403
404 // rebuild the currently displayed zone and refresh display
405 ZONE* curr_zone = gal->GetZone();
406 gal->ActivateSelectedZone( curr_zone );
407
408 gal->OnLayerSelected( layer );
409 }
410
411 //NOTE - But the connectivity need to be rebuild, otherwise if cancelling, it may
412 // segfault.
413 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetOriginalZoneList();
414 board->BuildConnectivity();
415
416 m_isFillingZones = false;
417}
418
419
420void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
421{
422 if( ZONE* zone = m_panelZoneProperties->GetZone(); zone != nullptr )
423 {
424 zone->SetZoneName( aEvent.GetString() );
425 m_modelZonesOverview->RowChanged( m_modelZonesOverview->GetRow( m_modelZonesOverview->GetItemByZone( zone ) ) );
426 }
427}
428
429
431{
432 unsigned count = aEvent.GetInt();
433
435 btn->Enable( count == m_modelZonesOverview->GetAllZonesCount() );
436}
437
438
439void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
440{
441 const wxObject* sender = aEvent.GetEventObject();
442
443 if( aEvent.GetEventObject() == m_checkName )
444 m_modelZonesOverview->EnableFitterByName( aEvent.IsChecked() );
445 else if( aEvent.GetEventObject() == m_checkNet )
446 m_modelZonesOverview->EnableFitterByNet( aEvent.IsChecked() );
447
448 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
449 m_modelZonesOverview->ApplyFilter( m_filterCtrl->GetValue(), m_viewZonesOverview->GetSelection() );
450}
451
452
454{
455 if( !m_viewZonesOverview->HasSelection() )
456 return;
457
458 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
459
460 if( !selectedItem.IsOk() )
461 return;
462
463 const unsigned int selectedRow = m_modelZonesOverview->GetRow( selectedItem );
464 const std::optional<unsigned> new_index = m_modelZonesOverview->MoveZoneIndex( selectedRow, aMove );
465
466 if( new_index.has_value() )
467 {
468 wxDataViewItem new_item = m_modelZonesOverview->GetItem( *new_index );
470 }
471}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
const ZONES & Zones() const
Definition: board.h:362
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:186
void IncrementTimeStamp()
Definition: board.cpp:254
Class DIALOG_ZONE_MANAGER_BASE.
void OnZoneNameUpdate(wxCommandEvent &aEvent)
PANE_ZONE_VIEWER * m_zoneViewer
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 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
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()
void OnUserConfirmChange() override
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.
static void SetRefillOnClose(bool aRepour)
Should all the zones be re-poured on dialog close.
void OnZoneSelectionChanged(ZONE *aZone)
Inform the subscriber about the zone selection change.
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:88
void CopyFrom(const ZONE_SETTINGS &aOther, bool aCopyFull=true)
Function CopyFrom copy settings from a different ZONE_SETTINGS object.
Handle a list of polygons defining a copper zone.
Definition: zone.h:74
#define _(s)
ZONE_INDEX_MOVEMENT
STL namespace.
std::vector< ZONE * > ZONES
Definition: pcb_io_eagle.h:47
BOARD * GetBoard()
#define PR_CAN_ABORT