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 The 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 <algorithm>
29#include <pgm_base.h>
30#include <confirm.h>
31#include <core/kicad_algo.h>
33#include <fp_lib_table.h>
34#include <string_utils.h>
35#include <kiface_ids.h>
36#include <kiway.h>
37#include <lockfile.h>
38#include <macros.h>
39#include <project.h>
41#include <trace_helpers.h>
45#include <title_block.h>
46
47
49 m_readOnly( false ),
50 m_textVarsTicker( 0 ),
51 m_netclassesTicker( 0 ),
52 m_projectFile( nullptr ),
53 m_localSettings( nullptr )
54{
55 m_elems.fill( nullptr );
56}
57
58
60{
61 // careful here, this should work, but the virtual destructor may not
62 // be in the same link image as PROJECT.
63 for( unsigned i = 0; i < m_elems.size(); ++i )
64 {
65 SetElem( static_cast<PROJECT::ELEM>( i ), nullptr );
66 }
67}
68
69
71{
72 elemsClear();
73}
74
75
76bool PROJECT::TextVarResolver( wxString* aToken ) const
77{
78 if( aToken->IsSameAs( wxT( "PROJECTNAME" ) ) )
79 {
80 *aToken = GetProjectName();
81 return true;
82 }
83 else if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
84 {
86 return true;
87 }
88 else if( GetTextVars().count( *aToken ) > 0 )
89 {
90 *aToken = GetTextVars().at( *aToken );
91 return true;
92 }
93
94 return false;
95}
96
97
98std::map<wxString, wxString>& PROJECT::GetTextVars() const
99{
100 return GetProjectFile().m_TextVars;
101}
102
103
104void PROJECT::ApplyTextVars( const std::map<wxString, wxString>& aVarsMap )
105{
106 if( aVarsMap.size() == 0 )
107 return;
108
109 std::map<wxString, wxString>& existingVarsMap = GetTextVars();
110
111 for( const auto& var : aVarsMap )
112 {
113 // create or update the existing vars
114 existingVarsMap[var.first] = var.second;
115 }
116}
117
118
119void PROJECT::setProjectFullName( const wxString& aFullPathAndName )
120{
121 // Compare paths, rather than inodes, to be less surprising to the user.
122 // Create a temporary wxFileName to normalize the path
123 wxFileName candidate_path( aFullPathAndName );
124
125 // Edge transitions only. This is what clears the project
126 // data using the Clear() function.
127 if( m_project_name.GetFullPath() != candidate_path.GetFullPath() )
128 {
129 Clear(); // clear the data when the project changes.
130
131 wxLogTrace( tracePathsAndFiles, "%s: old:'%s' new:'%s'", __func__,
132 TO_UTF8( GetProjectFullName() ), TO_UTF8( aFullPathAndName ) );
133
134 m_project_name = aFullPathAndName;
135
136 wxASSERT( m_project_name.IsAbsolute() );
137
138 wxASSERT( m_project_name.GetExt() == FILEEXT::ProjectFileExtension );
139 }
140}
141
142
143const wxString PROJECT::GetProjectFullName() const
144{
145 return m_project_name.GetFullPath();
146}
147
148
149const wxString PROJECT::GetProjectPath() const
150{
151 return m_project_name.GetPathWithSep();
152}
153
154
155const wxString PROJECT::GetProjectDirectory() const
156{
157 return m_project_name.GetPath();
158}
159
160
161const wxString PROJECT::GetProjectName() const
162{
163 return m_project_name.GetName();
164}
165
166
168{
169 return m_project_name.GetName().IsEmpty();
170}
171
172
173const wxString PROJECT::SymbolLibTableName() const
174{
176}
177
178
179const wxString PROJECT::FootprintLibTblName() const
180{
182}
183
184
185const wxString PROJECT::DesignBlockLibTblName() const
186{
188}
189
190
191void PROJECT::PinLibrary( const wxString& aLibrary, enum LIB_TYPE_T aLibType )
192{
194 std::vector<wxString>* pinnedLibsCfg = nullptr;
195 std::vector<wxString>* pinnedLibsFile = nullptr;
196
197 switch( aLibType )
198 {
200 pinnedLibsFile = &m_projectFile->m_PinnedSymbolLibs;
201 pinnedLibsCfg = &cfg->m_Session.pinned_symbol_libs;
202 break;
204 pinnedLibsFile = &m_projectFile->m_PinnedFootprintLibs;
205 pinnedLibsCfg = &cfg->m_Session.pinned_fp_libs;
206 break;
208 pinnedLibsFile = &m_projectFile->m_PinnedDesignBlockLibs;
209 pinnedLibsCfg = &cfg->m_Session.pinned_design_block_libs;
210 break;
211 default:
212 wxFAIL_MSG( "Cannot pin library: invalid library type" );
213 return;
214 }
215
216 if( !alg::contains( *pinnedLibsFile, aLibrary ) )
217 pinnedLibsFile->push_back( aLibrary );
218
220
221 if( !alg::contains( *pinnedLibsCfg, aLibrary ) )
222 pinnedLibsCfg->push_back( aLibrary );
223
224 cfg->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( cfg ) );
225}
226
227
228void PROJECT::UnpinLibrary( const wxString& aLibrary, enum LIB_TYPE_T aLibType )
229{
231 std::vector<wxString>* pinnedLibsCfg = nullptr;
232 std::vector<wxString>* pinnedLibsFile = nullptr;
233
234 switch( aLibType )
235 {
237 pinnedLibsFile = &m_projectFile->m_PinnedSymbolLibs;
238 pinnedLibsCfg = &cfg->m_Session.pinned_symbol_libs;
239 break;
241 pinnedLibsFile = &m_projectFile->m_PinnedFootprintLibs;
242 pinnedLibsCfg = &cfg->m_Session.pinned_fp_libs;
243 break;
245 pinnedLibsFile = &m_projectFile->m_PinnedDesignBlockLibs;
246 pinnedLibsCfg = &cfg->m_Session.pinned_design_block_libs;
247 break;
248 default:
249 wxFAIL_MSG( "Cannot unpin library: invalid library type" );
250 return;
251 }
252
253 std::erase( *pinnedLibsFile, aLibrary );
255
256 std::erase( *pinnedLibsCfg, aLibrary );
257 cfg->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( cfg ) );
258}
259
260
261const wxString PROJECT::libTableName( const wxString& aLibTableName ) const
262{
263 wxFileName fn = GetProjectFullName();
264 wxString path = fn.GetPath();
265
266 // if there's no path to the project name, or the name as a whole is bogus or its not
267 // write-able then use a template file.
268 if( !fn.GetDirCount() || !fn.IsOk() || !wxFileName::IsDirWritable( path ) )
269 {
270 // return a template filename now.
271
272 // this next line is likely a problem now, since it relies on an
273 // application title which is no longer constant or known. This next line needs
274 // to be re-thought out.
275
276#ifdef __WXMAC__
277 fn.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
278#else
279 // don't pollute home folder, temp folder seems to be more appropriate
280 fn.AssignDir( wxStandardPaths::Get().GetTempDir() );
281#endif
282
283#if defined( __WINDOWS__ )
284 fn.AppendDir( wxT( "kicad" ) );
285#endif
286
287 /*
288 * The library table name used when no project file is passed to the appropriate
289 * code. This is used temporarily to store the project specific library table
290 * until the project file being edited is saved. It is then moved to the correct
291 * file in the folder where the project file is saved.
292 */
293 fn.SetName( wxS( "prj-" ) + aLibTableName );
294 }
295 else // normal path.
296 {
297 fn.SetName( aLibTableName );
298 }
299
300 fn.ClearExt();
301
302 return fn.GetFullPath();
303}
304
305
306const wxString PROJECT::GetSheetName( const KIID& aSheetID )
307{
308 if( m_sheetNames.empty() )
309 {
310 for( const std::pair<KIID, wxString>& pair : GetProjectFile().GetSheets() )
311 m_sheetNames[pair.first] = pair.second;
312 }
313
314 if( m_sheetNames.count( aSheetID ) )
315 return m_sheetNames.at( aSheetID );
316 else
317 return aSheetID.AsString();
318}
319
320
321void PROJECT::SetRString( RSTRING_T aIndex, const wxString& aString )
322{
323 unsigned ndx = unsigned( aIndex );
324
325 if( ndx < m_rstrings.size() )
326 m_rstrings[ndx] = aString;
327 else
328 wxASSERT( 0 ); // bad index
329}
330
331
332const wxString& PROJECT::GetRString( RSTRING_T aIndex )
333{
334 unsigned ndx = unsigned( aIndex );
335
336 if( ndx < m_rstrings.size() )
337 {
338 return m_rstrings[ndx];
339 }
340 else
341 {
342 static wxString no_cookie_for_you;
343
344 wxASSERT( 0 ); // bad index
345
346 return no_cookie_for_you;
347 }
348}
349
350
352{
353 // This is virtual, so implement it out of line
354
355 if( static_cast<unsigned>( aIndex ) < m_elems.size() )
356 return m_elems[static_cast<unsigned>( aIndex )];
357
358 return nullptr;
359}
360
361
363{
364 // This is virtual, so implement it out of line
365 if( static_cast<unsigned>( aIndex ) < m_elems.size() )
366 {
367 delete m_elems[static_cast<unsigned>(aIndex)];
368 m_elems[static_cast<unsigned>( aIndex )] = aElem;
369 }
370}
371
372
373const wxString PROJECT::AbsolutePath( const wxString& aFileName ) const
374{
375 wxFileName fn = aFileName;
376
377 // Paths which start with an unresolved variable reference are more likely to be
378 // absolute than relative.
379 if( aFileName.StartsWith( wxT( "${" ) ) )
380 return aFileName;
381
382 if( !fn.IsAbsolute() )
383 {
384 wxString pro_dir = wxPathOnly( GetProjectFullName() );
385 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS, pro_dir );
386 }
387
388 return fn.GetFullPath();
389}
390
391
393{
394 // This is a lazy loading function, it loads the project specific table when
395 // that table is asked for, not before.
396
398
399 if( tbl )
400 {
401 wxASSERT( tbl->ProjectElementType() == PROJECT::ELEM::FPTBL );
402 }
403 else
404 {
405 try
406 {
407 // Build a new project specific FP_LIB_TABLE with the global table as a fallback.
408 // ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
409 // stack this way, all using the same global fallback table.
410 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
411
413 tbl->Load( FootprintLibTblName() );
414
416 }
417 catch( const IO_ERROR& ioe )
418 {
419 DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ),
420 ioe.What() );
421 }
422 catch( ... )
423 {
424 DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ) );
425 }
426 }
427
428 return tbl;
429}
430
431
433{
434 // This is a lazy loading function, it loads the project specific table when
435 // that table is asked for, not before.
436
438
439 if( tbl )
440 {
442 }
443 else
444 {
445 try
446 {
448 tbl->Load( DesignBlockLibTblName() );
449
451 }
452 catch( const IO_ERROR& ioe )
453 {
454 DisplayErrorMessage( nullptr, _( "Error loading project design block library table." ),
455 ioe.What() );
456 }
457 catch( ... )
458 {
459 DisplayErrorMessage( nullptr,
460 _( "Error loading project design block library table." ) );
461 }
462 }
463
464 return tbl;
465}
466
467
469{
470 m_project_lock.reset( aLockFile );
471}
472
473
475{
476 return m_project_lock.get();
477}
PROJECT::ELEM ProjectElementType() override
static DESIGN_BLOCK_LIB_TABLE & GetGlobalLibTable()
PROJECT::ELEM ProjectElementType() override
Definition: fp_lib_table.h:103
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:246
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:286
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:198
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:294
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:565
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:97
std::map< wxString, wxString > m_TextVars
Definition: project_file.h:141
std::vector< wxString > m_PinnedDesignBlockLibs
The list of pinned design block libraries.
Definition: project_file.h:139
std::vector< wxString > m_PinnedFootprintLibs
The list of pinned footprint libraries.
Definition: project_file.h:136
std::vector< wxString > m_PinnedSymbolLibs
Below are project-level settings that have not been moved to a dedicated file.
Definition: project_file.h:133
PROJECT_FILE * m_projectFile
Backing store for project data – owned by SETTINGS_MANAGER.
Definition: project.h:362
void SetProjectLock(LOCKFILE *aLockFile)
Definition: project.cpp:468
LOCKFILE * GetProjectLock() const
Definition: project.cpp:474
LIB_TYPE_T
Definition: project.h:193
@ SYMBOL_LIB
Definition: project.h:194
@ DESIGN_BLOCK_LIB
Definition: project.h:196
@ FOOTPRINT_LIB
Definition: project.h:195
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:143
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition: project.cpp:362
virtual const wxString DesignBlockLibTblName() const
Return the path and file name of this projects design block library table.
Definition: project.cpp:185
void PinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:191
RSTRING_T
Retain a number of project specific wxStrings, enumerated here:
Definition: project.h:218
std::map< KIID, wxString > m_sheetNames
Definition: project.h:367
std::unique_ptr< LOCKFILE > m_project_lock
Lock.
Definition: project.h:376
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:149
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:161
virtual const wxString SymbolLibTableName() const
Return the path and file name of this projects symbol library table.
Definition: project.cpp:173
virtual _ELEM * GetElem(PROJECT::ELEM aIndex)
Get and set the elements for this project.
Definition: project.cpp:351
virtual DESIGN_BLOCK_LIB_TABLE * DesignBlockLibs()
Return the table of design block libraries.
Definition: project.cpp:432
virtual ~PROJECT()
Definition: project.cpp:70
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:392
virtual void ApplyTextVars(const std::map< wxString, wxString > &aVarsMap)
Applies the given var map, it will create or update existing vars.
Definition: project.cpp:104
virtual bool TextVarResolver(wxString *aToken) const
Definition: project.cpp:76
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:204
virtual const wxString GetSheetName(const KIID &aSheetID)
Return the name of the sheet identified by the given UUID.
Definition: project.cpp:306
std::array< _ELEM *, static_cast< unsigned int >(PROJECT::ELEM::COUNT)> m_elems
Definition: project.h:373
virtual void elemsClear()
Delete all the _ELEMs and set their pointers to NULL.
Definition: project.cpp:59
virtual const wxString FootprintLibTblName() const
Returns the path and filename of this project's footprint library table.
Definition: project.cpp:179
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:321
wxFileName m_project_name
<fullpath>/<basename>.pro
Definition: project.h:355
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:373
void UnpinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:228
std::array< wxString, RSTRING_COUNT > m_rstrings
Definition: project.h:370
const wxString libTableName(const wxString &aLibTableName) const
Return the full path and file name of the project specific library table aLibTableName.
Definition: project.cpp:261
virtual void setProjectFullName(const wxString &aFullPathAndName)
Set the full directory, basename, and extension of the project.
Definition: project.cpp:119
virtual const wxString GetProjectDirectory() const
Return the full path of the project DIRECTORY.
Definition: project.cpp:155
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:98
PROJECT()
Definition: project.cpp:48
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:332
virtual bool IsNullProject() const
Check if this project is a null project (i.e.
Definition: project.cpp:167
ELEM
The set of #_ELEMs that a PROJECT can hold.
Definition: project.h:71
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Save a loaded project.
static wxString GetCurrentDate()
Definition: title_block.cpp:97
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
This file is part of the common library.
#define _(s)
static const std::string SymbolLibraryTableFileName
static const std::string ProjectFileExtension
static const std::string DesignBlockLibraryTableFileName
static const std::string FootprintLibraryTableFileName
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
File locking utilities.
This file contains miscellaneous commonly used macros and functions.
wxString GetUserConfigPath()
Retrieves the operating system specific path for a user's configuration store.
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:902
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:429
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:153
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