KiCad PCB EDA Suite
Loading...
Searching...
No Matches
position_relative_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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <set>
23
24#include <board_commit.h>
25#include <collectors.h>
28#include <footprint.h>
31#include <kiplatform/ui.h>
32#include <pad.h>
33#include <pcb_group.h>
36#include <pcb_painter.h>
37#include <pgm_base.h>
39#include <render_settings.h>
41#include <status_popup.h>
42#include <tools/pcb_actions.h>
46#include <view/view_controls.h>
47
48
61static void moveSelectionBy( const PCB_SELECTION& aSelection, const VECTOR2I& aMoveVec,
62 BOARD_COMMIT& aCommit, bool aAllowFreePads )
63{
64 std::set<BOARD_ITEM*> moved;
65
66 for( EDA_ITEM* item : aSelection )
67 {
68 if( !item->IsBOARD_ITEM() )
69 continue;
70
71 BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
72
73 if( boardItem->Type() == PCB_PAD_T && !aAllowFreePads )
74 boardItem = boardItem->GetParent();
75
76 if( !moved.insert( boardItem ).second )
77 continue;
78
79 aCommit.Modify( boardItem, nullptr, RECURSE_MODE::RECURSE );
80 boardItem->Move( aMoveVec );
81 }
82}
83
84
86 PCB_TOOL_BASE( "pcbnew.PositionRelative" ),
87 m_dialog( nullptr ),
88 m_selectionTool( nullptr ),
90{
91}
92
93
95{
96 if( aReason == MODEL_RELOAD || aReason == SUPERMODEL_RELOAD || aReason == SHUTDOWN )
97 {
98 if( m_dialog )
99 {
100 m_dialog->Destroy();
101 m_dialog = nullptr;
102 }
103
104 m_selection.Clear();
106 }
107
108 m_commit = std::make_unique<BOARD_COMMIT>( frame() );
109}
110
111
113{
114 // Find the selection tool, so they can cooperate
116
117 return m_selectionTool != nullptr;
118}
119
120
122{
124
125 const auto& selection = m_selectionTool->RequestSelection(
126 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
127 {
128 sTool->FilterCollectorForHierarchy( aCollector, true );
129 sTool->FilterCollectorForMarkers( aCollector );
130 sTool->FilterCollectorForLockedItems( aCollector );
131 } );
132
133 if( m_selectionTool->ReportFilteredLockedItems() )
134 return 0;
135
136 if( selection.Empty() )
137 return 0;
138
140
141 // We prefer footprints, then pads, then anything else here.
142 EDA_ITEM* preferredItem = m_selection.GetTopLeftItem( true );
143
144 if( !preferredItem && m_selection.HasType( PCB_PAD_T ) )
145 {
146 PCB_SELECTION padsOnly = m_selection;
147 std::deque<EDA_ITEM*>& items = padsOnly.Items();
148 items.erase( std::remove_if( items.begin(), items.end(),
149 []( const EDA_ITEM* aItem )
150 {
151 return aItem->Type() != PCB_PAD_T;
152 } ), items.end() );
153
154 preferredItem = padsOnly.GetTopLeftItem();
155 }
156
157 if( preferredItem )
158 m_selectionAnchor = preferredItem->GetPosition();
159 else
160 m_selectionAnchor = m_selection.GetTopLeftItem()->GetPosition();
161
162 // The dialog is not modal and not deleted between calls.
163 // It means some options can have changed since the last call.
164 // Therefore we need to rebuild it in case UI units have changed since the last call.
165 if( m_dialog && m_dialog->GetUserUnits() != editFrame->GetUserUnits() )
166 {
167 m_dialog->Destroy();
168 m_dialog = nullptr;
169 }
170
171 if( !m_dialog )
172 m_dialog = new DIALOG_POSITION_RELATIVE( editFrame );
173
174 m_dialog->Show( true );
175
176 return 0;
177}
178
179
181{
183 return false;
184
186
187 // First, acquire the selection that we will be moving after we have the new offset vector.
188 const PCB_SELECTION& selection = m_selectionTool->RequestSelection(
189 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
190 {
191 sTool->FilterCollectorForHierarchy( aCollector, true );
192 sTool->FilterCollectorForMarkers( aCollector );
193 sTool->FilterCollectorForLockedItems( aCollector );
194 } );
195
196 if( m_selectionTool->ReportFilteredLockedItems() )
197 return 0;
198
199 if( selection.Empty() )
200 return 0;
201
202 if( m_isFootprintEditor && !frame()->GetModel() )
203 return 0;
204
205 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
206 return 0;
207
210 SCOPED_TOOL_PUSHER raii( frame(), aEvent );
211
212 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
213 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
214
216 {
217 invertXAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertXAxis;
218 invertYAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertYAxis;
219 }
220
222 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
223 bool originSet = false;
224 EDA_UNITS units = frame()->GetUserUnits();
225 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
226 STATUS_TEXT_POPUP statusPopup( frame() );
227
228 bool allowFreePads = m_isFootprintEditor
229 || ( frame()->GetPcbNewSettings()
230 && frame()->GetPcbNewSettings()->m_AllowFreePads );
231
232 // Some colour to make it obviously not just a ruler
233 ruler.SetColor( view->GetPainter()->GetSettings()->GetLayerColor( LAYER_ANCHOR ) );
234 ruler.SetShowTicks( false );
235 ruler.SetShowEndArrowHead( true );
236
237 view->Add( &ruler );
238 view->SetVisible( &ruler, false );
239
240 auto setCursor =
241 [&]()
242 {
243 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::MEASURE );
244 };
245
246 const auto setInitialMsg =
247 [&]()
248 {
249 statusPopup.SetText( _( "Select the reference point on the item to move." ) );
250 };
251
252 const auto setDragMsg =
253 [&]()
254 {
255 statusPopup.SetText( _( "Select the point to define the new offset from." ) );
256 };
257
258 const auto setPopupPosition =
259 [&]()
260 {
261 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
262 };
263
264 auto cleanup =
265 [&] ()
266 {
267 view->SetVisible( &ruler, false );
268 controls->SetAutoPan( false );
269 controls->CaptureCursor( false );
270 controls->ForceCursorPosition( false );
271 originSet = false;
272 setInitialMsg();
273 };
274
275 const auto applyVector =
276 [&]( const VECTOR2I& aMoveVec )
277 {
278 BOARD_COMMIT commit( frame() );
279 moveSelectionBy( selection, aMoveVec, commit, allowFreePads );
280 commit.Push( _( "Set Relative Position Interactively" ) );
281 };
282
283 Activate();
284 // Must be done after Activate() so that it gets set into the correct context
285 controls->ShowCursor( true );
286 controls->SetAutoPan( false );
287 controls->CaptureCursor( false );
288 controls->ForceCursorPosition( false );
289
290 // Set initial cursor
291 setCursor();
292
293 setInitialMsg();
294
295 setPopupPosition();
296 statusPopup.Popup();
297 canvas()->SetStatusPopup( statusPopup.GetPanel() );
298
299 while( TOOL_EVENT* evt = Wait() )
300 {
301 setCursor();
302 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
303 grid.SetUseGrid( view->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
304 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls->GetMousePosition();
305 setPopupPosition();
306
307 if( !evt->IsActivate() && !evt->IsCancelInteractive() )
308 {
309 // If we are switching, the canvas may not be valid any more
310 cursorPos = grid.ResolveSnap( cursorPos, nullptr ).position;
311 controls->ForceCursorPosition( true, cursorPos );
312 }
313 else
314 {
315 grid.FullReset();
316 }
317
318 if( evt->IsCancelInteractive() )
319 {
320 if( originSet )
321 cleanup();
322 else
323 break;
324 }
325 else if( evt->IsActivate() )
326 {
327 if( originSet )
328 cleanup();
329
330 break;
331 }
332 // click or drag starts
333 else if( !originSet && ( evt->IsDrag( BUT_LEFT )
334 || evt->IsClick( BUT_LEFT )
335 || evt->IsAction( &ACTIONS::cursorClick ) ) )
336 {
337 twoPtMgr.SetOrigin( cursorPos );
338 twoPtMgr.SetEnd( cursorPos );
339
340 setDragMsg();
341
342 controls->CaptureCursor( true );
343 controls->SetAutoPan( true );
344
345 originSet = true;
346 }
347 // second click or mouse up after drag ends
348 else if( originSet && ( evt->IsClick( BUT_LEFT )
349 || evt->IsAction( &ACTIONS::cursorClick )
350 || evt->IsMouseUp( BUT_LEFT ) ) )
351 {
352 // Hide the popup text so it doesn't get in the way
353 statusPopup.Hide();
354
355 // This is the forward vector from the ruler item
356 VECTOR2I offsetVector = twoPtMgr.GetEnd() - twoPtMgr.GetOrigin();
357 const VECTOR2I toReferencePtVector = twoPtMgr.GetOrigin() - twoPtMgr.GetEnd();
358
359 // Start with the value of that vector in the dialog (will match the rule HUD)
360 DIALOG_OFFSET_ITEM dlg( *frame(), offsetVector );
361
362 if( dlg.ShowModal() == wxID_OK )
363 {
364 const VECTOR2I move = toReferencePtVector + offsetVector;
365
366 applyVector( move );
367
368 // Leave the arrow in place but update it
369 twoPtMgr.SetEnd( twoPtMgr.GetOrigin() + offsetVector );
370 view->Update( &ruler, KIGFX::GEOMETRY );
371 canvas()->Refresh();
372 }
373
374 originSet = false;
375
376 setInitialMsg();
377
378 controls->SetAutoPan( false );
379 controls->CaptureCursor( false );
380
381 statusPopup.Popup();
382 }
383 // move or drag when origin set updates rules
384 else if( originSet && ( evt->IsMotion()
385 || evt->IsAction( &ACTIONS::refreshPreview )
386 || evt->IsDrag( BUT_LEFT ) ) )
387 {
388 auto snap = LEADER_MODE::DIRECT;
389
390 if( frame()->IsType( FRAME_PCB_EDITOR ) )
391 snap = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AngleSnapMode;
392 else
393 snap = GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AngleSnapMode;
394
395 twoPtMgr.SetAngleSnap( snap );
396 // The end is fixed; we must update the origin
397 twoPtMgr.SetOrigin( cursorPos );
398
399 view->SetVisible( &ruler, true );
400 view->Update( &ruler, KIGFX::GEOMETRY );
401 }
402 else if( evt->IsAction( &ACTIONS::updateUnits ) )
403 {
404 if( frame()->GetUserUnits() != units )
405 {
406 units = frame()->GetUserUnits();
407 ruler.SwitchUnits( units );
408 view->Update( &ruler, KIGFX::GEOMETRY );
409 canvas()->ForceRefresh();
410 }
411
412 evt->SetPassEvent();
413 }
414 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
415 {
416 invertXAxis = displayOptions().m_DisplayInvertXAxis;
417 invertYAxis = displayOptions().m_DisplayInvertYAxis;
418
420 {
421 invertXAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertXAxis;
422 invertYAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertYAxis;
423 }
424
425 ruler.UpdateDir( invertXAxis, invertYAxis );
426
427 view->Update( &ruler, KIGFX::GEOMETRY );
428 canvas()->Refresh();
429 evt->SetPassEvent();
430 }
431 else if( !evt->IsMouseAction() )
432 {
433 // Often this will end up changing the items we just moved, so the ruler will be
434 // in the wrong place. Clear it away and the user can restart
435 twoPtMgr.Reset();
436 view->SetVisible( &ruler, false );
437 view->Update( &ruler, KIGFX::GEOMETRY );
438
439 evt->SetPassEvent();
440 }
441 else
442 {
443 evt->SetPassEvent();
444 }
445 }
446
447 view->SetVisible( &ruler, false );
448 view->Remove( &ruler );
449
450 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
451 controls->SetAutoPan( false );
452 controls->CaptureCursor( false );
453 controls->ForceCursorPosition( false );
454
455 canvas()->SetStatusPopup( nullptr );
456 return 0;
457}
458
459
461 const VECTOR2I& aTranslation )
462{
463 VECTOR2I aggregateTranslation = aPosAnchor + aTranslation - GetSelectionAnchorPosition();
464
465 bool allowFreePads = m_isFootprintEditor
466 || ( frame()->GetPcbNewSettings()
467 && frame()->GetPcbNewSettings()->m_AllowFreePads );
468
469 moveSelectionBy( m_selection, aggregateTranslation, *m_commit, allowFreePads );
470 m_commit->Push( _( "Position Relative" ) );
471
472 if( m_selection.IsHover() )
474
476
477 canvas()->Refresh();
478 return 0;
479}
480
481
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
static TOOL_ACTION updatePreferences
Definition actions.h:277
static TOOL_ACTION updateUnits
Definition actions.h:204
static TOOL_ACTION measureTool
Definition actions.h:249
static TOOL_ACTION cursorClick
Definition actions.h:177
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:221
static TOOL_ACTION refreshPreview
Definition actions.h:156
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:99
virtual void Move(const VECTOR2I &aMoveVector)
Move this object.
Definition board_item.h:459
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:281
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
Dialog that invites the user to enter some kind of offset.
int ShowModal() override
void ForceRefresh()
Force a redraw.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
void SetStatusPopup(wxWindow *aPopup)
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
static const TOOL_EVENT SelectedItemsModified
Selected item had a property changed (except movement)
Definition actions.h:354
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:203
A drawn ruler item for showing the distance between two points.
Definition ruler_item.h:42
void SetShowEndArrowHead(bool aShow)
Definition ruler_item.h:59
void SwitchUnits(EDA_UNITS aUnits)
Switch the ruler units.
Definition ruler_item.h:87
void UpdateDir(bool aFlipX, bool aFlipY)
Definition ruler_item.h:89
void SetColor(const COLOR4D &aColor)
Definition ruler_item.h:55
void SetShowTicks(bool aShow)
Definition ruler_item.h:57
Represent a very simple geometry manager for items that have a start and end point.
void SetOrigin(const VECTOR2I &aOrigin)
Set the origin of the ruler (the fixed end)
void Reset()
Reset the manager to the initial state.
void SetEnd(const VECTOR2I &aEnd)
Set the current end of the rectangle (the end that moves with the cursor.
An interface for classes handling user events controlling the view behavior such as zooming,...
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:68
static TOOL_ACTION interactiveOffsetTool
static TOOL_ACTION positionRelative
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
The selection tool: currently supports:
void FilterCollectorForMarkers(GENERAL_COLLECTOR &aCollector) const
Drop any PCB_MARKERs from the collector.
void FilterCollectorForHierarchy(GENERAL_COLLECTOR &aCollector, bool aMultiselect) const
In general we don't want to select both a parent and any of it's children.
void FilterCollectorForLockedItems(GENERAL_COLLECTOR &aCollector)
In the PCB editor strip out any locked items unless the OverrideLocks checkbox is set.
EDA_ITEM * GetTopLeftItem(bool aFootprintsOnly=false) const override
T * frame() const
KIGFX::PCB_VIEW * view() const
KIGFX::VIEW_CONTROLS * controls() const
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
PCB_DRAW_PANEL_GAL * canvas() const
PCBNEW_SETTINGS::DISPLAY_OPTIONS & displayOptions() const
const PCB_SELECTION & selection() const
int RelativeItemSelectionMove(const VECTOR2I &anchor, const VECTOR2I &translation)
Position the m_position_relative_selection selection relative to anchor position using the given tran...
int PositionRelative(const TOOL_EVENT &aEvent)
Invoke a dialog box to allow positioning of the item relative to another by an exact amount.
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
bool Init() override
Init() is called once upon a registration of the tool.
wxWeakRef< DIALOG_POSITION_RELATIVE > m_dialog
std::unique_ptr< BOARD_COMMIT > m_commit
VECTOR2I GetSelectionAnchorPosition() const
Return the position of the selected item(s)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int InteractiveOffset(const TOOL_EVENT &aEvent)
Draw a line connecting two points and allow the user to enter what it should be.
PCB_SELECTION_TOOL * m_selectionTool
std::deque< EDA_ITEM * > & Items()
Definition selection.h:181
wxWindow * GetPanel()
virtual void Popup(wxWindow *aFocus=nullptr)
virtual void Move(const wxPoint &aWhere)
Extension of STATUS_POPUP for displaying a single line text.
void SetText(const wxString &aText)
Display a text.
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:182
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition tool_base.cpp:40
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
KIGFX::VIEW * getView() const
Return the instance of KIGFX::VIEW object used in the application.
Definition tool_base.cpp:34
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
@ SHUTDOWN
Tool is being shut down.
Definition tool_base.h:80
@ MODEL_RELOAD
Model changes (the sheet for a schematic)
Definition tool_base.h:76
@ SUPERMODEL_RELOAD
For schematics, the entire schematic changed, not just the sheet.
Definition tool_base.h:77
Generic, UI-independent tool event.
Definition tool_event.h:167
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_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
void Activate()
Run the tool.
EDA_UNITS GetUserUnits() const
@ MEASURE
Definition cursors.h:65
@ ARROW
Definition cursors.h:43
#define _(s)
@ RECURSE
Definition eda_item.h:51
EDA_UNITS
Definition eda_units.h:45
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ DIRECT
Unconstrained point-to-point.
@ LAYER_ANCHOR
Anchor of items having an anchor point (texts, footprints).
Definition layer_ids.h:244
@ GEOMETRY
Position or shape has changed.
Definition view_item.h:51
wxPoint GetMousePosition()
Returns the mouse position in screen coordinates.
Class to handle a set of BOARD_ITEMs.
see class PGM_BASE
static void moveSelectionBy(const PCB_SELECTION &aSelection, const VECTOR2I &aMoveVec, BOARD_COMMIT &aCommit, bool aAllowFreePads)
Move each item in the selection by the given vector.
T * GetAppSettings(const char *aFilename)
bool moved
@ MD_SHIFT
Definition tool_event.h:139
@ BUT_LEFT
Definition tool_event.h:128
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708