KiCad PCB EDA Suite
Loading...
Searching...
No Matches
generator_tool.cpp
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) 2023 Alex Shvartzkop <[email protected]>
5 * Copyright (C) 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#include "generator_tool.h"
26
27#include <collectors.h>
28#include <tool/tool_manager.h>
30#include <tools/pcb_actions.h>
31#include <router/router_tool.h>
32
33#include <dialog_generators.h>
34
35
37 GENERATOR_TOOL_PNS_PROXY( "pcbnew.Generators" ),
38 m_mgrDialog( nullptr )
39{
41 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
42 {
43 // Special case: propagate lock from generated items to parent generator
44
45 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
46
47 if( item && aProperty->Name() == _HKI( "Locked" ) )
48 {
49 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item->GetParentGroup() ) )
50 {
51 if( aCommit->GetStatus( generator ) != CHT_MODIFY )
52 aCommit->Modify( generator );
53
54 // Must set generator to unlocked first or item->IsLocked() will just
55 // return the parent's locked state.
56 generator->SetLocked( false );
57 generator->SetLocked( item->IsLocked() );
58 }
59 }
60 } );
61
63 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
64 {
65 // Special case: regenerator generators when their properties change
66
67 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( aItem ) )
68 {
69 BOARD_COMMIT* commit = static_cast<BOARD_COMMIT*>( aCommit );
70
71 generator->EditStart( this, board(), commit );
72 generator->Update( this, board(), commit );
73 generator->EditPush( this, board(), commit );
74 }
75 } );
76}
77
78
80{
81}
82
83
85{
87}
88
89
91{
92 auto tuningPatternCondition =
93 []( const SELECTION& aSel )
94 {
95 for( EDA_ITEM* item : aSel )
96 {
97 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item ) )
98 {
99 if( generator->GetGeneratorType() == wxS( "tuning_pattern" ) )
100 return true;
101 }
102 }
103
104 return false;
105 };
106
107 // Add the generator control menus to relevant other tools
108
110
111 if( selTool )
112 {
113 TOOL_MENU& toolMenu = selTool->GetToolMenu();
114 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
115
116 menu.AddItem( PCB_ACTIONS::regenerateAllTuning, tuningPatternCondition, 100 );
117 }
118
119 ROUTER_TOOL* routerTool = m_toolMgr->GetTool<ROUTER_TOOL>();
120
121 if( routerTool )
122 {
123 TOOL_MENU& toolMenu = routerTool->GetToolMenu();
124 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
125
127 }
128
129 return true;
130}
131
132
134{
135 if( m_mgrDialog )
136 {
137 m_mgrDialog->Destroy();
138 m_mgrDialog = nullptr;
139 }
140}
141
142
144{
145 PCB_EDIT_FRAME* pcbFrame = static_cast<PCB_EDIT_FRAME*>( frame() );
146
147 if( !pcbFrame )
148 return 0;
149
150 if( !m_mgrDialog )
151 {
152 m_mgrDialog = new DIALOG_GENERATORS( pcbFrame, pcbFrame );
153 }
154 else
155 {
157 }
158
159 m_mgrDialog->Show( true );
160
161 return 0;
162}
163
164
166{
167 wxString generatorType = aEvent.Parameter<wxString>();
168 BOARD_COMMIT commit( this );
169 wxString commitMsg;
170 int commitFlags = 0;
171
172 if( generatorType == wxS( "*" ) )
173 commitMsg = _( "Regenerate All" );
174
175 for( PCB_GENERATOR* generator : board()->Generators() )
176 {
177 if( generatorType == wxS( "*" ) || generator->GetGeneratorType() == generatorType )
178 {
179 if( commitMsg.IsEmpty() )
180 commitMsg.Printf( _( "Update %s" ), generator->GetPluralName() );
181
182 generator->EditStart( this, board(), &commit );
183 generator->Update( this, board(), &commit );
184 generator->EditPush( this, board(), &commit, commitMsg, commitFlags );
185
186 commitFlags |= APPEND_UNDO;
187 }
188 }
189
190 frame()->RefreshCanvas();
191 return 0;
192}
193
194
196{
197 BOARD_COMMIT commit( this );
198 int commitFlags = 0;
199
201
202 PCB_SELECTION sel = selTool->RequestSelection(
203 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
204 {
205 // Iterate from the back so we don't have to worry about removals.
206 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
207 {
208 BOARD_ITEM* item = aCollector[i];
209
210 if( item->Type() != PCB_GENERATOR_T )
211 aCollector.Remove( item );
212 }
213 } );
214
215 GENERATORS generators;
216
217 for( EDA_ITEM* item : sel )
218 {
219 if( PCB_GENERATOR* gen = dynamic_cast<PCB_GENERATOR*>( item ) )
220 generators.push_back( gen );
221 }
222
223#ifdef GENERATOR_ORDER
224 std::sort( generators.begin(), generators.end(),
225 []( const PCB_GENERATOR* a, const PCB_GENERATOR* b ) -> bool
226 {
227 return a->GetUpdateOrder() < b->GetUpdateOrder();
228 } );
229#endif
230
231 for( PCB_GENERATOR* gen : generators )
232 {
233 gen->EditStart( this, board(), &commit );
234 gen->Update( this, board(), &commit );
235 gen->EditPush( this, board(), &commit, _( "Regenerate Selected" ), commitFlags );
236
237 commitFlags |= APPEND_UNDO;
238 }
239
240 frame()->RefreshCanvas();
241 return 0;
242}
243
244
246{
247 BOARD_COMMIT commit( this );
248 int commitFlags = 0;
249
250 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
251
252 gen->EditStart( this, board(), &commit );
253 gen->Update( this, board(), &commit );
254 gen->EditPush( this, board(), &commit, _( "Regenerate Item" ), commitFlags );
255
256 frame()->RefreshCanvas();
257 return 0;
258}
259
260
262{
263 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
264
265 wxCHECK( commit, 0 );
266
267 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
268
269 if( aEvent.IsAction( &PCB_ACTIONS::genStartEdit ) )
270 {
271 gen->EditStart( this, board(), commit );
272 }
273 else if( aEvent.IsAction( &PCB_ACTIONS::genUpdateEdit ) )
274 {
275 gen->Update( this, board(), commit );
276 }
277 else if( aEvent.IsAction( &PCB_ACTIONS::genPushEdit ) )
278 {
279 gen->EditPush( this, board(), commit, wxEmptyString );
280
281 wxASSERT( commit->Empty() );
282 }
283 else if( aEvent.IsAction( &PCB_ACTIONS::genRevertEdit ) )
284 {
285 gen->EditRevert( this, board(), commit );
286
287 wxASSERT( commit->Empty() );
288 }
289 else if( aEvent.IsAction( &PCB_ACTIONS::genRemove ) )
290 {
291 gen->Remove( this, board(), commit );
292 }
293
294 return 0;
295}
296
297
299{
300 // Generator actions
302
306
312}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
PCB_GROUP * GetParentGroup() const
Definition: board_item.h:91
virtual bool IsLocked() const
Definition: board_item.cpp:74
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:81
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition: collector.h:109
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:74
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
bool Empty() const
Returns status of an item.
Definition: commit.h:144
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Definition: commit.cpp:129
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.
bool Show(bool show) override
void RefreshCanvas() override
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
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:206
A proxy class to allow access to the PNS router from the generator tool.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int ShowGeneratorsManager(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void setTransitions() override
< Set up handlers for various events.
int RegenerateSelected(const TOOL_EVENT &aEvent)
int GenEditAction(const TOOL_EVENT &aEvent)
int RegenerateItem(const TOOL_EVENT &aEvent)
void DestroyManagerDialog()
DIALOG_GENERATORS * m_mgrDialog
int RegenerateAllOfType(const TOOL_EVENT &aEvent)
bool Init() override
Init() is called once upon a registration of the tool.
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
static TOOL_ACTION genPushEdit
Definition: pcb_actions.h:281
static TOOL_ACTION genStartEdit
Definition: pcb_actions.h:279
static TOOL_ACTION genRemove
Definition: pcb_actions.h:283
static TOOL_ACTION generatorsShowManager
Definition: pcb_actions.h:285
static TOOL_ACTION genUpdateEdit
Definition: pcb_actions.h:280
static TOOL_ACTION regenerateAll
Definition: pcb_actions.h:276
static TOOL_ACTION regenerateAllTuning
Generator tool.
Definition: pcb_actions.h:275
static TOOL_ACTION regenerateSelected
Definition: pcb_actions.h:277
static TOOL_ACTION genRevertEdit
Definition: pcb_actions.h:282
The main frame for Pcbnew.
virtual void Remove(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)
virtual void EditRevert(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)
virtual void EditPush(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit, const wxString &aCommitMsg=wxEmptyString, int aCommitFlags=0)
virtual void EditStart(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)
virtual bool Update(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)
The selection tool: currently supports:
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter, bool aConfirmLockedItems=false)
Return the current selection, filtered according to aClientFilter.
PCB_BASE_EDIT_FRAME * frame() const
BOARD * board() const
const wxString & Name() const
Definition: property.h:217
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
void RegisterListener(TYPE_ID aType, PROPERTY_LISTENER aListenerFunc)
Registers a listener for the given type.
Definition: property_mgr.h:275
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:167
COMMIT * Commit() const
Returns information about difference between current mouse cursor position and the place where draggi...
Definition: tool_event.h:275
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:82
T Parameter() const
Return a parameter assigned to the event.
Definition: tool_event.h:460
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
TOOL_MENU & GetToolMenu()
Manage a CONDITIONAL_MENU and some number of CONTEXT_MENUs as sub-menus.
Definition: tool_menu.h:43
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:44
@ CHT_MODIFY
Definition: commit.h:44
#define _HKI(x)
#define _(s)
#define TYPE_HASH(x)
Definition: property.h:71
#define APPEND_UNDO
Definition: sch_commit.h:42
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition: typeinfo.h:91