KiCad PCB EDA Suite
PROJECT_TEMPLATE Class Reference

A class which provides project template functionality. More...

#include <project_template.h>

Public Member Functions

 PROJECT_TEMPLATE (const wxString &aPath)
 Create a new project instance from aPath. More...
 
 ~PROJECT_TEMPLATE ()
 Non-virtual destructor (so no derived classes) More...
 
wxString GetPrjDirName ()
 Get the dir name of the project template (i.e. More...
 
wxFileName GetHtmlFile ()
 Get the full Html filename for the project template. More...
 
bool CreateProject (wxFileName &aNewProjectPath, wxString *aErrorMsg=nullptr)
 Copies and renames all template files to create a new project. More...
 
wxBitmap * GetIcon ()
 Get the 64px^2 icon for the project template. More...
 
wxString * GetTitle ()
 Get the title of the project (extracted from the html title tag) More...
 
std::vector< wxFileName > GetFileList ()
 Get a vector list of filenames for the template. More...
 
size_t GetDestinationFiles (const wxFileName &aNewProjectPath, std::vector< wxFileName > &aDestFiles)
 Fetch the list of destination files to be copied when the new project is created. More...
 

Protected Attributes

wxFileName m_basePath
 
wxFileName m_metaPath
 
wxFileName m_metaHtmlFile
 
wxFileName m_metaIconFile
 
wxBitmap * m_metaIcon
 
wxString m_title
 

Detailed Description

A class which provides project template functionality.

Definition at line 143 of file project_template.h.

Constructor & Destructor Documentation

◆ PROJECT_TEMPLATE()

PROJECT_TEMPLATE::PROJECT_TEMPLATE ( const wxString &  aPath)

Create a new project instance from aPath.

aPath should be a directory that conforms to the project template requirements

Parameters
aPathShould be a directory containing the template

Definition at line 39 of file project_template.cpp.

40{
41 m_basePath = wxFileName::DirName( aPath );
42 m_metaPath = wxFileName::DirName( aPath + SEP + METADIR );
43 m_metaHtmlFile = wxFileName::FileName( aPath + SEP + METADIR + SEP + METAFILE_INFO_HTML );
44 m_metaIconFile = wxFileName::FileName( aPath + SEP + METADIR + SEP + METAFILE_ICON );
45
46 m_title = wxEmptyString;
47
48 // Test the project template requirements to make sure aPath is a valid template structure.
49 if( !wxFileName::DirExists( m_basePath.GetPath() ) )
50 {
51 // Error, the path doesn't exist!
52 m_title = _( "Could open the template path!" ) + wxS( " " ) + aPath;
53 }
54 else if( !wxFileName::DirExists( m_metaPath.GetPath() ) )
55 {
56 // Error, the meta information directory doesn't exist!
57 m_title = _( "Couldn't open the meta information directory for this template!" ) + wxS( " " ) +
58 m_metaPath.GetPath();
59 }
60 else if( !wxFileName::FileExists( m_metaHtmlFile.GetFullPath() ) )
61 {
62 // Error, the meta information directory doesn't contain the informational html file!
63 m_title = _( "Couldn't find the meta HTML information file for this template!" );
64 }
65
66 // Try to load an icon
67 if( !wxFileName::FileExists( m_metaIconFile.GetFullPath() ) )
68 m_metaIcon = &wxNullBitmap;
69 else
70 m_metaIcon = new wxBitmap( m_metaIconFile.GetFullPath(), wxBITMAP_TYPE_PNG );
71}
wxFileName m_metaHtmlFile
wxFileName m_metaIconFile
#define _(s)
#define SEP
#define METAFILE_ICON
An optional png icon, exactly 64px x 64px which is used in the template selector if present.
#define METADIR
A directory which contains information about the project template and does not get copied.
#define METAFILE_INFO_HTML
A required html formatted file which contains information about the project template.

References _, m_basePath, m_metaHtmlFile, m_metaIcon, m_metaIconFile, m_metaPath, m_title, METADIR, METAFILE_ICON, METAFILE_INFO_HTML, and SEP.

◆ ~PROJECT_TEMPLATE()

PROJECT_TEMPLATE::~PROJECT_TEMPLATE ( )

Non-virtual destructor (so no derived classes)

Definition at line 121 of file project_template.cpp.

122{
123
124}

Member Function Documentation

◆ CreateProject()

bool PROJECT_TEMPLATE::CreateProject ( wxFileName &  aNewProjectPath,
wxString *  aErrorMsg = nullptr 
)

Copies and renames all template files to create a new project.

Parameters
aNewProjectPathThe full path of the new project file to create
aErrorMsgis an optional string to place project creation error messages.
Returns
true if the project creation was successful otherwise false.

Definition at line 184 of file project_template.cpp.

185{
186 // CreateProject copy the files from template to the new project folder and renames files
187 // which have the same name as the template .kicad_pro file
188 bool result = true;
189
190 std::vector<wxFileName> srcFiles = GetFileList();
191
192 // Find the template file name base. this is the name of the .kicad_pro (or .pro) template
193 // file
194 wxString basename;
195 bool multipleProjectFilesFound = false;
196
197 for( wxFileName& file : srcFiles )
198 {
199 if( file.GetExt() == ProjectFileExtension || file.GetExt() == LegacyProjectFileExtension )
200 {
201 if( !basename.IsEmpty() && basename != file.GetName() )
202 multipleProjectFilesFound = true;
203
204 basename = file.GetName();
205 }
206 }
207
208 if( multipleProjectFilesFound )
209 basename = GetPrjDirName();
210
211 for( wxFileName& srcFile : srcFiles )
212 {
213 // Replace the template path
214 wxFileName destFile = srcFile;
215
216 // Replace the template filename with the project filename for the new project creation
217 wxString currname = destFile.GetName();
218
219 if( destFile.GetExt() == DrawingSheetFileExtension )
220 {
221 // Don't rename drawing sheet definitions; they're often shared
222 }
223 else if( destFile.GetName().EndsWith( "-cache" )
224 || destFile.GetName().EndsWith( "-rescue" ) )
225 {
226 currname.Replace( basename, aNewProjectPath.GetName() );
227 }
228 else if( destFile.GetExt() == "dcm"
229 || destFile.GetExt() == "lib"
230 // Footprint libraries are directories not files, so GetExt() won't work
231 || destFile.GetPath().EndsWith( ".pretty" ) )
232 {
233 // Don't rename project-specific libraries. This will break the library tables and
234 // cause broken links in the schematic/pcb.
235 }
236 else
237 {
238 currname.Replace( basename, aNewProjectPath.GetName() );
239 }
240
241 destFile.SetName( currname );
242
243 // Replace the template path with the project path for the new project creation
244 // but keep the sub directory name, if exists
245 wxString destpath = destFile.GetPathWithSep();
246 destpath.Replace( m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
247
248 // Check to see if the path already exists, if not attempt to create it here.
249 if( !wxFileName::DirExists( destpath ) )
250 {
251 if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
252 {
253 if( aErrorMsg )
254 {
255 if( !aErrorMsg->empty() )
256 *aErrorMsg += "\n";
257
258 wxString msg;
259
260 msg.Printf( _( "Cannot create folder '%s'." ), destpath );
261 *aErrorMsg += msg;
262 }
263
264 continue;
265 }
266 }
267
268 destFile.SetPath( destpath );
269
270 if( srcFile.FileExists() && !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
271 {
272 if( aErrorMsg )
273 {
274 if( !aErrorMsg->empty() )
275 *aErrorMsg += "\n";
276
277 wxString msg;
278
279 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
280 *aErrorMsg += msg;
281 }
282
283 result = false;
284 }
285 }
286
287 return result;
288}
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
wxString GetPrjDirName()
Get the dir name of the project template (i.e.
const std::string LegacyProjectFileExtension
const std::string ProjectFileExtension
const std::string DrawingSheetFileExtension

References _, DrawingSheetFileExtension, GetFileList(), GetPrjDirName(), LegacyProjectFileExtension, m_basePath, and ProjectFileExtension.

Referenced by KICAD_MANAGER_CONTROL::NewFromTemplate().

◆ GetDestinationFiles()

size_t PROJECT_TEMPLATE::GetDestinationFiles ( const wxFileName &  aNewProjectPath,
std::vector< wxFileName > &  aDestFiles 
)

Fetch the list of destination files to be copied when the new project is created.

Parameters
aNewProjectPathis the path to the project to be created.
aDestFilesis a container to place the list of destination files to be created.
Returns
the number of destination files added to the container.

Definition at line 139 of file project_template.cpp.

141{
142 std::vector< wxFileName > srcFiles = GetFileList();
143
144 // Find the template file name base. this is the name of the .pro template file
145 wxString basename;
146 bool multipleProjectFilesFound = false;
147
148 for( wxFileName& file : srcFiles )
149 {
150 if( file.GetExt() == ProjectFileExtension || file.GetExt() == LegacyProjectFileExtension )
151 {
152 if( !basename.IsEmpty() && basename != file.GetName() )
153 multipleProjectFilesFound = true;
154
155 basename = file.GetName();
156 }
157 }
158
159 if( multipleProjectFilesFound )
160 basename = GetPrjDirName();
161
162 for( wxFileName& srcFile : srcFiles )
163 {
164 // Replace the template path
165 wxFileName destFile = srcFile;
166
167 // Replace the template filename with the project filename for the new project creation
168 wxString name = destFile.GetName();
169 name.Replace( basename, aNewProjectPath.GetName() );
170 destFile.SetName( name );
171
172 // Replace the template path with the project path.
173 wxString path = destFile.GetPathWithSep();
174 path.Replace( m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
175 destFile.SetPath( path );
176
177 aDestFiles.push_back( destFile );
178 }
179
180 return aDestFiles.size();
181}
const char * name
Definition: DXF_plotter.cpp:56

References GetFileList(), GetPrjDirName(), LegacyProjectFileExtension, m_basePath, name, path, and ProjectFileExtension.

Referenced by KICAD_MANAGER_CONTROL::NewFromTemplate().

◆ GetFileList()

std::vector< wxFileName > PROJECT_TEMPLATE::GetFileList ( )

Get a vector list of filenames for the template.

The files are the source files, and have not yet been through any renaming

Definition at line 104 of file project_template.cpp.

105{
106 std::vector<wxFileName> files;
107 FILE_TRAVERSER sink( files, m_metaPath.GetPath() );
108 wxDir dir( m_basePath.GetPath() );
109
110 dir.Traverse( sink );
111 return files;
112}

References m_basePath, and m_metaPath.

Referenced by CreateProject(), and GetDestinationFiles().

◆ GetHtmlFile()

wxFileName PROJECT_TEMPLATE::GetHtmlFile ( )

Get the full Html filename for the project template.

Returns
the html meta information file for this template

Definition at line 127 of file project_template.cpp.

128{
129 return m_metaHtmlFile;
130}

References m_metaHtmlFile.

Referenced by GetTitle(), and DIALOG_TEMPLATE_SELECTOR::SetWidget().

◆ GetIcon()

wxBitmap * PROJECT_TEMPLATE::GetIcon ( )

Get the 64px^2 icon for the project template.

Returns
an image file of 64px x 64px which is the templates icon

Definition at line 133 of file project_template.cpp.

134{
135 return m_metaIcon;
136}

References m_metaIcon.

Referenced by TEMPLATE_WIDGET::SetTemplate().

◆ GetPrjDirName()

wxString PROJECT_TEMPLATE::GetPrjDirName ( )

Get the dir name of the project template (i.e.

the name of the last folder containing the template files)

Returns
the dir name of the template

Definition at line 115 of file project_template.cpp.

116{
117 return m_basePath.GetDirs()[ m_basePath.GetDirCount() - 1 ];
118}

References m_basePath.

Referenced by CreateProject(), and GetDestinationFiles().

◆ GetTitle()

wxString * PROJECT_TEMPLATE::GetTitle ( )

Get the title of the project (extracted from the html title tag)

Definition at line 291 of file project_template.cpp.

292{
293 wxFFileInputStream input( GetHtmlFile().GetFullPath() );
294 wxString separator( wxT( "\x9" ) );
295 wxTextInputStream text( input, separator, wxConvUTF8 );
296
297 /* Open HTML file and get the text between the title tags */
298 if( m_title == wxEmptyString )
299 {
300 int start = 0;
301 int finish = 0;
302 bool done = false;
303
304 while( input.IsOk() && !input.Eof() && !done )
305 {
306 wxString line = text.ReadLine();
307 wxString upperline = line.Clone().Upper();
308
309 start = upperline.Find( wxT( "<TITLE>" ) );
310 finish = upperline.Find( wxT( "</TITLE>" ) );
311 int length = finish - start - 7;
312
313 // find the opening tag
314 if( start != wxNOT_FOUND )
315 {
316 if( finish != wxNOT_FOUND )
317 {
318 m_title = line( start + 7, length );
319 }
320 else
321 {
322 m_title = line.Mid( start + 7 );
323 }
324
325 done = true;
326 }
327 else
328 {
329 if( finish != wxNOT_FOUND )
330 {
331 m_title += line.SubString( 0, finish );
332 done = true;
333 }
334 }
335
336 // Remove line endings
337 m_title.Replace( wxT( "\r" ), wxT( " " ) );
338 m_title.Replace( wxT( "\n" ), wxT( " " ) );
339 }
340 }
341
342 return &m_title;
343}
wxFileName GetHtmlFile()
Get the full Html filename for the project template.

References GetHtmlFile(), m_title, and text.

Referenced by TEMPLATE_WIDGET::SetTemplate().

Member Data Documentation

◆ m_basePath

wxFileName PROJECT_TEMPLATE::m_basePath
protected

◆ m_metaHtmlFile

wxFileName PROJECT_TEMPLATE::m_metaHtmlFile
protected

Definition at line 148 of file project_template.h.

Referenced by GetHtmlFile(), and PROJECT_TEMPLATE().

◆ m_metaIcon

wxBitmap* PROJECT_TEMPLATE::m_metaIcon
protected

Definition at line 150 of file project_template.h.

Referenced by GetIcon(), and PROJECT_TEMPLATE().

◆ m_metaIconFile

wxFileName PROJECT_TEMPLATE::m_metaIconFile
protected

Definition at line 149 of file project_template.h.

Referenced by PROJECT_TEMPLATE().

◆ m_metaPath

wxFileName PROJECT_TEMPLATE::m_metaPath
protected

Definition at line 147 of file project_template.h.

Referenced by GetFileList(), and PROJECT_TEMPLATE().

◆ m_title

wxString PROJECT_TEMPLATE::m_title
protected

Definition at line 151 of file project_template.h.

Referenced by GetTitle(), and PROJECT_TEMPLATE().


The documentation for this class was generated from the following files: