KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_editor_context.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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 <lib_id.h>
23#include <lib_symbol.h>
24#include <pgm_base.h>
25#include <project.h>
26#include <project_sch.h>
27#include <sch_io/sch_io.h>
28#include <sch_io/sch_io_mgr.h>
29#include <symbol_edit_frame.h>
32
33#include <wx/filename.h>
34
35
37{
38public:
39 explicit SYMBOL_EDIT_FRAME_CONTEXT( SYMBOL_EDIT_FRAME* aFrame ) : m_frame( aFrame ) {}
40
41 LIB_SYMBOL* GetCurSymbol() const override { return m_frame->GetCurSymbol(); }
42
43 SCH_SCREEN* GetScreen() const override { return m_frame->GetScreen(); }
44
45 LIB_ID GetLoadedLibId() const override
46 {
47 if( LIB_SYMBOL* symbol = m_frame->GetCurSymbol() )
48 return symbol->GetLibId();
49
50 return LIB_ID();
51 }
52
53 bool IsContentModified() const override { return m_frame->IsContentModified(); }
54
55 void SetContentModified( bool aModified = true ) override
56 {
57 m_frame->GetScreen()->SetContentModified( aModified );
58 }
59
60 PROJECT& Prj() const override { return m_frame->Prj(); }
61
62 TOOL_MANAGER* GetToolManager() const override { return m_frame->GetToolManager(); }
63
64 KIWAY* GetKiway() const override { return &m_frame->Kiway(); }
65
66 bool CanAcceptApiCommands() const override { return m_frame->CanAcceptApiCommands(); }
67
68 bool SaveSymbol() override
69 {
70 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
71
72 if( !symbol )
73 return false;
74
75 wxString libName = symbol->GetLibId().GetLibNickname();
76
77 if( libName.IsEmpty() )
78 return false;
79
80 LIB_SYMBOL_LIBRARY_MANAGER& libMgr = m_frame->GetLibManager();
81
82 if( libMgr.IsLibraryReadOnly( libName ) )
83 return false;
84
85 return m_frame->SaveLibraryHeadless( libName );
86 }
87
88 bool SaveSymbolCopy( const wxString& aFileName, bool aOverwrite ) override
89 {
90 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
91
92 if( !symbol )
93 return false;
94
95 return SaveSymbolCopyToFile( *symbol, aFileName, aOverwrite );
96 }
97
98 bool RevertSymbol() override
99 {
100 LIB_SYMBOL* symbol = m_frame->GetCurSymbol();
101
102 if( !symbol )
103 return false;
104
105 LIB_ID libId = symbol->GetLibId();
106
107 if( libId.GetLibNickname().empty() || libId.GetLibItemName().empty() )
108 return false;
109
110 LIB_SYMBOL_LIBRARY_MANAGER& libMgr = m_frame->GetLibManager();
111
112 if( !libMgr.SymbolExists( libId.GetLibNickname(), libId.GetLibItemName() ) )
113 return false;
114
115 m_frame->Revert( libId, /* aConfirm = */ false );
116 return true;
117 }
118
119 bool LoadSymbol( const LIB_ID& aLibId ) override
120 {
121 return m_frame->LoadSymbol( aLibId, /* aUnit */ 1, /* aBodyStyle */ 1 );
122 }
123
124private:
126};
127
128
129std::shared_ptr<SYMBOL_EDITOR_CONTEXT> CreateSymbolEditorFrameContext( SYMBOL_EDIT_FRAME* aFrame )
130{
131 return std::make_shared<SYMBOL_EDIT_FRAME_CONTEXT>( aFrame );
132}
133
134
135bool SaveSymbolCopyToFile( const LIB_SYMBOL& aSymbol, const wxString& aFileName, bool aOverwrite )
136{
137 wxFileName fn( aFileName );
138 fn.MakeAbsolute();
139
140 if( !fn.IsOk() || !fn.IsDirWritable() )
141 return false;
142
143 if( fn.GetExt().IsEmpty() )
144 fn.SetExt( wxString( FILEEXT::KiCadSymbolLibFileExtension ) );
145
146 if( fn.FileExists() && ( !fn.IsFileWritable() || !aOverwrite ) )
147 return false;
148
149 SCH_IO_MGR::SCH_FILE_T pluginType = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
150
151 if( pluginType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
152 pluginType = SCH_IO_MGR::SCH_KICAD;
153
154 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( pluginType ) );
155
156 if( !pi )
157 return false;
158
159 try
160 {
161 if( !fn.FileExists() )
162 pi->CreateLibrary( fn.GetFullPath() );
163
164 pi->SaveSymbol( fn.GetFullPath(), std::make_unique<LIB_SYMBOL>( aSymbol ) );
165 }
166 catch( const IO_ERROR& )
167 {
168 return false;
169 }
170
171 return true;
172}
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
Symbol library management helper that is specific to the symbol library editor frame.
Define a library symbol object.
Definition lib_symbol.h:114
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:190
Container for project specific data.
Definition project.h:63
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a symbol library using the file extension of aLibPath.
TOOL_MANAGER * GetToolManager() const override
bool SaveSymbolCopy(const wxString &aFileName, bool aOverwrite) override
Save a copy of the current symbol to the given library file, without user interaction.
LIB_SYMBOL * GetCurSymbol() const override
void SetContentModified(bool aModified=true) override
bool SaveSymbol() override
Save the current symbol to its library, without any user interaction.
SCH_SCREEN * GetScreen() const override
bool IsContentModified() const override
bool CanAcceptApiCommands() const override
SYMBOL_EDIT_FRAME_CONTEXT(SYMBOL_EDIT_FRAME *aFrame)
KIWAY * GetKiway() const override
LIB_ID GetLoadedLibId() const override
bool RevertSymbol() override
Reload the current symbol from its library, discarding unsaved changes.
PROJECT & Prj() const override
bool LoadSymbol(const LIB_ID &aLibId) override
Load a symbol into the editor (activating it for editing); false if not found.
The symbol library editor main window.
bool IsLibraryReadOnly(const wxString &aLibrary) const
Return true if the library is stored in a read-only file.
bool SymbolExists(const wxString &aSymbolName, const wxString &aLibrary) const
Return true if symbol with a specific alias exists in library (either original one or buffered).
Master controller class:
bool empty() const
Definition utf8.h:105
static const std::string KiCadSymbolLibFileExtension
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
see class PGM_BASE
std::shared_ptr< SYMBOL_EDITOR_CONTEXT > CreateSymbolEditorFrameContext(SYMBOL_EDIT_FRAME *aFrame)
bool SaveSymbolCopyToFile(const LIB_SYMBOL &aSymbol, const wxString &aFileName, bool aOverwrite)
Shared implementation of SaveSymbolCopy for all contexts; writes the given symbol to a new library fi...
Definition of file extensions used in Kicad.