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