From 4a9df1e18e101718318e3b33106804bfbeeb68f1 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Tue, 21 Nov 2023 23:43:48 +0000 Subject: [PATCH] ADDED: actions for left-, center-, and right-justifying text items. (For both PCBNew and EESchema.) Fixes https://gitlab.com/kicad/code/kicad/-/issues/12375 --- common/tool/actions.cpp | 21 +++++++ eeschema/tools/sch_edit_tool.cpp | 99 ++++++++++++++++++++++++++++++++ eeschema/tools/sch_edit_tool.h | 2 + include/tool/actions.h | 3 + pcbnew/tools/edit_tool.cpp | 78 +++++++++++++++++++++++++ pcbnew/tools/edit_tool.h | 5 ++ 6 files changed, 208 insertions(+) diff --git a/common/tool/actions.cpp b/common/tool/actions.cpp index 494c9427a8..49d2e17038 100644 --- a/common/tool/actions.cpp +++ b/common/tool/actions.cpp @@ -277,6 +277,27 @@ TOOL_ACTION ACTIONS::deleteTool( TOOL_ACTION_ARGS() .Icon( BITMAPS::delete_cursor ) .Flags( AF_ACTIVATE ) ); +TOOL_ACTION ACTIONS::leftJustify( TOOL_ACTION_ARGS() + .Name( "common.Control.leftJustify" ) + .Scope( AS_GLOBAL ) + .FriendlyName( _( "Left Justify" ) ) + .Tooltip( _( "Left-justify fields and text items" ) ) + .Icon( BITMAPS::text_align_left ) ); + +TOOL_ACTION ACTIONS::centerJustify( TOOL_ACTION_ARGS() + .Name( "common.Control.centerJustify" ) + .Scope( AS_GLOBAL ) + .FriendlyName( _( "Center Justify" ) ) + .Tooltip( _( "Center-justify fields and text items" ) ) + .Icon( BITMAPS::text_align_center ) ); + +TOOL_ACTION ACTIONS::rightJustify( TOOL_ACTION_ARGS() + .Name( "common.Control.rightJustify" ) + .Scope( AS_GLOBAL ) + .FriendlyName( _( "Right Justify" ) ) + .Tooltip( _( "Right-justify fields and text items" ) ) + .Icon( BITMAPS::text_align_right ) ); + TOOL_ACTION ACTIONS::activatePointEditor( TOOL_ACTION_ARGS() .Name( "common.Control.activatePointEditor" ) .Scope( AS_GLOBAL ) ); diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp index 98d9aa04c1..feef497b03 100644 --- a/eeschema/tools/sch_edit_tool.cpp +++ b/eeschema/tools/sch_edit_tool.cpp @@ -2347,6 +2347,102 @@ int SCH_EDIT_TOOL::ChangeTextType( const TOOL_EVENT& aEvent ) } +int SCH_EDIT_TOOL::JustifyText( const TOOL_EVENT& aEvent ) +{ + static std::vector justifiableItems = { + SCH_FIELD_T, + SCH_TEXT_T, + SCH_TEXTBOX_T, + SCH_LABEL_T + }; + + EE_SELECTION& selection = m_selectionTool->RequestSelection( justifiableItems ); + + if( selection.GetSize() == 0 ) + return 0; + + SCH_ITEM* item = static_cast( selection.Front() ); + bool moving = item->IsMoving(); + SCH_COMMIT localCommit( m_toolMgr ); + SCH_COMMIT* commit = dynamic_cast( aEvent.Commit() ); + + if( !commit ) + commit = &localCommit; + + auto setJustify = + [&]( EDA_TEXT* aTextItem ) + { + if( aEvent.Matches( ACTIONS::leftJustify.MakeEvent() ) ) + aTextItem->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT ); + else if( aEvent.Matches( ACTIONS::centerJustify.MakeEvent() ) ) + aTextItem->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER ); + else + aTextItem->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT ); + }; + + for( EDA_ITEM* edaItem : selection ) + { + item = static_cast( edaItem ); + + if( !moving ) + commit->Modify( item, m_frame->GetScreen() ); + + if( item->Type() == SCH_FIELD_T ) + { + setJustify( static_cast( item ) ); + + // Now that we're re-justifying a field, they're no longer autoplaced. + static_cast( item->GetParent() )->ClearFieldsAutoplaced(); + } + else if( item->Type() == SCH_TEXT_T ) + { + setJustify( static_cast( item ) ); + } + else if( item->Type() == SCH_TEXTBOX_T ) + { + setJustify( static_cast( item ) ); + } + else if( item->Type() == SCH_LABEL_T ) + { + SCH_LABEL* label = static_cast( item ); + + if( label->GetTextAngle() == ANGLE_HORIZONTAL ) + setJustify( label ); + } + + m_frame->UpdateItem( item, false, true ); + } + + // Update R-Tree for modified items + for( EDA_ITEM* selected : selection ) + updateItem( selected, true ); + + if( item->IsMoving() ) + { + m_toolMgr->RunAction( ACTIONS::refreshPreview ); + } + else + { + EE_SELECTION selectionCopy = selection; + + if( selection.IsHover() ) + m_toolMgr->RunAction( EE_ACTIONS::clearSelection ); + + if( !localCommit.Empty() ) + { + if( aEvent.Matches( ACTIONS::leftJustify.MakeEvent() ) ) + localCommit.Push( _( "Left Justify" ) ); + else if( aEvent.Matches( ACTIONS::centerJustify.MakeEvent() ) ) + localCommit.Push( _( "Center Justify" ) ); + else + localCommit.Push( _( "Right Justify" ) ); + } + } + + return 0; +} + + int SCH_EDIT_TOOL::BreakWire( const TOOL_EVENT& aEvent ) { bool isSlice = aEvent.Matches( EE_ACTIONS::slice.MakeEvent() ); @@ -2659,6 +2755,9 @@ void SCH_EDIT_TOOL::setTransitions() Go( &SCH_EDIT_TOOL::ChangeTextType, EE_ACTIONS::toCLabel.MakeEvent() ); Go( &SCH_EDIT_TOOL::ChangeTextType, EE_ACTIONS::toText.MakeEvent() ); Go( &SCH_EDIT_TOOL::ChangeTextType, EE_ACTIONS::toTextBox.MakeEvent() ); + Go( &SCH_EDIT_TOOL::JustifyText, ACTIONS::leftJustify.MakeEvent() ); + Go( &SCH_EDIT_TOOL::JustifyText, ACTIONS::centerJustify.MakeEvent() ); + Go( &SCH_EDIT_TOOL::JustifyText, ACTIONS::rightJustify.MakeEvent() ); Go( &SCH_EDIT_TOOL::BreakWire, EE_ACTIONS::breakWire.MakeEvent() ); Go( &SCH_EDIT_TOOL::BreakWire, EE_ACTIONS::slice.MakeEvent() ); diff --git a/eeschema/tools/sch_edit_tool.h b/eeschema/tools/sch_edit_tool.h index dfe8a6fcc8..1e25d4b28f 100644 --- a/eeschema/tools/sch_edit_tool.h +++ b/eeschema/tools/sch_edit_tool.h @@ -69,6 +69,8 @@ public: */ int ChangeTextType( const TOOL_EVENT& aEvent ); + int JustifyText( const TOOL_EVENT& aEvent ); + int BreakWire( const TOOL_EVENT& aEvent ); int CleanupSheetPins( const TOOL_EVENT& aEvent ); diff --git a/include/tool/actions.h b/include/tool/actions.h index 182f516aaa..04cd63e9e1 100644 --- a/include/tool/actions.h +++ b/include/tool/actions.h @@ -74,6 +74,9 @@ public: static TOOL_ACTION duplicate; static TOOL_ACTION doDelete; // sadly 'delete' is a reserved word static TOOL_ACTION deleteTool; + static TOOL_ACTION leftJustify; + static TOOL_ACTION centerJustify; + static TOOL_ACTION rightJustify; // Find and Replace static TOOL_ACTION showSearch; diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp index 89f0f4c1cf..7bf43c7a57 100644 --- a/pcbnew/tools/edit_tool.cpp +++ b/pcbnew/tools/edit_tool.cpp @@ -2034,6 +2034,81 @@ int EDIT_TOOL::Mirror( const TOOL_EVENT& aEvent ) } +int EDIT_TOOL::JustifyText( const TOOL_EVENT& aEvent ) +{ + if( isRouterActive() ) + { + wxBell(); + return 0; + } + + BOARD_COMMIT localCommit( this ); + BOARD_COMMIT* commit = dynamic_cast( aEvent.Commit() ); + + if( !commit ) + commit = &localCommit; + + PCB_SELECTION& selection = m_selectionTool->RequestSelection( + []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool ) + { + sTool->FilterCollectorForHierarchy( aCollector, true ); + }, + !m_dragging /* prompt user regarding locked items */ ); + + if( selection.Empty() ) + return 0; + + auto setJustify = + [&]( EDA_TEXT* aTextItem ) + { + if( aEvent.Matches( ACTIONS::leftJustify.MakeEvent() ) ) + aTextItem->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT ); + else if( aEvent.Matches( ACTIONS::centerJustify.MakeEvent() ) ) + aTextItem->SetHorizJustify( GR_TEXT_H_ALIGN_CENTER ); + else + aTextItem->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT ); + }; + + for( EDA_ITEM* item : selection ) + { + if( item->Type() == PCB_FIELD_T || item->Type() == PCB_TEXT_T ) + { + if( !item->IsNew() && !item->IsMoving() ) + commit->Modify( item ); + + setJustify( static_cast( item ) ); + } + else if( item->Type() == PCB_TEXTBOX_T ) + { + if( !item->IsNew() && !item->IsMoving() ) + commit->Modify( item ); + + setJustify( static_cast( item ) ); + } + } + + if( !localCommit.Empty() ) + { + if( aEvent.Matches( ACTIONS::leftJustify.MakeEvent() ) ) + localCommit.Push( _( "Left Justify" ) ); + else if( aEvent.Matches( ACTIONS::centerJustify.MakeEvent() ) ) + localCommit.Push( _( "Center Justify" ) ); + else + localCommit.Push( _( "Right Justify" ) ); + } + + if( selection.IsHover() && !m_dragging ) + m_toolMgr->RunAction( PCB_ACTIONS::selectionClear ); + + m_toolMgr->ProcessEvent( EVENTS::SelectedItemsModified ); + + if( m_dragging ) + m_toolMgr->PostAction( PCB_ACTIONS::updateLocalRatsnest, VECTOR2I() ); + + return 0; +} + + int EDIT_TOOL::Flip( const TOOL_EVENT& aEvent ) { if( isRouterActive() ) @@ -2871,6 +2946,9 @@ void EDIT_TOOL::setTransitions() Go( &EDIT_TOOL::BooleanPolygons, PCB_ACTIONS::mergePolygons.MakeEvent() ); Go( &EDIT_TOOL::BooleanPolygons, PCB_ACTIONS::subtractPolygons.MakeEvent() ); Go( &EDIT_TOOL::BooleanPolygons, PCB_ACTIONS::intersectPolygons.MakeEvent() ); + Go( &EDIT_TOOL::JustifyText, ACTIONS::leftJustify.MakeEvent() ); + Go( &EDIT_TOOL::JustifyText, ACTIONS::centerJustify.MakeEvent() ); + Go( &EDIT_TOOL::JustifyText, ACTIONS::rightJustify.MakeEvent() ); Go( &EDIT_TOOL::copyToClipboard, ACTIONS::copy.MakeEvent() ); Go( &EDIT_TOOL::copyToClipboard, PCB_ACTIONS::copyWithReference.MakeEvent() ); diff --git a/pcbnew/tools/edit_tool.h b/pcbnew/tools/edit_tool.h index 3b4c3773af..89880718cc 100644 --- a/pcbnew/tools/edit_tool.h +++ b/pcbnew/tools/edit_tool.h @@ -103,6 +103,11 @@ public: static const std::vector MirrorableItems; + /** + * Set the justification on any text items (or fields) in the current selection. + */ + int JustifyText( const TOOL_EVENT& aEvent ); + /** * Swap currently selected items' positions. Changes position of each item to the next. */