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 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #ifndef EE_TOOL_BASE_H
25 #define EE_TOOL_BASE_H
26 
27 #include <tool/tool_event.h>
28 #include <tool/tool_interactive.h>
29 #include <tool/tool_manager.h>
30 #include <tool/tool_menu.h>
31 #include <tool/actions.h>
33 #include <sch_view.h>
34 #include <sch_edit_frame.h>
35 #include <lib_edit_frame.h>
36 #include <undo_redo_container.h>
37 
38 
39 class SELECTION;
40 
48 template <class T>
50 {
51 public:
57  EE_TOOL_BASE( const std::string& aName ) :
58  TOOL_INTERACTIVE ( aName ),
59  m_frame( nullptr ),
60  m_view( nullptr ),
61  m_selectionTool( nullptr ),
62  m_isLibEdit( false ),
63  m_menu( *this )
64  {};
65 
66  virtual ~EE_TOOL_BASE() {};
67 
69  bool Init() override
70  {
71  m_frame = getEditFrame<T>();
73  m_isLibEdit = dynamic_cast<LIB_EDIT_FRAME*>( m_frame ) != nullptr;
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( SELECTION_CONDITIONS::ShowAlways, 1 );
82 
83  // Finally, add the standard zoom/grid items
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_isLibEdit = dynamic_cast<LIB_EDIT_FRAME*>( m_frame ) != nullptr;
97  }
98 
99  m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
100  }
101 
103  inline TOOL_MENU& GetToolMenu() { return m_menu; }
104 
105 protected:
107  void updateView( EDA_ITEM* aItem ) const
108  {
109  KICAD_T itemType = aItem->Type();
110 
111  if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
112  getView()->Update( aItem->GetParent() );
113 
114  getView()->Update( aItem );
115  }
116 
117 
120  void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO_T aType, bool aAppend = false )
121  {
122  KICAD_T itemType = aItem->Type();
123 
124  if( m_isLibEdit )
125  {
126  LIB_EDIT_FRAME* editFrame = dynamic_cast<LIB_EDIT_FRAME*>( m_frame );
127  editFrame->SaveCopyInUndoList( (LIB_ITEM*) aItem, aType, aAppend );
128  }
129  else
130  {
131  SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
132 
133  if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
134  editFrame->SaveCopyInUndoList( (SCH_ITEM*) aItem->GetParent(), UR_CHANGED, aAppend );
135  else
136  editFrame->SaveCopyInUndoList( (SCH_ITEM*) aItem, aType, aAppend );
137  }
138  }
139 
140 protected:
145 
148 };
149 
150 #endif
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
void AddStandardSubMenus(EDA_DRAW_FRAME *aFrame)
Function CreateBasicMenu.
Definition: tool_menu.cpp:83
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
Definition: ee_tool_base.h:90
virtual ~EE_TOOL_BASE()
Definition: ee_tool_base.h:66
Model changes (required full reload)
Definition: tool_base.h:83
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
Class TOOL_MENU.
Definition: tool_menu.h:47
static TOOL_ACTION cancelInteractive
Definition: actions.h:45
Schematic editor (Eeschema) main window.
TOOL_MENU & GetToolMenu()
Get the tool's top-level context menu
Definition: ee_tool_base.h:103
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
The base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:67
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:69
KIGFX::SCH_VIEW * m_view
Definition: ee_tool_base.h:142
void SaveCopyInUndoList(SCH_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, bool aAppend=false, 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: base_struct.h:214
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:143
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1539
TOOL_MENU m_menu
Menu model displayed by the tool.
Definition: ee_tool_base.h:147
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:35
The symbol library editor main window.
void updateView(EDA_ITEM *aItem) const
Similar to getView()->Update(), but handles items that are redrawn by their parents.
Definition: ee_tool_base.h:107
EE_TOOL_BASE(const std::string &aName)
Constructor.
Definition: ee_tool_base.h:57
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO_T aType, bool aAppend=false)
Similar to m_frame->SaveCopyInUndoList(), but handles items that are owned by their parents.
Definition: ee_tool_base.h:120
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:157
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:80
Class EE_TOOL_BASE.
Definition: ee_tool_base.h:49
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
Definition: sch_item.h:114
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT, bool aAppend=false)
Create a copy of the current component, and save it in the undo list.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204