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 templateBasePath
 
wxFileName templateMetaPath
 
wxFileName templateMetaHtmlFile
 
wxFileName templateMetaIconFile
 
wxBitmap * metaIcon
 
wxString 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 ( 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 38 of file project_template.cpp.

References METADIR, METAFILE_ICON, METAFILE_INFO_HTML, metaIcon, SEP, templateBasePath, templateMetaHtmlFile, templateMetaIconFile, templateMetaPath, and title.

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

Non-virtual destructor (so no derived classes)

Definition at line 100 of file project_template.cpp.

101 {
102 
103 }

Member Function Documentation

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

References GetFileList(), and templateBasePath.

Referenced by KICAD_MANAGER_FRAME::OnCreateProjectFromTemplate().

157 {
158  // CreateProject copy the files from template to the new project folder
159  // and rename files which have the same name as the template .pro file
160  bool result = true;
161 
162  std::vector<wxFileName> srcFiles = GetFileList();
163 
164  // Find the template file name base. this is the name of the .pro template file
165  wxString basename;
166 
167  for( size_t i=0; i < srcFiles.size(); i++ )
168  {
169  if( srcFiles[i].GetExt() == wxT( "pro" ) )
170  {
171  basename = srcFiles[i].GetName();
172  break;
173  }
174  }
175 
176  for( size_t i=0; i < srcFiles.size(); i++ )
177  {
178  // Replace the template path
179  wxFileName destination = srcFiles[i];
180 
181  // Replace the template filename with the project filename for the new project creation
182  wxString currname = destination.GetName();
183  currname.Replace( basename, aNewProjectPath.GetName() );
184  destination.SetName( currname );
185 
186  // Replace the template path with the project path for the new project creation
187  // but keep the sub directory name, if exists
188  wxString destpath = destination.GetPathWithSep();
189  destpath.Replace( templateBasePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
190 
191  // Check to see if the path already exists, if not attempt to create it here. Don't worry
192  // about error checking, if the path isn't created the file copy will fail anyway
193 
194  if( !wxFileName::DirExists( destpath ) )
195  {
196  if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
197  {
198  if( aErrorMsg )
199  {
200  if( !aErrorMsg->empty() )
201  *aErrorMsg += "\n";
202 
203  wxString msg;
204 
205  msg.Printf( _( "Cannot create folder '%s'." ), destpath );
206  *aErrorMsg += msg;
207  }
208 
209  continue;
210  }
211  }
212 
213  destination.SetPath( destpath );
214 
215  wxString srcFile = srcFiles[i].GetFullPath();
216  wxString dstFile = destination.GetFullPath();
217 
218  if( !wxCopyFile( srcFile, dstFile ) )
219  {
220  if( aErrorMsg )
221  {
222  if( !aErrorMsg->empty() )
223  *aErrorMsg += "\n";
224 
225  wxString msg;
226 
227  msg.Printf( _( "Cannot copy file '%s'." ), dstFile );
228  *aErrorMsg += msg;
229  }
230 
231 
232  result = false;
233  }
234  }
235 
236  return result;
237 }
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
wxFileName templateBasePath
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 118 of file project_template.cpp.

References GetFileList(), name, and templateBasePath.

Referenced by KICAD_MANAGER_FRAME::OnCreateProjectFromTemplate().

120 {
121  std::vector< wxFileName > srcFiles = GetFileList();
122 
123  // Find the template file name base. this is the name of the .pro template file
124  wxString basename;
125 
126  for( auto file : srcFiles )
127  {
128  if( file.GetExt() == wxT( "pro" ) )
129  {
130  basename = file.GetName();
131  break;
132  }
133  }
134 
135  for( auto file : srcFiles )
136  {
137  wxFileName destFile = file;
138 
139  // Replace the template filename with the project filename for the new project creation
140  wxString name = destFile.GetName();
141  name.Replace( basename, aNewProjectPath.GetName() );
142  destFile.SetName( name );
143 
144  // Replace the template path with the project path.
145  wxString path = destFile.GetPathWithSep();
146  path.Replace( templateBasePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
147  destFile.SetPath( path );
148 
149  aDestFiles.push_back( destFile );
150  }
151 
152  return aDestFiles.size();
153 }
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
wxFileName templateBasePath
const char * name
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 71 of file project_template.cpp.

References templateBasePath, and templateMetaPath.

Referenced by CreateProject(), and GetDestinationFiles().

72 {
73  std::vector<wxFileName> files;
74  wxString f = templateBasePath.GetPath();
75  wxArrayString allfiles;
76  wxFileName p;
77 
78  wxDir::GetAllFiles( f, &allfiles );
79 
80  // Create the vector and ignore all of the meta data files!
81  for( size_t i=0; i < allfiles.size(); i++ )
82  {
83  p = allfiles[i];
84 
85  // Files that are in the meta directory must not be included
86  if( !p.GetPath().StartsWith( templateMetaPath.GetPath() ) )
87  files.push_back( allfiles[i] );
88  }
89 
90  return files;
91 }
wxFileName templateBasePath
wxFileName templateMetaPath
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 106 of file project_template.cpp.

References templateMetaHtmlFile.

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

107 {
108  return templateMetaHtmlFile;
109 }
wxFileName templateMetaHtmlFile
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 112 of file project_template.cpp.

References metaIcon.

Referenced by TEMPLATE_WIDGET::SetTemplate().

113 {
114  return metaIcon;
115 }
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 94 of file project_template.cpp.

References templateBasePath.

95 {
96  return templateBasePath.GetDirs()[ templateBasePath.GetDirCount()-1 ];
97 }
wxFileName templateBasePath
wxString * PROJECT_TEMPLATE::GetTitle ( void  )

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

Definition at line 240 of file project_template.cpp.

References GetHtmlFile(), and title.

Referenced by TEMPLATE_WIDGET::SetTemplate().

241 {
242  wxFileInputStream input( GetHtmlFile().GetFullPath() );
243  wxString separator( wxT( "\x9" ) );
244  wxTextInputStream text( input, separator, wxConvUTF8 );
245 
246  /* Open HTML file and get the text between the title tags */
247  if( title == wxEmptyString )
248  {
249  int start = 0;
250  int finish = 0;
251  bool done = false;
252 
253  while( input.IsOk() && !input.Eof() && !done )
254  {
255  wxString line = text.ReadLine();
256 
257  start = line.Find( wxT( "<title>" ) );
258  if( start == wxNOT_FOUND )
259  start = line.Find( wxT( "<TITLE>" ) );
260 
261  finish = line.Find( wxT( "</title>" ) );
262  if( finish == wxNOT_FOUND )
263  finish = line.Find( wxT( "</TITLE>" ) );
264 
265  // find the opening tag
266  if( start != wxNOT_FOUND )
267  {
268  if( finish != wxNOT_FOUND )
269  {
270  title = line.SubString( start + 7, finish );
271  }
272  else
273  {
274  title = line.SubString( start + 7, line.Len() - 1 );
275  done = true;
276  }
277  }
278  else
279  {
280  if( finish != wxNOT_FOUND )
281  {
282  title += line.SubString( 0, finish );
283  done = true;
284  }
285  else
286  {
287  title += line;
288  }
289  }
290 
291  // Remove line endings
292  title.Replace( wxT( "\r" ), wxT( "" ) );
293  title.Replace( wxT( "\n" ), wxT( "" ) );
294  }
295  }
296 
297  return &title;
298 }
wxFileName GetHtmlFile()
Get the full Html filename for the project template.

Member Data Documentation

wxBitmap* PROJECT_TEMPLATE::metaIcon
protected

Definition at line 150 of file project_template.h.

Referenced by GetIcon(), and PROJECT_TEMPLATE().

wxFileName PROJECT_TEMPLATE::templateBasePath
protected
wxFileName PROJECT_TEMPLATE::templateMetaHtmlFile
protected

Definition at line 148 of file project_template.h.

Referenced by GetHtmlFile(), and PROJECT_TEMPLATE().

wxFileName PROJECT_TEMPLATE::templateMetaIconFile
protected

Definition at line 149 of file project_template.h.

Referenced by PROJECT_TEMPLATE().

wxFileName PROJECT_TEMPLATE::templateMetaPath
protected

Definition at line 147 of file project_template.h.

Referenced by GetFileList(), and PROJECT_TEMPLATE().

wxString PROJECT_TEMPLATE::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: