KiCad PCB EDA Suite
ee_tool_base.h
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 #ifndef EE_TOOL_BASE_H
26 #define EE_TOOL_BASE_H
27 
28 #include <tool/tool_event.h>
29 #include <tool/tool_interactive.h>
30 #include <tool/tool_manager.h>
31 #include <tool/tool_menu.h>
32 #include <tool/actions.h>
34 #include <sch_view.h>
35 #include <sch_edit_frame.h>
36 #include <symbol_edit_frame.h>
37 #include <undo_redo_container.h>
38 
39 
40 class EE_SELECTION;
41 
49 template <class T>
51 {
52 public:
58  EE_TOOL_BASE( const std::string& aName ) :
59  TOOL_INTERACTIVE ( aName ),
60  m_frame( nullptr ),
61  m_view( nullptr ),
62  m_selectionTool( nullptr ),
63  m_isSymbolEditor( false )
64  {};
65 
66  ~EE_TOOL_BASE() override {};
67 
69  bool Init() override
70  {
71  m_frame = getEditFrame<T>();
74 
75  // A basic context manu. Many (but not all) tools will choose to override this.
76 
77  auto& ctxMenu = m_menu.GetMenu();
78 
79  // cancel current tool goes in main context menu at the top if present
81  ctxMenu.AddSeparator( 1 );
82 
83  // Finally, add the standard zoom/grid items
84  m_frame->AddStandardSubMenus( m_menu );
85 
86  return true;
87  }
88 
90  void Reset( RESET_REASON aReason ) override
91  {
92  if( aReason == MODEL_RELOAD )
93  {
94  // Init variables used by every drawing tool
95  m_frame = getEditFrame<T>();
96  m_isSymbolEditor = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) != nullptr;
97  }
98 
99  m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
100  }
101 
102 protected:
107  void updateItem( EDA_ITEM* aItem, bool aUpdateRTree ) const
108  {
109  switch( aItem->Type() )
110  {
111  case SCH_SHEET_PIN_T:
112  getView()->Update( aItem );
113  getView()->Update( aItem->GetParent() );
114 
115  // Moving sheet pins does not change the BBox.
116  break;
117 
118  case SCH_PIN_T:
119  case SCH_FIELD_T:
120  getView()->Update( aItem );
121  getView()->Update( aItem->GetParent() );
122 
123  if( aUpdateRTree )
124  m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem->GetParent() ) );
125 
126  break;
127 
128  default:
129  getView()->Update( aItem );
130 
131  if( aUpdateRTree )
132  m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem ) );
133  }
134  }
135 
138  void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aType, bool aAppend = false )
139  {
140  KICAD_T itemType = aItem->Type();
141  bool selected = aItem->IsSelected();
142 
143  // IS_SELECTED flag should not be set on undo items which were added for
144  // a drag operation.
145  if( selected && aItem->HasFlag( TEMP_SELECTED ) )
146  aItem->ClearSelected();
147 
148  if( m_isSymbolEditor )
149  {
150  SYMBOL_EDIT_FRAME* editFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
151  wxASSERT( editFrame );
152 
153  editFrame->SaveCopyInUndoList( static_cast<LIB_ITEM*>( aItem ), aType, aAppend );
154  }
155  else
156  {
157  SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
158  wxASSERT( editFrame );
159 
160  if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
161  {
162  editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
163  static_cast<SCH_ITEM*>( aItem->GetParent() ),
164  UNDO_REDO::CHANGED, aAppend );
165  }
166  else
167  {
168  editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
169  static_cast<SCH_ITEM*>( aItem ),
170  aType, aAppend );
171  }
172  }
173 
174  if( selected && aItem->HasFlag( TEMP_SELECTED ) )
175  aItem->SetSelected();
176  }
177 
178 protected:
183 };
184 
185 #endif
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: ee_tool_base.h:90
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
bool IsSelected() const
Definition: eda_item.h:191
Model changes (required full reload)
Definition: tool_base.h:82
void ClearSelected()
Definition: eda_item.h:199
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
static TOOL_ACTION cancelInteractive
Definition: actions.h:65
~EE_TOOL_BASE() override
Definition: ee_tool_base.h:66
Schematic editor (Eeschema) main window.
#define TEMP_SELECTED
flag indicating that the structure has already selected
Definition: eda_item.h:114
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:69
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
KIGFX::SCH_VIEW * m_view
Definition: ee_tool_base.h:180
void SetSelected()
Definition: eda_item.h:196
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aType, bool aAppend=false)
Similar to m_frame->SaveCopyInUndoList(), but handles items that are owned by their parents.
Definition: ee_tool_base.h:138
bool m_isSymbolEditor
Definition: ee_tool_base.h:182
void SaveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aUndoType=UNDO_REDO::LIBEDIT, bool aAppend=false)
Create a copy of the current symbol, and save it in the undo list.
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
EDA_ITEM * GetParent() const
Definition: eda_item.h:183
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:181
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
void updateItem(EDA_ITEM *aItem, bool aUpdateRTree) const
Similar to getView()->Update(), but handles items that are redrawn by their parents and updating the ...
Definition: ee_tool_base.h:107
EE_TOOL_BASE(const std::string &aName)
Constructor.
Definition: ee_tool_base.h:58
EDA_ITEM is a base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:148
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
EE_TOOL_BASE.
Definition: ee_tool_base.h:50
bool HasFlag(STATUS_FLAGS aFlag)
Definition: eda_item.h:223
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Adds a menu entry to run a TOOL_ACTION on selected items.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1513
KICAD_T Type() const
Function Type()
Definition: eda_item.h:181
The symbol library editor main window.