KiCad PCB EDA Suite
sch_drawing_tools.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) 2019 CERN
5  * Copyright (C) 2019-2020 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 "sch_drawing_tools.h"
26 #include "ee_selection_tool.h"
27 #include "ee_point_editor.h"
28 #include <ee_actions.h>
29 #include <sch_edit_frame.h>
30 #include <project.h>
31 #include <id.h>
32 #include <eeschema_id.h>
33 #include <confirm.h>
34 #include <view/view_controls.h>
35 #include <view/view.h>
36 #include <sch_component.h>
37 #include <sch_no_connect.h>
38 #include <sch_line.h>
39 #include <sch_junction.h>
40 #include <sch_bus_entry.h>
41 #include <sch_text.h>
42 #include <sch_sheet.h>
43 #include <sch_bitmap.h>
44 #include <schematic.h>
45 #include <class_library.h>
46 #include <eeschema_settings.h>
48 
50  EE_TOOL_BASE<SCH_EDIT_FRAME>( "eeschema.InteractiveDrawing" ),
51  m_lastGlobalLabelShape( PINSHEETLABEL_SHAPE::PS_INPUT ),
52  m_lastTextOrientation( LABEL_SPIN_STYLE::LEFT ),
53  m_lastTextBold( false ),
54  m_lastTextItalic( false )
55 {
56 }
57 
58 
60 {
62 
63  auto belowRootSheetCondition =
64  [&]( const SELECTION& aSel )
65  {
66  return m_frame->GetCurrentSheet().Last() != &m_frame->Schematic().Root();
67  };
68 
69  auto& ctxMenu = m_menu.GetMenu();
70  ctxMenu.AddItem( EE_ACTIONS::leaveSheet, belowRootSheetCondition, 2 );
71 
72  return true;
73 }
74 
75 
77 {
78  SCH_COMPONENT* component = aEvent.Parameter<SCH_COMPONENT*>();
79  SCHLIB_FILTER filter;
80  std::vector<COMPONENT_SELECTION>* historyList = nullptr;
81 
82  if( aEvent.IsAction( &EE_ACTIONS::placeSymbol ) )
83  historyList = &m_symbolHistoryList;
84  else if (aEvent.IsAction( &EE_ACTIONS::placePower ) )
85  {
86  historyList = &m_powerHistoryList;
87  filter.FilterPowerParts( true );
88  }
89  else
90  wxFAIL_MSG( "PlaceCompontent(): unexpected request" );
91 
92  getViewControls()->ShowCursor( true );
93 
94  // If a component was passed in get it ready for placement.
95  if( component )
96  {
97  component->SetFlags( IS_NEW | IS_MOVED );
98 
100  m_selectionTool->AddItemToSel( component );
101  }
102 
103  std::string tool = aEvent.GetCommandStr().get();
104  m_frame->PushTool( tool );
105  Activate();
106 
107  // Prime the pump
108  if( component )
109  {
110  getViewControls()->WarpCursor( getViewControls()->GetMousePosition( false ) );
112  }
113  else if( aEvent.HasPosition() )
115 
116  // Main loop: keep receiving events
117  while( TOOL_EVENT* evt = Wait() )
118  {
119  m_frame->GetCanvas()->SetCurrentCursor( component ? wxCURSOR_ARROW : wxCURSOR_PENCIL );
120  VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
121 
122  auto cleanup = [&] () {
124  m_view->ClearPreview();
125  delete component;
126  component = nullptr;
127  };
128 
129  if( evt->IsCancelInteractive() )
130  {
131  if( component )
132  cleanup();
133  else
134  {
135  m_frame->PopTool( tool );
136  break;
137  }
138  }
139  else if( evt->IsActivate() )
140  {
141  if( component )
142  cleanup();
143 
144  if( evt->IsMoveTool() )
145  {
146  // leave ourselves on the stack so we come back after the move
147  break;
148  }
149  else
150  {
151  m_frame->PopTool( tool );
152  break;
153  }
154  }
155  else if( evt->IsClick( BUT_LEFT ) )
156  {
157  if( !component )
158  {
160 
161  // Pick the module to be placed
162  bool footprintPreviews = m_frame->eeconfig()->m_Appearance.footprint_preview;
163  COMPONENT_SELECTION sel = m_frame->SelectCompFromLibTree( &filter, *historyList,
164  true, 1, 1,
165  footprintPreviews );
166 
167  // Restore cursor after dialog
168  getViewControls()->WarpCursor( getViewControls()->GetCursorPosition(), true );
169 
170  LIB_PART* part = sel.LibId.IsValid() ? m_frame->GetLibPart( sel.LibId ) : nullptr;
171 
172  if( !part )
173  continue;
174 
175  component = new SCH_COMPONENT(
176  *part, &m_frame->GetCurrentSheet(), sel, (wxPoint) cursorPos );
177  component->SetParent( m_frame->GetCurrentSheet().LastScreen() );
178  component->SetFlags( IS_NEW | IS_MOVED );
179 
181  component->AutoplaceFields( /* aScreen */ NULL, /* aManual */ false );
182 
183  m_frame->SaveCopyForRepeatItem( component );
184 
185  m_view->ClearPreview();
186  m_view->AddToPreview( component->Clone() );
187  m_selectionTool->AddItemToSel( component );
188  }
189  else
190  {
191  SCH_COMPONENT* next_comp = nullptr;
192 
193  m_view->ClearPreview();
194  m_frame->AddItemToScreenAndUndoList( component );
195 
196  EE_SELECTION new_sel;
197  new_sel.Add( component );
198 
200  m_frame->OnModify();
201 
204  {
205  int new_unit = component->GetUnit();
206 
208  && component->GetUnit() < component->GetUnitCount() )
209  new_unit++;
210  else
211  new_unit = 1;
212 
213  // We are either stepping to the next unit or next component
214  if( m_frame->eeconfig()->m_SymChooserPanel.keep_symbol || new_unit > 1 )
215  {
216  // Deselect the last placed symbol: obviously we do not want to
217  // apply some changes (like rotation, mirror...) to previously placed
218  // symbols.
220 
221  next_comp = static_cast<SCH_COMPONENT*>( component->Duplicate() );
222  next_comp->SetFlags( IS_NEW | IS_MOVED );
223  next_comp->SetUnit( new_unit );
224  next_comp->SetUnitSelection( &m_frame->GetCurrentSheet(), new_unit );
225 
227  component->AutoplaceFields( /* aScreen */ NULL, /* aManual */ false );
228 
229  m_frame->SaveCopyForRepeatItem( next_comp );
230  m_view->AddToPreview( next_comp->Clone() );
231  m_selectionTool->AddItemToSel( next_comp );
232  }
233  }
234 
235  component = next_comp;
236  }
237  }
238  else if( evt->IsClick( BUT_RIGHT ) )
239  {
240  // Warp after context menu only if dragging...
241  if( !component )
243 
245  }
246  else if( evt->Category() == TC_COMMAND && evt->Action() == TA_CHOICE_MENU_CHOICE )
247  {
248  if( evt->GetCommandId().get() >= ID_POPUP_SCH_SELECT_UNIT_CMP
249  && evt->GetCommandId().get() <= ID_POPUP_SCH_SELECT_UNIT_CMP_MAX )
250  {
251  int unit = evt->GetCommandId().get() - ID_POPUP_SCH_SELECT_UNIT_CMP;
252 
253  if( component )
254  {
255  m_frame->SelectUnit( component, unit );
257  }
258  }
259  }
260  else if( component && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
261  {
262  component->SetPosition( (wxPoint)cursorPos );
263  m_view->ClearPreview();
264  m_view->AddToPreview( component->Clone() );
265  }
266  else
267  evt->SetPassEvent();
268 
269  // Enable autopanning and cursor capture only when there is a module to be placed
270  getViewControls()->SetAutoPan( component != nullptr );
271  getViewControls()->CaptureCursor( component != nullptr );
272  }
273 
274  return 0;
275 }
276 
277 
279 {
280  SCH_BITMAP* image = aEvent.Parameter<SCH_BITMAP*>();
281  bool immediateMode = image;
282  VECTOR2I cursorPos = getViewControls()->GetCursorPosition();
283 
285  getViewControls()->ShowCursor( true );
286 
287  // Add all the drawable parts to preview
288  if( image )
289  {
290  image->SetPosition( (wxPoint)cursorPos );
291  m_view->ClearPreview();
292  m_view->AddToPreview( image->Clone() );
293  }
294 
295  std::string tool = aEvent.GetCommandStr().get();
296  m_frame->PushTool( tool );
297  Activate();
298 
299  // Prime the pump
300  if( image )
302  else if( aEvent.HasPosition() )
304 
305  // Main loop: keep receiving events
306  while( TOOL_EVENT* evt = Wait() )
307  {
308  m_frame->GetCanvas()->SetCurrentCursor( image ? wxCURSOR_ARROW : wxCURSOR_PENCIL );
309  cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
310 
311  auto cleanup = [&] () {
313  m_view->ClearPreview();
314  delete image;
315  image = nullptr;
316  };
317 
318  if( evt->IsCancelInteractive() )
319  {
320  if( image )
321  cleanup();
322  else
323  {
324  m_frame->PopTool( tool );
325  break;
326  }
327 
328  if( immediateMode )
329  {
330  m_frame->PopTool( tool );
331  break;
332  }
333  }
334  else if( evt->IsActivate() )
335  {
336  if( image )
337  cleanup();
338 
339  if( evt->IsMoveTool() )
340  {
341  // leave ourselves on the stack so we come back after the move
342  break;
343  }
344  else
345  {
346  m_frame->PopTool( tool );
347  break;
348  }
349  }
350  else if( evt->IsClick( BUT_LEFT ) )
351  {
352  if( !image )
353  {
355  wxFileDialog dlg( m_frame, _( "Choose Image" ), wxEmptyString, wxEmptyString,
356  _( "Image Files " ) + wxImage::GetImageExtWildcard(), wxFD_OPEN );
357 
358  if( dlg.ShowModal() != wxID_OK )
359  continue;
360 
361  // Restore cursor after dialog
362  getViewControls()->WarpCursor( getViewControls()->GetCursorPosition(), true );
363 
364  wxString fullFilename = dlg.GetPath();
365 
366  if( wxFileExists( fullFilename ) )
367  image = new SCH_BITMAP( (wxPoint)cursorPos );
368 
369  if( !image || !image->ReadImageFile( fullFilename ) )
370  {
371  wxMessageBox( _( "Couldn't load image from \"%s\"" ), fullFilename );
372  delete image;
373  image = nullptr;
374  continue;
375  }
376 
377  image->SetFlags( IS_NEW | IS_MOVED );
378 
379  m_frame->SaveCopyForRepeatItem( image );
380 
381  m_view->ClearPreview();
382  m_view->AddToPreview( image->Clone() );
383  m_view->RecacheAllItems(); // Bitmaps are cached in Opengl
384 
385  m_selectionTool->AddItemToSel( image );
386 
387  getViewControls()->SetCursorPosition( cursorPos, false );
388  }
389  else
390  {
392  image = nullptr;
394 
395  m_view->ClearPreview();
396 
397  if( immediateMode )
398  {
399  m_frame->PopTool( tool );
400  break;
401  }
402  }
403  }
404  else if( evt->IsClick( BUT_RIGHT ) )
405  {
406  // Warp after context menu only if dragging...
407  if( !image )
409 
411  }
412  else if( image && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
413  {
414  image->SetPosition( (wxPoint)cursorPos );
415  m_view->ClearPreview();
416  m_view->AddToPreview( image->Clone() );
417  m_view->RecacheAllItems(); // Bitmaps are cached in Opengl
418  }
419  else
420  evt->SetPassEvent();
421 
422  // Enable autopanning and cursor capture only when there is a module to be placed
423  getViewControls()->SetAutoPan( image != nullptr );
424  getViewControls()->CaptureCursor( image != nullptr );
425  }
426 
427  return 0;
428 }
429 
430 
432 {
433  wxPoint cursorPos;
434  KICAD_T type = aEvent.Parameter<KICAD_T>();
435 
436  if( type == SCH_JUNCTION_T && aEvent.HasPosition() )
437  {
438  EE_SELECTION& selection = m_selectionTool->GetSelection();
439  SCH_LINE* wire = dynamic_cast<SCH_LINE*>( selection.Front() );
440 
441  if( wire )
442  {
443  SEG seg( wire->GetStartPoint(), wire->GetEndPoint() );
444  VECTOR2I nearest = seg.NearestPoint( getViewControls()->GetCursorPosition() );
445  getViewControls()->SetCrossHairCursorPosition( nearest, false );
446  getViewControls()->WarpCursor( getViewControls()->GetCursorPosition(), true );
447  }
448  }
449 
451  getViewControls()->ShowCursor( true );
452  getViewControls()->SetSnapping( true );
453 
454  SCH_ITEM* previewItem;
455  switch( type )
456  {
457  case SCH_NO_CONNECT_T:
458  previewItem = new SCH_NO_CONNECT( cursorPos );
459  break;
460  case SCH_JUNCTION_T:
461  previewItem = new SCH_JUNCTION( cursorPos );
462  break;
464  previewItem = new SCH_BUS_WIRE_ENTRY( cursorPos );
465  break;
466  default:
467  wxASSERT_MSG( false, "Unknown item type in SCH_DRAWING_TOOLS::SingleClickPlace" );
468  return 0;
469  }
470 
471  previewItem->SetParent( m_frame->GetScreen() );
472 
473  m_view->ClearPreview();
474  m_view->AddToPreview( previewItem->Clone() );
475 
476  std::string tool = aEvent.GetCommandStr().get();
477  m_frame->PushTool( tool );
478  Activate();
479 
480  // Prime the pump
481  if( aEvent.HasPosition() )
483 
484  // Main loop: keep receiving events
485  while( TOOL_EVENT* evt = Wait() )
486  {
487  m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_ARROW );
488  cursorPos = (wxPoint) getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
489 
490  if( evt->IsCancelInteractive() )
491  {
492  m_frame->PopTool( tool );
493  break;
494  }
495  else if( evt->IsActivate() )
496  {
497  if( evt->IsMoveTool() )
498  {
499  // leave ourselves on the stack so we come back after the move
500  break;
501  }
502  else
503  {
504  m_frame->PopTool( tool );
505  break;
506  }
507  }
508  else if( evt->IsClick( BUT_LEFT ) )
509  {
510  if( !m_frame->GetScreen()->GetItem( cursorPos, 0, type ) )
511  {
512  if( type == SCH_JUNCTION_T )
513  m_frame->AddJunction( cursorPos );
514  else
515  {
516  SCH_ITEM* newItem = static_cast<SCH_ITEM*>( previewItem->Clone() );
517  newItem->SetPosition( cursorPos );
518  newItem->SetFlags( IS_NEW );
519 
521  m_frame->SaveCopyForRepeatItem( newItem );
522 
525  m_frame->OnModify();
526  }
527  }
528  }
529  else if( evt->IsClick( BUT_RIGHT ) )
530  {
532  }
533  else if( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() )
534  {
535  previewItem->SetPosition( (wxPoint)cursorPos );
536  m_view->ClearPreview();
537  m_view->AddToPreview( previewItem->Clone() );
538  }
539  else if( evt->Category() == TC_COMMAND )
540  {
541  if( ( type == SCH_BUS_WIRE_ENTRY_T )
542  && ( evt->IsAction( &EE_ACTIONS::rotateCW )
543  || evt->IsAction( &EE_ACTIONS::rotateCCW )
544  || evt->IsAction( &EE_ACTIONS::mirrorX )
545  || evt->IsAction( &EE_ACTIONS::mirrorY ) ) )
546  {
547  SCH_BUS_ENTRY_BASE* busItem = static_cast<SCH_BUS_ENTRY_BASE*>( previewItem );
548 
549  // The bus entries only rotate in one direction
550  if( evt->IsAction( &EE_ACTIONS::rotateCW )
551  || evt->IsAction( &EE_ACTIONS::rotateCCW ) )
552  busItem->Rotate( busItem->GetPosition() );
553  else if( evt->IsAction( &EE_ACTIONS::mirrorX ) )
554  busItem->MirrorX( busItem->GetPosition().x );
555  else if( evt->IsAction( &EE_ACTIONS::mirrorY ) )
556  busItem->MirrorY( busItem->GetPosition().y );
557 
558  m_view->ClearPreview();
559  m_view->AddToPreview( previewItem->Clone() );
560  }
561  }
562  else
563  evt->SetPassEvent();
564  }
565 
566  delete previewItem;
567  m_view->ClearPreview();
568 
569  return 0;
570 }
571 
572 
573 // History lists for placing labels and text
574 
576 {
577  if( m_queuedTexts.empty() )
578  return nullptr;
579 
580  auto next_text = std::move( m_queuedTexts.front() );
581  m_queuedTexts.pop_front();
582 
583  return next_text.release();
584 }
585 
586 
587 SCH_TEXT* SCH_DRAWING_TOOLS::createNewText( const VECTOR2I& aPosition, int aType )
588 {
589  SCHEMATIC* schematic = getModel<SCHEMATIC>();
590  SCHEMATIC_SETTINGS& settings = schematic->Settings();
591  SCH_TEXT* textItem = nullptr;
592 
593  m_queuedTexts.clear();
594 
595  switch( aType )
596  {
597  case LAYER_NOTES:
598  textItem = new SCH_TEXT( (wxPoint) aPosition );
599  break;
600 
601  case LAYER_LOCLABEL:
602  textItem = new SCH_LABEL( (wxPoint) aPosition );
603  break;
604 
605  case LAYER_HIERLABEL:
606  textItem = new SCH_HIERLABEL( (wxPoint) aPosition );
607  textItem->SetShape( m_lastGlobalLabelShape );
608  break;
609 
610  case LAYER_GLOBLABEL:
611  textItem = new SCH_GLOBALLABEL( (wxPoint) aPosition );
612  textItem->SetShape( m_lastGlobalLabelShape );
613  break;
614 
615  default:
616  DisplayError( m_frame, wxT( "SCH_EDIT_FRAME::CreateNewText() Internal error" ) );
617  return nullptr;
618  }
619 
620  textItem->SetParent( schematic );
621  textItem->SetBold( m_lastTextBold );
622  textItem->SetItalic( m_lastTextItalic );
624  textItem->SetTextSize( wxSize( settings.m_DefaultTextSize, settings.m_DefaultTextSize ) );
625  textItem->SetFlags( IS_NEW | IS_MOVED );
626 
627  DIALOG_LABEL_EDITOR dlg( m_frame, textItem );
628 
629  if( dlg.ShowModal() != wxID_OK || textItem->GetText().IsEmpty() )
630  {
631  delete textItem;
632  return nullptr;
633  }
634 
635  if( aType != LAYER_NOTES )
636  {
637  UTF8 text( textItem->GetText() );
638  int brace_count = 0;
639  int bracket_count = 0;
640  bool last_space = false;
641  UTF8 token;
642 
643  for( auto chIt = text.ubegin(); chIt != text.uend(); chIt++ )
644  {
645  switch( *chIt )
646  {
647  case '{':
648  brace_count++;
649  last_space = false;
650  break;
651 
652  case '[':
653  bracket_count++;
654  last_space = false;
655  break;
656 
657  case '}':
658  brace_count = std::max( 0, brace_count - 1 );
659  last_space = false;
660  break;
661 
662  case ']':
663  bracket_count = std::max( 0, bracket_count - 1 );
664  last_space = false;
665  break;
666 
667  case ' ':
668  case '\n':
669  case '\r':
670  case '\t':
671  if( !token.empty() && bracket_count == 0 && brace_count == 0 )
672  {
673  std::unique_ptr<SCH_TEXT> nextitem( static_cast<SCH_TEXT*>( textItem->Clone() ) );
674  nextitem->SetText( token.wx_str() );
675  m_queuedTexts.push_back( std::move( nextitem ) );
676  token.clear();
677  continue;
678  }
679 
680  // Skip leading whitespace
681  if( token.empty() || last_space )
682  continue;
683 
684  last_space = true;
685  break;
686 
687  default:
688  last_space = false;
689  break;
690  }
691 
692  token += *chIt;
693  }
694 
695  if( !token.empty() )
696  {
697  std::unique_ptr<SCH_TEXT> nextitem( static_cast<SCH_TEXT*>( textItem->Clone() ) );
698  nextitem->SetText( token.wx_str() );
699  m_queuedTexts.push_back( std::move( nextitem ) );
700  }
701 
702  delete textItem;
703  textItem = getNextNewText();
704 
705  if( !textItem )
706  return nullptr;
707  }
708 
709  m_lastTextBold = textItem->IsBold();
710  m_lastTextItalic = textItem->IsItalic();
712 
713  if( textItem->Type() == SCH_GLOBAL_LABEL_T || textItem->Type() == SCH_HIER_LABEL_T )
714  m_lastGlobalLabelShape = textItem->GetShape();
715 
716  return textItem;
717 }
718 
719 
721 {
722  EDA_ITEM* item = nullptr;
723  bool importMode = aEvent.IsAction( &EE_ACTIONS::importSheetPin );
724  KICAD_T type = aEvent.Parameter<KICAD_T>();
725 
727  getViewControls()->ShowCursor( true );
728 
729  std::string tool = aEvent.GetCommandStr().get();
730  m_frame->PushTool( tool );
731  Activate();
732 
733  // Prime the pump
734  if( aEvent.HasPosition() )
736 
737  // Main loop: keep receiving events
738  while( TOOL_EVENT* evt = Wait() )
739  {
740  m_frame->GetCanvas()->SetCurrentCursor( item ? wxCURSOR_ARROW : wxCURSOR_PENCIL );
741  VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
742 
743  auto cleanup = [&] () {
745  m_view->ClearPreview();
746  delete item;
747  item = nullptr;
748  };
749 
750  if( evt->IsCancelInteractive() )
751  {
752  if( item )
753  cleanup();
754  else
755  {
756  m_frame->PopTool( tool );
757  break;
758  }
759  }
760  else if( evt->IsActivate() )
761  {
762  if( item )
763  cleanup();
764 
765  if( evt->IsPointEditor() )
766  {
767  // don't exit (the point editor runs in the background)
768  }
769  else if( evt->IsMoveTool() )
770  {
771  // leave ourselves on the stack so we come back after the move
772  break;
773  }
774  else
775  {
776  m_frame->PopTool( tool );
777  break;
778  }
779  }
780  else if( evt->IsClick( BUT_LEFT ) )
781  {
782  // First click creates...
783  if( !item )
784  {
786 
787  switch( type )
788  {
789  case SCH_LABEL_T:
790  item = createNewText( cursorPos, LAYER_LOCLABEL );
791  break;
792  case SCH_HIER_LABEL_T:
793  item = createNewText( cursorPos, LAYER_HIERLABEL );
794  break;
795  case SCH_GLOBAL_LABEL_T:
796  item = createNewText( cursorPos, LAYER_GLOBLABEL );
797  break;
798  case SCH_TEXT_T:
799  item = createNewText( cursorPos, LAYER_NOTES );
800  break;
801  case SCH_SHEET_PIN_T:
802  {
803  SCH_HIERLABEL* label = nullptr;
804  SCH_SHEET* sheet = nullptr;
805 
806  if( m_selectionTool->SelectPoint( cursorPos, EE_COLLECTOR::SheetsOnly, &item ) )
807  sheet = dynamic_cast<SCH_SHEET*>( item );
808 
809  item = nullptr;
810 
811  if( !sheet )
812  {
813  m_statusPopup.reset( new STATUS_TEXT_POPUP( m_frame ) );
814  m_statusPopup->SetText( _( "Click over a sheet." ) );
815  m_statusPopup->Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
816  m_statusPopup->PopupFor( 2000 );
817  break;
818  }
819 
820  if( importMode )
821  {
822  label = m_frame->ImportHierLabel( sheet );
823 
824  if( !label )
825  {
826  m_statusPopup.reset( new STATUS_TEXT_POPUP( m_frame ) );
827  m_statusPopup->SetText( _( "No new hierarchical labels found." ) );
828  m_statusPopup->Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
829  m_statusPopup->PopupFor( 2000 );
830  break;
831  }
832  }
833 
834  item = m_frame->CreateSheetPin( sheet, label );
835  break;
836  }
837  default:
838  break;
839  }
840 
841  // Restore cursor after dialog
842  getViewControls()->WarpCursor( getViewControls()->GetCursorPosition(), true );
843 
844  if( item )
845  {
846  item->SetFlags( IS_NEW | IS_MOVED );
847  m_view->ClearPreview();
848  m_view->AddToPreview( item->Clone() );
849  m_selectionTool->AddItemToSel( item );
850  }
851 
852  getViewControls()->SetCursorPosition( cursorPos, false );
853  }
854 
855  // ... and second click places:
856  else
857  {
858  item->ClearFlags( IS_MOVED );
860  item = getNextNewText();
861 
862  if( item )
863  {
865  item->SetFlags( IS_NEW | IS_MOVED );
866  m_view->ClearPreview();
867  m_view->AddToPreview( item->Clone() );
868  m_selectionTool->AddItemToSel( item );
869  }
870  else
871  {
872  m_view->ClearPreview();
873  }
874  }
875  }
876  else if( evt->IsClick( BUT_RIGHT ) )
877  {
878  // Warp after context menu only if dragging...
879  if( !item )
881 
883  }
884  else if( item && evt->IsSelectionEvent() )
885  {
886  // This happens if our text was replaced out from under us by ConvertTextType()
887  EE_SELECTION& selection = m_selectionTool->GetSelection();
888 
889  if( selection.GetSize() == 1 )
890  {
891  item = (SCH_ITEM*) selection.Front();
892  m_view->ClearPreview();
893  m_view->AddToPreview( item->Clone() );
894  }
895  else
896  item = nullptr;
897  }
898  else if( item && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
899  {
900  static_cast<SCH_ITEM*>( item )->SetPosition( (wxPoint) cursorPos );
901  m_view->ClearPreview();
902  m_view->AddToPreview( item->Clone() );
903  }
904  else
905  evt->SetPassEvent();
906 
907  // Enable autopanning and cursor capture only when there is a module to be placed
908  getViewControls()->SetAutoPan( item != nullptr );
909  getViewControls()->CaptureCursor( item != nullptr );
910  }
911 
912  return 0;
913 }
914 
915 
917 {
919  SCH_SHEET* sheet = nullptr;
920 
922  getViewControls()->ShowCursor( true );
923 
924  std::string tool = aEvent.GetCommandStr().get();
925  m_frame->PushTool( tool );
926  Activate();
927 
928  // Prime the pump
929  if( aEvent.HasPosition() )
931 
932  // Main loop: keep receiving events
933  while( TOOL_EVENT* evt = Wait() )
934  {
935  if( !pointEditor->HasPoint() )
936  m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_PENCIL );
937 
938  VECTOR2I cursorPos = getViewControls()->GetCursorPosition( !evt->Modifier( MD_ALT ) );
939 
940  auto cleanup = [&] () {
942  m_view->ClearPreview();
943  delete sheet;
944  sheet = nullptr;
945  };
946 
947  if( evt->IsCancelInteractive() )
948  {
949  if( sheet )
950  cleanup();
951  else
952  {
953  m_frame->PopTool( tool );
954  break;
955  }
956  }
957  else if( evt->IsActivate() )
958  {
959  if( sheet )
960  cleanup();
961 
962  if( evt->IsPointEditor() )
963  {
964  // don't exit (the point editor runs in the background)
965  }
966  else if( evt->IsMoveTool() )
967  {
968  // leave ourselves on the stack so we come back after the move
969  break;
970  }
971  else
972  {
973  m_frame->PopTool( tool );
974  break;
975  }
976  }
977 
978  else if( evt->IsClick( BUT_LEFT ) && !sheet )
979  {
981 
983 
984  sheet = new SCH_SHEET( m_frame->GetCurrentSheet().Last(),
985  static_cast<wxPoint>( cursorPos ) );
986  sheet->SetFlags( IS_NEW | IS_RESIZED );
987  sheet->SetScreen( NULL );
988  sheet->SetBorderWidth( cfg->m_Drawing.default_line_thickness );
989  sheet->SetBorderColor( cfg->m_Drawing.default_sheet_border_color );
990  sheet->SetBackgroundColor( cfg->m_Drawing.default_sheet_background_color );
991  sheet->GetFields()[ SHEETNAME ].SetText( "Untitled Sheet" );
992  sheet->GetFields()[ SHEETFILENAME ].SetText( "untitled.kicad_sch" );
993  sizeSheet( sheet, cursorPos );
994 
995  m_view->ClearPreview();
996  m_view->AddToPreview( sheet->Clone() );
997  }
998 
999  else if( sheet && ( evt->IsClick( BUT_LEFT )
1000  || evt->IsAction( &EE_ACTIONS::finishSheet ) ) )
1001  {
1002  m_view->ClearPreview();
1003  getViewControls()->SetAutoPan( false );
1004  getViewControls()->CaptureCursor( false );
1005 
1006  if( m_frame->EditSheetProperties( static_cast<SCH_SHEET*>( sheet ),
1007  &m_frame->GetCurrentSheet(), nullptr ) )
1008  {
1009  sheet->AutoplaceFields( /* aScreen */ NULL, /* aManual */ false );
1010 
1013  m_selectionTool->AddItemToSel( sheet );
1014  }
1015  else
1016  {
1017  delete sheet;
1018  }
1019 
1020  sheet = nullptr;
1021  }
1022 
1023  else if( sheet && ( evt->IsAction( &ACTIONS::refreshPreview ) || evt->IsMotion() ) )
1024  {
1025  sizeSheet( sheet, cursorPos );
1026  m_view->ClearPreview();
1027  m_view->AddToPreview( sheet->Clone() );
1028  }
1029 
1030  else if( evt->IsClick( BUT_RIGHT ) )
1031  {
1032  // Warp after context menu only if dragging...
1033  if( !sheet )
1035 
1037  }
1038  else
1039  evt->SetPassEvent();
1040 
1041  // Enable autopanning and cursor capture only when there is a sheet to be placed
1042  getViewControls()->SetAutoPan( sheet != nullptr );
1043  getViewControls()->CaptureCursor( sheet != nullptr );
1044  }
1045 
1046  return 0;
1047 }
1048 
1049 
1051 {
1052  wxPoint pos = aSheet->GetPosition();
1053  wxPoint size = (wxPoint) aPos - pos;
1054 
1055  size.x = std::max( size.x, MIN_SHEET_WIDTH );
1056  size.y = std::max( size.y, MIN_SHEET_HEIGHT );
1057 
1058  wxPoint grid = m_frame->GetNearestGridPosition( pos + size );
1059  aSheet->Resize( wxSize( grid.x - pos.x, grid.y - pos.y ) );
1060 }
1061 
1062 
1064 {
1077 }
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
bool IsBold() const
Definition: eda_text.h:182
bool SchematicCleanUp(SCH_SCREEN *aScreen=nullptr)
Performs routine schematic cleaning including breaking wire and buses and deleting identical objects ...
void SetShape(PINSHEETLABEL_SHAPE aShape)
Definition: sch_text.h:239
PINSHEETLABEL_SHAPE m_lastGlobalLabelShape
int TwoClickPlace(const TOOL_EVENT &aEvent)
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
void AddToPreview(EDA_ITEM *aItem, bool aTakeOwnership=true)
Definition: sch_view.cpp:195
static TOOL_ACTION finishSheet
Definition: ee_actions.h:98
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
int PlaceComponent(const TOOL_EVENT &aEvent)
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
std::vector< COMPONENT_SELECTION > m_symbolHistoryList
static TOOL_ACTION activatePointEditor
Definition: actions.h:163
wxPoint GetStartPoint() const
Definition: sch_line.h:94
Holds all the data relating to one schematic A schematic may consist of one or more sheets (and one r...
Definition: schematic.h:42
SCH_SHEET * Last() const
Function Last returns a pointer to the last sheet of the list One can see the others sheet as the "pa...
This file is part of the common library.
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
bool HasPoint()
Indicates the cursor is over an edit point.
STATUS_TEXT_POPUP.
Definition: status_popup.h:79
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
static TOOL_ACTION mirrorY
Definition: ee_actions.h:119
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:134
const wxPoint GetPosition() const override
static TOOL_ACTION placeHierLabel
Definition: ee_actions.h:87
void SetItalic(bool isItalic)
Definition: eda_text.h:178
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
static TOOL_ACTION placeJunction
Definition: ee_actions.h:83
void RecacheAllItems()
Function RecacheAllItems() Rebuilds GAL display lists.
Definition: view.cpp:1402
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
int GetUnitCount() const
Return the number of units per package of the symbol.
void Rotate(wxPoint aPosition) override
Rotate the item around aPosition 90 degrees in the clockwise direction.
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:140
void SetPosition(const wxPoint &aPosition) override
Set the schematic item position to aPosition.
virtual void SetCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true, bool aTriggeredByArrows=false, long aArrowCommand=0)=0
Moves cursor to the requested position expressed in world coordinates.
Schematic editor (Eeschema) main window.
LIB_PART * GetLibPart(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
static TOOL_ACTION placeBusWireEntry
Definition: ee_actions.h:84
bool TestDanglingEnds()
Test all of the connectable objects in the schematic for unused connection points.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:237
std::vector< COMPONENT_SELECTION > m_powerHistoryList
static const KICAD_T SheetsOnly[]
Definition: ee_collectors.h:48
wxPoint GetNearestGridPosition(const wxPoint &aPosition) const
Return the nearest aGridSize location to aPosition.
bool IsValid() const
Definition: lib_id.h:171
int PlaceImage(const TOOL_EVENT &aEvent)
PANEL_SYM_CHOOSER m_SymChooserPanel
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
#define MIN_SHEET_WIDTH
Definition: sch_sheet.h:42
virtual void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:196
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
static TOOL_ACTION rotateCW
Definition: ee_actions.h:116
virtual void Add(EDA_ITEM *aItem)
Definition: selection.h:76
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:67
static TOOL_ACTION leaveSheet
Definition: ee_actions.h:176
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:43
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:69
bool IsItalic() const
Definition: eda_text.h:179
EE_SELECTION & GetSelection()
Function GetSelection()
#define IS_NEW
New item, just created.
Definition: base_struct.h:117
EESCHEMA_SETTINGS * eeconfig() const
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
static TOOL_ACTION drawSheet
Definition: ee_actions.h:88
virtual void WarpCursor(const VECTOR2D &aPosition, bool aWorldCoordinates=false, bool aWarpView=false)=0
Function WarpCursor() If enabled (.
static TOOL_ACTION rotateCCW
Definition: ee_actions.h:117
int GetUnit() const
AUTOPLACE_FIELDS m_AutoplaceFields
static TOOL_ACTION placePower
Definition: ee_actions.h:78
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: sch_text.cpp:147
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:232
SCH_ITEM * Duplicate(bool doClone=false) const
Routine to create a new copy of given item.
Definition: sch_item.cpp:78
#define NULL
virtual void SetPosition(const wxPoint &aPosition)=0
Set the schematic item position to aPosition.
virtual void PopTool(const std::string &actionName)
virtual void CaptureCursor(bool aEnabled)
Function CaptureCursor() Forces the cursor to stay within the drawing panel area.
int SingleClickPlace(const TOOL_EVENT &aEvent)
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:435
static TOOL_ACTION placeSchematicText
Definition: ee_actions.h:91
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
TOOL_EVENT.
Definition: tool_event.h:171
static TOOL_ACTION placeImage
Definition: ee_actions.h:93
void ClearPreview()
Definition: sch_view.cpp:183
SCHEMATIC & Schematic() const
void UpdateHierarchyNavigator(bool aForceUpdate=false)
Run the Hierarchy Navigator dialog.
Define a library symbol object.
void SaveCopyForRepeatItem(SCH_ITEM *aItem)
Clone aItem and owns that clone in this container.
SCH_JUNCTION * AddJunction(const wxPoint &aPos, bool aAppendToUndo=false, bool aFinal=true)
static TOOL_ACTION mirrorX
Definition: ee_actions.h:118
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:153
LABEL_SPIN_STYLE GetLabelSpinStyle() const
Definition: sch_text.h:232
static TOOL_ACTION importSheetPin
Definition: ee_actions.h:90
void SetUnit(int aUnit)
Change the unit number to aUnit.
static TOOL_ACTION clearSelection
Clears the current selection.
Definition: ee_actions.h:56
static TOOL_ACTION placeSymbol
Definition: ee_actions.h:77
virtual void SetCrossHairCursorPosition(const VECTOR2D &aPosition, bool aWarpView=true)=0
Moves the graphic crosshair cursor to the requested position expressed in world coordinates.
static TOOL_ACTION addNeededJunctions
Definition: ee_actions.h:75
COMPONENT_SELECTION SelectCompFromLibTree(const SCHLIB_FILTER *aFilter, std::vector< COMPONENT_SELECTION > &aHistoryList, bool aUseLibBrowser, int aUnit, int aConvert, bool aShowFootprints, const LIB_ID *aHighlight=nullptr, bool aAllowFields=true)
Function SelectComponentFromLib Calls the library viewer to select component to import into schematic...
Definition: getpart.cpp:89
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:42
virtual void SetAutoPan(bool aEnabled)
Function SetAutoPan Turns on/off auto panning (this feature is used when there is a tool active (eg.
void SelectUnit(SCH_COMPONENT *aComponent, int aUnit)
Definition: getpart.cpp:196
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:216
virtual unsigned int GetSize() const override
Function GetSize() Returns the number of stored items.
Definition: selection.h:100
Definition: seg.h:39
static TOOL_ACTION placeLabel
Definition: ee_actions.h:85
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:463
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
PINSHEETLABEL_SHAPE
Definition: sch_text.h:150
SCH_SHEET & Root() const
Definition: schematic.h:97
SCH_TEXT * getNextNewText()
Gets the next queued text item.
SCH_HIERLABEL * ImportHierLabel(SCH_SHEET *aSheet)
Import a hierarchical label with no attached sheet pin.
Definition: sheet.cpp:537
int AddItemToSel(const TOOL_EVENT &aEvent)
SCH_SCREEN * LastScreen()
Function LastScreen.
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:37
bool SelectPoint(const VECTOR2I &aWhere, const KICAD_T *aFilterList=EE_COLLECTOR::AllItems, EDA_ITEM **aItem=nullptr, bool *aSelectionCancelledFlag=nullptr, bool aCheckLocked=false, bool aAdd=false, bool aSubtract=false, bool aExclusiveOr=false)
Function SelectPoint() Selects one or all items pointed by the parameter aWhere.
int ClearSelection(const TOOL_EVENT &aEvent)
Clear current selection event handler.
#define _(s)
Definition: 3d_actions.cpp:33
EE_POINT_EDITOR.
void MirrorX(int aXaxis_position) override
Mirror item relative to the X axis about aXaxis_position.
const wxPoint GetPosition() const override
Definition: sch_sheet.h:570
wxString wx_str() const
Definition: utf8.cpp:51
void VetoContextMenuMouseWarp()
Disables mouse warping after the current context menu is closed.
Definition: tool_manager.h:408
SCH_TEXT * createNewText(const VECTOR2I &aPosition, int aType)
static TOOL_ACTION placeNoConnect
Definition: ee_actions.h:82
void AutoplaceFields(SCH_SCREEN *aScreen, bool aManual) override
Automatically orient all the fields in the component.
Schematic symbol object.
Definition: sch_component.h:88
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
Definition: sch_bitmap.cpp:89
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
std::deque< std::unique_ptr< SCH_TEXT > > m_queuedTexts
#define MIN_SHEET_HEIGHT
Definition: sch_sheet.h:43
#define IS_RESIZED
Item being resized.
Definition: base_struct.h:118
These settings were stored in SCH_BASE_FRAME previously.
Class for a wire to bus entry.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:233
bool EditSheetProperties(SCH_SHEET *aSheet, SCH_SHEET_PATH *aHierarchy, bool *aClearAnnotationNewItems)
Edit an existing sheet or add a new sheet to the schematic.
Definition: sheet.cpp:482
void Activate()
Function Activate() Runs the tool.
SCH_SHEET_PATH & GetCurrentSheet() const
bool ReadImageFile(const wxString &aFullFilename)
Reads and stores an image file.
Definition: sch_bitmap.cpp:83
EE_TOOL_BASE.
Definition: ee_tool_base.h:50
LABEL_SPIN_STYLE m_lastTextOrientation
SCH_ITEM * GetItem(const wxPoint &aPosition, int aAccuracy=0, KICAD_T aType=SCH_LOCATE_ANY_T)
Check aPosition within a distance of aAccuracy for items of type aFilter.
Definition: sch_screen.cpp:310
bool HasPosition() const
Returns if it this event has a valid position (true for mouse events and context-menu or hotkey-based...
Definition: tool_event.h:260
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current screen and u...
Definition for part library class.
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
virtual void SetLabelSpinStyle(LABEL_SPIN_STYLE aSpinStyle)
Set a spin or rotation angle, along with specific horizontal and vertical justification styles with e...
Definition: sch_text.cpp:233
void clear()
Definition: utf8.h:113
static TOOL_ACTION placeGlobalLabel
Definition: ee_actions.h:86
void SetBold(bool aBold)
Definition: eda_text.h:181
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
void MirrorY(int aYaxis_position) override
Mirror item relative to the Y axis about aYaxis_position.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:186
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
SCH_SHEET_PIN * CreateSheetPin(SCH_SHEET *aSheet, SCH_HIERLABEL *aLabel)
Create a new SCH_SHEET_PIN object and add it to aSheet at the current cursor position.
Definition: sheet.cpp:501
void SetPosition(const wxPoint &aPosition) override
Set the schematic item position to aPosition.
Definition: sch_bitmap.h:141
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:126
void setTransitions() override
Sets up handlers for various events.
static TOOL_ACTION refreshPreview
Definition: actions.h:101
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
EDA_ITEM * Front() const
Definition: selection.h:184
int DrawSheet(const TOOL_EVENT &aEvent)
static TOOL_ACTION cursorClick
Definition: actions.h:118
PINSHEETLABEL_SHAPE GetShape() const
Definition: sch_text.h:237
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
bool empty() const
Definition: utf8.h:108
void Resize(const wxSize &aSize)
Resize this sheet to aSize and adjust all of the labels accordingly.
Definition: sch_sheet.cpp:797
void sizeSheet(SCH_SHEET *aSheet, VECTOR2I aPos)
void AddItemToScreenAndUndoList(SCH_ITEM *aItem, bool aUndoAppend=false)
Add an item to the schematic and adds the changes to the undo/redo container.
#define IS_MOVED
Item being moved.
Definition: base_struct.h:116
wxPoint GetEndPoint() const
Definition: sch_line.h:97
std::unique_ptr< STATUS_TEXT_POPUP > m_statusPopup