libfield.cpp 4.72 KB
Newer Older
1 2 3
/*****************************************************/
/*  Component library edit field manipulation code.  */
/*****************************************************/
4

5 6 7 8 9
#include <fctsys.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <class_sch_screen.h>
10

11 12 13 14 15
#include <general.h>
#include <sch_component.h>
#include <libeditframe.h>
#include <class_library.h>
#include <template_fieldnames.h>
16
#include <dialog_edit_one_field.h>
17 18


19
void LIB_EDIT_FRAME::EditField( LIB_FIELD* aField )
20
{
21
    wxString text;
22
    wxString title;
23
    wxString caption;
24
    wxString oldName;
25

26
    if( aField == NULL )
27
        return;
28

29
    LIB_COMPONENT* parent = aField->GetParent();
30

31 32
    // Editing the component value field is equivalent to creating a new component based
    // on the current component.  Set the dialog message to inform the user.
33
    if( aField->GetId() == VALUE )
34 35 36 37 38 39
    {
        caption = _( "Component Name" );
        title = _( "Enter a name to create a new component based on this one." );
    }
    else
    {
40
        caption.Printf( _( "Edit Field %s" ), GetChars( aField->GetName() ) );
41 42 43
        title.Printf( _( "Enter a new value for the %s field." ),
                      GetChars( aField->GetName().Lower() ) );
    }
44

45
    DIALOG_LIB_EDIT_ONE_FIELD dlg( this, caption, aField );
46

47
    if( dlg.ShowModal() != wxID_OK  )
48
        return;
49

50
    text = dlg.GetTextField();
51

52
    // Perform some controls:
53
    if( ( aField->GetId() == REFERENCE || aField->GetId() == VALUE ) && text.IsEmpty ( ) )
54
    {
55 56
        title.Printf( _( "A %s field cannot be empty." ), GetChars(aField->GetName().Lower() ) );
        DisplayError( this, title );
57
        return;
58 59
    }

60 61 62 63 64 65 66 67
    // Ensure the reference prefix is acceptable:
    if( ( aField->GetId() == REFERENCE ) &&
        ! SCH_COMPONENT::IsReferenceStringValid( text ) )
    {
        DisplayError( this, _( "Illegal reference. A reference must start by a letter" ) );
        return;
    }

68
    wxString fieldText = aField->GetFullText( m_unit );
69

70 71 72
    /* If the value field is changed, this is equivalent to creating a new component from
     * the old one.  Rename the component and remove any conflicting aliases to prevent name
     * errors when updating the library.
73
     */
74
    if( (aField->GetId() == VALUE) && ( text != aField->GetText() ) )
75
    {
76 77
        wxString msg;

78
        // Test the current library for name conflicts.
79
        if( m_library && m_library->FindEntry( text ) != NULL )
80
        {
81 82 83
            msg.Printf( _( "The name <%s> conflicts with an existing entry in the component \
library <%s>.\n\nDo you wish to replace the current component in library with this one?" ),
                        GetChars( text ),
84
                        GetChars( m_library->GetName() ) );
85 86 87 88 89 90

            int rsp = wxMessageBox( msg, _( "Confirm" ),
                                    wxYES_NO | wxICON_QUESTION | wxNO_DEFAULT, this );

            if( rsp == wxNO )
                return;
91
        }
92

93 94 95 96 97
        // Test the current component for name conflicts.
        if( parent->HasAlias( text ) )
        {
            msg.Printf( _( "The current component already has an alias named <%s>.\n\nDo you \
wish to remove this alias from the component?" ),
98
                        GetChars( text ) );
99 100 101 102 103 104 105 106 107 108 109 110

            int rsp = wxMessageBox( msg, _( "Confirm" ), wxYES_NO | wxICON_QUESTION, this );

            if( rsp == wxNO )
                return;

            parent->RemoveAlias( text );
        }

        parent->SetName( text );

        // Test the library for any conflicts with the any aliases in the current component.
111
        if( parent->GetAliasCount() > 1 && m_library && m_library->Conflicts( parent ) )
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        {
            msg.Printf( _( "The new component contains alias names that conflict with entries \
in the component library <%s>.\n\nDo you wish to remove all of the conflicting aliases from \
this component?" ),
                        GetChars( m_library->GetName() ) );

            int rsp = wxMessageBox( msg, _( "Confirm" ), wxYES_NO | wxICON_QUESTION, this );

            if( rsp == wxNO )
            {
                parent->SetName( fieldText );
                return;
            }

            wxArrayString aliases = parent->GetAliasNames( false );

            for( size_t i = 0;  i < aliases.GetCount();  i++ )
            {
                if( m_library->FindEntry( aliases[ i ] ) != NULL )
                    parent->RemoveAlias( aliases[ i ] );
            }
        }

        if( !parent->HasAlias( m_aliasName ) )
            m_aliasName = text;
    }
    else
    {
        aField->SetText( text );
    }
142

143
    if( !aField->InEditMode() )
144
        SaveCopyInUndoList( parent );
145

146 147
    // Update field
    dlg.TransfertDataToField();
148

149
    m_canvas->Refresh();
150

151
    OnModify();
152
    UpdateAliasSelectList();
153
}