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  DBG(printf("mousep %d %d gridp %d %d\n", aPosition.x, aPosition.y, gridPosition.x, gridPosition.y );)
66 
67 
68  if( ( GetToolId() == ID_NO_TOOL_SELECTED ) || ( item && item->GetFlags() ) )
69  {
70  m_canvas->SetAutoPanRequest( false );
71  SetRepeatItem( NULL );
72 
73  if( item && item->GetFlags() )
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  switch( GetToolId() )
112  {
113  case ID_NO_TOOL_SELECTED:
114  break;
115 
116  case ID_ZOOM_SELECTION:
117  break;
118 
119  case ID_HIGHLIGHT:
120  HighlightConnectionAtPosition( aPosition );
121  break;
122 
123  case ID_NOCONN_BUTT:
124  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
125  {
126  if( GetScreen()->GetItem( gridPosition, 0, SCH_NO_CONNECT_T ) == NULL )
127  {
128  SCH_NO_CONNECT* no_connect = AddNoConnect( gridPosition );
129  SetRepeatItem( no_connect );
130  GetScreen()->SetCurItem( no_connect );
131  m_canvas->SetAutoPanRequest( true );
132  }
133  }
134  else
135  {
137  }
138  break;
139 
140  case ID_JUNCTION_BUTT:
141  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
142  {
143  if( GetScreen()->GetItem( gridPosition, 0, SCH_JUNCTION_T ) == NULL )
144  {
145  SCH_JUNCTION* junction = AddJunction( gridPosition );
146  SetRepeatItem( junction );
147  GetScreen()->SetCurItem( junction );
148  m_canvas->SetAutoPanRequest( true );
149  }
150  }
151  else
152  {
154  }
155  break;
156 
158  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
159  {
161  m_canvas->SetAutoPanRequest( true );
162  }
163  else
164  {
166  }
167  break;
168 
170  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
171  {
173  m_canvas->SetAutoPanRequest( true );
174  }
175  else
176  {
178  }
179  break;
180 
183  break;
184 
185  case ID_WIRE_BUTT:
187  m_canvas->SetAutoPanRequest( true );
188  break;
189 
190  case ID_BUS_BUTT:
192  m_canvas->SetAutoPanRequest( true );
193  break;
194 
197  m_canvas->SetAutoPanRequest( true );
198  break;
199 
201  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
202  {
204  m_canvas->SetAutoPanRequest( true );
205  }
206  else
207  {
209  }
210  break;
211 
212  case ID_ADD_IMAGE_BUTT:
213  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
214  {
215  GetScreen()->SetCurItem( CreateNewImage( aDC ) );
216  m_canvas->SetAutoPanRequest( true );
217  }
218  else
219  {
221  }
222  break;
223 
224  case ID_LABEL_BUTT:
225  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
226  {
228  m_canvas->SetAutoPanRequest( true );
229  }
230  else
231  {
233  }
234  break;
235 
236  case ID_GLABEL_BUTT:
237  case ID_HIERLABEL_BUTT:
238  if( (item == NULL) || (item->GetFlags() == 0) )
239  {
240  if( GetToolId() == ID_GLABEL_BUTT )
242 
243  if( GetToolId() == ID_HIERLABEL_BUTT )
245 
246  m_canvas->SetAutoPanRequest( true );
247  }
248  else
249  {
251  }
252  break;
253 
255  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
256  {
257  item = CreateSheet( aDC );
258 
259  if( item != NULL )
260  {
261  GetScreen()->SetCurItem( item );
262  m_canvas->SetAutoPanRequest( true );
263  }
264  }
265  else
266  {
268  }
269  break;
270 
272  case ID_SHEET_PIN_BUTT:
273  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
275 
276  if( item == NULL )
277  break;
278 
279  if( (item->Type() == SCH_SHEET_T) && (item->GetFlags() == 0) )
280  {
282  GetScreen()->SetCurItem( ImportSheetPin( (SCH_SHEET*) item ) );
283  else
284  GetScreen()->SetCurItem( CreateSheetPin( (SCH_SHEET*) item ) );
285  }
286  else if( (item->Type() == SCH_SHEET_PIN_T) && (item->GetFlags() != 0) )
287  {
289  }
290  break;
291 
293  if( (item == NULL) || (item->GetFlags() == 0) )
294  {
295  // ERC dialog interferes with moving items so we close it before starting
296  CloseErc();
297  GetScreen()->SetCurItem( Load_Component( NULL, s_CmpNameList, true ) );
298  m_canvas->SetAutoPanRequest( true );
299  }
300  else
301  {
303  }
304  break;
305 
306  case ID_PLACE_POWER_BUTT:
307  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
308  {
309  SCHLIB_FILTER filter;
310  filter.FilterPowerParts( true );
311  GetScreen()->SetCurItem( Load_Component( &filter, s_PowerNameList, false ) );
312  m_canvas->SetAutoPanRequest( true );
313  }
314  else
315  {
317  }
318  break;
319 
320 #ifdef KICAD_SPICE
321  case ID_SIM_PROBE:
322  {
323  constexpr KICAD_T wiresAndComponents[] = { SCH_LINE_T,
326  EOT };
327  item = LocateAndShowItem( aPosition, wiresAndComponents );
328 
329  if( !item )
330  break;
331 
332  std::unique_ptr<NETLIST_OBJECT_LIST> netlist( BuildNetListBase() );
333 
334  for( NETLIST_OBJECT* obj : *netlist )
335  {
336  if( obj->m_Comp == item )
337  {
338  SIM_PLOT_FRAME* simFrame = (SIM_PLOT_FRAME*) Kiway().Player( FRAME_SIMULATOR, false );
339 
340  if( simFrame )
341  simFrame->AddVoltagePlot( obj->GetNetName() );
342 
343  break;
344  }
345  }
346  }
347  break;
348 
349  case ID_SIM_TUNE:
350  {
351  constexpr KICAD_T fieldsAndComponents[] = { SCH_COMPONENT_T, SCH_FIELD_T, EOT };
352  item = LocateAndShowItem( aPosition, fieldsAndComponents );
353 
354  if( !item )
355  return;
356 
357  if( item->Type() != SCH_COMPONENT_T )
358  {
359  item = static_cast<SCH_ITEM*>( item->GetParent() );
360 
361  if( item->Type() != SCH_COMPONENT_T )
362  return;
363  }
364 
365  SIM_PLOT_FRAME* simFrame = (SIM_PLOT_FRAME*) Kiway().Player( FRAME_SIMULATOR, false );
366 
367  if( simFrame )
368  simFrame->AddTuner( static_cast<SCH_COMPONENT*>( item ) );
369  }
370  break;
371 #endif /* KICAD_SPICE */
372 
373  default:
375  wxFAIL_MSG( wxT( "SCH_EDIT_FRAME::OnLeftClick invalid tool ID <" ) +
376  wxString::Format( wxT( "%d> selected." ), GetToolId() ) );
377  }
378 }
379 
380 
389 void SCH_EDIT_FRAME::OnLeftDClick( wxDC* aDC, const wxPoint& aPosition )
390 
391 {
392  EDA_ITEM* item = GetScreen()->GetCurItem();
393 
394  switch( GetToolId() )
395  {
396  case ID_NO_TOOL_SELECTED:
397  if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
398  {
400  }
401 
402  if( ( item == NULL ) || ( item->GetFlags() != 0 ) )
403  break;
404 
405  switch( item->Type() )
406  {
407  case SCH_SHEET_T:
408  m_CurrentSheet->push_back( (SCH_SHEET*) item );
410  break;
411 
412  case SCH_COMPONENT_T:
413  EditComponent( (SCH_COMPONENT*) item );
415 
416  if( item->GetFlags() == 0 )
417  GetScreen()->SetCurItem( NULL );
418 
419  GetCanvas()->Refresh();
420  break;
421 
422  case SCH_TEXT_T:
423  case SCH_LABEL_T:
424  case SCH_GLOBAL_LABEL_T:
426  EditSchematicText( (SCH_TEXT*) item );
427  break;
428 
429  case SCH_BITMAP_T:
430  EditImage( (SCH_BITMAP*) item );
431  break;
432 
433  case SCH_FIELD_T:
436  break;
437 
438  case SCH_MARKER_T:
439  ( (SCH_MARKER*) item )->DisplayMarkerInfo( this );
440  break;
441 
442  default:
443  break;
444  }
445 
446  break;
447 
448  case ID_BUS_BUTT:
449  case ID_WIRE_BUTT:
451  if( item && item->IsNew() )
452  EndSegment();
453 
454  break;
455  }
456 }
Definition of the SCH_SHEET class for Eeschema.
bool DeleteItemAtCrossHair()
Delete the item found under the cross hair.
Definition: schedit.cpp:653
SCH_JUNCTION * AddJunction(const wxPoint &aPosition, bool aPutInUndoList=false)
Add a new junction at aPosition.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
Class SCH_FIELD instances are attached to a component and provide a place for the component&#39;s value...
Definition: sch_field.h:56
void EditComponent(SCH_COMPONENT *aComponent)
Display the edit component dialog to edit the parameters of aComponent.
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:258
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.
void ClearHiddenFlags()
Definition: sch_view.cpp:177
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.
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 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.
bool IsNew() const
Definition: base_struct.h:219
wxPoint GetGridPosition(const wxPoint &aPosition) const
Return the nearest grid position to aPosition if a screen is defined and snap to grid is enabled...
virtual void MoveCursorToCrossHair() override
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
EDA_ITEM * GetParent() const
Definition: base_struct.h:211
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...
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.
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
void SetRepeatItem(SCH_ITEM *aItem)
Clone aItem and owns that clone in this container.
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_ITEM * GetCurItem() const
Return the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
Definition: sch_screen.h:196
SCH_DRAW_PANEL * GetCanvas() const override
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:214
void ClearPreview()
Definition: sch_view.cpp:131
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
Subclass of SIM_PLOT_FRAME_BASE, which is generated by wxFormBuilder.
static SCH_BASE_FRAME::HISTORY_LIST s_CmpNameList
void SetAutoPanRequest(bool aEnable)
void EditSchematicText(SCH_TEXT *TextStruct)
KIGFX::SCH_VIEW * GetView() const
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.
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:123
Definition the SCH_COMPONENT class for Eeschema.
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
SCH_ITEM * GetItem(const wxPoint &aPosition, int aAccuracy=0, KICAD_T aType=NOT_USED) const
Check aPosition within a distance of aAccuracy for items of type aFilter.
Definition: sch_screen.cpp:221
void OnLeftClick(wxDC *aDC, const wxPoint &aPosition) override
int GetToolId() const
Definition: draw_frame.h:519
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:69
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
void EditImage(SCH_BITMAP *aItem)
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
#define DBG(x)
Definition: fctsys.h:33
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.
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.