KiCad PCB EDA Suite
Loading...
Searching...
No Matches
headless_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 <kiface_base.h>
23#include <lib_id.h>
24#include <lib_symbol.h>
26#include <project.h>
27#include <project_sch.h>
28#include <sch_screen.h>
29#include <tool/tool_manager.h>
30
31#include <wx/debug.h>
32
34 const LIB_ID& aLibId, PROJECT* aProject,
35 APP_SETTINGS_BASE* aSettings,
36 KIWAY* aKiway ) :
37 m_symbol( std::move( aSymbol ) ),
38 m_screen( std::make_unique<SCH_SCREEN>() ),
39 m_libId( aLibId ),
40 m_project( aProject ),
41 m_kiway( aKiway ),
42 m_toolManager( std::make_unique<TOOL_MANAGER>() )
43{
44 wxCHECK( m_symbol, /* void */ );
45 wxCHECK( m_project, /* void */ );
46
47 if( m_symbol )
48 m_symbol->SetLibId( m_libId );
49
50 m_toolManager->SetEnvironment( m_screen.get(), nullptr, nullptr, aSettings, nullptr );
51}
52
53
55
56
58{
59 wxASSERT( m_project );
60 return *m_project;
61}
62
63
68
69
71{
72 wxCHECK( m_symbol && m_project, false );
73
74 if( m_libId.GetLibNickname().empty() )
75 return false;
76
78
79 if( !adapter->IsSymbolLibWritable( m_libId.GetUniStringLibNickname() ) )
80 return false;
81
82 try
83 {
84 std::unique_ptr<LIB_SYMBOL> copy = std::make_unique<LIB_SYMBOL>( *m_symbol );
85 copy->SetLibId( m_libId );
86
87 if( adapter->SaveSymbol( m_libId.GetUniStringLibNickname(), std::move( copy ) )
89 {
90 return false;
91 }
92 }
93 catch( const IO_ERROR& )
94 {
95 return false;
96 }
97
98 m_contentModified = false;
99 return true;
100}
101
102
103bool HEADLESS_SYMBOL_EDITOR_CONTEXT::SaveSymbolCopy( const wxString& aFileName, bool aOverwrite )
104{
105 wxCHECK( m_symbol, false );
106
107 return SaveSymbolCopyToFile( *m_symbol, aFileName, aOverwrite );
108}
109
110
112{
113 wxCHECK( m_symbol && m_project, false );
114
115 if( m_libId.GetLibNickname().empty() || m_libId.GetLibItemName().empty() )
116 return false;
117
119
120 LIB_SYMBOL* loaded = adapter->LoadSymbol( m_libId );
121
122 if( !loaded )
123 return false;
124
125 m_symbol = std::make_unique<LIB_SYMBOL>( *loaded );
126 m_contentModified = false;
127
128 return true;
129}
130
131
133{
134 wxCHECK( m_project, false );
135
137
138 LIB_SYMBOL* loaded = adapter->LoadSymbol( aLibId );
139
140 if( !loaded )
141 return false;
142
143 m_symbol = std::make_unique<LIB_SYMBOL>( *loaded );
144 m_libId = aLibId;
145 m_contentModified = false;
146
147 return true;
148}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
HEADLESS_SYMBOL_EDITOR_CONTEXT(std::unique_ptr< LIB_SYMBOL > aSymbol, const LIB_ID &aLibId, PROJECT *aProject, APP_SETTINGS_BASE *aSettings, KIWAY *aKiway=nullptr)
std::unique_ptr< TOOL_MANAGER > m_toolManager
bool SaveSymbol() override
Save the current symbol to its library, without any user interaction.
bool LoadSymbol(const LIB_ID &aLibId) override
Load a symbol into the editor (activating it for editing); false if not found.
~HEADLESS_SYMBOL_EDITOR_CONTEXT() override
bool SaveSymbolCopy(const wxString &aFileName, bool aOverwrite) override
Save a copy of the current symbol to the given library file, without user interaction.
TOOL_MANAGER * GetToolManager() const override
bool RevertSymbol() override
Reload the current symbol from its library, discarding unsaved changes.
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
Define a library symbol object.
Definition lib_symbol.h:114
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
Container for project specific data.
Definition project.h:63
An interface to the global shared library manager that is schematic-specific and linked to one projec...
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
SAVE_T SaveSymbol(const wxString &aNickname, std::unique_ptr< LIB_SYMBOL > aSymbol, bool aOverwrite=true)
Write aSymbol to an existing library given by aNickname.
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
Master controller class:
STL namespace.
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...