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
58static void moveSelectionBy( const PCB_SELECTION& aSelection, const VECTOR2I& aMoveVec,
59 BOARD_COMMIT& commit, bool aAllowFreePads )
60{
61 std::set<BOARD_ITEM*> moved;
62
63 for( EDA_ITEM* item : aSelection )
64 {
65 if( !item->IsBOARD_ITEM() )
66 continue;
67
68 BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
69
70 if( boardItem->Type() == PCB_PAD_T && !aAllowFreePads )
71 boardItem = boardItem->GetParent();
72
73 if( !moved.insert( boardItem ).second )
74 continue;
75
76 commit.Modify( boardItem, nullptr, RECURSE_MODE::RECURSE );
77 boardItem->Move( aMoveVec );
78 }
79}
80
81
83 PCB_TOOL_BASE( "pcbnew.PositionRelative" ),
84 m_dialog( nullptr ),
85 m_selectionTool( nullptr ),
87{
88}
89
90
92{
93 if( aReason != RUN )
94 m_commit = std::make_unique<BOARD_COMMIT>( this );
95}
96
97
99{
100 // Find the selection tool, so they can cooperate
102
103 return m_selectionTool != nullptr;
104}
105
106
108{
110
111 const auto& selection = m_selectionTool->RequestSelection(
112 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
113 {
114 sTool->FilterCollectorForHierarchy( aCollector, true );
115 sTool->FilterCollectorForMarkers( aCollector );
116 sTool->FilterCollectorForLockedItems( aCollector );
117 } );
118
119 if( m_selectionTool->ReportFilteredLockedItems() )
120 return 0;
121
122 if( selection.Empty() )
123 return 0;
124
126
127 // We prefer footprints, then pads, then anything else here.
128 EDA_ITEM* preferredItem = m_selection.GetTopLeftItem( true );
129
130 if( !preferredItem && m_selection.HasType( PCB_PAD_T ) )
131 {
132 PCB_SELECTION padsOnly = m_selection;
133 std::deque<EDA_ITEM*>& items = padsOnly.Items();
134 items.erase( std::remove_if( items.begin(), items.end(),
135 []( const EDA_ITEM* aItem )
136 {
137 return aItem->Type() != PCB_PAD_T;
138 } ), items.end() );
139
140 preferredItem = padsOnly.GetTopLeftItem();
141 }
142
143 if( preferredItem )
144 m_selectionAnchor = preferredItem->GetPosition();
145 else
146 m_selectionAnchor = m_selection.GetTopLeftItem()->GetPosition();
147
148 // The dialog is not modal and not deleted between calls.
149 // It means some options can have changed since the last call.
150 // Therefore we need to rebuild it in case UI units have changed since the last call.
151 if( m_dialog && m_dialog->GetUserUnits() != editFrame->GetUserUnits() )
152 {
153 m_dialog->Destroy();
154 m_dialog = nullptr;
155 }
156
157 if( !m_dialog )
158 m_dialog = new DIALOG_POSITION_RELATIVE( editFrame );
159
160 m_dialog->Show( true );
161
162 return 0;
163}
164
165
167{
169 return false;
170
172
173 // First, acquire the selection that we will be moving after we have the new offset vector.
174 const PCB_SELECTION& selection = m_selectionTool->RequestSelection(
175 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
176 {
177 sTool->FilterCollectorForHierarchy( aCollector, true );
178 sTool->FilterCollectorForMarkers( aCollector );
179 sTool->FilterCollectorForLockedItems( aCollector );
180 } );
181
182 if( m_selectionTool->ReportFilteredLockedItems() )
183 return 0;
184
185 if( selection.Empty() )
186 return 0;
187
188 if( m_isFootprintEditor && !frame()->GetModel() )
189 return 0;
190
191 if( frame()->IsCurrentTool( ACTIONS::measureTool ) )
192 return 0;
193
194 auto& view = *getView();
195 auto& controls = *getViewControls();
196
197 frame()->PushTool( aEvent );
198
199 bool invertXAxis = displayOptions().m_DisplayInvertXAxis;
200 bool invertYAxis = displayOptions().m_DisplayInvertYAxis;
201
203 {
204 invertXAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertXAxis;
205 invertYAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertYAxis;
206 }
207
209 PCB_GRID_HELPER grid( m_toolMgr, frame()->GetMagneticItemsSettings() );
210 bool originSet = false;
211 EDA_UNITS units = frame()->GetUserUnits();
212 KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr, pcbIUScale, units, invertXAxis, invertYAxis );
213 STATUS_TEXT_POPUP statusPopup( frame() );
214
215 bool allowFreePads = m_isFootprintEditor
216 || ( frame()->GetPcbNewSettings()
217 && frame()->GetPcbNewSettings()->m_AllowFreePads );
218
219 // Some colour to make it obviously not just a ruler
220 ruler.SetColor( view.GetPainter()->GetSettings()->GetLayerColor( LAYER_ANCHOR ) );
221 ruler.SetShowTicks( false );
222 ruler.SetShowEndArrowHead( true );
223
224 view.Add( &ruler );
225 view.SetVisible( &ruler, false );
226
227 auto setCursor =
228 [&]()
229 {
230 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::MEASURE );
231 };
232
233 const auto setInitialMsg =
234 [&]()
235 {
236 statusPopup.SetText( _( "Select the reference point on the item to move." ) );
237 };
238
239 const auto setDragMsg =
240 [&]()
241 {
242 statusPopup.SetText( _( "Select the point to define the new offset from." ) );
243 };
244
245 const auto setPopupPosition =
246 [&]()
247 {
248 statusPopup.Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, -50 ) );
249 };
250
251 auto cleanup =
252 [&] ()
253 {
254 view.SetVisible( &ruler, false );
255 controls.SetAutoPan( false );
256 controls.CaptureCursor( false );
257 controls.ForceCursorPosition( false );
258 originSet = false;
259 setInitialMsg();
260 };
261
262 const auto applyVector =
263 [&]( const VECTOR2I& aMoveVec )
264 {
265 BOARD_COMMIT commit( frame() );
266 moveSelectionBy( selection, aMoveVec, commit, allowFreePads );
267 commit.Push( _( "Set Relative Position Interactively" ) );
268 };
269
270 Activate();
271 // Must be done after Activate() so that it gets set into the correct context
272 controls.ShowCursor( true );
273 controls.SetAutoPan( false );
274 controls.CaptureCursor( false );
275 controls.ForceCursorPosition( false );
276
277 // Set initial cursor
278 setCursor();
279
280 setInitialMsg();
281
282 setPopupPosition();
283 statusPopup.Popup();
284 canvas()->SetStatusPopup( statusPopup.GetPanel() );
285
286 while( TOOL_EVENT* evt = Wait() )
287 {
288 setCursor();
289 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
290 grid.SetUseGrid( view.GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
291 VECTOR2I cursorPos = evt->HasPosition() ? evt->Position() : controls.GetMousePosition();
292 setPopupPosition();
293
294 if( !evt->IsActivate() && !evt->IsCancelInteractive() )
295 {
296 // If we are switching, the canvas may not be valid any more
297 cursorPos = grid.ResolveSnap( cursorPos, nullptr ).position;
298 controls.ForceCursorPosition( true, cursorPos );
299 }
300 else
301 {
302 grid.FullReset();
303 }
304
305 if( evt->IsCancelInteractive() )
306 {
307 if( originSet )
308 {
309 cleanup();
310 }
311 else
312 {
313 frame()->PopTool( aEvent );
314 break;
315 }
316 }
317 else if( evt->IsActivate() )
318 {
319 if( originSet )
320 cleanup();
321
322 frame()->PopTool( aEvent );
323 break;
324 }
325 // click or drag starts
326 else if( !originSet && ( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) ) )
327 {
328 twoPtMgr.SetOrigin( cursorPos );
329 twoPtMgr.SetEnd( cursorPos );
330
331 setDragMsg();
332
333 controls.CaptureCursor( true );
334 controls.SetAutoPan( true );
335
336 originSet = true;
337 }
338 // second click or mouse up after drag ends
339 else if( originSet && ( evt->IsClick( BUT_LEFT ) || evt->IsMouseUp( BUT_LEFT ) ) )
340 {
341 // Hide the popup text so it doesn't get in the way
342 statusPopup.Hide();
343
344 // This is the forward vector from the ruler item
345 VECTOR2I offsetVector = twoPtMgr.GetEnd() - twoPtMgr.GetOrigin();
346 const VECTOR2I toReferencePtVector = twoPtMgr.GetOrigin() - twoPtMgr.GetEnd();
347
348 // Start with the value of that vector in the dialog (will match the rule HUD)
349 DIALOG_OFFSET_ITEM dlg( *frame(), offsetVector );
350
351 if( dlg.ShowModal() == wxID_OK )
352 {
353 const VECTOR2I move = toReferencePtVector + offsetVector;
354
355 applyVector( move );
356
357 // Leave the arrow in place but update it
358 twoPtMgr.SetEnd( twoPtMgr.GetOrigin() + offsetVector );
359 view.Update( &ruler, KIGFX::GEOMETRY );
360 canvas()->Refresh();
361 }
362
363 originSet = false;
364
365 setInitialMsg();
366
367 controls.SetAutoPan( false );
368 controls.CaptureCursor( false );
369
370 statusPopup.Popup();
371 }
372 // move or drag when origin set updates rules
373 else if( originSet && ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
374 {
375 auto snap = LEADER_MODE::DIRECT;
376
377 if( frame()->IsType( FRAME_PCB_EDITOR ) )
378 snap = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" )->m_AngleSnapMode;
379 else
380 snap = GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" )->m_AngleSnapMode;
381
382 twoPtMgr.SetAngleSnap( snap );
383 // The end is fixed; we must update the origin
384 twoPtMgr.SetOrigin( cursorPos );
385
386 view.SetVisible( &ruler, true );
387 view.Update( &ruler, KIGFX::GEOMETRY );
388 }
389 else if( evt->IsAction( &ACTIONS::updateUnits ) )
390 {
391 if( frame()->GetUserUnits() != units )
392 {
393 units = frame()->GetUserUnits();
394 ruler.SwitchUnits( units );
395 view.Update( &ruler, KIGFX::GEOMETRY );
396 canvas()->ForceRefresh();
397 }
398
399 evt->SetPassEvent();
400 }
401 else if( evt->IsAction( &ACTIONS::updatePreferences ) )
402 {
403 invertXAxis = displayOptions().m_DisplayInvertXAxis;
404 invertYAxis = displayOptions().m_DisplayInvertYAxis;
405
407 {
408 invertXAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertXAxis;
409 invertYAxis = frame()->GetFootprintEditorSettings()->m_DisplayInvertYAxis;
410 }
411
412 ruler.UpdateDir( invertXAxis, invertYAxis );
413
414 view.Update( &ruler, KIGFX::GEOMETRY );
415 canvas()->Refresh();
416 evt->SetPassEvent();
417 }
418 else if( !evt->IsMouseAction() )
419 {
420 // Often this will end up changing the items we just moved, so the ruler will be
421 // in the wrong place. Clear it away and the user can restart
422 twoPtMgr.Reset();
423 view.SetVisible( &ruler, false );
424 view.Update( &ruler, KIGFX::GEOMETRY );
425
426 evt->SetPassEvent();
427 }
428 else
429 {
430 evt->SetPassEvent();
431 }
432 }
433
434 view.SetVisible( &ruler, false );
435 view.Remove( &ruler );
436
437 frame()->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
438 controls.SetAutoPan( false );
439 controls.CaptureCursor( false );
440 controls.ForceCursorPosition( false );
441
442 canvas()->SetStatusPopup( nullptr );
443 return 0;
444}
445
446
448 const VECTOR2I& aTranslation )
449{
450 VECTOR2I aggregateTranslation = aPosAnchor + aTranslation - GetSelectionAnchorPosition();
451
452 bool allowFreePads = m_isFootprintEditor
453 || ( frame()->GetPcbNewSettings()
454 && frame()->GetPcbNewSettings()->m_AllowFreePads );
455
456 moveSelectionBy( m_selection, aggregateTranslation, *m_commit, allowFreePads );
457 m_commit->Push( _( "Position Relative" ) );
458
459 if( m_selection.IsHover() )
461
463
464 canvas()->Refresh();
465 return 0;
466}
467
468
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
static TOOL_ACTION updatePreferences
Definition actions.h:274
static TOOL_ACTION updateUnits
Definition actions.h:203
static TOOL_ACTION measureTool
Definition actions.h:248
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:220
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:83
virtual void Move(const VECTOR2I &aMoveVector)
Move this object.
Definition board_item.h:404
BOARD_ITEM_CONTAINER * GetParent() const
Definition board_item.h:235
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:96
virtual VECTOR2I GetPosition() const
Definition eda_item.h:282
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition actions.h:350
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:60
void SwitchUnits(EDA_UNITS aUnits)
Switch the ruler units.
Definition ruler_item.h:88
void UpdateDir(bool aFlipX, bool aFlipY)
Definition ruler_item.h:90
void SetColor(const COLOR4D &aColor)
Definition ruler_item.h:56
void SetShowTicks(bool aShow)
Definition ruler_item.h:58
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.
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.
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
Returns the instance of #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
@ RUN
Tool is invoked after being inactive.
Definition tool_base.h:75
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:64
@ ARROW
Definition cursors.h:42
#define _(s)
@ RECURSE
Definition eda_item.h:49
EDA_UNITS
Definition eda_units.h:44
@ 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.
Definition wxgtk/ui.cpp:839
Class to handle a set of BOARD_ITEMs.
see class PGM_BASE
static void moveSelectionBy(const PCB_SELECTION &aSelection, const VECTOR2I &aMoveVec, BOARD_COMMIT &commit, 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:80
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683