KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
sch_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 The 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 SCH_TOOL_BASE_H
26#define SCH_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#include <sch_shape.h>
39
40class SCH_SELECTION;
41
47template <class T>
49{
50public:
54 SCH_TOOL_BASE( const std::string& aName ) :
55 TOOL_INTERACTIVE ( aName ),
56 m_frame( nullptr ),
57 m_view( nullptr ),
58 m_selectionTool( nullptr ),
59 m_isSymbolEditor( false )
60 {};
61
62 ~SCH_TOOL_BASE() override {};
63
65 bool Init() override
66 {
67 m_frame = getEditFrame<T>();
70
71 // A basic context menu. Many (but not all) tools will choose to override this.
72 auto& ctxMenu = m_menu->GetMenu();
73
74 // cancel current tool goes in main context menu at the top if present
76 ctxMenu.AddSeparator( 1 );
77
78 // Finally, add the standard zoom/grid items
79 m_frame->AddStandardSubMenus( *m_menu.get() );
80
81 return true;
82 }
83
85 void Reset( RESET_REASON aReason ) override
86 {
87 if( aReason == MODEL_RELOAD || aReason == SUPERMODEL_RELOAD )
88 {
89 // Init variables used by every drawing tool
90 m_frame = getEditFrame<T>();
91 m_isSymbolEditor = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame ) != nullptr;
92 }
93
94 m_view = static_cast<KIGFX::SCH_VIEW*>( getView() );
95 }
96
100 bool IsSymbolEditor() const
101 {
102 return m_isSymbolEditor;
103 }
104
105protected:
110 void updateItem( EDA_ITEM* aItem, bool aUpdateRTree ) const
111 {
112 m_frame->UpdateItem( aItem, false, aUpdateRTree );
113 }
114
117 void saveCopyInUndoList( EDA_ITEM* aItem, UNDO_REDO aType, bool aAppend = false,
118 bool aDirtyConnectivity = true )
119 {
120 wxASSERT( aItem );
121
122 KICAD_T itemType = aItem->Type();
123 bool selected = aItem->IsSelected();
124
125 // IS_SELECTED flag should not be set on undo items which were added for
126 // a drag operation.
127 if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
128 aItem->ClearSelected();
129
130 if( m_isSymbolEditor )
131 {
132 SYMBOL_EDIT_FRAME* editFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( m_frame );
133 wxCHECK_RET( editFrame, wxT( "editFrame is null" ) );
134
135 editFrame->SaveCopyInUndoList( wxEmptyString, dynamic_cast<LIB_SYMBOL*>( aItem ) );
136 }
137 else
138 {
139 SCH_EDIT_FRAME* editFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_frame );
140 wxASSERT( editFrame );
141
142 if( editFrame )
143 {
144 if( itemType == SCH_FIELD_T )
145 {
146 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
147 static_cast<SCH_ITEM*>( aItem->GetParent() ),
148 UNDO_REDO::CHANGED, aAppend,
149 false );
150 }
151 else if( itemType == SCH_PIN_T || itemType == SCH_SHEET_PIN_T )
152 {
153 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
154 static_cast<SCH_ITEM*>( aItem->GetParent() ),
155 UNDO_REDO::CHANGED, aAppend,
156 aDirtyConnectivity );
157 }
158 else
159 {
160 editFrame->SaveCopyInUndoList( editFrame->GetScreen(),
161 static_cast<SCH_ITEM*>( aItem ), aType,
162 aAppend, aDirtyConnectivity );
163 }
164 }
165 }
166
167 if( selected && aItem->HasFlag( SELECTED_BY_DRAG ) )
168 aItem->SetSelected();
169 }
170
171protected:
176};
177
178
179#endif
static TOOL_ACTION cancelInteractive
Definition: actions.h:72
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:96
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:108
void ClearSelected()
Definition: eda_item.h:133
bool IsSelected() const
Definition: eda_item.h:123
void SetSelected()
Definition: eda_item.h:130
EDA_ITEM * GetParent() const
Definition: eda_item.h:110
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:142
Define a library symbol object.
Definition: lib_symbol.h:85
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:167
A foundation class for a tool operating on a schematic or symbol.
Definition: sch_tool_base.h:49
void updateItem(EDA_ITEM *aItem, bool aUpdateRTree) const
Similar to getView()->Update(), but handles items that are redrawn by their parents and updating the ...
void saveCopyInUndoList(EDA_ITEM *aItem, UNDO_REDO aType, bool aAppend=false, bool aDirtyConnectivity=true)
bool IsSymbolEditor() const
Returns true if the tool is running in the symbol editor.
KIGFX::SCH_VIEW * m_view
bool Init() override
Init() is called once upon a registration of the tool.
Definition: sch_tool_base.h:65
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
Definition: sch_tool_base.h:85
~SCH_TOOL_BASE() override
Definition: sch_tool_base.h:62
SCH_TOOL_BASE(const std::string &aName)
Create a tool with given name.
Definition: sch_tool_base.h:54
SCH_SELECTION_TOOL * m_selectionTool
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:220
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:38
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
std::unique_ptr< TOOL_MENU > m_menu
The functions below are not yet implemented - their interface may change.
#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_FIELD_T
Definition: typeinfo.h:150
@ SCH_SHEET_PIN_T
Definition: typeinfo.h:174
@ 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...