KiCad PCB EDA Suite
eeschema/onleftclick.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
30 #include <fctsys.h>
31 #include <kiway.h>
32 #include <eeschema_id.h>
33 #include <sch_draw_panel.h>
34 #include <confirm.h>
35 #include <sch_edit_frame.h>
36 #include <sim/sim_plot_frame.h>
37 #include <menus_helpers.h>
38 
39 #include <sch_bus_entry.h>
40 #include <sch_text.h>
41 #include <sch_marker.h>
42 #include <sch_junction.h>
43 #include <sch_line.h>
44 #include <sch_no_connect.h>
45 #include <sch_component.h>
46 #include <sch_sheet.h>
47 #include <sch_sheet_path.h>
48 #include <sch_bitmap.h>
49 
50 #include <netlist_object.h>
51 #include <class_library.h> // for class SCHLIB_FILTER to filter power parts
52 
53 #include <sch_view.h>
54 
55 // TODO(hzeller): These pairs of elmenets should be represented by an object, but don't want
56 // to refactor too much right now to not get in the way with other code changes.
59 
60 
61 void SCH_EDIT_FRAME::OnLeftClick( wxDC* aDC, const wxPoint& aPosition )
62 {
63  SCH_ITEM* item = GetScreen()->GetCurItem();
64  wxPoint gridPosition = GetGridPosition( aPosition );
65  // item_flags != 0 means a current item in edit, or new ...
66  int item_flags = item ? (item->GetFlags() & ~HIGHLIGHTED) : 0;
67 
68  if( ( GetToolId() == ID_NO_TOOL_SELECTED ) || item_flags )
69  {
70  m_canvas->SetAutoPanRequest( false );
71  SetRepeatItem( NULL );
72 
73  if( item_flags )
74  {
75  switch( item->Type() )
76  {
77  case SCH_LABEL_T:
78  case SCH_GLOBAL_LABEL_T:
80  case SCH_TEXT_T:
81  case SCH_SHEET_PIN_T:
82  case SCH_SHEET_T:
85  case SCH_JUNCTION_T:
86  case SCH_COMPONENT_T:
87  case SCH_FIELD_T:
88  case SCH_BITMAP_T:
89  case SCH_NO_CONNECT_T:
93  return;
94 
95  case SCH_LINE_T: // May already be drawing segment.
96  break;
97 
98  default:
99  wxFAIL_MSG( wxT( "SCH_EDIT_FRAME::OnLeftClick error. Item type <" ) +
100  item->GetClass() + wxT( "> is already being edited." ) );
101  item->ClearFlags();
102  break;
103  }
104  }
105  else
106  {
107  item = LocateAndShowItem( aPosition );
108  }
109  }
110 
111  if( !item ) // If clicked on a empty area, clear any highligthed symbol
112  GetCanvas()->GetView()->HighlightItem( nullptr, nullptr );
113 
114  switch( GetToolId() )
115  {
116  case ID_NO_TOOL_SELECTED:
117  break;
118 
119  case ID_ZOOM_SELECTION:
120  break;
121 
122  case ID_HIGHLIGHT:
123  HighlightConnectionAtPosition( aPosition );
124  break;
125 
126  case ID_NOCONN_BUTT:
127  if( item_flags == 0 )
128  {
129  if( GetScreen()->GetItem( gridPosition, 0, SCH_NO_CONNECT_T ) == NULL )
130  {
131  SCH_NO_CONNECT* no_connect = AddNoConnect( gridPosition );
132  SetRepeatItem( no_connect );
133  GetScreen()->SetCurItem( no_connect );
134  m_canvas->SetAutoPanRequest( true );
135  }
136  }
137  else
138  {
140  }
141  break;
142 
143  case ID_JUNCTION_BUTT:
144  if( item_flags == 0 )
145  {
146  if( GetScreen()->GetItem( gridPosition, 0, SCH_JUNCTION_T ) == NULL )
147  {
148  SCH_JUNCTION* junction = AddJunction( gridPosition );
149  SetRepeatItem( junction );
150  GetScreen()->SetCurItem( junction );
151  m_canvas->SetAutoPanRequest( true );
152  }
153  }
154  else
155  {
157  }
158  break;
159 
161  if( item_flags == 0 )
162  {
164  m_canvas->SetAutoPanRequest( true );
165  }
166  else
167  {
169  }
170  break;
171 
173  if( item_flags == 0 )
174  {
176  m_canvas->SetAutoPanRequest( true );
177  }
178  else
179  {
181  }
182  break;
183 
186  break;
187 
188  case ID_WIRE_BUTT:
190  m_canvas->SetAutoPanRequest( true );
191  break;
192 
193  case ID_BUS_BUTT:
195  m_canvas->SetAutoPanRequest( true );
196  break;
197 
200  m_canvas->SetAutoPanRequest( true );
201  break;
202 
204  if( item_flags == 0 )
205  {
207  m_canvas->SetAutoPanRequest( true );
208  }
209  else
210  {
212  }
213  break;
214 
215  case ID_ADD_IMAGE_BUTT:
216  if( item_flags == 0 )
217  {
218  GetScreen()->SetCurItem( CreateNewImage( aDC ) );
219  m_canvas->SetAutoPanRequest( true );
220  }
221  else
222  {
224  }
225  break;
226 
227  case ID_LABEL_BUTT:
228  if( item_flags == 0 )
229  {
231  m_canvas->SetAutoPanRequest( true );
232  }
233  else
234  {
236  }
237  break;
238 
239  case ID_GLABEL_BUTT:
240  case ID_HIERLABEL_BUTT:
241  if( item_flags == 0 )
242  {
243  if( GetToolId() == ID_GLABEL_BUTT )
245 
246  if( GetToolId() == ID_HIERLABEL_BUTT )
248 
249  m_canvas->SetAutoPanRequest( true );
250  }
251  else
252  {
254  }
255  break;
256 
258  if( item_flags == 0 )
259  {
260  item = CreateSheet( aDC );
261 
262  if( item != NULL )
263  {
264  GetScreen()->SetCurItem( item );
265  m_canvas->SetAutoPanRequest( true );
266  }
267  }
268  else
269  {
271  }
272  break;
273 
275  case ID_SHEET_PIN_BUTT:
276  if( item_flags == 0 )
278 
279  if( item == NULL )
280  break;
281 
282  if( (item->Type() == SCH_SHEET_T) && (item_flags == 0) )
283  {
285  GetScreen()->SetCurItem( ImportSheetPin( (SCH_SHEET*) item ) );
286  else
287  GetScreen()->SetCurItem( CreateSheetPin( (SCH_SHEET*) item ) );
288  }
289  else if( (item->Type() == SCH_SHEET_PIN_T) && (item->GetFlags() != 0) )
290  {
292  }
293  break;
294 
296  if( item_flags == 0 )
297  {
298  // ERC dialog interferes with moving items so we close it before starting
299  CloseErc();
300  GetScreen()->SetCurItem( Load_Component( NULL, s_CmpNameList, true ) );
301  m_canvas->SetAutoPanRequest( true );
302  }
303  else
304  {
306  }
307  break;
308 
309  case ID_PLACE_POWER_BUTT:
310  if( item_flags == 0 )
311  {
312  SCHLIB_FILTER filter;
313  filter.FilterPowerParts( true );
314  GetScreen()->SetCurItem( Load_Component( &filter, s_PowerNameList, false ) );
315  m_canvas->SetAutoPanRequest( true );
316  }
317  else
318  {
320  }
321  break;
322 
323 #ifdef KICAD_SPICE
324  case ID_SIM_PROBE:
325  {
326  constexpr KICAD_T wiresAndComponents[] = { SCH_LINE_T,
329  EOT };
330  item = LocateAndShowItem( aPosition, wiresAndComponents );
331 
332  if( !item )
333  break;
334 
335  std::unique_ptr<NETLIST_OBJECT_LIST> netlist( BuildNetListBase() );
336 
337  for( NETLIST_OBJECT* obj : *netlist )
338  {
339  if( obj->m_Comp == item )
340  {
341  SIM_PLOT_FRAME* simFrame = (SIM_PLOT_FRAME*) Kiway().Player( FRAME_SIMULATOR, false );
342 
343  if( simFrame )
344  simFrame->AddVoltagePlot( obj->GetNetName() );
345 
346  break;
347  }
348  }
349  }
350  break;
351 
352  case ID_SIM_TUNE:
353  {
354  constexpr KICAD_T fieldsAndComponents[] = { SCH_COMPONENT_T, SCH_FIELD_T, EOT };
355  item = LocateAndShowItem( aPosition, fieldsAndComponents );
356 
357  if( !item )
358  return;
359 
360  if( item->Type() != SCH_COMPONENT_T )
361  {
362  item = static_cast<SCH_ITEM*>( item->GetParent() );
363 
364  if( item->Type() != SCH_COMPONENT_T )
365  return;
366  }
367 
368  SIM_PLOT_FRAME* simFrame = (SIM_PLOT_FRAME*) Kiway().Player( FRAME_SIMULATOR, false );
369 
370  if( simFrame )
371  simFrame->AddTuner( static_cast<SCH_COMPONENT*>( item ) );
372  }
373  break;
374 #endif /* KICAD_SPICE */
375 
376  default:
378  wxFAIL_MSG( wxT( "SCH_EDIT_FRAME::OnLeftClick invalid tool ID <" ) +
379  wxString::Format( wxT( "%d> selected." ), GetToolId() ) );
380  }
381 }
382 
383 
392 void SCH_EDIT_FRAME::OnLeftDClick( wxDC* aDC, const wxPoint& aPosition )
393 
394 {
395  EDA_ITEM* item = GetScreen()->GetCurItem();
396 
397  switch( GetToolId() )
398  {
399  case ID_NO_TOOL_SELECTED:
400  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
401  {
403  }
404 
405  if( ( item == NULL ) || ( item->GetFlags() != 0 ) )
406  break;
407 
408  switch( item->Type() )
409  {
410  case SCH_SHEET_T:
411  m_CurrentSheet->push_back( (SCH_SHEET*) item );
413  break;
414 
415  case SCH_COMPONENT_T:
416  EditComponent( (SCH_COMPONENT*) item );
418 
419  if( item->GetFlags() == 0 )
420  GetScreen()->SetCurItem( NULL );
421 
422  GetCanvas()->Refresh();
423  break;
424 
425  case SCH_TEXT_T:
426  case SCH_LABEL_T:
427  case SCH_GLOBAL_LABEL_T:
429  EditSchematicText( (SCH_TEXT*) item );
430  break;
431 
432  case SCH_BITMAP_T:
433 
434  // The bitmap is cached in Opengl: clear the cache, because
435  // the cache data is perhaps invalid
436  if( EditImage( (SCH_BITMAP*) item ) )
438 
439  break;
440 
441  case SCH_FIELD_T:
444  break;
445 
446  case SCH_MARKER_T:
447  ( (SCH_MARKER*) item )->DisplayMarkerInfo( this );
448  break;
449 
450  default:
451  break;
452  }
453 
454  break;
455 
456  case ID_BUS_BUTT:
457  case ID_WIRE_BUTT:
459  if( item && item->IsNew() )
460  EndSegment();
461 
462  break;
463  }
464 }
Definition of the SCH_SHEET class for Eeschema.
bool DeleteItemAtCrossHair()
Delete the item found under the cross hair.
Definition: schedit.cpp:637
SCH_JUNCTION * AddJunction(const wxPoint &aPosition, bool aPutInUndoList=false)
Add a new junction at aPosition.
Class SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:56
void EditComponent(SCH_COMPONENT *aComponent)
Display the edit component dialog to edit the parameters of aComponent.
static const KICAD_T SheetsAndSheetLabels[]
A scan list for schematic sheet and sheet label items.
void BeginSegment(int type)
Creates a new segment ( WIRE, BUS ) or terminates the current segment in progress.
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
void ClearHiddenFlags()
Clear the hide flag of all items in the view.
Definition: sch_view.cpp:179
void CloseErc()
Close the ERC dialog if it is open.
NETLIST_OBJECT_LIST * BuildNetListBase(bool updateStatusText=true)
Create a flat list which stores all connected objects.
void FilterPowerParts(bool aFilterEnable)
set the filtering of power parts
SCH_TEXT * CreateNewText(int aType)
Definition: edit_label.cpp:68
static const KICAD_T DoubleClickItems[]
A scan list for schematic items that react to a double-click.
This file is part of the common library.
virtual void MoveCursorToCrossHair() override
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
SCH_MARKER class definition.
void RecacheAllItems()
Function RecacheAllItems() Rebuilds GAL display lists.
Definition: view.cpp:1401
SCH_SHEET_PIN * CreateSheetPin(SCH_SHEET *aSheet)
Create a new SCH_SHEET_PIN object and add it to aSheet at the current cursor position.
Definition: sheetlab.cpp:77
std::vector< COMPONENT_SELECTION > HISTORY_LIST
void AddVoltagePlot(const wxString &aNetName)
Adds a voltage plot for a given net name.
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
virtual void SetAutoPanRequest(bool aEnable)=0
void DisplayCurrentSheet()
Draw the current sheet on the display.
Definition: hierarch.cpp:265
void EditComponentFieldText(SCH_FIELD *aField)
Display the edit field dialog to edit the parameters of aField.
void EndSegment()
Terminate a bus, wire, or line creation.
void HighlightItem(EDA_ITEM *aItem, LIB_PIN *aPin=nullptr)
Definition: sch_view.cpp:191
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
SCH_ITEM * GetCurItem() const
Return the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
Definition: sch_screen.h:196
void SetRepeatItem(SCH_ITEM *aItem)
Clone aItem and owns that clone in this container.
bool IsNew() const
Definition: base_struct.h:219
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCH_SHEET_PATH * m_CurrentSheet
which sheet we are presently working on.
SCH_BUS_BUS_ENTRY * CreateBusBusEntry()
Definition: busentry.cpp:42
static SCH_BASE_FRAME::HISTORY_LIST s_PowerNameList
SCH_SHEET * CreateSheet(wxDC *DC)
Definition: sheet.cpp:406
bool HighlightConnectionAtPosition(wxPoint aPosition)
Highlight the connection found at aPosition.
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:300
SCH_DRAW_PANEL * GetCanvas() const override
wxPoint GetGridPosition(const wxPoint &aPosition) const
Return the nearest grid position to aPosition if a screen is defined and snap to grid is enabled.
SCH_COMPONENT * Load_Component(const SCHLIB_FILTER *aFilter, SCH_BASE_FRAME::HISTORY_LIST &aHistoryList, bool aUseLibBrowser)
Load a symbol library and places it on the current schematic.
Definition: getpart.cpp:212
void ClearPreview()
Definition: sch_view.cpp:133
SCH_SHEET_PIN * ImportSheetPin(SCH_SHEET *aSheet)
Automatically create a sheet pin from the hierarchical labels in the schematic referenced by aSheet.
Definition: sheetlab.cpp:106
EDA_ITEM * GetParent() const
Definition: base_struct.h:211
Subclass of SIM_PLOT_FRAME_BASE, which is generated by wxFormBuilder.
static SCH_BASE_FRAME::HISTORY_LIST s_CmpNameList
void EditSchematicText(SCH_TEXT *TextStruct)
SCH_ITEM * LocateAndShowItem(const wxPoint &aPosition, const KICAD_T aFilterList[]=SCH_COLLECTOR::AllItems, int aHotKeyCommandId=0, bool *aClarifySelectionMenuCancelled=nullptr)
Check the schematic at aPosition in logical (drawing) units for a item matching the types in aFilterL...
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:42
SCH_BITMAP * CreateNewImage(wxDC *aDC)
Definition: edit_bitmap.cpp:39
Implementing SIM_PLOT_FRAME_BASE.
KIGFX::SCH_VIEW * GetView() const
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
virtual void SetNoToolSelected()
Select the ID_NO_TOOL_SELECTED id tool (Idle tool)
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:126
Definition the SCH_COMPONENT class for Eeschema.
#define HIGHLIGHTED
item is drawn in normal colors, when the rest is darkened
Definition: base_struct.h:137
void OnLeftDClick(wxDC *aDC, const wxPoint &aPosition) override
Function OnLeftDClick called on a double click event from the drawpanel mouse handler if an editable ...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool EditImage(SCH_BITMAP *aItem)
Launches the "Edit Image" dialog to modify an image.
void OnLeftClick(wxDC *aDC, const wxPoint &aPosition) override
void SetCurItem(SCH_ITEM *aItem)
Sets the currently selected object, m_CurrentItem.
Definition: sch_screen.h:204
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:70
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
virtual wxString GetClass() const override
Function GetClass returns the class name.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:257
Definition for part library class.
void AddTuner(SCH_COMPONENT *aComponent)
Adds a tuner for a component.
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
void addCurrentItemToScreen()
Add the item currently being edited to the schematic and adds the changes to the undo/redo container.
Definition of the NETLIST_OBJECT class.
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:258
SCH_BUS_WIRE_ENTRY * CreateBusWireEntry()
Definition: busentry.cpp:53
SCH_NO_CONNECT * AddNoConnect(const wxPoint &aPosition)
Add no connect item to the current schematic sheet at aPosition.
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Implementation of the label properties dialog.
int GetToolId() const
Definition: draw_frame.h:526
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201