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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014-2016 CERN
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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 "pcbnew_control.h"
#include "common_actions.h"
#include "selection_tool.h"
#include "picker_tool.h"
#include "grid_helper.h"
#include <class_board.h>
#include <class_module.h>
#include <class_track.h>
#include <class_zone.h>
#include <class_pcb_screen.h>
#include <confirm.h>
#include <hotkeys_basic.h>
#include <io_mgr.h>
#include <pcbnew_id.h>
#include <wxPcbStruct.h>
#include <pcb_draw_panel_gal.h>
#include <ratsnest_data.h>
#include <tool/tool_manager.h>
#include <gal/graphics_abstraction_layer.h>
#include <view/view_controls.h>
#include <pcb_painter.h>
#include <origin_viewitem.h>
#include <boost/bind.hpp>
// files.cpp
extern bool AskLoadBoardFileName( wxWindow* aParent, int* aCtl, wxString* aFileName,
bool aKicadFilesOnly = false );
extern IO_MGR::PCB_FILE_T plugin_type( const wxString& aFileName, int aCtl );
PCBNEW_CONTROL::PCBNEW_CONTROL() :
TOOL_INTERACTIVE( "pcbnew.Control" ), m_frame( NULL )
{
m_gridOrigin = new KIGFX::ORIGIN_VIEWITEM();
}
PCBNEW_CONTROL::~PCBNEW_CONTROL()
{
delete m_gridOrigin;
}
void PCBNEW_CONTROL::Reset( RESET_REASON aReason )
{
m_frame = getEditFrame<PCB_BASE_FRAME>();
if( aReason == MODEL_RELOAD || aReason == GAL_SWITCH )
{
m_gridOrigin->SetPosition( getModel<BOARD>()->GetGridOrigin() );
getView()->Remove( m_gridOrigin );
getView()->Add( m_gridOrigin );
}
}
int PCBNEW_CONTROL::ZoomInOut( const TOOL_EVENT& aEvent )
{
KIGFX::VIEW* view = m_frame->GetGalCanvas()->GetView();
KIGFX::VIEW_CONTROLS* ctls = getViewControls();
double zoomScale = 1.0;
if( aEvent.IsAction( &COMMON_ACTIONS::zoomIn ) )
zoomScale = 1.3;
else if( aEvent.IsAction( &COMMON_ACTIONS::zoomOut ) )
zoomScale = 0.7;
view->SetScale( view->GetScale() * zoomScale, getViewControls()->GetCursorPosition() );
if( ctls->IsCursorWarpingEnabled() )
ctls->CenterOnCursor();
return 0;
}
int PCBNEW_CONTROL::ZoomInOutCenter( const TOOL_EVENT& aEvent )
{
KIGFX::VIEW* view = getView();
double zoomScale = 1.0;
if( aEvent.IsAction( &COMMON_ACTIONS::zoomInCenter ) )
zoomScale = 1.3;
else if( aEvent.IsAction( &COMMON_ACTIONS::zoomOutCenter ) )
zoomScale = 0.7;
view->SetScale( view->GetScale() * zoomScale );
return 0;
}
int PCBNEW_CONTROL::ZoomCenter( const TOOL_EVENT& aEvent )
{
KIGFX::VIEW_CONTROLS* ctls = getViewControls();
if( ctls->IsCursorWarpingEnabled() )
ctls->CenterOnCursor();
else
getView()->SetCenter( getViewControls()->GetCursorPosition() );
return 0;
}
int PCBNEW_CONTROL::ZoomFitScreen( const TOOL_EVENT& aEvent )
{
KIGFX::VIEW* view = getView();
EDA_DRAW_PANEL_GAL* galCanvas = m_frame->GetGalCanvas();
BOARD* board = getModel<BOARD>();
board->ComputeBoundingBox();
BOX2I boardBBox = board->ViewBBox();
VECTOR2D scrollbarSize = VECTOR2D( galCanvas->GetSize() - galCanvas->GetClientSize() );
VECTOR2D screenSize = view->ToWorld( galCanvas->GetClientSize(), false );
if( boardBBox.GetWidth() == 0 || boardBBox.GetHeight() == 0 )
{
// Empty view
view->SetScale( 17.0 ); // works fine for the standard worksheet frame
view->SetCenter( screenSize / 2.0 );
}
else
{
VECTOR2D vsize = boardBBox.GetSize();
double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
fabs( vsize.y / screenSize.y ) );
view->SetScale( scale );
view->SetCenter( boardBBox.Centre() );
}
// Take scrollbars into account
VECTOR2D worldScrollbarSize = view->ToWorld( scrollbarSize, false );
view->SetCenter( view->GetCenter() + worldScrollbarSize / 2.0 );
return 0;
}
int PCBNEW_CONTROL::ZoomPreset( const TOOL_EVENT& aEvent )
{
unsigned int idx = aEvent.Parameter<long>();
std::vector<double>& zoomList = m_frame->GetScreen()->m_ZoomList;
KIGFX::VIEW* view = m_frame->GetGalCanvas()->GetView();
KIGFX::GAL* gal = m_frame->GetGalCanvas()->GetGAL();
m_frame->SetPresetZoom( idx );
if( idx == 0 ) // Zoom Auto
{
return ZoomFitScreen( aEvent );
}
else if( idx >= zoomList.size() )
{
assert( false );
return 0;
}
double selectedZoom = zoomList[idx];
double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor();
view->SetScale( 1.0 / ( zoomFactor * selectedZoom ) );
return 0;
}
int PCBNEW_CONTROL::TrackDisplayMode( const TOOL_EVENT& aEvent )
{
KIGFX::PCB_PAINTER* painter =
static_cast<KIGFX::PCB_PAINTER*>( m_frame->GetGalCanvas()->GetView()->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings =
static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
// Apply new display options to the GAL canvas
DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*)m_frame->GetDisplayOptions();
displ_opts->m_DisplayPcbTrackFill = !displ_opts->m_DisplayPcbTrackFill;
settings->LoadDisplayOptions( displ_opts );
for( TRACK* track = getModel<BOARD>()->m_Track; track; track = track->Next() )
{
if( track->Type() == PCB_TRACE_T )
track->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
}
m_frame->GetGalCanvas()->Refresh();
return 0;
}
int PCBNEW_CONTROL::PadDisplayMode( const TOOL_EVENT& aEvent )
{
KIGFX::PCB_PAINTER* painter =
static_cast<KIGFX::PCB_PAINTER*>( m_frame->GetGalCanvas()->GetView()->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings =
static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*)m_frame->GetDisplayOptions();
// Apply new display options to the GAL canvas
displ_opts->m_DisplayPadFill = !displ_opts->m_DisplayPadFill;
settings->LoadDisplayOptions( displ_opts );
for( MODULE* module = getModel<BOARD>()->m_Modules; module; module = module->Next() )
{
for( D_PAD* pad = module->Pads(); pad; pad = pad->Next() )
pad->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
}
m_frame->GetGalCanvas()->Refresh();
return 0;
}
int PCBNEW_CONTROL::ViaDisplayMode( const TOOL_EVENT& aEvent )
{
KIGFX::PCB_PAINTER* painter =
static_cast<KIGFX::PCB_PAINTER*>( m_frame->GetGalCanvas()->GetView()->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings =
static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*)m_frame->GetDisplayOptions();
// Apply new display options to the GAL canvas
displ_opts->m_DisplayViaFill = !displ_opts->m_DisplayViaFill;
settings->LoadDisplayOptions( displ_opts );
for( TRACK* track = getModel<BOARD>()->m_Track; track; track = track->Next() )
{
if( track->Type() == PCB_VIA_T )
track->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
}
m_frame->GetGalCanvas()->Refresh();
return 0;
}
int PCBNEW_CONTROL::ZoneDisplayMode( const TOOL_EVENT& aEvent )
{
KIGFX::PCB_PAINTER* painter =
static_cast<KIGFX::PCB_PAINTER*>( m_frame->GetGalCanvas()->GetView()->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings =
static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*)m_frame->GetDisplayOptions();
// Apply new display options to the GAL canvas
if( aEvent.IsAction( &COMMON_ACTIONS::zoneDisplayEnable ) )
displ_opts->m_DisplayZonesMode = 0;
else if( aEvent.IsAction( &COMMON_ACTIONS::zoneDisplayDisable ) )
displ_opts->m_DisplayZonesMode = 1;
else if( aEvent.IsAction( &COMMON_ACTIONS::zoneDisplayOutlines ) )
displ_opts->m_DisplayZonesMode = 2;
else
assert( false );
settings->LoadDisplayOptions( displ_opts );
BOARD* board = getModel<BOARD>();
for( int i = 0; i < board->GetAreaCount(); ++i )
board->GetArea( i )->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
m_frame->GetGalCanvas()->Refresh();
return 0;
}
int PCBNEW_CONTROL::HighContrastMode( const TOOL_EVENT& aEvent )
{
KIGFX::PCB_PAINTER* painter =
static_cast<KIGFX::PCB_PAINTER*>( m_frame->GetGalCanvas()->GetView()->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings =
static_cast<KIGFX::PCB_RENDER_SETTINGS*> ( painter->GetSettings() );
DISPLAY_OPTIONS* displ_opts = (DISPLAY_OPTIONS*)m_frame->GetDisplayOptions();
displ_opts->m_ContrastModeDisplay = !displ_opts->m_ContrastModeDisplay;
settings->LoadDisplayOptions( displ_opts );
m_frame->GetGalCanvas()->SetHighContrastLayer( m_frame->GetActiveLayer() );
return 0;
}
int PCBNEW_CONTROL::HighContrastInc( const TOOL_EVENT& aEvent )
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return 0;
}
int PCBNEW_CONTROL::HighContrastDec( const TOOL_EVENT& aEvent )
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return 0;
}
// Layer control
int PCBNEW_CONTROL::LayerSwitch( const TOOL_EVENT& aEvent )
{
m_frame->SwitchLayer( NULL, (LAYER_ID) aEvent.Parameter<long>() );
return 0;
}
int PCBNEW_CONTROL::LayerNext( const TOOL_EVENT& aEvent )
{
PCB_BASE_FRAME* editFrame = m_frame;
LAYER_NUM layer = editFrame->GetActiveLayer();
if( layer < F_Cu || layer > B_Cu )
return 0;
int layerCount = getModel<BOARD>()->GetCopperLayerCount();
if( layer == layerCount - 2 || layerCount < 2 )
layer = B_Cu;
else if( layer == B_Cu )
layer = F_Cu;
else
++layer;
assert( IsCopperLayer( layer ) );
editFrame->SwitchLayer( NULL, ToLAYER_ID( layer ) );
return 0;
}
int PCBNEW_CONTROL::LayerPrev( const TOOL_EVENT& aEvent )
{
PCB_BASE_FRAME* editFrame = m_frame;
LAYER_NUM layer = editFrame->GetActiveLayer();
if( layer < F_Cu || layer > B_Cu )
return 0;
int layerCount = getModel<BOARD>()->GetCopperLayerCount();
if( layer == F_Cu || layerCount < 2 )
layer = B_Cu;
else if( layer == B_Cu )
layer = layerCount - 2;
else
--layer;
assert( IsCopperLayer( layer ) );
editFrame->SwitchLayer( NULL, ToLAYER_ID( layer ) );
return 0;
}
int PCBNEW_CONTROL::LayerToggle( const TOOL_EVENT& aEvent )
{
LAYER_NUM currentLayer = m_frame->GetActiveLayer();
PCB_SCREEN* screen = m_frame->GetScreen();
if( currentLayer == screen->m_Route_Layer_TOP )
m_frame->SwitchLayer( NULL, screen->m_Route_Layer_BOTTOM );
else
m_frame->SwitchLayer( NULL, screen->m_Route_Layer_TOP );
return 0;
}
int PCBNEW_CONTROL::LayerAlphaInc( const TOOL_EVENT& aEvent )
{
KIGFX::PCB_PAINTER* painter =
static_cast<KIGFX::PCB_PAINTER*>( m_frame->GetGalCanvas()->GetView()->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings =
static_cast<KIGFX::PCB_RENDER_SETTINGS*> ( painter->GetSettings() );
LAYER_NUM currentLayer = m_frame->GetActiveLayer();
KIGFX::COLOR4D currentColor = settings->GetLayerColor( currentLayer );
if( currentColor.a <= 0.95 )
{
currentColor.a += 0.05;
settings->SetLayerColor( currentLayer, currentColor );
m_frame->GetGalCanvas()->GetView()->UpdateLayerColor( currentLayer );
}
return 0;
}
int PCBNEW_CONTROL::LayerAlphaDec( const TOOL_EVENT& aEvent )
{
KIGFX::PCB_PAINTER* painter =
static_cast<KIGFX::PCB_PAINTER*>( m_frame->GetGalCanvas()->GetView()->GetPainter() );
KIGFX::PCB_RENDER_SETTINGS* settings =
static_cast<KIGFX::PCB_RENDER_SETTINGS*> ( painter->GetSettings() );
LAYER_NUM currentLayer = m_frame->GetActiveLayer();
KIGFX::COLOR4D currentColor = settings->GetLayerColor( currentLayer );
if( currentColor.a >= 0.05 )
{
currentColor.a -= 0.05;
settings->SetLayerColor( currentLayer, currentColor );
m_frame->GetGalCanvas()->GetView()->UpdateLayerColor( currentLayer );
}
return 0;
}
// Cursor control
int PCBNEW_CONTROL::CursorControl( const TOOL_EVENT& aEvent )
{
long type = aEvent.Parameter<long>();
bool fastMove = type & COMMON_ACTIONS::CURSOR_FAST_MOVE;
type &= ~COMMON_ACTIONS::CURSOR_FAST_MOVE;
GRID_HELPER gridHelper( m_frame );
VECTOR2D cursor = getViewControls()->GetCursorPosition();
VECTOR2I gridSize = gridHelper.GetGrid();
VECTOR2D newCursor = gridHelper.Align( cursor );
if( fastMove )
gridSize = gridSize * 10;
switch( type )
{
case COMMON_ACTIONS::CURSOR_UP:
newCursor -= VECTOR2D( 0, gridSize.y );
break;
case COMMON_ACTIONS::CURSOR_DOWN:
newCursor += VECTOR2D( 0, gridSize.y );
break;
case COMMON_ACTIONS::CURSOR_LEFT:
newCursor -= VECTOR2D( gridSize.x, 0 );
break;
case COMMON_ACTIONS::CURSOR_RIGHT:
newCursor += VECTOR2D( gridSize.x, 0 );
break;
case COMMON_ACTIONS::CURSOR_CLICK: // fall through
case COMMON_ACTIONS::CURSOR_DBL_CLICK:
{
TOOL_ACTIONS action;
int modifiers = 0;
modifiers |= wxGetKeyState( WXK_SHIFT ) ? MD_SHIFT : 0;
modifiers |= wxGetKeyState( WXK_CONTROL ) ? MD_CTRL : 0;
modifiers |= wxGetKeyState( WXK_ALT ) ? MD_ALT : 0;
if( type == COMMON_ACTIONS::CURSOR_CLICK )
action = TA_MOUSE_CLICK;
else if( type == COMMON_ACTIONS::CURSOR_DBL_CLICK )
action = TA_MOUSE_DBLCLICK;
else
assert( false );
TOOL_EVENT evt( TC_MOUSE, action, BUT_LEFT | modifiers );
evt.SetMousePosition( getViewControls()->GetCursorPosition() );
m_toolMgr->ProcessEvent( evt );
return 0;
}
break;
}
// Handler cursor movement
KIGFX::VIEW* view = getView();
newCursor = view->ToScreen( newCursor );
newCursor.x = KiROUND( newCursor.x );
newCursor.y = KiROUND( newCursor.y );
// Pan the screen if required
const VECTOR2I& screenSize = view->GetGAL()->GetScreenPixelSize();
BOX2I screenBox( VECTOR2I( 0, 0 ), screenSize );
if( !screenBox.Contains( newCursor ) )
{
VECTOR2D delta( 0, 0 );
if( newCursor.x < screenBox.GetLeft() )
{
delta.x = newCursor.x - screenBox.GetLeft();
newCursor.x = screenBox.GetLeft();
}
else if( newCursor.x > screenBox.GetRight() )
{
delta.x = newCursor.x - screenBox.GetRight();
// -1 is to keep the cursor within the drawing area,
// so the cursor coordinates are still updated
newCursor.x = screenBox.GetRight() - 1;
}
if( newCursor.y < screenBox.GetTop() )
{
delta.y = newCursor.y - screenBox.GetTop();
newCursor.y = screenBox.GetTop();
}
else if( newCursor.y > screenBox.GetBottom() )
{
delta.y = newCursor.y - screenBox.GetBottom();
// -1 is to keep the cursor within the drawing area,
// so the cursor coordinates are still updated
newCursor.y = screenBox.GetBottom() - 1;
}
view->SetCenter( view->GetCenter() + view->ToWorld( delta, false ) );
}
m_frame->GetGalCanvas()->WarpPointer( newCursor.x, newCursor.y );
return 0;
}
int PCBNEW_CONTROL::PanControl( const TOOL_EVENT& aEvent )
{
long type = aEvent.Parameter<long>();
KIGFX::VIEW* view = getView();
GRID_HELPER gridHelper( m_frame );
VECTOR2D center = view->GetCenter();
VECTOR2I gridSize = gridHelper.GetGrid() * 10;
switch( type )
{
case COMMON_ACTIONS::CURSOR_UP:
center -= VECTOR2D( 0, gridSize.y );
break;
case COMMON_ACTIONS::CURSOR_DOWN:
center += VECTOR2D( 0, gridSize.y );
break;
case COMMON_ACTIONS::CURSOR_LEFT:
center -= VECTOR2D( gridSize.x, 0 );
break;
case COMMON_ACTIONS::CURSOR_RIGHT:
center += VECTOR2D( gridSize.x, 0 );
break;
default:
assert( false );
break;
}
view->SetCenter( center );
return 0;
}
// Grid control
int PCBNEW_CONTROL::GridFast1( const TOOL_EVENT& aEvent )
{
m_frame->SetFastGrid1();
updateGrid();
return 0;
}
int PCBNEW_CONTROL::GridFast2( const TOOL_EVENT& aEvent )
{
m_frame->SetFastGrid2();
updateGrid();
return 0;
}
int PCBNEW_CONTROL::GridNext( const TOOL_EVENT& aEvent )
{
m_frame->SetNextGrid();
updateGrid();
return 0;
}
int PCBNEW_CONTROL::GridPrev( const TOOL_EVENT& aEvent )
{
m_frame->SetPrevGrid();
updateGrid();
return 0;
}
static bool setOrigin( KIGFX::VIEW* aView, PCB_BASE_FRAME* aFrame,
KIGFX::ORIGIN_VIEWITEM* aItem, const VECTOR2D& aPoint )
{
aFrame->SetGridOrigin( wxPoint( aPoint.x, aPoint.y ) );
aView->GetGAL()->SetGridOrigin( aPoint );
aItem->SetPosition( aPoint );
aView->MarkDirty();
return true;
}
int PCBNEW_CONTROL::GridSetOrigin( const TOOL_EVENT& aEvent )
{
VECTOR2D* origin = aEvent.Parameter<VECTOR2D*>();
if( origin )
{
setOrigin( getView(), m_frame, m_gridOrigin, *origin );
delete origin;
}
else
{
Activate();
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
assert( picker );
// TODO it will not check the toolbar button in module editor, as it uses a different ID..
m_frame->SetToolID( ID_PCB_PLACE_GRID_COORD_BUTT, wxCURSOR_PENCIL, _( "Adjust grid origin" ) );
picker->SetClickHandler( boost::bind( setOrigin, getView(), m_frame, m_gridOrigin, _1 ) );
picker->Activate();
Wait();
}
return 0;
}
int PCBNEW_CONTROL::GridResetOrigin( const TOOL_EVENT& aEvent )
{
getModel<BOARD>()->SetGridOrigin( wxPoint( 0, 0 ) );
m_gridOrigin->SetPosition( VECTOR2D( 0, 0 ) );
return 0;
}
int PCBNEW_CONTROL::GridPreset( const TOOL_EVENT& aEvent )
{
long idx = aEvent.Parameter<long>();
m_frame->SetPresetGrid( idx );
updateGrid();
return 0;
}
// Miscellaneous
int PCBNEW_CONTROL::ResetCoords( const TOOL_EVENT& aEvent )
{
VECTOR2I cursorPos = getViewControls()->GetCursorPosition();
m_frame->GetScreen()->m_O_Curseur = wxPoint( cursorPos.x, cursorPos.y );
m_frame->UpdateStatusBar();
return 0;
}
int PCBNEW_CONTROL::SwitchCursor( const TOOL_EVENT& aEvent )
{
const unsigned int BIG_CURSOR = 8000;
const unsigned int SMALL_CURSOR = 80;
PCB_BASE_FRAME* frame = getEditFrame<PCB_BASE_FRAME>();
KIGFX::GAL* gal = frame->GetGalCanvas()->GetGAL();
gal->SetCursorSize( frame->GetCursorShape() ? BIG_CURSOR : SMALL_CURSOR );
return 0;
}
int PCBNEW_CONTROL::SwitchUnits( const TOOL_EVENT& aEvent )
{
// TODO should not it be refactored to pcb_frame member function?
wxCommandEvent evt( wxEVT_COMMAND_MENU_SELECTED );
if( g_UserUnit == INCHES )
evt.SetId( ID_TB_OPTIONS_SELECT_UNIT_MM );
else
evt.SetId( ID_TB_OPTIONS_SELECT_UNIT_INCH );
m_frame->ProcessEvent( evt );
return 0;
}
static bool deleteItem( TOOL_MANAGER* aToolMgr, const VECTOR2D& aPosition )
{
SELECTION_TOOL* selectionTool = aToolMgr->GetTool<SELECTION_TOOL>();
assert( selectionTool );
aToolMgr->RunAction( COMMON_ACTIONS::selectionClear, true );
aToolMgr->RunAction( COMMON_ACTIONS::selectionCursor, true );
selectionTool->SanitizeSelection();
const SELECTION& selection = selectionTool->GetSelection();
if( selection.Empty() )
return true;
bool canBeRemoved = ( selection.Item<EDA_ITEM>( 0 )->Type() != PCB_MODULE_T );
if( canBeRemoved || IsOK( aToolMgr->GetEditFrame(), _( "Are you sure you want to delete item?" ) ) )
aToolMgr->RunAction( COMMON_ACTIONS::remove, true );
else
aToolMgr->RunAction( COMMON_ACTIONS::selectionClear, true );
return true;
}
int PCBNEW_CONTROL::DeleteItemCursor( const TOOL_EVENT& aEvent )
{
Activate();
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
assert( picker );
// TODO it will not check the toolbar button in the module editor, as it uses a different ID..
m_frame->SetToolID( ID_PCB_DELETE_ITEM_BUTT, wxCURSOR_PENCIL, _( "Delete item" ) );
picker->SetSnapping( false );
picker->SetClickHandler( boost::bind( deleteItem, m_toolMgr, _1 ) );
picker->Activate();
Wait();
return 0;
}
int PCBNEW_CONTROL::AppendBoard( const TOOL_EVENT& aEvent )
{
int open_ctl;
wxString fileName;
PICKED_ITEMS_LIST undoListPicker;
ITEM_PICKER picker( NULL, UR_NEW );
PCB_EDIT_FRAME* editFrame = dynamic_cast<PCB_EDIT_FRAME*>( m_frame );
BOARD* board = getModel<BOARD>();
KIGFX::VIEW* view = getView();
if( !editFrame )
return 0;
// Pick a file to append
if( !AskLoadBoardFileName( editFrame, &open_ctl, &fileName, true ) )
return 0;
IO_MGR::PCB_FILE_T pluginType = plugin_type( fileName, open_ctl );
PLUGIN::RELEASER pi( IO_MGR::PluginFind( pluginType ) );
// keep track of existing items, in order to know what are the new items
// (for undo command for instance)
// Tracks are inserted, not appended, so mark the existing tracks to know what are the new tracks
for( TRACK* track = board->m_Track; track; track = track->Next() )
track->SetFlags( FLAG0 );
// Other items are appended to the item list, so keep trace to the last existing item is enough
MODULE* module = board->m_Modules.GetLast();
BOARD_ITEM* drawing = board->m_Drawings.GetLast();
int zonescount = board->GetAreaCount();
// Keep also the count of copper layers, to adjust if necessary
int initialCopperLayerCount = board->GetCopperLayerCount();
LSET initialEnabledLayers = board->GetEnabledLayers();
// Load the data
try
{
PROPERTIES props;
char xbuf[30];
char ybuf[30];
// EAGLE_PLUGIN can use this info to center the BOARD, but it does not yet.
sprintf( xbuf, "%d", editFrame->GetPageSizeIU().x );
sprintf( ybuf, "%d", editFrame->GetPageSizeIU().y );
props["page_width"] = xbuf;
props["page_height"] = ybuf;
editFrame->GetDesignSettings().m_NetClasses.Clear();
pi->Load( fileName, board, &props );
}
catch( const IO_ERROR& ioe )
{
wxString msg = wxString::Format( _( "Error loading board.\n%s" ), GetChars( ioe.errorText ));
DisplayError( editFrame, msg );
return 0;
}
// rebuild nets and ratsnest before any use of nets
board->BuildListOfNets();
board->GetRatsnest()->Recalculate();
board->SynchronizeNetsAndNetClasses();
m_toolMgr->RunAction( COMMON_ACTIONS::selectionClear, true );
// Process the new items
for( TRACK* track = board->m_Track; track; track = track->Next() )
{
if( track->GetFlags() & FLAG0 )
{
track->ClearFlags( FLAG0 );
continue;
}
picker.SetItem( track );
undoListPicker.PushItem( picker );
view->Add( track );
m_toolMgr->RunAction( COMMON_ACTIONS::selectItem, true, track );
}
module = module ? module->Next() : board->m_Modules;
for( ; module; module = module->Next() )
{
picker.SetItem( module );
undoListPicker.PushItem( picker );
module->RunOnChildren( boost::bind( &KIGFX::VIEW::Add, view, _1 ) );
view->Add( module );
m_toolMgr->RunAction( COMMON_ACTIONS::selectItem, true, module );
}
drawing = drawing ? drawing->Next() : board->m_Drawings;
for( ; drawing; drawing = drawing->Next() )
{
picker.SetItem( drawing );
undoListPicker.PushItem( picker );
view->Add( drawing );
m_toolMgr->RunAction( COMMON_ACTIONS::selectItem, true, drawing );
}
for( ZONE_CONTAINER* zone = board->GetArea( zonescount ); zone;
zone = board->GetArea( zonescount ) )
{
picker.SetItem( zone );
undoListPicker.PushItem( picker );
zonescount++;
view->Add( zone );
m_toolMgr->RunAction( COMMON_ACTIONS::selectItem, true, zone );
}
if( undoListPicker.GetCount() == 0 )
return 0;
editFrame->SaveCopyInUndoList( undoListPicker, UR_NEW );
// Synchronize layers
// we should not ask PLUGINs to do these items:
int copperLayerCount = board->GetCopperLayerCount();
if( copperLayerCount > initialCopperLayerCount )
board->SetCopperLayerCount( copperLayerCount );
// Enable all used layers, and make them visible:
LSET enabledLayers = board->GetEnabledLayers();
enabledLayers |= initialEnabledLayers;
board->SetEnabledLayers( enabledLayers );
board->SetVisibleLayers( enabledLayers );
editFrame->ReCreateLayerBox();
editFrame->ReFillLayerWidget();
static_cast<PCB_DRAW_PANEL_GAL*>( editFrame->GetGalCanvas() )->SyncLayersVisibility( board );
// Start dragging the appended board
VECTOR2D v( static_cast<BOARD_ITEM*>( undoListPicker.GetPickedItem( 0 ) )->GetPosition() );
getViewControls()->WarpCursor( v, true, true );
m_toolMgr->InvokeTool( "pcbnew.InteractiveEdit" );
return 0;
}
int PCBNEW_CONTROL::ShowHelp( const TOOL_EVENT& aEvent )
{
DisplayHotkeyList( m_frame, m_frame->GetHotkeyConfig() );
return 0;
}
int PCBNEW_CONTROL::ToBeDone( const TOOL_EVENT& aEvent )
{
DisplayInfoMessage( m_frame, _( "Not available in OpenGL/Cairo canvases." ) );
return 0;
}
void PCBNEW_CONTROL::SetTransitions()
{
// View controls
Go( &PCBNEW_CONTROL::ZoomInOut, COMMON_ACTIONS::zoomIn.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoomInOut, COMMON_ACTIONS::zoomOut.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoomInOutCenter, COMMON_ACTIONS::zoomInCenter.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoomInOutCenter, COMMON_ACTIONS::zoomOutCenter.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoomCenter, COMMON_ACTIONS::zoomCenter.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoomFitScreen, COMMON_ACTIONS::zoomFitScreen.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoomPreset, COMMON_ACTIONS::zoomPreset.MakeEvent() );
// Display modes
Go( &PCBNEW_CONTROL::TrackDisplayMode, COMMON_ACTIONS::trackDisplayMode.MakeEvent() );
Go( &PCBNEW_CONTROL::PadDisplayMode, COMMON_ACTIONS::padDisplayMode.MakeEvent() );
Go( &PCBNEW_CONTROL::ViaDisplayMode, COMMON_ACTIONS::viaDisplayMode.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoneDisplayMode, COMMON_ACTIONS::zoneDisplayEnable.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoneDisplayMode, COMMON_ACTIONS::zoneDisplayDisable.MakeEvent() );
Go( &PCBNEW_CONTROL::ZoneDisplayMode, COMMON_ACTIONS::zoneDisplayOutlines.MakeEvent() );
Go( &PCBNEW_CONTROL::HighContrastMode, COMMON_ACTIONS::highContrastMode.MakeEvent() );
Go( &PCBNEW_CONTROL::HighContrastInc, COMMON_ACTIONS::highContrastInc.MakeEvent() );
Go( &PCBNEW_CONTROL::HighContrastDec, COMMON_ACTIONS::highContrastDec.MakeEvent() );
// Layer control
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerTop.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerInner1.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerInner2.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerInner3.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerInner4.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerInner5.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerInner6.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerSwitch, COMMON_ACTIONS::layerBottom.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerNext, COMMON_ACTIONS::layerNext.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerPrev, COMMON_ACTIONS::layerPrev.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerToggle, COMMON_ACTIONS::layerToggle.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerAlphaInc, COMMON_ACTIONS::layerAlphaInc.MakeEvent() );
Go( &PCBNEW_CONTROL::LayerAlphaDec, COMMON_ACTIONS::layerAlphaDec.MakeEvent() );
// Cursor control
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorUp.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorDown.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorLeft.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorRight.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorUpFast.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorDownFast.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorLeftFast.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorRightFast.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorClick.MakeEvent() );
Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorDblClick.MakeEvent() );
// Pan control
Go( &PCBNEW_CONTROL::PanControl, COMMON_ACTIONS::panUp.MakeEvent() );
Go( &PCBNEW_CONTROL::PanControl, COMMON_ACTIONS::panDown.MakeEvent() );
Go( &PCBNEW_CONTROL::PanControl, COMMON_ACTIONS::panLeft.MakeEvent() );
Go( &PCBNEW_CONTROL::PanControl, COMMON_ACTIONS::panRight.MakeEvent() );
// Grid control
Go( &PCBNEW_CONTROL::GridFast1, COMMON_ACTIONS::gridFast1.MakeEvent() );
Go( &PCBNEW_CONTROL::GridFast2, COMMON_ACTIONS::gridFast2.MakeEvent() );
Go( &PCBNEW_CONTROL::GridNext, COMMON_ACTIONS::gridNext.MakeEvent() );
Go( &PCBNEW_CONTROL::GridPrev, COMMON_ACTIONS::gridPrev.MakeEvent() );
Go( &PCBNEW_CONTROL::GridSetOrigin, COMMON_ACTIONS::gridSetOrigin.MakeEvent() );
Go( &PCBNEW_CONTROL::GridResetOrigin, COMMON_ACTIONS::gridResetOrigin.MakeEvent() );
Go( &PCBNEW_CONTROL::GridPreset, COMMON_ACTIONS::gridPreset.MakeEvent() );
// Miscellaneous
Go( &PCBNEW_CONTROL::ResetCoords, COMMON_ACTIONS::resetCoords.MakeEvent() );
Go( &PCBNEW_CONTROL::SwitchCursor, COMMON_ACTIONS::switchCursor.MakeEvent() );
Go( &PCBNEW_CONTROL::SwitchUnits, COMMON_ACTIONS::switchUnits.MakeEvent() );
Go( &PCBNEW_CONTROL::DeleteItemCursor, COMMON_ACTIONS::deleteItemCursor.MakeEvent() );
Go( &PCBNEW_CONTROL::AppendBoard, COMMON_ACTIONS::appendBoard.MakeEvent() );
Go( &PCBNEW_CONTROL::ShowHelp, COMMON_ACTIONS::showHelp.MakeEvent() );
Go( &PCBNEW_CONTROL::ToBeDone, COMMON_ACTIONS::toBeDone.MakeEvent() );
}
void PCBNEW_CONTROL::updateGrid()
{
BASE_SCREEN* screen = m_frame->GetScreen();
//GRID_TYPE grid = screen->GetGrid( idx );
getView()->GetGAL()->SetGridSize( VECTOR2D( screen->GetGridSize() ) );
getView()->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
}
|