7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-21 00:21:25 +00:00

Break out increment function

This commit is contained in:
John Beard 2024-10-09 11:23:56 +08:00
parent 01133d6150
commit f77d830d79
5 changed files with 118 additions and 59 deletions

View File

@ -583,6 +583,7 @@ set( COMMON_SRCS
grid_tricks.cpp
hotkey_store.cpp
hotkeys_basic.cpp
increment.cpp
kiface_base.cpp
kiway_player.cpp
lib_table_grid_tricks.cpp

77
common/increment.cpp Normal file
View File

@ -0,0 +1,77 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2024 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 "increment.h"
#include <wx/wxcrt.h>
bool IncrementString( wxString& name, int aIncrement )
{
if( name.IsEmpty() )
return true;
wxString suffix;
wxString digits;
wxString outputFormat;
wxString outputNumber;
int ii = name.Len() - 1;
int dCount = 0;
while( ii >= 0 && !wxIsdigit( name.GetChar( ii ) ) )
{
suffix = name.GetChar( ii ) + suffix;
ii--;
}
while( ii >= 0 && wxIsdigit( name.GetChar( ii ) ) )
{
digits = name.GetChar( ii ) + digits;
ii--;
dCount++;
}
if( digits.IsEmpty() )
return true;
long number = 0;
if( digits.ToLong( &number ) )
{
number += aIncrement;
// Don't let result go below zero
if( number > -1 )
{
name.Remove( ii + 1 );
//write out a format string with correct number of leading zeroes
outputFormat.Printf( wxS( "%%0%dld" ), dCount );
//write out the number using the format string
outputNumber.Printf( outputFormat, number );
name << outputNumber << suffix;
return true;
}
}
return false;
}

View File

@ -25,6 +25,7 @@
#include <advanced_config.h>
#include <base_units.h>
#include <increment.h>
#include <pgm_base.h>
#include <sch_edit_frame.h>
#include <sch_plotter.h>
@ -49,58 +50,6 @@
#include <api/schematic/schematic_types.pb.h>
bool IncrementLabelMember( wxString& name, int aIncrement )
{
if( name.IsEmpty() )
return true;
wxString suffix;
wxString digits;
wxString outputFormat;
wxString outputNumber;
int ii = name.Len() - 1;
int dCount = 0;
while( ii >= 0 && !wxIsdigit( name.GetChar( ii ) ) )
{
suffix = name.GetChar( ii ) + suffix;
ii--;
}
while( ii >= 0 && wxIsdigit( name.GetChar( ii ) ) )
{
digits = name.GetChar( ii ) + digits;
ii--;
dCount++;
}
if( digits.IsEmpty() )
return true;
long number = 0;
if( digits.ToLong( &number ) )
{
number += aIncrement;
// Don't let result go below zero
if( number > -1 )
{
name.Remove( ii + 1 );
//write out a format string with correct number of leading zeroes
outputFormat.Printf( wxS( "%%0%dld" ), dCount );
//write out the number using the format string
outputNumber.Printf( outputFormat, number );
name << outputNumber << suffix;
return true;
}
}
return false;
}
/* Coding polygons for global symbol graphic shapes.
* the first parml is the number of corners
* others are the corners coordinates in reduced units
@ -538,7 +487,7 @@ bool SCH_LABEL_BASE::IncrementLabel( int aIncrement )
{
wxString text = GetText();
if( IncrementLabelMember( text, aIncrement ) )
if( IncrementString( text, aIncrement ) )
{
SetText( text );
return true;

View File

@ -22,17 +22,19 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "symbol_editor_pin_tool.h"
#include <tools/ee_selection_tool.h>
#include <symbol_edit_frame.h>
#include <sch_commit.h>
#include <kidialog.h>
#include <ee_actions.h>
#include <dialogs/dialog_pin_properties.h>
#include <increment.h>
#include <settings/settings_manager.h>
#include <symbol_editor/symbol_editor_settings.h>
#include <pgm_base.h>
#include <wx/debug.h>
#include "symbol_editor_pin_tool.h"
static ELECTRICAL_PINTYPE g_LastPinType = ELECTRICAL_PINTYPE::PT_INPUT;
@ -81,9 +83,6 @@ static int GetLastPinNumSize()
}
extern bool IncrementLabelMember( wxString& name, int aIncrement );
SYMBOL_EDITOR_PIN_TOOL::SYMBOL_EDITOR_PIN_TOOL() :
EE_TOOL_BASE<SYMBOL_EDIT_FRAME>( "eeschema.PinEditing" )
{
@ -434,11 +433,11 @@ SCH_PIN* SYMBOL_EDITOR_PIN_TOOL::RepeatPin( const SCH_PIN* aSourcePin )
pin->Move( step );
wxString nextName = pin->GetName();
IncrementLabelMember( nextName, settings->m_Repeat.label_delta );
IncrementString( nextName, settings->m_Repeat.label_delta );
pin->SetName( nextName );
wxString nextNumber = pin->GetNumber();
IncrementLabelMember( nextNumber, settings->m_Repeat.label_delta );
IncrementString( nextNumber, settings->m_Repeat.label_delta );
pin->SetNumber( nextNumber );
if( m_frame->SynchronizePins() )

33
include/increment.h Normal file
View File

@ -0,0 +1,33 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2024 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
*/
#pragma once
#include <wx/string.h>
#include <kicommon.h>
/**
* Generic string incrementer.
*/
bool IncrementString( wxString& aStr, int aDelta );