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 = _( "Cound'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_metaIconFile
#define METADIR
A directory which contains information about the project template and does not get copied.
wxFileName m_metaHtmlFile
#define METAFILE_INFO_HTML
A required html formatted file which contains information about the project template.
#define SEP
#define METAFILE_ICON
An optional png icon, exactly 64px x 64px which is used in the template selector if present.
#define _(s)

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 103 of file project_template.cpp.

104 {
105 
106 }

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 166 of file project_template.cpp.

167 {
168  // CreateProject copy the files from template to the new project folder and renames files
169  // which have the same name as the template .kicad_pro file
170  bool result = true;
171 
172  std::vector<wxFileName> srcFiles = GetFileList();
173 
174  // Find the template file name base. this is the name of the .kicad_pro (or .pro) template
175  // file
176  wxString basename;
177  bool multipleProjectFilesFound = false;
178 
179  for( wxFileName& file : srcFiles )
180  {
181  if( file.GetExt() == ProjectFileExtension || file.GetExt() == LegacyProjectFileExtension )
182  {
183  if( !basename.IsEmpty() && basename != file.GetName() )
184  multipleProjectFilesFound = true;
185 
186  basename = file.GetName();
187  }
188  }
189 
190  if( multipleProjectFilesFound )
191  basename = GetPrjDirName();
192 
193  for( wxFileName& srcFile : srcFiles )
194  {
195  // Replace the template path
196  wxFileName destFile = srcFile;
197 
198  // Replace the template filename with the project filename for the new project creation
199  wxString currname = destFile.GetName();
200 
201  if( destFile.GetExt() == DrawingSheetFileExtension )
202  {
203  // Skip these; they're often shared
204  }
205  if( destFile.GetName().EndsWith( "-cache" ) || destFile.GetName().EndsWith( "-rescue" ) )
206  {
207  currname.Replace( basename, aNewProjectPath.GetName() );
208  }
209  else if( destFile.GetExt() == "dcm" || destFile.GetExt() == "lib" )
210  {
211  // Don't rename project-specific symbol libraries. This will break the symbol library
212  // table which will cause broken symbol library links in the schematic.
213  }
214  else
215  {
216  currname.Replace( basename, aNewProjectPath.GetName() );
217  }
218 
219  destFile.SetName( currname );
220 
221  // Replace the template path with the project path for the new project creation
222  // but keep the sub directory name, if exists
223  wxString destpath = destFile.GetPathWithSep();
224  destpath.Replace( m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
225 
226  // Check to see if the path already exists, if not attempt to create it here.
227  if( !wxFileName::DirExists( destpath ) )
228  {
229  if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
230  {
231  if( aErrorMsg )
232  {
233  if( !aErrorMsg->empty() )
234  *aErrorMsg += "\n";
235 
236  wxString msg;
237 
238  msg.Printf( _( "Cannot create folder '%s'." ), destpath );
239  *aErrorMsg += msg;
240  }
241 
242  continue;
243  }
244  }
245 
246  destFile.SetPath( destpath );
247 
248  if( !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
249  {
250  if( aErrorMsg )
251  {
252  if( !aErrorMsg->empty() )
253  *aErrorMsg += "\n";
254 
255  wxString msg;
256 
257  msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
258  *aErrorMsg += msg;
259  }
260 
261  result = false;
262  }
263  }
264 
265  return result;
266 }
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
const std::string ProjectFileExtension
#define _(s)
wxString GetPrjDirName()
Get the dir name of the project template (i.e.
const std::string LegacyProjectFileExtension
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 121 of file project_template.cpp.

123 {
124  std::vector< wxFileName > srcFiles = GetFileList();
125 
126  // Find the template file name base. this is the name of the .pro template file
127  wxString basename;
128  bool multipleProjectFilesFound = false;
129 
130  for( wxFileName& file : srcFiles )
131  {
132  if( file.GetExt() == ProjectFileExtension || file.GetExt() == LegacyProjectFileExtension )
133  {
134  if( !basename.IsEmpty() && basename != file.GetName() )
135  multipleProjectFilesFound = true;
136 
137  basename = file.GetName();
138  }
139  }
140 
141  if( multipleProjectFilesFound )
142  basename = GetPrjDirName();
143 
144  for( wxFileName& srcFile : srcFiles )
145  {
146  // Replace the template path
147  wxFileName destFile = srcFile;
148 
149  // Replace the template filename with the project filename for the new project creation
150  wxString name = destFile.GetName();
151  name.Replace( basename, aNewProjectPath.GetName() );
152  destFile.SetName( name );
153 
154  // Replace the template path with the project path.
155  wxString path = destFile.GetPathWithSep();
156  path.Replace( m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
157  destFile.SetPath( path );
158 
159  aDestFiles.push_back( destFile );
160  }
161 
162  return aDestFiles.size();
163 }
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
const std::string ProjectFileExtension
wxString GetPrjDirName()
Get the dir name of the project template (i.e.
const std::string LegacyProjectFileExtension
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 74 of file project_template.cpp.

75 {
76  std::vector<wxFileName> files;
77  wxString f = m_basePath.GetPath();
78  wxArrayString allfiles;
79  wxFileName p;
80 
81  wxDir::GetAllFiles( f, &allfiles );
82 
83  // Create the vector and ignore all of the meta data files!
84  for( size_t i=0; i < allfiles.size(); i++ )
85  {
86  p = allfiles[i];
87 
88  // Files that are in the meta directory must not be included
89  if( !p.GetPath().StartsWith( m_metaPath.GetPath() ) )
90  files.emplace_back(allfiles[i] );
91  }
92 
93  return files;
94 }

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 109 of file project_template.cpp.

110 {
111  return m_metaHtmlFile;
112 }
wxFileName m_metaHtmlFile

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 115 of file project_template.cpp.

116 {
117  return m_metaIcon;
118 }

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 97 of file project_template.cpp.

98 {
99  return m_basePath.GetDirs()[ m_basePath.GetDirCount() - 1 ];
100 }

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 269 of file project_template.cpp.

270 {
271  wxFFileInputStream input( GetHtmlFile().GetFullPath() );
272  wxString separator( wxT( "\x9" ) );
273  wxTextInputStream text( input, separator, wxConvUTF8 );
274 
275  /* Open HTML file and get the text between the title tags */
276  if( m_title == wxEmptyString )
277  {
278  int start = 0;
279  int finish = 0;
280  bool done = false;
281 
282  while( input.IsOk() && !input.Eof() && !done )
283  {
284  wxString line = text.ReadLine();
285  wxString upperline = line.Clone().Upper();
286 
287  start = upperline.Find( wxT( "<TITLE>" ) );
288  finish = upperline.Find( wxT( "</TITLE>" ) );
289  int length = finish - start - 7;
290 
291  // find the opening tag
292  if( start != wxNOT_FOUND )
293  {
294  if( finish != wxNOT_FOUND )
295  {
296  m_title = line( start + 7, length );
297  }
298  else
299  {
300  m_title = line.Mid( start + 7 );
301  }
302 
303  done = true;
304  }
305  else
306  {
307  if( finish != wxNOT_FOUND )
308  {
309  m_title += line.SubString( 0, finish );
310  done = true;
311  }
312  }
313 
314  // Remove line endings
315  m_title.Replace( wxT( "\r" ), wxT( " " ) );
316  m_title.Replace( wxT( "\n" ), wxT( " " ) );
317  }
318  }
319 
320  return &m_title;
321 }
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: