forked from KiCad/kicad-source-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_footprints_frame.cpp
550 lines (420 loc) · 17.9 KB
/
display_footprints_frame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2015 Wayne Stambaugh <[email protected]>
* Copyright (C) 2007-2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <bitmaps.h>
#include <board.h>
#include <footprint.h>
#include <common.h>
#include <confirm.h>
#include <cvpcb_settings.h>
#include <footprint_editor_settings.h>
#include <fp_lib_table.h>
#include <id.h>
#include <kiface_base.h>
#include <lib_id.h>
#include <macros.h>
#include <widgets/msgpanel.h>
#include <pcb_draw_panel_gal.h>
#include <pcb_painter.h>
#include <pgm_base.h>
#include <reporter.h>
#include <settings/settings_manager.h>
#include <tool/action_toolbar.h>
#include <tool/common_tools.h>
#include <tool/tool_dispatcher.h>
#include <tool/tool_manager.h>
#include <tool/zoom_tool.h>
#include <cvpcb_mainframe.h>
#include <display_footprints_frame.h>
#include <tools/cvpcb_actions.h>
#include <tools/pcb_actions.h>
#include <tools/pcb_editor_conditions.h> // Shared conditions with other Pcbnew frames
#include <tools/pcb_viewer_tools.h> // shared tools with other Pcbnew frames
#include <tools/cvpcb_fpviewer_selection_tool.h>
#include <widgets/infobar.h>
#include <wx/choice.h>
#include <wx/debug.h>
BEGIN_EVENT_TABLE( DISPLAY_FOOTPRINTS_FRAME, PCB_BASE_FRAME )
EVT_CLOSE( DISPLAY_FOOTPRINTS_FRAME::OnCloseWindow )
EVT_CHOICE( ID_ON_ZOOM_SELECT, DISPLAY_FOOTPRINTS_FRAME::OnSelectZoom )
EVT_CHOICE( ID_ON_GRID_SELECT, DISPLAY_FOOTPRINTS_FRAME::OnSelectGrid )
END_EVENT_TABLE()
DISPLAY_FOOTPRINTS_FRAME::DISPLAY_FOOTPRINTS_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
PCB_BASE_FRAME( aKiway, aParent, FRAME_CVPCB_DISPLAY, _( "Footprint Viewer" ),
wxDefaultPosition, wxDefaultSize,
KICAD_DEFAULT_DRAWFRAME_STYLE, FOOTPRINTVIEWER_FRAME_NAME )
{
// Give an icon
wxIcon icon;
icon.CopyFromBitmap( KiBitmap( BITMAPS::icon_cvpcb ) );
SetIcon( icon );
SetBoard( new BOARD() );
// This board will only be used to hold a footprint for viewing
GetBoard()->SetBoardUse( BOARD_USE::FPHOLDER );
SetScreen( new PCB_SCREEN( GetPageSizeIU() ) );
// Create GAL canvas before loading settings
auto* gal_drawPanel = new PCB_DRAW_PANEL_GAL( this, -1, wxPoint( 0, 0 ), m_frameSize,
GetGalDisplayOptions(),
EDA_DRAW_PANEL_GAL::GAL_FALLBACK );
SetCanvas( gal_drawPanel );
// Don't show the default board solder mask clearance. Only the
// footprint or pad clearance setting should be shown if it is not 0.
GetBoard()->GetDesignSettings().m_SolderMaskMargin = 0;
LoadSettings( config() );
// Initialize some display options
auto displ_opts = GetDisplayOptions();
displ_opts.m_DisplayPadClearance = false; // Pad clearance has no meaning here
// Track and via clearance has no meaning here.
displ_opts.m_ShowTrackClearanceMode = PCB_DISPLAY_OPTIONS::DO_NOT_SHOW_CLEARANCE;
SetDisplayOptions( displ_opts );
// Create the manager and dispatcher & route draw panel events to the dispatcher
m_toolManager = new TOOL_MANAGER;
m_toolManager->SetEnvironment( GetBoard(), gal_drawPanel->GetView(),
gal_drawPanel->GetViewControls(), config(), this );
m_actions = new CVPCB_ACTIONS();
m_toolDispatcher = new TOOL_DISPATCHER( m_toolManager );
gal_drawPanel->SetEventDispatcher( m_toolDispatcher );
m_toolManager->RegisterTool( new COMMON_TOOLS );
m_toolManager->RegisterTool( new ZOOM_TOOL );
m_toolManager->RegisterTool( new CVPCB_FOOTPRINT_VIEWER_SELECTION_TOOL );
m_toolManager->RegisterTool( new PCB_VIEWER_TOOLS );
m_toolManager->GetTool<PCB_VIEWER_TOOLS>()->SetFootprintFrame( true );
m_toolManager->InitTools();
setupUIConditions();
// Run the control tool, it is supposed to be always active
m_toolManager->InvokeTool( "cvpcb.FootprintViewerInteractiveSelection" );
ReCreateHToolbar();
ReCreateVToolbar();
ReCreateOptToolbar();
m_auimgr.SetManagedWindow( this );
CreateInfoBar();
m_auimgr.AddPane( m_mainToolBar, EDA_PANE().HToolbar().Name( "MainToolbar" )
.Top().Layer( 6 ) );
m_auimgr.AddPane( m_optionsToolBar, EDA_PANE().VToolbar().Name( "OptToolbar" )
.Left().Layer( 3 ) );
m_auimgr.AddPane( GetCanvas(), EDA_PANE().Canvas().Name( "DrawFrame" )
.Center() );
m_auimgr.AddPane( m_messagePanel, EDA_PANE().Messages().Name( "MsgPanel" )
.Bottom().Layer( 6 ) );
FinishAUIInitialization();
auto& galOpts = GetGalDisplayOptions();
galOpts.m_axesEnabled = true;
ActivateGalCanvas();
setupUnits( config() );
// Restore last zoom. (If auto-zooming we'll adjust when we load the footprint.)
CVPCB_SETTINGS* cfg = dynamic_cast<CVPCB_SETTINGS*>( config() );
if( cfg )
GetCanvas()->GetView()->SetScale( cfg->m_FootprintViewerZoom );
updateView();
Show( true );
// Register a call to update the toolbar sizes. It can't be done immediately because
// it seems to require some sizes calculated that aren't yet (at least on GTK).
CallAfter( [&]()
{
// Ensure the controls on the toolbars all are correctly sized
UpdateToolbarControlSizes();
} );
}
DISPLAY_FOOTPRINTS_FRAME::~DISPLAY_FOOTPRINTS_FRAME()
{
// Shutdown all running tools
if( m_toolManager )
m_toolManager->ShutdownAllTools();
GetBoard()->DeleteAllFootprints();
GetCanvas()->StopDrawing();
GetCanvas()->GetView()->Clear();
// Be sure any event cannot be fired after frame deletion:
GetCanvas()->SetEvtHandlerEnabled( false );
delete GetScreen();
SetScreen( nullptr ); // Be sure there is no double deletion
}
void DISPLAY_FOOTPRINTS_FRAME::setupUIConditions()
{
PCB_BASE_FRAME::setupUIConditions();
ACTION_MANAGER* mgr = m_toolManager->GetActionManager();
PCB_EDITOR_CONDITIONS cond( this );
wxASSERT( mgr );
#define CHECK( x ) ACTION_CONDITIONS().Check( x )
mgr->SetConditions( ACTIONS::zoomTool,
CHECK( cond.CurrentTool( ACTIONS::zoomTool ) ) );
mgr->SetConditions( ACTIONS::selectionTool,
CHECK( cond.CurrentTool( ACTIONS::selectionTool ) ) );
mgr->SetConditions( ACTIONS::measureTool,
CHECK( cond.CurrentTool( ACTIONS::measureTool ) ) );
mgr->SetConditions( ACTIONS::toggleGrid, CHECK( cond.GridVisible() ) );
mgr->SetConditions( ACTIONS::toggleCursorStyle, CHECK( cond.FullscreenCursor() ) );
mgr->SetConditions( ACTIONS::millimetersUnits, CHECK( cond.Units( EDA_UNITS::MILLIMETRES ) ) );
mgr->SetConditions( ACTIONS::inchesUnits, CHECK( cond.Units( EDA_UNITS::INCHES ) ) );
mgr->SetConditions( ACTIONS::milsUnits, CHECK( cond.Units( EDA_UNITS::MILS ) ) );
mgr->SetConditions( PCB_ACTIONS::showPadNumbers,
CHECK( cond.PadNumbersDisplay() ) );
mgr->SetConditions( PCB_ACTIONS::padDisplayMode, CHECK( !cond.PadFillDisplay() ) );
mgr->SetConditions( PCB_ACTIONS::textOutlines, CHECK( !cond.TextFillDisplay() ) );
mgr->SetConditions( PCB_ACTIONS::graphicsOutlines,
CHECK( !cond.GraphicsFillDisplay() ) );
#undef CHECK
}
void DISPLAY_FOOTPRINTS_FRAME::doCloseWindow()
{
Destroy();
}
void DISPLAY_FOOTPRINTS_FRAME::ReCreateVToolbar()
{
// Currently, no vertical right toolbar.
}
void DISPLAY_FOOTPRINTS_FRAME::ReCreateOptToolbar()
{
if( m_optionsToolBar )
{
m_optionsToolBar->ClearToolbar();
}
else
{
m_optionsToolBar = new ACTION_TOOLBAR( this, ID_OPT_TOOLBAR, wxDefaultPosition,
wxDefaultSize,
KICAD_AUI_TB_STYLE | wxAUI_TB_VERTICAL );
m_optionsToolBar->SetAuiManager( &m_auimgr );
}
m_optionsToolBar->Add( ACTIONS::selectionTool, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( ACTIONS::measureTool, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->AddScaledSeparator( this );
m_optionsToolBar->Add( ACTIONS::toggleGrid, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( ACTIONS::togglePolarCoords, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( ACTIONS::inchesUnits, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( ACTIONS::milsUnits, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( ACTIONS::millimetersUnits, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( ACTIONS::toggleCursorStyle, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->AddScaledSeparator( this );
m_optionsToolBar->Add( PCB_ACTIONS::showPadNumbers, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( PCB_ACTIONS::padDisplayMode, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( PCB_ACTIONS::textOutlines, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Add( PCB_ACTIONS::graphicsOutlines, ACTION_TOOLBAR::TOGGLE );
m_optionsToolBar->Realize();
}
void DISPLAY_FOOTPRINTS_FRAME::ReCreateHToolbar()
{
// Note:
// To rebuild the aui toolbar, the more easy way is to clear ( calling m_mainToolBar.Clear() )
// all wxAuiToolBarItems.
// However the wxAuiToolBarItems are not the owners of controls managed by
// them ( m_zoomSelectBox and m_gridSelectBox ), and therefore do not delete them
// So we do not recreate them after clearing the tools.
if( m_mainToolBar )
{
m_mainToolBar->ClearToolbar();
}
else
{
m_mainToolBar = new ACTION_TOOLBAR( this, ID_H_TOOLBAR, wxDefaultPosition, wxDefaultSize,
KICAD_AUI_TB_STYLE | wxAUI_TB_HORZ_LAYOUT );
m_mainToolBar->SetAuiManager( &m_auimgr );
}
m_mainToolBar->Add( ACTIONS::zoomRedraw );
m_mainToolBar->Add( ACTIONS::zoomInCenter );
m_mainToolBar->Add( ACTIONS::zoomOutCenter );
m_mainToolBar->Add( ACTIONS::zoomFitScreen );
m_mainToolBar->Add( ACTIONS::zoomTool, ACTION_TOOLBAR::TOGGLE, ACTION_TOOLBAR::CANCEL );
m_mainToolBar->AddScaledSeparator( this );
m_mainToolBar->Add( ACTIONS::show3DViewer );
m_mainToolBar->AddScaledSeparator( this );
// Grid selection choice box.
if( !m_gridSelectBox )
m_gridSelectBox = new wxChoice( m_mainToolBar, ID_ON_GRID_SELECT );
UpdateGridSelectBox();
m_mainToolBar->AddControl( m_gridSelectBox );
m_mainToolBar->AddScaledSeparator( this );
// Zoom selection choice box.
if( !m_zoomSelectBox )
m_zoomSelectBox = new wxChoice( m_mainToolBar, ID_ON_ZOOM_SELECT );
UpdateZoomSelectBox();
m_mainToolBar->AddControl( m_zoomSelectBox );
m_mainToolBar->UpdateControlWidth( ID_ON_GRID_SELECT );
m_mainToolBar->UpdateControlWidth( ID_ON_ZOOM_SELECT );
// after adding the buttons to the toolbar, must call Realize() to reflect the changes
m_mainToolBar->Realize();
}
void DISPLAY_FOOTPRINTS_FRAME::UpdateToolbarControlSizes()
{
if( m_mainToolBar )
{
// Update the item widths
m_mainToolBar->UpdateControlWidth( ID_ON_GRID_SELECT );
m_mainToolBar->UpdateControlWidth( ID_ON_ZOOM_SELECT );
}
}
void DISPLAY_FOOTPRINTS_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg )
{
auto cfg = dynamic_cast<CVPCB_SETTINGS*>( aCfg );
wxCHECK( cfg, /* void */ );
// We don't allow people to change this right now, so make sure it's on
GetWindowSettings( cfg )->cursor.always_show_cursor = true;
PCB_BASE_FRAME::LoadSettings( cfg );
SetDisplayOptions( cfg->m_FootprintViewerDisplayOptions );
}
void DISPLAY_FOOTPRINTS_FRAME::SaveSettings( APP_SETTINGS_BASE* aCfg )
{
CVPCB_SETTINGS* cfg = dynamic_cast<CVPCB_SETTINGS*>( aCfg );
wxCHECK( cfg, /* void */ );
PCB_BASE_FRAME::SaveSettings( cfg );
cfg->m_FootprintViewerDisplayOptions = GetDisplayOptions();
cfg->m_FootprintViewerZoom = GetCanvas()->GetView()->GetScale();
}
WINDOW_SETTINGS* DISPLAY_FOOTPRINTS_FRAME::GetWindowSettings( APP_SETTINGS_BASE* aCfg )
{
CVPCB_SETTINGS* cfg = dynamic_cast<CVPCB_SETTINGS*>( aCfg );
wxCHECK( cfg, nullptr );
return &cfg->m_FootprintViewer;
}
MAGNETIC_SETTINGS* DISPLAY_FOOTPRINTS_FRAME::GetMagneticItemsSettings()
{
CVPCB_SETTINGS* cfg = dynamic_cast<CVPCB_SETTINGS*>( Kiface().KifaceSettings() );
wxCHECK( cfg, nullptr );
return &cfg->m_FootprintViewerMagneticSettings;
}
COLOR4D DISPLAY_FOOTPRINTS_FRAME::GetGridColor()
{
return COLOR4D( DARKGRAY );
}
FOOTPRINT* DISPLAY_FOOTPRINTS_FRAME::GetFootprint( const wxString& aFootprintName,
REPORTER& aReporter )
{
FOOTPRINT* footprint = nullptr;
LIB_ID fpid;
if( fpid.Parse( aFootprintName ) >= 0 )
{
aReporter.Report( wxString::Format( _( "Footprint ID '%s' is not valid." ),
aFootprintName ),
RPT_SEVERITY_ERROR );
return nullptr;
}
wxString libNickname = FROM_UTF8( fpid.GetLibNickname().c_str() );
wxString fpName = FROM_UTF8( fpid.GetLibItemName().c_str() );
FP_LIB_TABLE* fpTable = Prj().PcbFootprintLibs();
wxASSERT( fpTable );
// See if the library requested is in the library table
if( !fpTable->HasLibrary( libNickname ) )
{
aReporter.Report( wxString::Format( _( "Library '%s' is not in the footprint library table." ),
libNickname ),
RPT_SEVERITY_ERROR );
return nullptr;
}
// See if the footprint requested is in the library
if( !fpTable->FootprintExists( libNickname, fpName ) )
{
aReporter.Report( wxString::Format( _( "Footprint '%s' not found." ), aFootprintName ),
RPT_SEVERITY_ERROR );
return nullptr;
}
try
{
const FOOTPRINT* fp = fpTable->GetEnumeratedFootprint( libNickname, fpName );
if( fp )
footprint = static_cast<FOOTPRINT*>( fp->Duplicate() );
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.What() );
return nullptr;
}
if( footprint )
{
footprint->SetParent( (EDA_ITEM*) GetBoard() );
footprint->SetPosition( wxPoint( 0, 0 ) );
return footprint;
}
aReporter.Report( wxString::Format( _( "Footprint '%s' not found." ), aFootprintName ),
RPT_SEVERITY_ERROR );
return nullptr;
}
void DISPLAY_FOOTPRINTS_FRAME::InitDisplay()
{
CVPCB_MAINFRAME* parentframe = (CVPCB_MAINFRAME *) GetParent();
FOOTPRINT* footprint = nullptr;
const FOOTPRINT_INFO* fpInfo = nullptr;
GetBoard()->DeleteAllFootprints();
GetCanvas()->GetView()->Clear();
wxString footprintName = parentframe->GetSelectedFootprint();
if( footprintName.IsEmpty() )
{
COMPONENT* comp = parentframe->GetSelectedComponent();
if( comp )
footprintName = comp->GetFPID().GetUniStringLibId();
}
INFOBAR_REPORTER infoReporter( m_infoBar );
m_infoBar->Dismiss();
if( !footprintName.IsEmpty() )
{
SetTitle( wxString::Format( _( "Footprint: %s" ), footprintName ) );
footprint = GetFootprint( footprintName, infoReporter );
fpInfo = parentframe->m_FootprintsList->GetFootprintInfo( footprintName );
}
if( footprint )
GetBoard()->Add( footprint );
if( fpInfo )
SetStatusText( wxString::Format( _( "Lib: %s" ), fpInfo->GetLibNickname() ), 0 );
else
SetStatusText( wxEmptyString, 0 );
infoReporter.Finalize();
updateView();
UpdateStatusBar();
GetCanvas()->Refresh();
Update3DView( true, true );
}
void DISPLAY_FOOTPRINTS_FRAME::updateView()
{
PCB_DRAW_PANEL_GAL* dp = static_cast<PCB_DRAW_PANEL_GAL*>( GetCanvas() );
dp->UpdateColors();
dp->DisplayBoard( GetBoard() );
m_toolManager->ResetTools( TOOL_BASE::MODEL_RELOAD );
if( m_zoomSelectBox->GetSelection() == 0 )
m_toolManager->RunAction( ACTIONS::zoomFitScreen, true );
else
m_toolManager->RunAction( ACTIONS::centerContents, true );
UpdateMsgPanel();
}
void DISPLAY_FOOTPRINTS_FRAME::UpdateMsgPanel()
{
FOOTPRINT* footprint = GetBoard()->GetFirstFootprint();
std::vector<MSG_PANEL_ITEM> items;
if( footprint )
footprint->GetMsgPanelInfo( this, items );
SetMsgPanel( items );
}
COLOR_SETTINGS* DISPLAY_FOOTPRINTS_FRAME::GetColorSettings() const
{
auto* settings = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
if( settings )
return Pgm().GetSettingsManager().GetColorSettings( settings->m_ColorTheme );
else
return Pgm().GetSettingsManager().GetColorSettings();
}
BOARD_ITEM_CONTAINER* DISPLAY_FOOTPRINTS_FRAME::GetModel() const
{
return GetBoard()->GetFirstFootprint();
}
SELECTION& DISPLAY_FOOTPRINTS_FRAME::GetCurrentSelection()
{
return m_toolManager->GetTool<CVPCB_FOOTPRINT_VIEWER_SELECTION_TOOL>()->GetSelection();
}