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 <string_utils.h>
34#include <kiface_ids.h>
35#include <kiway.h>
38#include <lockfile.h>
39#include <macros.h>
41#include <git2.h>
42#include <project.h>
43
45
47#include <trace_helpers.h>
51#include <title_block.h>
52#include <local_history.h>
53
54
55
57 m_readOnly( false ),
58 m_lockOverrideGranted( false ),
61 m_projectFile( nullptr ),
62 m_localSettings( nullptr )
63{
64 m_elems.fill( nullptr );
65}
66
67
69{
70 // careful here, this should work, but the virtual destructor may not
71 // be in the same link image as PROJECT.
72 for( unsigned i = 0; i < m_elems.size(); ++i )
73 {
74 SetElem( static_cast<PROJECT::ELEM>( i ), nullptr );
75 }
76}
77
78
83
84
85bool PROJECT::TextVarResolver( wxString* aToken ) const
86{
87 if( aToken->IsSameAs( wxT( "PROJECTNAME" ) ) )
88 {
89 *aToken = GetProjectName();
90 return true;
91 }
92 else if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
93 {
95 return true;
96 }
97 else if( aToken->IsSameAs( wxT( "VCSHASH" ) ) )
98 {
100 return true;
101 }
102 else if( aToken->IsSameAs( wxT( "VCSSHORTHASH" ) ) )
103 {
105 return true;
106 }
107 else if( GetTextVars().count( *aToken ) > 0 )
108 {
109 *aToken = GetTextVars().at( *aToken );
110 return true;
111 }
112
113 return false;
114}
115
116
117std::map<wxString, wxString>& PROJECT::GetTextVars() const
118{
119 return GetProjectFile().m_TextVars;
120}
121
122
123void PROJECT::ApplyTextVars( const std::map<wxString, wxString>& aVarsMap )
124{
125 if( aVarsMap.size() == 0 )
126 return;
127
128 std::map<wxString, wxString>& existingVarsMap = GetTextVars();
129
130 for( const auto& var : aVarsMap )
131 {
132 // create or update the existing vars
133 existingVarsMap[var.first] = var.second;
134 }
135}
136
137
138void PROJECT::setProjectFullName( const wxString& aFullPathAndName )
139{
140 // Compare paths, rather than inodes, to be less surprising to the user.
141 // Create a temporary wxFileName to normalize the path
142 wxFileName candidate_path( aFullPathAndName );
143
144 // Edge transitions only. This is what clears the project
145 // data using the Clear() function.
146 if( m_project_name.GetFullPath() != candidate_path.GetFullPath() )
147 {
148 Clear(); // clear the data when the project changes.
149
150 wxLogTrace( tracePathsAndFiles, "%s: old:'%s' new:'%s'", __func__,
151 TO_UTF8( GetProjectFullName() ), TO_UTF8( aFullPathAndName ) );
152
153 m_project_name = aFullPathAndName;
154
155 wxASSERT( m_project_name.IsAbsolute() );
156
157 wxASSERT( m_project_name.GetExt() == FILEEXT::ProjectFileExtension );
158 }
159}
160
161
162const wxString PROJECT::GetProjectFullName() const
163{
164 return m_project_name.GetFullPath();
165}
166
167
168const wxString PROJECT::GetProjectPath() const
169{
170 return m_project_name.GetPathWithSep();
171}
172
173
174const wxString PROJECT::GetProjectDirectory() const
175{
176 return m_project_name.GetPath();
177}
178
179
180const wxString PROJECT::GetProjectName() const
181{
182 return m_project_name.GetName();
183}
184
185
187{
188 return m_project_name.GetName().IsEmpty();
189}
190
191
196
197
202
203
208
209
210void PROJECT::PinLibrary( const wxString& aLibrary, enum LIB_TYPE_T aLibType )
211{
213 std::vector<wxString>* pinnedLibsCfg = nullptr;
214 std::vector<wxString>* pinnedLibsFile = nullptr;
215
216 switch( aLibType )
217 {
219 pinnedLibsFile = &m_projectFile->m_PinnedSymbolLibs;
220 pinnedLibsCfg = &cfg->m_Session.pinned_symbol_libs;
221 break;
223 pinnedLibsFile = &m_projectFile->m_PinnedFootprintLibs;
224 pinnedLibsCfg = &cfg->m_Session.pinned_fp_libs;
225 break;
227 pinnedLibsFile = &m_projectFile->m_PinnedDesignBlockLibs;
228 pinnedLibsCfg = &cfg->m_Session.pinned_design_block_libs;
229 break;
230 default:
231 wxFAIL_MSG( "Cannot pin library: invalid library type" );
232 return;
233 }
234
235 if( !alg::contains( *pinnedLibsFile, aLibrary ) )
236 pinnedLibsFile->push_back( aLibrary );
237
239
240 if( !alg::contains( *pinnedLibsCfg, aLibrary ) )
241 pinnedLibsCfg->push_back( aLibrary );
242
243 cfg->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( cfg ) );
244}
245
246
247void PROJECT::UnpinLibrary( const wxString& aLibrary, enum LIB_TYPE_T aLibType )
248{
250 std::vector<wxString>* pinnedLibsCfg = nullptr;
251 std::vector<wxString>* pinnedLibsFile = nullptr;
252
253 switch( aLibType )
254 {
256 pinnedLibsFile = &m_projectFile->m_PinnedSymbolLibs;
257 pinnedLibsCfg = &cfg->m_Session.pinned_symbol_libs;
258 break;
260 pinnedLibsFile = &m_projectFile->m_PinnedFootprintLibs;
261 pinnedLibsCfg = &cfg->m_Session.pinned_fp_libs;
262 break;
264 pinnedLibsFile = &m_projectFile->m_PinnedDesignBlockLibs;
265 pinnedLibsCfg = &cfg->m_Session.pinned_design_block_libs;
266 break;
267 default:
268 wxFAIL_MSG( "Cannot unpin library: invalid library type" );
269 return;
270 }
271
272 std::erase( *pinnedLibsFile, aLibrary );
274
275 std::erase( *pinnedLibsCfg, aLibrary );
276 cfg->SaveToFile( Pgm().GetSettingsManager().GetPathForSettingsFile( cfg ) );
277}
278
279
280const wxString PROJECT::libTableName( const wxString& aLibTableName ) const
281{
282 wxFileName fn = GetProjectFullName();
283 wxString path = fn.GetPath();
284
285 // if there's no path to the project name, or the name as a whole is bogus or its not
286 // write-able then use a template file.
287 if( !fn.GetDirCount() || !fn.IsOk() || !wxFileName::IsDirWritable( path ) )
288 {
289 // return a template filename now.
290
291 // this next line is likely a problem now, since it relies on an
292 // application title which is no longer constant or known. This next line needs
293 // to be re-thought out.
294
295#ifdef __WXMAC__
296 fn.AssignDir( KIPLATFORM::ENV::GetUserConfigPath() );
297#else
298 // don't pollute home folder, temp folder seems to be more appropriate
299 fn.AssignDir( wxStandardPaths::Get().GetTempDir() );
300#endif
301
302#if defined( __WINDOWS__ )
303 fn.AppendDir( wxT( "kicad" ) );
304#endif
305
306 /*
307 * The library table name used when no project file is passed to the appropriate
308 * code. This is used temporarily to store the project specific library table
309 * until the project file being edited is saved. It is then moved to the correct
310 * file in the folder where the project file is saved.
311 */
312 fn.SetName( wxS( "prj-" ) + aLibTableName );
313 }
314 else // normal path.
315 {
316 fn.SetName( aLibTableName );
317 }
318
319 fn.ClearExt();
320
321 return fn.GetFullPath();
322}
323
324
325const wxString PROJECT::GetSheetName( const KIID& aSheetID )
326{
327 if( m_sheetNames.empty() )
328 {
329 for( const std::pair<KIID, wxString>& pair : GetProjectFile().GetSheets() )
330 m_sheetNames[pair.first] = pair.second;
331 }
332
333 if( m_sheetNames.count( aSheetID ) )
334 return m_sheetNames.at( aSheetID );
335 else
336 return aSheetID.AsString();
337}
338
339
340void PROJECT::SetRString( RSTRING_T aIndex, const wxString& aString )
341{
342 unsigned ndx = unsigned( aIndex );
343
344 if( ndx < m_rstrings.size() )
345 m_rstrings[ndx] = aString;
346 else
347 wxASSERT( 0 ); // bad index
348}
349
350
351const wxString& PROJECT::GetRString( RSTRING_T aIndex )
352{
353 unsigned ndx = unsigned( aIndex );
354
355 if( ndx < m_rstrings.size() )
356 {
357 return m_rstrings[ndx];
358 }
359 else
360 {
361 static wxString no_cookie_for_you;
362
363 wxASSERT( 0 ); // bad index
364
365 return no_cookie_for_you;
366 }
367}
368
369
371{
372 // This is virtual, so implement it out of line
373
374 if( static_cast<unsigned>( aIndex ) < m_elems.size() )
375 return m_elems[static_cast<unsigned>( aIndex )];
376
377 return nullptr;
378}
379
380
382{
383 // This is virtual, so implement it out of line
384 if( static_cast<unsigned>( aIndex ) < m_elems.size() )
385 {
386 delete m_elems[static_cast<unsigned>(aIndex)];
387 m_elems[static_cast<unsigned>( aIndex )] = aElem;
388 }
389}
390
391
392const wxString PROJECT::AbsolutePath( const wxString& aFileName ) const
393{
394 wxFileName fn = aFileName;
395
396 // Paths which start with an unresolved variable reference are more likely to be
397 // absolute than relative.
398 if( aFileName.StartsWith( wxT( "${" ) ) )
399 return aFileName;
400
401 if( !fn.IsAbsolute() )
402 {
403 wxString pro_dir = wxPathOnly( GetProjectFullName() );
404 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS, pro_dir );
405 }
406
407 return fn.GetFullPath();
408}
409
410
416
417
419{
420 std::scoped_lock lock( m_designBlockLibsMutex );
421
423 std::optional<LIBRARY_MANAGER_ADAPTER*> adapter = mgr.Adapter( LIBRARY_TABLE_TYPE::DESIGN_BLOCK );
424
425 if( !adapter )
426 {
428 std::make_unique<DESIGN_BLOCK_LIBRARY_ADAPTER>( mgr ) );
429
430 std::optional<LIBRARY_MANAGER_ADAPTER*> created = mgr.Adapter( LIBRARY_TABLE_TYPE::DESIGN_BLOCK );
431 wxCHECK( created && ( *created )->Type() == LIBRARY_TABLE_TYPE::DESIGN_BLOCK, nullptr );
432 return static_cast<DESIGN_BLOCK_LIBRARY_ADAPTER*>( *created );
433 }
434
435 wxCHECK( ( *adapter )->Type() == LIBRARY_TABLE_TYPE::DESIGN_BLOCK, nullptr );
436 return static_cast<DESIGN_BLOCK_LIBRARY_ADAPTER*>( *adapter );
437}
438
439
441{
442 return m_project_lock.get();
443}
444
445
447{
448 m_project_lock.reset( aLockFile );
449}
450
451
452void PROJECT::SaveToHistory( const wxString& aProjectPath, std::vector<wxString>& aFiles )
453{
454 wxString projectFile = GetProjectFullName();
455
456 if( projectFile.IsEmpty() )
457 return;
458
459 wxFileName projectFn( projectFile );
460 wxFileName requestedFn( aProjectPath );
461 // wxPATH_NORM_ALL is now deprecated.
462 // So define a similar option
463 int norm_opt = wxPATH_NORM_ENV_VARS|wxPATH_NORM_DOTS|wxPATH_NORM_TILDE|wxPATH_NORM_ABSOLUTE
464 |wxPATH_NORM_LONG|wxPATH_NORM_SHORTCUT;
465
466 if( !projectFn.Normalize( norm_opt ) || !requestedFn.Normalize( norm_opt ) )
467 return;
468
469 if( projectFn.GetFullPath() != requestedFn.GetFullPath() )
470 return;
471
472 wxFileName historyDir( projectFn.GetPath(), wxS( ".history" ) );
473
474 if( !historyDir.DirExists() )
475 {
476 if( !historyDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
477 return;
478 }
479
480 // Save project file (.kicad_pro)
481 wxFileName historyProFile( historyDir.GetFullPath(), projectFn.GetName(),
482 projectFn.GetExt() );
483 wxCopyFile( projectFile, historyProFile.GetFullPath(), true );
484 aFiles.push_back( historyProFile.GetFullPath() );
485
486 // Save project local settings (.kicad_prl) if it exists
487 wxFileName prlFile( projectFn.GetPath(), projectFn.GetName(), FILEEXT::ProjectLocalSettingsFileExtension );
488
489 if( prlFile.FileExists() )
490 {
491 wxFileName historyPrlFile( historyDir.GetFullPath(), prlFile.GetName(),
492 prlFile.GetExt() );
493 wxCopyFile( prlFile.GetFullPath(), historyPrlFile.GetFullPath(), true );
494 aFiles.push_back( historyPrlFile.GetFullPath() );
495 }
496}
An interface to the global shared library manager that is schematic-specific and linked to one projec...
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
static wxString GetCurrentHash(const wxString &aProjectFile, bool aShort)
Return the current HEAD commit hash for the repository containing aProjectFile.
Definition kiid.h:49
wxString AsString() const
Definition kiid.cpp:247
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:294
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition kiway.cpp:206
@ FACE_PCB
pcbnew DSO
Definition kiway.h:302
std::optional< LIBRARY_MANAGER_ADAPTER * > Adapter(LIBRARY_TABLE_TYPE aType) const
void RegisterAdapter(LIBRARY_TABLE_TYPE aType, std::unique_ptr< LIBRARY_MANAGER_ADAPTER > &&aAdapter)
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:535
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:132
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:134
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition project.h:92
std::map< wxString, wxString > m_TextVars
PROJECT_FILE * m_projectFile
Backing store for project data – owned by SETTINGS_MANAGER.
Definition project.h:371
void SetProjectLock(LOCKFILE *aLockFile)
Definition project.cpp:446
bool m_lockOverrideGranted
User granted override at project level.
Definition project.h:366
LOCKFILE * GetProjectLock() const
Definition project.cpp:440
@ SYMBOL_LIB
Definition project.h:193
@ DESIGN_BLOCK_LIB
Definition project.h:195
@ FOOTPRINT_LIB
Definition project.h:194
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition project.cpp:162
virtual void SetElem(PROJECT::ELEM aIndex, _ELEM *aElem)
Definition project.cpp:381
virtual const wxString DesignBlockLibTblName() const
Return the path and file name of this projects design block library table.
Definition project.cpp:204
void PinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition project.cpp:210
RSTRING_T
Retain a number of project specific wxStrings, enumerated here:
Definition project.h:217
std::map< KIID, wxString > m_sheetNames
Definition project.h:376
std::unique_ptr< LOCKFILE > m_project_lock
Lock.
Definition project.h:385
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:168
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition project.cpp:180
int m_netclassesTicker
Update counter on netclasses.
Definition project.h:368
virtual const wxString SymbolLibTableName() const
Return the path and file name of this projects symbol library table.
Definition project.cpp:192
virtual _ELEM * GetElem(PROJECT::ELEM aIndex)
Get and set the elements for this project.
Definition project.cpp:370
virtual ~PROJECT()
Definition project.cpp:79
void SaveToHistory(const wxString &aProjectPath, std::vector< wxString > &aFiles)
Save project files (.kicad_pro and .kicad_prl) to the .history directory.
Definition project.cpp:452
virtual void ApplyTextVars(const std::map< wxString, wxString > &aVarsMap)
Applies the given var map, it will create or update existing vars.
Definition project.cpp:123
virtual DESIGN_BLOCK_LIBRARY_ADAPTER * DesignBlockLibs()
Return the table of design block libraries.
Definition project.cpp:418
virtual bool TextVarResolver(wxString *aToken) const
Definition project.cpp:85
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:203
virtual const wxString GetSheetName(const KIID &aSheetID)
Return the name of the sheet identified by the given UUID.
Definition project.cpp:325
std::mutex m_designBlockLibsMutex
Synchronise access to DesignBlockLibs()
Definition project.h:388
std::array< _ELEM *, static_cast< unsigned int >(PROJECT::ELEM::COUNT)> m_elems
Definition project.h:382
virtual void elemsClear()
Delete all the _ELEMs and set their pointers to NULL.
Definition project.cpp:68
virtual const wxString FootprintLibTblName() const
Returns the path and filename of this project's footprint library table.
Definition project.cpp:198
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:340
wxFileName m_project_name
<fullpath>/<basename>.pro
Definition project.h:363
void Clear()
Clear the _ELEMs and RSTRINGs.
Definition project.h:274
virtual FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(KIWAY &aKiway)
Fetches the footprint library adapter from the PCB editor instance.
Definition project.cpp:411
PROJECT_LOCAL_SETTINGS * m_localSettings
Backing store for project local settings – owned by SETTINGS_MANAGER.
Definition project.h:374
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:392
int m_textVarsTicker
Update counter on text vars.
Definition project.h:367
void UnpinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition project.cpp:247
std::array< wxString, RSTRING_COUNT > m_rstrings
Definition project.h:379
const wxString libTableName(const wxString &aLibTableName) const
Return the full path and file name of the project specific library table aLibTableName.
Definition project.cpp:280
virtual void setProjectFullName(const wxString &aFullPathAndName)
Set the full directory, basename, and extension of the project.
Definition project.cpp:138
bool m_readOnly
No project files will be written to disk.
Definition project.h:365
virtual const wxString GetProjectDirectory() const
Return the full path of the project DIRECTORY.
Definition project.cpp:174
virtual std::map< wxString, wxString > & GetTextVars() const
Definition project.cpp:117
PROJECT()
Definition project.cpp:56
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:351
virtual bool IsNullProject() const
Check if this project is a null project (i.e.
Definition project.cpp:186
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()
This file is part of the common library.
static const std::string SymbolLibraryTableFileName
static const std::string ProjectFileExtension
static const std::string ProjectLocalSettingsFileExtension
static const std::string DesignBlockLibraryTableFileName
static const std::string FootprintLibraryTableFileName
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
@ KIFACE_FOOTPRINT_LIBRARY_ADAPTER
Definition kiface_ids.h:34
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.
see class PGM_BASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
std::vector< wxString > pinned_design_block_libs
std::vector< wxString > pinned_fp_libs
std::vector< wxString > pinned_symbol_libs
Implement a participant in the KIWAY alchemy.
Definition kiway.h:155
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
std::string path
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