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 <wx/radiobut.h>
32#include <kiface_base.h>
33#include <confirm.h>
34#include <pcb_edit_frame.h>
35#include <pcbnew_settings.h>
36#include <wx/string.h>
37#include <zones.h>
38#include <board_commit.h>
39#include <widgets/unit_binder.h>
40#include <zone.h>
41#include <pad.h>
42#include <board.h>
43#include <trigo.h>
44#include <bitmaps.h>
45#include <eda_pattern_match.h>
46#include <string_utils.h>
47#include <bitmaps.h>
48#include <zone_filler.h>
49
53#include "dialog_zone_manager.h"
59#include "zones_container.h"
60#include "pane_zone_viewer.h"
62
63
65{
66 if( PANEL_ZONE_GAL* canvas = m_zoneViewer->GetZoneGAL() )
67 canvas->ZoomFitScreen();
68}
69
70
71inline void DIALOG_ZONE_MANAGER::PostProcessZoneViewSelectionChange( wxDataViewItem const& aItem )
72{
73 bool textCtrlHasFocus = m_filterCtrl->HasFocus();
74 long filterInsertPos = m_filterCtrl->GetInsertionPoint();
75
76 if( aItem.IsOk() )
77 {
78 m_viewZonesOverview->Select( aItem );
79 m_viewZonesOverview->EnsureVisible( aItem );
80 }
81 else
82 {
83 if( m_modelZoneOverviewTable->GetCount() )
84 {
85 wxDataViewItem first_item = m_modelZoneOverviewTable->GetItem( 0 );
86 m_viewZonesOverview->Select( first_item );
87 m_viewZonesOverview->EnsureVisible( first_item );
89 }
90 else
91 {
93 }
94 }
95
96 if( textCtrlHasFocus )
97 {
98 m_filterCtrl->SetFocus();
99 m_filterCtrl->SetInsertionPoint( filterInsertPos );
100 }
101}
102
103
104inline void DIALOG_ZONE_MANAGER::GenericProcessChar( wxKeyEvent& aEvent )
105{
106 aEvent.Skip();
107
108 if( aEvent.GetKeyCode() == WXK_DOWN || aEvent.GetKeyCode() == WXK_UP )
109 {
110 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIDle, this );
111 }
112}
113
114
115void DIALOG_ZONE_MANAGER::OnTableChar( wxKeyEvent& aEvent )
116{
117 GenericProcessChar( aEvent );
118}
119
120
121void DIALOG_ZONE_MANAGER::OnTableCharHook( wxKeyEvent& aEvent )
122{
123 GenericProcessChar( aEvent );
124}
125
126
127void DIALOG_ZONE_MANAGER::OnIDle( wxIdleEvent& aEvent )
128{
129 WXUNUSED( aEvent )
130 m_viewZonesOverview->SetFocus();
131 Unbind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIDle, this );
132
133 if( !m_needZoomGAL )
134 return;
135
136 m_needZoomGAL = false;
138}
139
140
142 DIALOG_ZONE_MANAGER_BASE( aParent ), m_pcbFrame( aParent ), m_zoneInfo( aZoneInfo ),
143 m_zonesContainer( std::make_unique<ZONES_CONTAINER>( aParent->GetBoard() ) ),
144 m_panelZoneProperties( new PANEL_ZONE_PROPERTIES( this, aParent, *m_zonesContainer ) ),
145 m_modelZoneOverviewTable(
146 new MODEL_ZONES_OVERVIEW_TABLE( m_zonesContainer->GetZonesPriorityContainers(),
147 aParent->GetBoard(), aParent, this ) ),
148 m_zoneViewer( new PANE_ZONE_VIEWER( this, aParent ) ), m_priorityDragIndex( {} ),
149 m_needZoomGAL( true ), m_isFillingZones( false ), m_zoneFillComplete( false )
150{
151#ifdef __APPLE__
152 m_sizerZoneOP->InsertSpacer( m_sizerZoneOP->GetItemCount(), 5 );
153#endif
154
155 m_btnMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
156 m_btnMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
157 m_sizerProperties->Add( m_panelZoneProperties, 1, wxALL | wxEXPAND );
158 m_sizerTop->Add( m_zoneViewer, 1, wxBOTTOM | wxTOP | wxRIGHT | wxEXPAND, 5 );
159 m_checkRepour->SetValue( ZONE_MANAGER_PREFERENCE::GetRepourOnClose() );
160 m_zoneViewer->SetId( ZONE_VIEWER );
161
162 for( const auto& [k, v] : MODEL_ZONES_OVERVIEW_TABLE::GetColumnNames() )
163 {
165 m_viewZonesOverview->AppendIconTextColumn( v, k );
166 else
167 m_viewZonesOverview->AppendTextColumn( v, k );
168 }
169
170 m_viewZonesOverview->AssociateModel( m_modelZoneOverviewTable.get() );
171
172#if wxUSE_DRAG_AND_DROP
173
174 m_viewZonesOverview->EnableDragSource( wxDF_UNICODETEXT );
175 m_viewZonesOverview->EnableDropTarget( wxDF_UNICODETEXT );
176
177 Bind( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, &DIALOG_ZONE_MANAGER::OnBeginDrag, this,
178 VIEW_ZONE_TABLE );
179 Bind( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, &DIALOG_ZONE_MANAGER::OnDropPossible, this,
180 VIEW_ZONE_TABLE );
181 Bind( wxEVT_DATAVIEW_ITEM_DROP, &DIALOG_ZONE_MANAGER::OnDrop, this, VIEW_ZONE_TABLE );
182
183#endif // wxUSE_DRAG_AND_DROP
184
185 Bind( wxEVT_BUTTON, &DIALOG_ZONE_MANAGER::OnOk, this, wxID_OK );
186 Bind( EVT_ZONE_NAME_UPDATE, &DIALOG_ZONE_MANAGER::OnZoneNameUpdate, this );
187 Bind( EVT_ZONES_OVERVIEW_COUNT_CHANGE, &DIALOG_ZONE_MANAGER::OnZonesTableRowCountChange, this );
188 Bind( wxEVT_CHECKBOX, &DIALOG_ZONE_MANAGER::OnCheckBoxClicked, this );
189 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIDle, this );
190 Bind( wxEVT_BUTTON, &DIALOG_ZONE_MANAGER::OnMoveUpClick, this, BTN_MOVE_UP );
191 Bind( wxEVT_BUTTON, &DIALOG_ZONE_MANAGER::OnMoveDownClick, this, BTN_MOVE_DOWN );
192 Bind(
193 wxEVT_BOOKCTRL_PAGE_CHANGED,
194 [this]( wxNotebookEvent& aEvent )
195 {
196 Layout();
197 },
198 ZONE_VIEWER );
199
200 bool foundZone = m_modelZoneOverviewTable->GetCount();
201
202 if( foundZone )
203 SelectZoneTableItem( m_modelZoneOverviewTable->GetItem( 0 ) );
204
205 Layout();
206 m_MainBoxSizer->Fit( this );
207
208 //NOTE - Works on Windows and MacOS , need further handling in IDLE on Ubuntu
209 FitCanvasToScreen();
210}
211
212
214
215
217{
218 DIALOG_ZONE_MANAGER dlg( aCaller, aZoneInfo );
219
220 const int res = dlg.ShowQuasiModal();
221
223 return ZONE_MANAGER_REPOUR;
224
225 return res;
226}
227
228
230{
232 std::list<ZONE_SELECTION_CHANGE_NOTIFIER*>{ m_panelZoneProperties, m_zoneViewer } )
233 {
234 i->OnZoneSelectionChanged( zone );
235 }
236
237 Layout();
238}
239
240
242{
243 Bind( wxEVT_IDLE, &DIALOG_ZONE_MANAGER::OnIDle, this );
244}
245
246
248{
249 SelectZoneTableItem( aEvent.GetItem() );
250}
251
252
253void DIALOG_ZONE_MANAGER::SelectZoneTableItem( wxDataViewItem const& aItem )
254{
255 ZONE* zone = m_modelZoneOverviewTable->GetZone( aItem );
256
257 if( !zone )
258 return;
259
261}
262
263
264void DIALOG_ZONE_MANAGER::OnOk( wxCommandEvent& aEvt )
265{
266 for( ZONE_MANAGEMENT_BASE* zone_management :
267 std::list<ZONE_MANAGEMENT_BASE*>{ m_panelZoneProperties, m_zonesContainer.get() } )
268 {
269 zone_management->OnUserConfirmChange();
270 }
271
272 if( m_zoneInfo )
273 {
274 if( std::shared_ptr<ZONE_SETTINGS> zone = m_panelZoneProperties->GetZoneSettings() )
275 *m_zoneInfo = *zone;
276 }
277
278 aEvt.Skip();
279}
280
281
282#if wxUSE_DRAG_AND_DROP
283
284void DIALOG_ZONE_MANAGER::OnBeginDrag( wxDataViewEvent& aEvent )
285{
286 wxTextDataObject* obj = new wxTextDataObject;
287 obj->SetText( "42" ); //FIXME - Workaround for drop on GTK
288 aEvent.SetDataObject( obj );
289 aEvent.SetDragFlags( wxDrag_AllowMove );
290 const wxDataViewItem it = aEvent.GetItem();
291
292 if( it.IsOk() )
294}
295
296
297void DIALOG_ZONE_MANAGER::OnDropPossible( wxDataViewEvent& aEvent )
298{
299 aEvent.SetDropEffect( wxDragMove ); // check 'move' drop effect
300}
301
302
303void DIALOG_ZONE_MANAGER::OnRepourCheck( wxCommandEvent& aEvent )
304{
306}
307
308
309void DIALOG_ZONE_MANAGER::OnDrop( wxDataViewEvent& aEvent )
310{
311 if( aEvent.GetDataFormat() != wxDF_UNICODETEXT )
312 {
313 aEvent.Veto();
314 return;
315 }
316
317 if( !m_priorityDragIndex.has_value() )
318 return;
319
320 const wxDataViewItem it = aEvent.GetItem();
321
322 if( !it.IsOk() )
323 {
324 aEvent.Veto();
325 return;
326 }
327
328 unsigned int drop_index = m_modelZoneOverviewTable->GetRow( it );
329 const std::optional<unsigned> rtn =
330 m_modelZoneOverviewTable->SwapZonePriority( *m_priorityDragIndex, drop_index );
331
332 if( rtn.has_value() )
333 {
334 const wxDataViewItem item = m_modelZoneOverviewTable->GetItem( *rtn );
335 if( item.IsOk() )
336 m_viewZonesOverview->Select( item );
337 }
338}
339
340#endif // wxUSE_DRAG_AND_DROP
341
342
343void DIALOG_ZONE_MANAGER::OnMoveUpClick( wxCommandEvent& aEvent )
344{
345 WXUNUSED( aEvent );
346 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_UP );
347}
348
349
350void DIALOG_ZONE_MANAGER::OnMoveDownClick( wxCommandEvent& aEvent )
351{
352 WXUNUSED( aEvent );
353 MoveSelectedZonePriority( ZONE_INDEX_MOVEMENT::MOVE_DOWN );
354}
355
356
357void DIALOG_ZONE_MANAGER::OnFilterCtrlCancel( wxCommandEvent& aEvent )
358{
360 m_modelZoneOverviewTable->ClearFilter( m_viewZonesOverview->GetSelection() ) );
361 aEvent.Skip();
362}
363
364
365void DIALOG_ZONE_MANAGER::OnFilterCtrlSearch( wxCommandEvent& aEvent )
366{
368 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
369 aEvent.Skip();
370}
371
372
374{
376 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
377 aEvent.Skip();
378}
379
380
381void DIALOG_ZONE_MANAGER::OnFilterCtrlEnter( wxCommandEvent& aEvent )
382{
384 aEvent.GetString(), m_viewZonesOverview->GetSelection() ) );
385 aEvent.Skip();
386}
387
388
389void DIALOG_ZONE_MANAGER::OnButtonApplyClick( wxCommandEvent& aEvent )
390{
391 if( m_isFillingZones )
392 return;
393
394 m_isFillingZones = true;
395 m_zonesContainer->FlushZoneSettingsChange();
396 m_zonesContainer->FlushPriorityChange();
397
398 BOARD* board = m_pcbFrame->GetBoard();
399 board->IncrementTimeStamp();
400
401 auto commit = std::make_unique<BOARD_COMMIT>( m_pcbFrame );
402 m_filler = std::make_unique<ZONE_FILLER>( board, commit.get() );
403 auto reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, _( "Fill All Zones" ), 5 );
404 m_filler->SetProgressReporter( reporter.get() );
405
406 // TODO: replace these const_cast calls with a different solution that avoids mutating the
407 // container of the board. This is relatively safe as-is because the original zones list is
408 // swapped back in below, but still should be changed to avoid invalidating the board state
409 // in case this code is refactored to be a non-modal dialog in the future.
410 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetClonedZoneList();
411
412 //NOTE - Nether revert nor commit is needed here , cause the cloned zones are not owned by
413 // the pcb frame.
414 m_zoneFillComplete = m_filler->Fill( board->Zones() );
415 board->BuildConnectivity();
416 const_cast<ZONES&>( board->Zones() ) = m_zonesContainer->GetOriginalZoneList();
417
418 if( auto gal = m_zoneViewer->GetZoneGAL() )
419 {
420 gal->RedrawRatsnest();
421 gal->GetView()->UpdateItems();
422 gal->Refresh();
423 int layer = gal->GetLayer();
424 gal->ActivateSelectedZone(
425 m_modelZoneOverviewTable->GetZone( m_viewZonesOverview->GetSelection() ) );
426 gal->OnLayerSelected( layer );
427 }
428
429 m_isFillingZones = false;
430}
431
432
433void DIALOG_ZONE_MANAGER::OnZoneNameUpdate( wxCommandEvent& aEvent )
434{
435 if( ZONE* zone = m_panelZoneProperties->GetZone(); zone != nullptr )
436 {
437 zone->SetZoneName( aEvent.GetString() );
439 m_modelZoneOverviewTable->GetItemByZone( zone ) ) );
440 }
441}
442
443
445{
446 unsigned count = aEvent.GetInt();
447
449 btn->Enable( count == m_modelZoneOverviewTable->GetAllZonesCount() );
450}
451
452
453void DIALOG_ZONE_MANAGER::OnCheckBoxClicked( wxCommandEvent& aEvent )
454{
455 const wxObject* sender = aEvent.GetEventObject();
456
457 if( aEvent.GetEventObject() == m_checkName )
458 {
459 m_modelZoneOverviewTable->EnableFitterByName( aEvent.IsChecked() );
460 }
461 else if( aEvent.GetEventObject() == m_checkNet )
462 {
463 m_modelZoneOverviewTable->EnableFitterByNet( aEvent.IsChecked() );
464 }
465
466 if( ( sender == m_checkName || sender == m_checkNet ) && !m_filterCtrl->IsEmpty() )
467 {
468 m_modelZoneOverviewTable->ApplyFilter( m_filterCtrl->GetValue(),
469 m_viewZonesOverview->GetSelection() );
470 }
471}
472
473
475{
476 if( !m_viewZonesOverview->HasSelection() )
477 return;
478
479 const wxDataViewItem selectedItem = m_viewZonesOverview->GetSelection();
480
481 if( !selectedItem.IsOk() )
482 return;
483
484 const unsigned int selectedRow = m_modelZoneOverviewTable->GetRow( selectedItem );
485 const std::optional<unsigned> new_index =
486 m_modelZoneOverviewTable->MoveZoneIndex( selectedRow, aMove );
487
488 if( new_index.has_value() )
489 {
490 wxDataViewItem new_item = m_modelZoneOverviewTable->GetItem( *new_index );
492 }
493}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
const ZONES & Zones() const
Definition: board.h:327
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:180
void IncrementTimeStamp()
Definition: board.cpp:249
int ShowQuasiModal()
Class DIALOG_ZONE_MANAGER_BASE.
void OnMoveDownClick(wxCommandEvent &aEvent)
void OnButtonApplyClick(wxCommandEvent &aEvent) override
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
void OnIDle(wxIdleEvent &aEvent)
PANEL_ZONE_PROPERTIES * m_panelZoneProperties
std::unique_ptr< ZONE_FILLER > m_filler
void OnRepourCheck(wxCommandEvent &aEvent) override
void OnTableChar(wxKeyEvent &event) 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 OnOk(wxCommandEvent &aEvt)
void OnCheckBoxClicked(wxCommandEvent &aEvent)
void OnMoveUpClick(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 SetRepourOnClose(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:71
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
This file is part of the common library.
int InvokeZonesManager(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aZoneInfo)
Function InvokeZonesManager invokes up a modal dialog window for zones manager.
#define _(s)
Abstract pattern-matching tool and implementations.
STL namespace.
std::vector< ZONE * > ZONES
Definition: pcb_io_eagle.h:46
BOARD * GetBoard()
VECTOR3I res
#define ZONE_MANAGER_REPOUR
Definition: zones.h:44