KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project.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 (C) 2014-2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <wx/log.h>
25#include <wx/stdpaths.h> // required on Mac
27
28#include <pgm_base.h>
29#include <confirm.h>
30#include <core/kicad_algo.h>
32#include <fp_lib_table.h>
33#include <string_utils.h>
34#include <kiface_ids.h>
35#include <kiway.h>
36#include <macros.h>
37#include <project.h>
39#include <trace_helpers.h>
43
45 m_readOnly( false ),
46 m_textVarsTicker( 0 ),
47 m_netclassesTicker( 0 ),
48 m_projectFile( nullptr ),
49 m_localSettings( nullptr )
50{
51 m_elems.fill( nullptr );
52}
53
54
56{
57 // careful here, this should work, but the virtual destructor may not
58 // be in the same link image as PROJECT.
59 for( unsigned i = 0; i < m_elems.size(); ++i )
60 {
61 SetElem( static_cast<PROJECT::ELEM>( i ), nullptr );
62 }
63}
64
65
67{
68 ElemsClear();
69}
70
71
72bool PROJECT::TextVarResolver( wxString* aToken ) const
73{
74 if( GetTextVars().count( *aToken ) > 0 )
75 {
76 *aToken = GetTextVars().at( *aToken );
77 return true;
78 }
79
80 return false;
81}
82
83
84std::map<wxString, wxString>& PROJECT::GetTextVars() const
85{
87}
88
89
90void PROJECT::ApplyTextVars( const std::map<wxString, wxString>& aVarsMap )
91{
92 if( aVarsMap.size() == 0 )
93 return;
94
95 std::map<wxString, wxString>& existingVarsMap = GetTextVars();
96
97 for( const auto& var : aVarsMap )
98 {
99 // create or update the existing vars
100 existingVarsMap[var.first] = var.second;
101 }
102}
103
104
105void PROJECT::setProjectFullName( const wxString& aFullPathAndName )
106{
107 // Compare paths, rather than inodes, to be less surprising to the user.
108 // Create a temporary wxFileName to normalize the path
109 wxFileName candidate_path( aFullPathAndName );
110
111 // Edge transitions only. This is what clears the project
112 // data using the Clear() function.
113 if( m_project_name.GetFullPath() != candidate_path.GetFullPath() )
114 {
115 Clear(); // clear the data when the project changes.
116
117 wxLogTrace( tracePathsAndFiles, "%s: old:'%s' new:'%s'", __func__,
118 TO_UTF8( GetProjectFullName() ), TO_UTF8( aFullPathAndName ) );
119
120 m_project_name = aFullPathAndName;
121
122 wxASSERT( m_project_name.IsAbsolute() );
123
124 wxASSERT( m_project_name.GetExt() == FILEEXT::ProjectFileExtension );
125 }
126}
127
128
129const wxString PROJECT::GetProjectFullName() const
130{
131 return m_project_name.GetFullPath();
132}
133
134
135const wxString PROJECT::GetProjectPath() const
136{
137 return m_project_name.GetPathWithSep();
138}
139
140
141const wxString PROJECT::GetProjectDirectory() const
142{
143 return m_project_name.GetPath();
144}
145
146
147const wxString PROJECT::GetProjectName() const
148{
149 return m_project_name.GetName();
150}
151
152
154{
155 return m_project_name.GetName().IsEmpty();
156}
157
158
159const wxString PROJECT::SymbolLibTableName() const
160{
161 return libTableName( wxS( "sym-lib-table" ) );
162}
163
164
165const wxString PROJECT::FootprintLibTblName() const
166{
167 return libTableName( wxS( "fp-lib-table" ) );
168}
169
170
171const wxString PROJECT::DesignBlockLibTblName() const
172{
173 return libTableName( wxS( "design-block-lib-table" ) );
174}
175
176
177void PROJECT::PinLibrary( const wxString& aLibrary, enum LIB_TYPE_T aLibType )
178{
180 std::vector<wxString>* pinnedLibsCfg = nullptr;
181 std::vector<wxString>* pinnedLibsFile = nullptr;
182
183 switch( aLibType )
184 {
186 pinnedLibsFile = &m_projectFile->m_PinnedSymbolLibs;
187 pinnedLibsCfg = &cfg->m_Session.pinned_symbol_libs;
188 break;
190 pinnedLibsFile = &m_projectFile->m_PinnedFootprintLibs;
191 pinnedLibsCfg = &cfg->m_Session.pinned_fp_libs;
192 break;
194 pinnedLibsFile = &m_projectFile->m_PinnedDesignBlockLibs;
195 pinnedLibsCfg = &cfg->m_Session.pinned_design_block_libs;
196 break;
197 default:
198 wxFAIL_MSG( "Cannot pin library: invalid library type" );
199 return;
200 }
201
202 if( !alg::contains( *pinnedLibsFile, aLibrary ) )
203 pinnedLibsFile->push_back( aLibrary );
204
206
207 if( !alg::contains( *pinnedLibsCfg, aLibrary ) )
208 pinnedLibsCfg->push_back( aLibrary );
209
210 cfg->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( cfg ) );
211}
212
213
214void PROJECT::UnpinLibrary( const wxString& aLibrary, enum LIB_TYPE_T aLibType )
215{
217 std::vector<wxString>* pinnedLibsCfg = nullptr;
218 std::vector<wxString>* pinnedLibsFile = nullptr;
219
220 switch( aLibType )
221 {
223 pinnedLibsFile = &m_projectFile->m_PinnedSymbolLibs;
224 pinnedLibsCfg = &cfg->m_Session.pinned_symbol_libs;
225 break;
227 pinnedLibsFile = &m_projectFile->m_PinnedFootprintLibs;
228 pinnedLibsCfg = &cfg->m_Session.pinned_fp_libs;
229 break;
231 pinnedLibsFile = &m_projectFile->m_PinnedDesignBlockLibs;
232 pinnedLibsCfg = &cfg->m_Session.pinned_design_block_libs;
233 break;
234 default:
235 wxFAIL_MSG( "Cannot unpin library: invalid library type" );
236 return;
237 }
238
239 alg::delete_matching( *pinnedLibsFile, aLibrary );
241
242 alg::delete_matching( *pinnedLibsCfg, aLibrary );
243 cfg->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( cfg ) );
244}
245
246
247const wxString PROJECT::libTableName( const wxString& aLibTableName ) const
248{
249 wxFileName fn = GetProjectFullName();
250 wxString path = fn.GetPath();
251
252 // if there's no path to the project name, or the name as a whole is bogus or its not
253 // write-able then use a template file.
254 if( !fn.GetDirCount() || !fn.IsOk() || !wxFileName::IsDirWritable( path ) )
255 {
256 // return a template filename now.
257
258 // this next line is likely a problem now, since it relies on an
259 // application title which is no longer constant or known. This next line needs
260 // to be re-thought out.
261
262#ifdef __WXMAC__
263 fn.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
264#else
265 // don't pollute home folder, temp folder seems to be more appropriate
266 fn.AssignDir( wxStandardPaths::Get().GetTempDir() );
267#endif
268
269#if defined( __WINDOWS__ )
270 fn.AppendDir( wxT( "kicad" ) );
271#endif
272
273 /*
274 * The library table name used when no project file is passed to the appropriate
275 * code. This is used temporarily to store the project specific library table
276 * until the project file being edited is saved. It is then moved to the correct
277 * file in the folder where the project file is saved.
278 */
279 fn.SetName( wxS( "prj-" ) + aLibTableName );
280 }
281 else // normal path.
282 {
283 fn.SetName( aLibTableName );
284 }
285
286 fn.ClearExt();
287
288 return fn.GetFullPath();
289}
290
291
292const wxString PROJECT::GetSheetName( const KIID& aSheetID )
293{
294 if( m_sheetNames.empty() )
295 {
296 for( const std::pair<KIID, wxString>& pair : GetProjectFile().GetSheets() )
297 m_sheetNames[pair.first] = pair.second;
298 }
299
300 if( m_sheetNames.count( aSheetID ) )
301 return m_sheetNames.at( aSheetID );
302 else
303 return aSheetID.AsString();
304}
305
306
307void PROJECT::SetRString( RSTRING_T aIndex, const wxString& aString )
308{
309 unsigned ndx = unsigned( aIndex );
310
311 if( ndx < m_rstrings.size() )
312 m_rstrings[ndx] = aString;
313 else
314 wxASSERT( 0 ); // bad index
315}
316
317
318const wxString& PROJECT::GetRString( RSTRING_T aIndex )
319{
320 unsigned ndx = unsigned( aIndex );
321
322 if( ndx < m_rstrings.size() )
323 {
324 return m_rstrings[ndx];
325 }
326 else
327 {
328 static wxString no_cookie_for_you;
329
330 wxASSERT( 0 ); // bad index
331
332 return no_cookie_for_you;
333 }
334}
335
336
338{
339 // This is virtual, so implement it out of line
340
341 if( static_cast<unsigned>( aIndex ) < m_elems.size() )
342 return m_elems[static_cast<unsigned>( aIndex )];
343
344 return nullptr;
345}
346
347
349{
350 // This is virtual, so implement it out of line
351 if( static_cast<unsigned>( aIndex ) < m_elems.size() )
352 {
353 delete m_elems[static_cast<unsigned>(aIndex)];
354 m_elems[static_cast<unsigned>( aIndex )] = aElem;
355 }
356}
357
358
359const wxString PROJECT::AbsolutePath( const wxString& aFileName ) const
360{
361 wxFileName fn = aFileName;
362
363 // Paths which start with an unresolved variable reference are more likely to be
364 // absolute than relative.
365 if( aFileName.StartsWith( wxT( "${" ) ) )
366 return aFileName;
367
368 if( !fn.IsAbsolute() )
369 {
370 wxString pro_dir = wxPathOnly( GetProjectFullName() );
371 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS, pro_dir );
372 }
373
374 return fn.GetFullPath();
375}
376
377
379{
380 // This is a lazy loading function, it loads the project specific table when
381 // that table is asked for, not before.
382
384
385 if( tbl )
386 {
387 wxASSERT( tbl->ProjectElementType() == PROJECT::ELEM::FPTBL );
388 }
389 else
390 {
391 try
392 {
393 // Build a new project specific FP_LIB_TABLE with the global table as a fallback.
394 // ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
395 // stack this way, all using the same global fallback table.
396 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
397
399 tbl->Load( FootprintLibTblName() );
400
402 }
403 catch( const IO_ERROR& ioe )
404 {
405 DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ),
406 ioe.What() );
407 }
408 catch( ... )
409 {
410 DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ) );
411 }
412 }
413
414 return tbl;
415}
416
417
419{
420 // This is a lazy loading function, it loads the project specific table when
421 // that table is asked for, not before.
422
424
425 if( tbl )
426 {
428 }
429 else
430 {
431 try
432 {
434 tbl->Load( DesignBlockLibTblName() );
435
437 }
438 catch( const IO_ERROR& ioe )
439 {
440 DisplayErrorMessage( nullptr, _( "Error loading project design block library table." ),
441 ioe.What() );
442 }
443 catch( ... )
444 {
445 DisplayErrorMessage( nullptr,
446 _( "Error loading project design block library table." ) );
447 }
448 }
449
450 return tbl;
451}
PROJECT::ELEM ProjectElementType() override
static DESIGN_BLOCK_LIB_TABLE & GetGlobalLibTable()
PROJECT::ELEM ProjectElementType() override
Definition: fp_lib_table.h:101
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
Definition: kiid.h:49
wxString AsString() const
Definition: kiid.cpp:238
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:284
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:202
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:292
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:679
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:93
std::map< wxString, wxString > m_TextVars
Definition: project_file.h:130
std::vector< wxString > m_PinnedDesignBlockLibs
The list of pinned design block libraries.
Definition: project_file.h:128
std::vector< wxString > m_PinnedFootprintLibs
The list of pinned footprint libraries.
Definition: project_file.h:125
std::vector< wxString > m_PinnedSymbolLibs
Below are project-level settings that have not been moved to a dedicated file.
Definition: project_file.h:122
PROJECT_FILE * m_projectFile
Backing store for project data – owned by SETTINGS_MANAGER.
Definition: project.h:353
LIB_TYPE_T
Definition: project.h:189
@ SYMBOL_LIB
Definition: project.h:190
@ DESIGN_BLOCK_LIB
Definition: project.h:192
@ FOOTPRINT_LIB
Definition: project.h:191
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:129
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition: project.cpp:348
virtual const wxString DesignBlockLibTblName() const
Return the path and file name of this projects design block library table.
Definition: project.cpp:171
void PinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:177
RSTRING_T
Retain a number of project specific wxStrings, enumerated here:
Definition: project.h:214
std::map< KIID, wxString > m_sheetNames
Definition: project.h:358
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:135
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:147
virtual const wxString SymbolLibTableName() const
Return the path and file name of this projects symbol library table.
Definition: project.cpp:159
virtual _ELEM * GetElem(PROJECT::ELEM aIndex)
Get and set the elements for this project.
Definition: project.cpp:337
virtual DESIGN_BLOCK_LIB_TABLE * DesignBlockLibs()
Return the table of design block libraries.
Definition: project.cpp:418
virtual ~PROJECT()
Definition: project.cpp:66
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:378
virtual void ApplyTextVars(const std::map< wxString, wxString > &aVarsMap)
Applies the given var map, it will create or update existing vars.
Definition: project.cpp:90
virtual void ElemsClear()
Delete all the _ELEMs and set their pointers to NULL.
Definition: project.cpp:55
virtual bool TextVarResolver(wxString *aToken) const
Definition: project.cpp:72
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:200
virtual const wxString GetSheetName(const KIID &aSheetID)
Return the name of the sheet identified by the given UUID.
Definition: project.cpp:292
std::array< _ELEM *, static_cast< unsigned int >(PROJECT::ELEM::COUNT)> m_elems
Definition: project.h:364
virtual const wxString FootprintLibTblName() const
Returns the path and filename of this project's footprint library table.
Definition: project.cpp:165
virtual void SetRString(RSTRING_T aStringId, const wxString &aString)
Store a "retained string", which is any session and project specific string identified in enum RSTRIN...
Definition: project.cpp:307
wxFileName m_project_name
<fullpath>/<basename>.pro
Definition: project.h:345
void Clear()
Clear the _ELEMs and RSTRINGs.
Definition: project.h:275
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:359
void UnpinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:214
std::array< wxString, RSTRING_COUNT > m_rstrings
Definition: project.h:361
const wxString libTableName(const wxString &aLibTableName) const
Return the full path and file name of the project specific library table aLibTableName.
Definition: project.cpp:247
virtual void setProjectFullName(const wxString &aFullPathAndName)
Set the full directory, basename, and extension of the project.
Definition: project.cpp:105
virtual const wxString GetProjectDirectory() const
Return the full path of the project DIRECTORY.
Definition: project.cpp:141
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:84
PROJECT()
Definition: project.cpp:44
virtual const wxString & GetRString(RSTRING_T aStringId)
Return a "retained string", which is any session and project specific string identified in enum RSTRI...
Definition: project.cpp:318
virtual bool IsNullProject() const
Check if this project is a null project (i.e.
Definition: project.cpp:153
ELEM
The set of #_ELEMs that a PROJECT can hold.
Definition: project.h:70
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Saves a loaded project.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
#define _(s)
static const std::string ProjectFileExtension
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
@ KIFACE_NEW_FOOTPRINT_TABLE
Return a new FP_LIB_TABLE with the global table installed as a fallback.
Definition: kiface_ids.h:46
This file contains miscellaneous commonly used macros and functions.
wxString GetUserConfigPath()
Retrieves the operating system specific path for a user's configuration store.
void delete_matching(_Container &__c, _Value __value)
Covers for the horrifically named std::remove and std::remove_if (neither of which remove anything).
Definition: kicad_algo.h:165
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
SETTINGS_MANAGER * GetSettingsManager()
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:391
std::vector< wxString > pinned_design_block_libs
std::vector< wxString > pinned_fp_libs
std::vector< wxString > pinned_symbol_libs
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:151
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39