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 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "generator_tool.h"
22
23#include <collectors.h>
24#include <tool/tool_manager.h>
26#include <tools/pcb_actions.h>
27#include <router/router_tool.h>
28
29#include <dialog_generators.h>
30#include <properties/property.h>
32
33
35 GENERATOR_TOOL_PNS_PROXY( "pcbnew.Generators" ),
36 m_mgrDialog( nullptr )
37{
40 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
41 {
42 // Special case: propagate lock from generated items to parent generator
43
44 BOARD_ITEM* item = dynamic_cast<BOARD_ITEM*>( aItem );
45
46 if( item && aProperty->Name() == _HKI( "Locked" ) )
47 {
48 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item->GetParentGroup() ) )
49 {
50 if( aCommit->GetStatus( generator ) != CHT_MODIFY )
51 aCommit->Modify( generator );
52
53 // Must set generator to unlocked first or item->IsLocked() will just
54 // return the parent's locked state.
55 generator->SetLocked( false );
56 generator->SetLocked( item->IsLocked() );
57 }
58 }
59 } );
60
63 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
64 {
65 // Special case: regenerate 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->EditFinish( this, board(), commit );
74 }
75 } );
76}
77
78
80{
81 // Unregister before the rest of the object tears down so a property
82 // change firing mid-destruction cannot dispatch into a half-destroyed
83 // listener body.
84 m_generatorListener.reset();
85 m_boardItemListener.reset();
86}
87
88
93
94
96{
97 auto tuningPatternCondition =
98 []( const SELECTION& aSel )
99 {
100 for( EDA_ITEM* item : aSel )
101 {
102 if( PCB_GENERATOR* generator = dynamic_cast<PCB_GENERATOR*>( item ) )
103 {
104 if( generator->GetGeneratorType() == wxS( "tuning_pattern" ) )
105 return true;
106 }
107 }
108
109 return false;
110 };
111
112 // Add the generator control menus to relevant other tools
113
114 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
115
116 if( selTool )
117 {
118 TOOL_MENU& toolMenu = selTool->GetToolMenu();
119 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
120
121 menu.AddItem( PCB_ACTIONS::regenerateAllTuning, tuningPatternCondition, 100 );
122 }
123
124 ROUTER_TOOL* routerTool = m_toolMgr->GetTool<ROUTER_TOOL>();
125
126 if( routerTool )
127 {
128 TOOL_MENU& toolMenu = routerTool->GetToolMenu();
129 CONDITIONAL_MENU& menu = toolMenu.GetMenu();
130
132 }
133
134 return true;
135}
136
137
139{
140 if( m_mgrDialog )
141 {
142 m_mgrDialog->Destroy();
143 m_mgrDialog = nullptr;
144 }
145}
146
147
149{
150 PCB_EDIT_FRAME* pcbFrame = static_cast<PCB_EDIT_FRAME*>( frame() );
151
152 if( !pcbFrame )
153 return 0;
154
155 if( !m_mgrDialog )
156 {
157 m_mgrDialog = new DIALOG_GENERATORS( pcbFrame, pcbFrame );
158 }
159 else
160 {
161 m_mgrDialog->RebuildModels();
162 }
163
164 m_mgrDialog->Show( true );
165
166 return 0;
167}
168
169
171{
172 wxString generatorType = aEvent.Parameter<wxString>();
173 BOARD_COMMIT commit( this );
174 wxString commitMsg;
175 int commitFlags = 0;
176
177 if( generatorType == wxS( "*" ) )
178 commitMsg = _( "Regenerate All" );
179
180 for( PCB_GENERATOR* generator : board()->Generators() )
181 {
182 if( generatorType == wxS( "*" ) || generator->GetGeneratorType() == generatorType )
183 {
184 if( commitMsg.IsEmpty() )
185 commitMsg.Printf( _( "Update %s" ), generator->GetPluralName() );
186
187 generator->EditStart( this, board(), &commit );
188 generator->Update( this, board(), &commit );
189 generator->EditFinish( this, board(), &commit );
190
191 commit.Push( commitMsg, commitFlags );
192 commitFlags |= APPEND_UNDO;
193 }
194 }
195
196 frame()->RefreshCanvas();
197 return 0;
198}
199
200
202{
203 BOARD_COMMIT commit( this );
204 int commitFlags = 0;
205
206 PCB_SELECTION_TOOL* selTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
207
208 PCB_SELECTION sel = selTool->RequestSelection(
209 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
210 {
211 // Iterate from the back so we don't have to worry about removals.
212 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
213 {
214 BOARD_ITEM* item = aCollector[i];
215
216 if( item->Type() != PCB_GENERATOR_T )
217 aCollector.Remove( item );
218 }
219 } );
220
221 GENERATORS generators;
222
223 for( EDA_ITEM* item : sel )
224 {
225 if( PCB_GENERATOR* gen = dynamic_cast<PCB_GENERATOR*>( item ) )
226 generators.push_back( gen );
227 }
228
229#ifdef GENERATOR_ORDER
230 std::sort( generators.begin(), generators.end(),
231 []( const PCB_GENERATOR* a, const PCB_GENERATOR* b ) -> bool
232 {
233 return a->GetUpdateOrder() < b->GetUpdateOrder();
234 } );
235#endif
236
237 for( PCB_GENERATOR* gen : generators )
238 {
239 gen->EditStart( this, board(), &commit );
240 gen->Update( this, board(), &commit );
241 gen->EditFinish( this, board(), &commit );
242
243 commit.Push( _( "Regenerate Selected" ), commitFlags );
244 commitFlags |= APPEND_UNDO;
245 }
246
247 frame()->RefreshCanvas();
248 return 0;
249}
250
251
253{
254 BOARD_COMMIT commit( this );
255
256 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
257
258 gen->EditStart( this, board(), &commit );
259 gen->Update( this, board(), &commit );
260 gen->EditFinish( this, board(), &commit );
261
262 commit.Push( gen->GetCommitMessage() );
263
264 frame()->RefreshCanvas();
265 return 0;
266}
267
268
270{
271 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
272
273 wxCHECK( commit, 0 );
274
275 PCB_GENERATOR* gen = aEvent.Parameter<PCB_GENERATOR*>();
276
277 if( aEvent.IsAction( &PCB_ACTIONS::genStartEdit ) )
278 {
279 gen->EditStart( this, board(), commit );
280 }
281 else if( aEvent.IsAction( &PCB_ACTIONS::genUpdateEdit ) )
282 {
283 gen->Update( this, board(), commit );
284 }
285 else if( aEvent.IsAction( &PCB_ACTIONS::genFinishEdit ) )
286 {
287 gen->EditFinish( this, board(), commit );
288 }
289 else if( aEvent.IsAction( &PCB_ACTIONS::genCancelEdit ) )
290 {
291 gen->EditCancel( this, board(), commit );
292 }
293 else if( aEvent.IsAction( &PCB_ACTIONS::genRemove ) )
294 {
295 gen->Remove( this, board(), commit );
296 }
297
298 return 0;
299}
300
301
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
bool IsLocked() const override
int GetCount() const
Return the number of objects in the list.
Definition collector.h:79
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:107
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition commit.h:68
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:191
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:96
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:114
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:203
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
GENERATOR_TOOL_PNS_PROXY(const std::string &aToolName)
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)
DIALOG_GENERATORS * m_mgrDialog
PROPERTY_LISTENER_SUBSCRIPTION m_boardItemListener
RAII PROPERTY_MANAGER listener subscriptions; auto-unregister on destruction so this tool composes cl...
int RegenerateAllOfType(const TOOL_EVENT &aEvent)
PROPERTY_LISTENER_SUBSCRIPTION m_generatorListener
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:38
static TOOL_ACTION genFinishEdit
static TOOL_ACTION genStartEdit
static TOOL_ACTION genRemove
static TOOL_ACTION genCancelEdit
static TOOL_ACTION generatorsShowManager
static TOOL_ACTION genUpdateEdit
static TOOL_ACTION regenerateAll
static TOOL_ACTION regenerateAllTuning
Generator tool.
static TOOL_ACTION regenerateSelected
The main frame for Pcbnew.
virtual void Remove(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual void EditCancel(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual wxString GetCommitMessage() const =0
virtual void EditStart(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual bool Update(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
virtual void EditFinish(GENERATOR_TOOL *aTool, BOARD *aBoard, BOARD_COMMIT *aCommit)=0
The selection tool: currently supports:
PCB_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter)
Return the current selection, filtered according to aClientFilter.
T * frame() const
BOARD * board() const
const wxString & Name() const
Definition property.h:220
static PROPERTY_MANAGER & Instance()
class PROPERTY_LISTENER_SUBSCRIPTION RegisterListener(TYPE_ID aType, PROPERTY_LISTENER aListenerFunc)
Register a listener for the given type and return a move-only subscription that auto-unregisters in i...
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
Generic, UI-independent tool event.
Definition tool_event.h:167
COMMIT * Commit() const
Definition tool_event.h:279
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:469
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:39
CONDITIONAL_MENU & GetMenu()
Definition tool_menu.cpp:40
@ CHT_MODIFY
Definition commit.h:40
#define _(s)
#define _HKI(x)
Definition page_info.cpp:40
std::deque< PCB_GENERATOR * > GENERATORS
#define TYPE_HASH(x)
Definition property.h:74
#define APPEND_UNDO
Definition sch_commit.h:37
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:84
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683