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-2022 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 <math/vector2d.h>
29#include <tool/tool_event.h>
31#include <tool/tool_manager.h>
32#include <tool/tool_menu.h>
33#include <tool/actions.h>
35#include <sch_view.h>
36#include <sch_edit_frame.h>
37#include <symbol_edit_frame.h>
38#include <undo_redo_container.h>
39
40
41class EE_SELECTION;
42
48template <class T>
50{
51public:
55 EE_TOOL_BASE( const std::string& aName ) :
56 TOOL_INTERACTIVE ( aName ),
57 m_frame( nullptr ),
58 m_view( nullptr ),
59 m_selectionTool( nullptr ),
60 m_isSymbolEditor( false )
61 {};
62
63 ~EE_TOOL_BASE() override {};
64
66 bool Init() override
67 {
68 m_frame = getEditFrame<T>();
71
72 // A basic context menu. Many (but not all) tools will choose to override this.
73 auto& ctxMenu = m_menu.GetMenu();
74
75 // cancel current tool goes in main context menu at the top if present
77 ctxMenu.AddSeparator( 1 );
78
79 // Finally, add the standard zoom/grid items
80 m_frame->AddStandardSubMenus( m_menu );
81
82 return true;
83 }
84
86 void Reset( RESET_REASON aReason ) override
87 {
88 if( aReason == MODEL_RELOAD )
89 {
90 // Init variables used by every drawing tool
91 m_frame = getEditFrame<T>();
92 m_isSymbolEditor = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) != nullptr;
93 }
94
95 m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
96 }
97
98protected:
103 void updateItem( EDA_ITEM* aItem, bool aUpdateRTree ) const
104 {
105 switch( aItem->Type() )
106 {
107 case SCH_SHEET_PIN_T:
108 getView()->Update( aItem );
109 getView()->Update( aItem->GetParent() );
110
111 // Moving sheet pins does not change the BBox.
112 break;
113
114 case SCH_PIN_T:
115 case SCH_FIELD_T:
116 getView()->Update( aItem );
117 getView()->Update( aItem->GetParent() );
118
119 if( aUpdateRTree )
120 m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem->GetParent() ) );
121
122 break;
123
124 default:
125 getView()->Update( aItem );
126
127 if( aUpdateRTree )
128 m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem ) );
129 }
130 }
131
134 void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aType, bool aAppend = false,
135 bool aDirtyConnectivity = true )
136 {
137 wxASSERT( aItem );
138
139 KICAD_T itemType = aItem->Type();
140 bool selected = aItem->IsSelected();
141
142 // IS_SELECTED flag should not be set on undo items which were added for
143 // a drag operation.
144 if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
145 aItem->ClearSelected();
146
147 if( m_isSymbolEditor )
148 {
149 SYMBOL_EDIT_FRAME* editFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
150 wxCHECK_RET( editFrame, wxT( "editFrame is null" ) );
151
152 editFrame->SaveCopyInUndoList( static_cast<LIB_ITEM*>( aItem ), aType, aAppend );
153 }
154 else
155 {
156 SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
157 wxASSERT( editFrame );
158
159 if( editFrame )
160 {
161 if( itemType == SCH_PIN_T || itemType == SCH_FIELD_T || itemType == SCH_SHEET_PIN_T )
162 {
163 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
164 static_cast<SCH_ITEM*>( aItem->GetParent() ),
165 UNDO_REDO::CHANGED, aAppend,
166 aDirtyConnectivity );
167 }
168 else
169 {
170 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
171 static_cast<SCH_ITEM*>( aItem ), aType,
172 aAppend, aDirtyConnectivity );
173 }
174 }
175 }
176
177 if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
178 aItem->SetSelected();
179 }
180
181protected:
186};
187
188
189// For LibEdit
190inline VECTOR2I mapCoords( const wxPoint& aCoord )
191{
192 return VECTOR2I( aCoord.x, -aCoord.y );
193}
194
195inline VECTOR2I mapCoords( const VECTOR2I& aCoord )
196{
197 return VECTOR2I( aCoord.x, -aCoord.y );
198}
199
200inline VECTOR2I mapCoords( const int x, const int y )
201{
202 return VECTOR2I( x, -y );
203}
204
205#endif
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
void ClearSelected()
Definition: eda_item.h:121
bool IsSelected() const
Definition: eda_item.h:107
void SetSelected()
Definition: eda_item.h:118
EDA_ITEM * GetParent() const
Definition: eda_item.h:99
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:145
A foundation class for a tool operating on a schematic or symbol.
Definition: ee_tool_base.h:50
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: ee_tool_base.h:86
EE_TOOL_BASE(const std::string &aName)
Create a tool with given name.
Definition: ee_tool_base.h:55
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:103
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aType, bool aAppend=false, bool aDirtyConnectivity=true)
Definition: ee_tool_base.h:134
KIGFX::SCH_VIEW * m_view
Definition: ee_tool_base.h:183
~EE_TOOL_BASE() override
Definition: ee_tool_base.h:63
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:184
bool Init() override
Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:66
bool m_isSymbolEditor
Definition: ee_tool_base.h:185
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: view.cpp:1586
The base class for drawable items used by schematic library symbols.
Definition: lib_item.h:61
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, bool aDirtyConnectivity=true)
Create a copy of the current schematic item, and put it in the undo list.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:147
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
The symbol library editor main window.
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.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
@ MODEL_RELOAD
Model changes (required full reload)
Definition: tool_base.h:80
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:44
#define SELECTED_BY_DRAG
Item was algorithmically selected as a dragged item.
VECTOR2I mapCoords(const wxPoint &aCoord)
Definition: ee_tool_base.h:190
@ FRAME_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCH_FIELD_T
Definition: typeinfo.h:155
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:157
@ SCH_PIN_T
Definition: typeinfo.h:159
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618