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 dervied 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)
 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...
 

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

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

38 {
39  templateBasePath = wxFileName::DirName( aPath );
40  templateMetaPath = wxFileName::DirName( aPath + SEP() + METADIR );
41  templateMetaHtmlFile = wxFileName::FileName( aPath + SEP() + METADIR + SEP() +
43  templateMetaIconFile = wxFileName::FileName( aPath + SEP() + METADIR + SEP() + METAFILE_ICON );
44 
45  title = wxEmptyString;
46 
47  // Test the project template requirements to make sure aPath is a valid
48  // template structure
49  if( !wxFileName::DirExists( templateBasePath.GetPath() ) )
50  {
51  // Error, the path doesn't exist!
52  title = wxT( "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 = wxT( "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 = wxT( "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 dervied classes)

Definition at line 99 of file project_template.cpp.

100 {
101 
102 }

Member Function Documentation

bool PROJECT_TEMPLATE::CreateProject ( wxFileName &  aNewProjectPath)

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

Parameters
aNewProjectPathThe full path of the new project file to create

Definition at line 117 of file project_template.cpp.

References GetFileList(), and templateBasePath.

Referenced by KICAD_MANAGER_FRAME::CreateNewProject().

118 {
119  // CreateProject copy the files from template to the new project folder
120  // and rename files which have the same name as the template .pro file
121  bool result = true;
122 
123  std::vector<wxFileName> srcFiles = GetFileList();
124 
125  // Find the template file name base. this is the name of the .pro templte file
126  wxString basename;
127  for( size_t i=0; i < srcFiles.size(); i++ )
128  {
129  if( srcFiles[i].GetExt() == wxT( "pro" ) )
130  {
131  basename = srcFiles[i].GetName();
132  break;
133  }
134  }
135 
136  for( size_t i=0; i < srcFiles.size(); i++ )
137  {
138  // Replace the template path
139  wxFileName destination = srcFiles[i];
140 
141  // Replace the template filename with the project filename for the new project creation
142  wxString currname = destination.GetName();
143  currname.Replace( basename, aNewProjectPath.GetName() );
144  destination.SetName( currname );
145 
146  // Replace the template path with the project path for the new project creation
147  // but keep the sub directory name, if exists
148  wxString destpath = destination.GetPathWithSep();
149  destpath.Replace( templateBasePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
150 
151  // Check to see if the path already exists, if not attempt to create it here. Don't worry
152  // about error checking, if the path isn't created the file copy will fail anyway
153 
154  if( !wxFileName::DirExists( destpath ) )
155  wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL );
156 
157  destination.SetPath( destpath );
158 
159  wxString srcFile = srcFiles[i].GetFullPath();
160  wxString dstFile = destination.GetFullPath();
161 
162  if( !wxCopyFile( srcFile, dstFile ) )
163  {
164  result = false;
165  break;
166  }
167  }
168 
169  return result;
170 }
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
wxFileName templateBasePath
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 70 of file project_template.cpp.

References templateBasePath, and templateMetaPath.

Referenced by CreateProject().

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

References templateMetaHtmlFile.

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

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

References metaIcon.

Referenced by TEMPLATE_WIDGET::SetTemplate().

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

References templateBasePath.

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

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

Definition at line 173 of file project_template.cpp.

References GetHtmlFile(), and title.

Referenced by TEMPLATE_WIDGET::SetTemplate().

174 {
175  wxFileInputStream input( GetHtmlFile().GetFullPath() );
176  wxString separator( wxT( "\x9" ) );
177  wxTextInputStream text( input, separator, wxConvUTF8 );
178 
179  /* Open HTML file and get the text between the title tags */
180  if( title == wxEmptyString )
181  {
182  int start = 0;
183  int finish = 0;
184  bool done = false;
185 
186  while( input.IsOk() && !input.Eof() && !done )
187  {
188  wxString line = text.ReadLine();
189 
190  start = line.Find( wxT( "<title>" ) );
191  if( start == wxNOT_FOUND )
192  start = line.Find( wxT( "<TITLE>" ) );
193 
194  finish = line.Find( wxT( "</title>" ) );
195  if( finish == wxNOT_FOUND )
196  finish = line.Find( wxT( "</TITLE>" ) );
197 
198  // find the opening tag
199  if( start != wxNOT_FOUND )
200  {
201  if( finish != wxNOT_FOUND )
202  {
203  title = line.SubString( start + 7, finish );
204  }
205  else
206  {
207  title = line.SubString( start + 7, line.Len() - 1 );
208  done = true;
209  }
210  }
211  else
212  {
213  if( finish != wxNOT_FOUND )
214  {
215  title += line.SubString( 0, finish );
216  done = true;
217  }
218  else
219  {
220  title += line;
221  }
222  }
223 
224  // Remove line endings
225  title.Replace( wxT( "\r" ), wxT( "" ) );
226  title.Replace( wxT( "\n" ), wxT( "" ) );
227  }
228  }
229 
230  return &title;
231 }
wxFileName GetHtmlFile()
Get the full Html filename for the project template.

Member Data Documentation

wxBitmap* PROJECT_TEMPLATE::metaIcon
protected

Definition at line 152 of file project_template.h.

Referenced by GetIcon(), and PROJECT_TEMPLATE().

wxFileName PROJECT_TEMPLATE::templateBasePath
protected

Definition at line 148 of file project_template.h.

Referenced by CreateProject(), GetFileList(), GetPrjDirName(), and PROJECT_TEMPLATE().

wxFileName PROJECT_TEMPLATE::templateMetaHtmlFile
protected

Definition at line 150 of file project_template.h.

Referenced by GetHtmlFile(), and PROJECT_TEMPLATE().

wxFileName PROJECT_TEMPLATE::templateMetaIconFile
protected

Definition at line 151 of file project_template.h.

Referenced by PROJECT_TEMPLATE().

wxFileName PROJECT_TEMPLATE::templateMetaPath
protected

Definition at line 149 of file project_template.h.

Referenced by GetFileList(), and PROJECT_TEMPLATE().

wxString PROJECT_TEMPLATE::title
protected

Definition at line 153 of file project_template.h.

Referenced by GetTitle(), and PROJECT_TEMPLATE().


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