KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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_edit_frame.h>
36#include <sch_view.h>
37#include <symbol_edit_frame.h>
38
39class EE_SELECTION;
40
46template <class T>
48{
49public:
53 EE_TOOL_BASE( const std::string& aName ) :
54 TOOL_INTERACTIVE ( aName ),
55 m_frame( nullptr ),
56 m_view( nullptr ),
57 m_selectionTool( nullptr ),
58 m_isSymbolEditor( false )
59 {};
60
61 ~EE_TOOL_BASE() override {};
62
64 bool Init() override
65 {
66 m_frame = getEditFrame<T>();
69
70 // A basic context menu. Many (but not all) tools will choose to override this.
71 auto& ctxMenu = m_menu.GetMenu();
72
73 // cancel current tool goes in main context menu at the top if present
75 ctxMenu.AddSeparator( 1 );
76
77 // Finally, add the standard zoom/grid items
78 m_frame->AddStandardSubMenus( m_menu );
79
80 return true;
81 }
82
84 void Reset( RESET_REASON aReason ) override
85 {
86 if( aReason == MODEL_RELOAD || aReason == SUPERMODEL_RELOAD )
87 {
88 // Init variables used by every drawing tool
89 m_frame = getEditFrame<T>();
90 m_isSymbolEditor = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) != nullptr;
91 }
92
93 m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
94 }
95
99 bool IsSymbolEditor() const
100 {
101 return m_isSymbolEditor;
102 }
103
104protected:
109 void updateItem( EDA_ITEM* aItem, bool aUpdateRTree ) const
110 {
111 switch( aItem->Type() )
112 {
113 case SCH_SHEET_PIN_T:
114 getView()->Update( aItem );
115 getView()->Update( aItem->GetParent() );
116
117 // Moving sheet pins does not change the BBox.
118 break;
119
120 case SCH_PIN_T:
121 case SCH_FIELD_T:
122 case SCH_TABLECELL_T:
123 getView()->Update( aItem );
124 getView()->Update( aItem->GetParent() );
125
126 if( aUpdateRTree )
127 m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem->GetParent() ) );
128
129 break;
130
131 default:
132 getView()->Update( aItem );
133
134 if( aUpdateRTree && dynamic_cast<SCH_ITEM*>( aItem ) )
135 m_frame->GetScreen()->Update( static_cast<SCH_ITEM*>( aItem ) );
136
137 break;
138 }
139 }
140
143 void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aType, bool aAppend = false,
144 bool aDirtyConnectivity = true )
145 {
146 wxASSERT( aItem );
147
148 KICAD_T itemType = aItem->Type();
149 bool selected = aItem->IsSelected();
150
151 // IS_SELECTED flag should not be set on undo items which were added for
152 // a drag operation.
153 if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
154 aItem->ClearSelected();
155
156 if( m_isSymbolEditor )
157 {
158 SYMBOL_EDIT_FRAME* editFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
159 wxCHECK_RET( editFrame, wxT( "editFrame is null" ) );
160
161 editFrame->SaveCopyInUndoList( wxEmptyString, dynamic_cast<LIB_SYMBOL*>( aItem ) );
162 }
163 else
164 {
165 SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
166 wxASSERT( editFrame );
167
168 if( editFrame )
169 {
170 if( itemType == SCH_FIELD_T )
171 {
172 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
173 static_cast<SCH_ITEM*>( aItem->GetParent() ),
174 UNDO_REDO::CHANGED, aAppend,
175 false );
176 }
177 else if( itemType == SCH_PIN_T || itemType == SCH_SHEET_PIN_T )
178 {
179 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
180 static_cast<SCH_ITEM*>( aItem->GetParent() ),
181 UNDO_REDO::CHANGED, aAppend,
182 aDirtyConnectivity );
183 }
184 else
185 {
186 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
187 static_cast<SCH_ITEM*>( aItem ), aType,
188 aAppend, aDirtyConnectivity );
189 }
190 }
191 }
192
193 if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
194 aItem->SetSelected();
195 }
196
197protected:
202};
203
204
205#endif
static TOOL_ACTION cancelInteractive
Definition: actions.h:64
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:88
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
void ClearSelected()
Definition: eda_item.h:121
bool IsSelected() const
Definition: eda_item.h:109
void SetSelected()
Definition: eda_item.h:118
EDA_ITEM * GetParent() const
Definition: eda_item.h:102
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:130
A foundation class for a tool operating on a schematic or symbol.
Definition: ee_tool_base.h:48
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: ee_tool_base.h:84
EE_TOOL_BASE(const std::string &aName)
Create a tool with given name.
Definition: ee_tool_base.h:53
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:109
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aType, bool aAppend=false, bool aDirtyConnectivity=true)
Definition: ee_tool_base.h:143
KIGFX::SCH_VIEW * m_view
Definition: ee_tool_base.h:199
~EE_TOOL_BASE() override
Definition: ee_tool_base.h:61
bool IsSymbolEditor() const
Returns true if the tool is running in the symbol editor.
Definition: ee_tool_base.h:99
EE_SELECTION_TOOL * m_selectionTool
Definition: ee_tool_base.h:200
bool Init() override
Init() is called once upon a registration of the tool.
Definition: ee_tool_base.h:64
bool m_isSymbolEditor
Definition: ee_tool_base.h:201
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:1631
Define a library symbol object.
Definition: lib_symbol.h:77
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:166
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
The symbol library editor main window.
void SaveCopyInUndoList(const wxString &aDescription, LIB_SYMBOL *aSymbol, UNDO_REDO aUndoType=UNDO_REDO::LIBEDIT)
Create a copy of the current symbol, and save it in the undo list.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:217
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 (the sheet for a schematic)
Definition: tool_base.h:80
@ SUPERMODEL_RELOAD
For schematics, the entire schematic changed, not just the sheet.
Definition: tool_base.h:81
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.
@ 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_TABLECELL_T
Definition: typeinfo.h:166
@ SCH_FIELD_T
Definition: typeinfo.h:150
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:173
@ SCH_PIN_T
Definition: typeinfo.h:153
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...