From f3e4e61fa742e3e087669be20189ff49c0976411 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Wed, 27 May 2020 12:13:57 +0100 Subject: [PATCH] Push some more editing code out to the tool framework. --- eeschema/CMakeLists.txt | 1 - eeschema/edit_label.cpp | 154 ------------------------------- eeschema/sch_edit_frame.h | 12 --- eeschema/sch_text.cpp | 27 +++++- eeschema/tools/sch_edit_tool.cpp | 81 +++++++++++++++- eeschema/tools/sch_edit_tool.h | 11 +++ 6 files changed, 116 insertions(+), 170 deletions(-) delete mode 100644 eeschema/edit_label.cpp diff --git a/eeschema/CMakeLists.txt b/eeschema/CMakeLists.txt index effce305a0..2a5ceaf436 100644 --- a/eeschema/CMakeLists.txt +++ b/eeschema/CMakeLists.txt @@ -155,7 +155,6 @@ set( EESCHEMA_SRCS component_references_lister.cpp connection_graph.cpp cross-probing.cpp - edit_label.cpp eeschema_config.cpp eeschema_settings.cpp erc.cpp diff --git a/eeschema/edit_label.cpp b/eeschema/edit_label.cpp deleted file mode 100644 index 81505b350e..0000000000 --- a/eeschema/edit_label.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* - * This program source code file is part of KiCad, a free EDA CAD application. - * - * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr - * Copyright (C) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, you may find one here: - * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html - * or you may search the http://www.gnu.org website for the version 2 license, - * or you may write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "invoke_sch_dialog.h" - -void SCH_EDIT_FRAME::ConvertTextType( SCH_TEXT* aText, KICAD_T aNewType ) -{ - KICAD_T oldType = aText->Type(); - bool selected = aText->IsSelected(); - - if( oldType == aNewType ) - return; - - SCH_TEXT* newtext = nullptr; - const wxPoint& position = aText->GetPosition(); - LABEL_SPIN_STYLE orientation = aText->GetLabelSpinStyle(); - wxString txt = UnescapeString( aText->GetText() ); - - // There can be characters in a SCH_TEXT object that can break labels so we have to - // fix them here. - if( oldType == SCH_TEXT_T ) - { - txt.Replace( "\n", "_" ); - txt.Replace( "\r", "_" ); - txt.Replace( "\t", "_" ); - txt.Replace( " ", "_" ); - } - - // label strings are "escaped" i.e. a '/' is replaced by "{slash}" - if( aNewType != SCH_TEXT_T ) - txt = EscapeString( txt, CTX_NETNAME ); - - switch( aNewType ) - { - case SCH_LABEL_T: newtext = new SCH_LABEL( position, txt ); break; - case SCH_GLOBAL_LABEL_T: newtext = new SCH_GLOBALLABEL( position, txt ); break; - case SCH_HIER_LABEL_T: newtext = new SCH_HIERLABEL( position, txt ); break; - case SCH_TEXT_T: newtext = new SCH_TEXT( position, txt ); break; - - default: - wxFAIL_MSG( wxString::Format( "Invalid text type: %d.", aNewType ) ); - return; - } - - // Copy the old text item settings to the new one. Justifications are not copied - // because they are not used in labels. Justifications will be set to default value - // in the new text item type. - // - newtext->SetFlags( aText->GetEditFlags() ); - newtext->SetShape( aText->GetShape() ); - newtext->SetLabelSpinStyle( orientation ); - newtext->SetTextSize( aText->GetTextSize() ); - newtext->SetTextThickness( aText->GetTextThickness() ); - newtext->SetItalic( aText->IsItalic() ); - newtext->SetBold( aText->IsBold() ); - newtext->SetIsDangling( aText->IsDangling() ); - - if( selected ) - m_toolManager->RunAction( EE_ACTIONS::removeItemFromSel, true, aText ); - - if( !aText->IsNew() ) - { - SaveCopyInUndoList( aText, UR_DELETED ); - SaveCopyInUndoList( newtext, UR_NEW, true ); - - RemoveFromScreen( aText ); - AddToScreen( newtext ); - } - - if( selected ) - m_toolManager->RunAction( EE_ACTIONS::addItemToSel, true, newtext ); - - // Otherwise, pointer is owned by the undo stack - if( aText->IsNew() ) - delete aText; - - if( aNewType == SCH_TEXT_T ) - { - if( newtext->IsDangling() ) - { - newtext->SetIsDangling( false ); - GetCanvas()->GetView()->Update( newtext, KIGFX::REPAINT ); - } - } - else - TestDanglingEnds(); - - OnModify(); -} - - -/* - * Function to increment bus label numbers. Adds aIncrement to labels which end in numbers. - */ -void IncrementLabelMember( wxString& name, int aIncrement ) -{ - int ii, nn; - long number = 0; - - ii = name.Len() - 1; nn = 0; - - if( !wxIsdigit( name.GetChar( ii ) ) ) - return; - - while( (ii >= 0) && wxIsdigit( name.GetChar( ii ) ) ) - { - ii--; nn++; - } - - ii++; /* digits are starting at ii position */ - wxString litt_number = name.Right( nn ); - - if( litt_number.ToLong( &number ) ) - { - number += aIncrement; - name.Remove( ii ); name << number; - } -} diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h index 8eabbc3227..dae2e315bc 100644 --- a/eeschema/sch_edit_frame.h +++ b/eeschema/sch_edit_frame.h @@ -706,18 +706,6 @@ public: */ bool AllowCaseSensitiveFileNameClashes( const wxString& aSchematicFileName ); - /** - * Change a text type to another one. - * - * The new text, label, hierarchical label, or global label is created from the old text - * and the old text object is deleted. - * - * A tricky case is when the 'old" text is being edited (i.e. moving) because we must - * create a new text, and prepare the undo/redo command data for this change and the - * current move/edit command - */ - void ConvertTextType( SCH_TEXT* aText, KICAD_T aNewType ); - /** * Edit an existing sheet or add a new sheet to the schematic. * diff --git a/eeschema/sch_text.cpp b/eeschema/sch_text.cpp index f86a4a3451..c55cd46a10 100644 --- a/eeschema/sch_text.cpp +++ b/eeschema/sch_text.cpp @@ -52,7 +52,32 @@ using KIGFX::SCH_RENDER_SETTINGS; -extern void IncrementLabelMember( wxString& name, int aIncrement ); +void IncrementLabelMember( wxString& name, int aIncrement ) +{ + int ii, nn; + long number = 0; + + ii = name.Len() - 1; nn = 0; + + if( !wxIsdigit( name.GetChar( ii ) ) ) + return; + + while( ii >= 0 && wxIsdigit( name.GetChar( ii ) ) ) + { + ii--; + nn++; + } + + ii++; /* digits are starting at ii position */ + wxString litt_number = name.Right( nn ); + + if( litt_number.ToLong( &number ) ) + { + number += aIncrement; + name.Remove( ii ); name << number; + } +} + /* Coding polygons for global symbol graphic shapes. * the first parml is the number of corners diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp index d2e94b6279..f6128435ae 100644 --- a/eeschema/tools/sch_edit_tool.cpp +++ b/eeschema/tools/sch_edit_tool.cpp @@ -1468,8 +1468,85 @@ int SCH_EDIT_TOOL::ChangeTextType( const TOOL_EVENT& aEvent ) { SCH_TEXT* text = dynamic_cast( selection.GetItem( i ) ); - if( text ) - m_frame->ConvertTextType( text, convertTo ); + if( text && text->Type() != convertTo ) + { + bool selected = text->IsSelected(); + SCH_TEXT* newtext = nullptr; + const wxPoint& position = text->GetPosition(); + LABEL_SPIN_STYLE orientation = text->GetLabelSpinStyle(); + wxString txt = UnescapeString( text->GetText() ); + + // There can be characters in a SCH_TEXT object that can break labels so we have to + // fix them here. + if( text->Type() == SCH_TEXT_T ) + { + txt.Replace( "\n", "_" ); + txt.Replace( "\r", "_" ); + txt.Replace( "\t", "_" ); + txt.Replace( " ", "_" ); + } + + // label strings are "escaped" i.e. a '/' is replaced by "{slash}" + if( convertTo != SCH_TEXT_T ) + txt = EscapeString( txt, CTX_NETNAME ); + + switch( convertTo ) + { + case SCH_LABEL_T: newtext = new SCH_LABEL( position, txt ); break; + case SCH_GLOBAL_LABEL_T: newtext = new SCH_GLOBALLABEL( position, txt ); break; + case SCH_HIER_LABEL_T: newtext = new SCH_HIERLABEL( position, txt ); break; + case SCH_TEXT_T: newtext = new SCH_TEXT( position, txt ); break; + + default: + wxFAIL_MSG( wxString::Format( "Invalid text type: %d.", convertTo ) ); + return 0; + } + + // Copy the old text item settings to the new one. Justifications are not copied + // because they are not used in labels. Justifications will be set to default value + // in the new text item type. + // + newtext->SetFlags( text->GetEditFlags() ); + newtext->SetShape( text->GetShape() ); + newtext->SetLabelSpinStyle( orientation ); + newtext->SetTextSize( text->GetTextSize() ); + newtext->SetTextThickness( text->GetTextThickness() ); + newtext->SetItalic( text->IsItalic() ); + newtext->SetBold( text->IsBold() ); + newtext->SetIsDangling( text->IsDangling() ); + + if( selected ) + m_toolMgr->RunAction( EE_ACTIONS::removeItemFromSel, true, text ); + + if( !text->IsNew() ) + { + m_frame->SaveCopyInUndoList( text, UR_DELETED ); + m_frame->SaveCopyInUndoList( newtext, UR_NEW, true ); + + m_frame->RemoveFromScreen( text ); + m_frame->AddToScreen( newtext ); + } + + if( selected ) + m_toolMgr->RunAction( EE_ACTIONS::addItemToSel, true, newtext ); + + // Otherwise, pointer is owned by the undo stack + if( text->IsNew() ) + delete text; + + if( convertTo == SCH_TEXT_T ) + { + if( newtext->IsDangling() ) + { + newtext->SetIsDangling( false ); + getView()->Update( newtext, KIGFX::REPAINT ); + } + } + else + m_frame->TestDanglingEnds(); + + m_frame->OnModify(); + } } if( selection.IsHover() ) diff --git a/eeschema/tools/sch_edit_tool.h b/eeschema/tools/sch_edit_tool.h index 91beb2104d..93bb00e037 100644 --- a/eeschema/tools/sch_edit_tool.h +++ b/eeschema/tools/sch_edit_tool.h @@ -55,6 +55,17 @@ public: int ConvertDeMorgan( const TOOL_EVENT& aEvent ); int ChangeShape( const TOOL_EVENT& aEvent ); + + /** + * Change a text type to another one. + * + * The new text, label, hierarchical label, or global label is created from the old text + * and the old text object is deleted. + * + * A tricky case is when the 'old" text is being edited (i.e. moving) because we must + * create a new text, and prepare the undo/redo command data for this change and the + * current move/edit command + */ int ChangeTextType( const TOOL_EVENT& aEvent ); int BreakWire( const TOOL_EVENT& aEvent );