33#include <wx/settings.h>
38#include <wx/textdlg.h>
39#include <wx/textfile.h>
47 const wxString& aPath ) :
66 std::vector<TEMPLATE_WIDGET*> sortedList;
73 for(
size_t idx = 0; idx < count; idx++ )
76 if( item && item->IsWindow() )
82 if( default_temp ==
nullptr && title.CmpNoCase(
"default" ) == 0 )
85 sortedList.push_back( temp );
90 sortedList.begin(), sortedList.end(),
93 const wxString* a = aWidgetA->GetTemplate()->GetTitle();
94 const wxString* b = aWidgetB->GetTemplate()->GetTitle();
96 return ( *a ).CmpNoCase( *b ) < 0;
101 if( default_temp !=
nullptr )
155 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
156 m_staticTitle->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
164 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
165 m_staticTitle->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) );
177 wxBitmap* icon = aTemplate->
GetIcon();
179 if( icon && icon->IsOk() )
183 if( icon->GetWidth() > maxSize.x || icon->GetHeight() > maxSize.y )
185 double scale = std::min( (
double) maxSize.x / icon->GetWidth(),
186 (
double) maxSize.y / icon->GetHeight() );
187 wxImage
image = icon->ConvertToImage();
188 int w = wxRound( icon->GetWidth() *
scale );
189 int h = wxRound( icon->GetHeight() *
scale );
190 image.Rescale( w, h, wxIMAGE_QUALITY_HIGH );
230 menu.Append( wxID_EDIT,
_(
"Edit Template" ) );
231 menu.Append( wxID_COPY,
_(
"Duplicate Template" ) );
233 menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
234 [
this]( wxCommandEvent& evt )
236 if( evt.GetId() == wxID_EDIT )
238 else if( evt.GetId() == wxID_COPY )
253 templatePath.RemoveLastDir();
256 wxDir dir( templatePath.GetPath() );
258 if( !dir.IsOpened() )
261 _(
"Could not open template directory." ) );
266 bool found = dir.GetFirst( &filename,
"*.kicad_pro", wxDIR_FILES );
271 _(
"No project file found in template directory." ) );
275 wxFileName projectFile( templatePath.GetPath(), filename );
278 m_dialog->SetProjectToEdit( projectFile.GetFullPath() );
293 templatePath.RemoveLastDir();
294 wxString srcTemplatePath = templatePath.GetPath();
298 wxTextEntryDialog nameDlg(
m_dialog,
299 _(
"Enter name for the new template:" ),
300 _(
"Duplicate Template" ),
301 srcTemplateName +
_(
"_copy" ) );
303 if( nameDlg.ShowModal() != wxID_OK )
306 wxString newTemplateName = nameDlg.GetValue();
308 if( newTemplateName.IsEmpty() )
315 wxString userTemplatesPath =
m_dialog->GetUserTemplatesPath();
317 if( userTemplatesPath.IsEmpty() )
324 wxFileName destPath( userTemplatesPath, wxEmptyString );
325 destPath.AppendDir( newTemplateName );
326 wxString newTemplatePath = destPath.GetPath();
328 if( destPath.DirExists() )
331 wxString::Format(
_(
"Directory '%s' already exists." ),
336 if( !destPath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
339 wxString::Format(
_(
"Could not create directory '%s'." ),
346 wxDir sourceDir( srcTemplatePath );
348 if( !sourceDir.IsOpened() )
355 newTemplatePath, newTemplateName );
357 sourceDir.Traverse( traverser );
366 wxFileName metaHtmlFile( newTemplatePath,
"info.html" );
367 metaHtmlFile.AppendDir(
"meta" );
369 if( metaHtmlFile.FileExists() )
371 wxTextFile htmlFile( metaHtmlFile.GetFullPath() );
373 if( htmlFile.Open() )
375 bool modified =
false;
377 for(
size_t i = 0; i < htmlFile.GetLineCount(); i++ )
379 wxString line = htmlFile.GetLine( i );
382 if( line.Contains( wxT(
"<title>" ) ) && line.Contains( wxT(
"</title>" ) ) )
384 int titleStart = line.Find( wxT(
"<title>" ) );
385 int titleEnd = line.Find( wxT(
"</title>" ) );
387 if( titleStart != wxNOT_FOUND && titleEnd != wxNOT_FOUND && titleEnd > titleStart )
389 wxString before = line.Left( titleStart + 7 );
390 wxString after = line.Mid( titleEnd );
391 line = before + newTemplateName + after;
406 wxString::Format(
_(
"Template duplicated successfully to '%s'." ),
417 int newPage =
event.GetSelection();
418 int oldPage =
event.GetOldSelection();
421 if( oldPage != wxNOT_FOUND && (
unsigned)oldPage <
m_panels.size() )
428 if( newPage != wxNOT_FOUND && (
unsigned)newPage <
m_panels.size() )
450 std::vector<std::pair<wxString, wxFileName>> aTitleDirList,
451 const wxFileName& aDefaultTemplate ) :
462 for(
auto& [title, pathFname] : aTitleDirList )
465 wxString
path = pathFname.GetFullPath();
470 if( title ==
_(
"User Templates" ) )
487 wxSizer* parentSizer = m_webviewPanel->GetContainingSizer();
491 parentSizer->Detach( m_webviewPanel );
502 if( m_defaultWidget )
503 m_defaultWidget->Select();
510 m_tcTemplatePath->SelectNone();
513 if( m_selectedWidget )
515 wxFileName htmlFile = m_selectedWidget->GetTemplate()->GetHtmlFile();
517 if( htmlFile.FileExists() && htmlFile.IsFileReadable() && htmlFile.GetSize() > 100 )
518 m_webviewPanel->LoadURL( wxFileName::FileNameToURL( htmlFile ) );
529 finishDialogSettings();
541 if( htmlFile.FileExists() && htmlFile.IsFileReadable() )
557 m_panels[aPage]->AddTemplateWidget( w );
561 base.RemoveLastDir();
566 wxString dirName = base.GetDirs().IsEmpty() ? wxString() : base.GetDirs().back();
568 if( dirName.CmpNoCase(
"default" ) == 0 )
592 if( panel->IsUserTemplates() )
593 return panel->GetPath();
597 return wxEmptyString;
604 size_t initialTemplateCount =
m_panels[aPage]->m_SizerChoice->GetItemCount();
609 if( dir.Open( aPath ) )
611 if( dir.HasSubDirs(
"meta" ) )
618 wxArrayString subdirs;
620 bool cont = dir.GetFirst( &sub_name, wxEmptyString, wxDIR_DIRS );
624 subdirs.Add( wxString( sub_name ) );
625 cont = dir.GetNext( &sub_name );
628 if( !subdirs.IsEmpty() )
631 for(
const wxString& dir_name : subdirs )
634 wxString sub_full = aPath + dir_name;
636 if( sub_dir.Open( sub_full ) )
642 m_panels[aPage]->SortAlphabetically();
645 size_t finalTemplateCount =
m_panels[aPage]->m_SizerChoice->GetItemCount();
647 if( finalTemplateCount == initialTemplateCount )
650 if( (
unsigned)aPage <
m_panels.size() )
671 wxString currPath = fn.GetFullPath();
673 wxDirDialog dirDialog(
this,
_(
"Select Templates Directory" ), currPath,
674 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
676 if( dirDialog.ShowModal() != wxID_OK )
679 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
700 currPath = fn.GetFullPath();
715 wxString title =
m_notebook->GetPageText( page );
719 bool wasUserTemplates =
false;
720 if( (
unsigned)page <
m_panels.size() )
721 wasUserTemplates =
m_panels[page]->IsUserTemplates();
727 if( (
unsigned)page <
m_panels.size() )
739 m_notebook->InsertPage( page, tpanel, title,
true );
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
STD_BITMAP_BUTTON * m_browseButton
DIALOG_TEMPLATE_SELECTOR_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Project Template Selector"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(513, 523), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
wxTextCtrl * m_tcTemplatePath
STD_BITMAP_BUTTON * m_reloadButton
WEBVIEW_PANEL * m_webviewPanel
void AddTemplate(int aPage, PROJECT_TEMPLATE *aTemplate)
void OnPageChange(wxNotebookEvent &event) override
void onReload(wxCommandEvent &event) override
DIALOG_TEMPLATE_SELECTOR(wxWindow *aParent, const wxPoint &aPos, const wxSize &aSize, std::vector< std::pair< wxString, wxFileName > > aTitleDirList, const wxFileName &aDefaultTemplate)
PROJECT_TEMPLATE * GetDefaultTemplate()
TEMPLATE_WIDGET * m_selectedWidget
void onDirectoryBrowseClicked(wxCommandEvent &event) override
wxFileName m_defaultTemplatePath
void replaceCurrentPage()
Refresh the current page to show updated template list.
PROJECT_TEMPLATE * GetSelectedTemplate()
std::vector< TEMPLATE_WIDGET * > m_allWidgets
void SetWidget(TEMPLATE_WIDGET *aWidget)
void buildPageContent(const wxString &aPath, int aPage)
wxString GetUserTemplatesPath() const
Get the path to the user templates directory (first panel marked as user templates)
std::vector< TEMPLATE_SELECTION_PANEL * > m_panels
TEMPLATE_WIDGET * m_defaultWidget
A class which provides project template functionality.
wxBitmap * GetIcon()
Get the 64px^2 icon for the project template.
wxFileName GetHtmlFile()
Get the full Html filename for the project template.
wxString * GetTitle()
Get the title of the project (extracted from the html title tag)
Traverser class to duplicate/copy project or template files with proper renaming.
wxString GetErrors() const
wxBoxSizer * m_SizerChoice
TEMPLATE_SELECTION_PANEL_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL|wxBORDER_NONE, const wxString &name=wxEmptyString)
TEMPLATE_SELECTION_PANEL(wxNotebookPage *aParent, const wxString &aPath)
wxString m_templatesPath
the path to access to the folder containing the templates (which are also folders)
void AddTemplateWidget(TEMPLATE_WIDGET *aTemplateWidget)
bool m_isUserTemplates
true if this panel contains user templates
void SetIsUserTemplates(bool aIsUser)
Set whether templates in this panel are user templates (can be edited/duplicated)
wxNotebookPage * m_parent
void SortAlphabetically()
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
wxString GetWelcomeHtml()
wxString GetNoTemplatesHtml()
wxString GetTemplateInfoHtml(const wxString &aTemplateName)
Functions to provide common constants and other functions to assist in making a consistent UI.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().