36#include <wx/settings.h>
41#include <wx/textdlg.h>
42#include <wx/textfile.h>
57 if(
m_dialog->m_refreshPauseCount++ == 0 &&
m_dialog->m_refreshTimer.IsRunning() )
59 m_dialog->m_refreshTimer.Stop();
60 m_dialog->m_refreshPending = true;
67 wxASSERT(
m_dialog->m_refreshPauseCount > 0 );
72 m_dialog->m_refreshTimer.StartOnce( 500 );
78 const wxString& aPath,
const wxString& aTitle,
79 const wxBitmap& aIcon ) :
80 wxPanel( aParent, wxID_ANY ),
84 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
86 wxBoxSizer* sizer =
new wxBoxSizer( wxHORIZONTAL );
88 wxStaticBitmap* icon =
new wxStaticBitmap(
this, wxID_ANY, aIcon );
89 icon->SetMinSize( FromDIP( wxSize( 16, 16 ) ) );
90 sizer->Add( icon, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxTOP | wxBOTTOM, 6 );
92 wxStaticText* label =
new wxStaticText(
this, wxID_ANY, aTitle );
94 sizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT | wxTOP | wxBOTTOM, 6 );
129 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ).ChangeLightness( 150 ) );
137 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
153 SetMinSize( FromDIP( wxSize( 200, -1 ) ) );
174#if wxCHECK_VERSION( 3, 3, 2 )
190 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
191 m_titleLabel->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
192 m_descLabel->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
200 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
201 m_titleLabel->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
202 m_descLabel->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
210 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
211 m_titleLabel->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) );
212 m_descLabel->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) );
226 const std::vector<int> c_bitmapSizes = { 48, 64, 96, 128 };
228 wxBitmapBundle bundle;
229 wxVector<wxBitmap> bitmaps;
230 wxBitmap* icon = aTemplate->
GetIcon();
232 if( icon && icon->IsOk() )
237 wxSize defSize = bundle.GetDefaultSize();
239 for(
const int genSize : c_bitmapSizes )
241 double scale = std::min( (
double) genSize / defSize.GetWidth(),
242 (
double) genSize / defSize.GetHeight() );
244 wxSize scaledSize( wxRound( defSize.x *
scale ),
245 wxRound( defSize.y *
scale ) );
247 wxBitmap scaled = bundle.GetBitmap( scaledSize );
248 scaled.SetScaleFactor( 1.0 );
250 bitmaps.push_back( scaled );
253 m_bitmapIcon->SetBitmap( wxBitmapBundle::FromBitmaps( bitmaps ) );
260 return wxEmptyString;
263 htmlFile.RemoveLastDir();
264 return htmlFile.GetPath();
285 wxString displayDesc = aDescription;
287 if( displayDesc.Length() > 120 )
288 displayDesc = displayDesc.Left( 120 ) + wxS(
"..." );
299#if !wxCHECK_VERSION( 3, 3, 2 )
301 int wrapWidth = GetClientSize().GetWidth() - 48 - 20;
303 if( wrapWidth > 100 )
309 if( displayDesc.Length() > 120 )
310 displayDesc = displayDesc.Left( 120 ) + wxS(
"..." );
347 menu.Append( wxID_EDIT,
348 m_category == CATEGORY::USER ?
_(
"Edit Template" ) :
_(
"Open Template (Read-Only)" ) );
349 menu.Append( wxID_OPEN,
_(
"Open Template Folder" ) );
350 menu.Append( wxID_COPY,
_(
"Duplicate Template" ) );
351 menu.AppendSeparator();
354 menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
355 [
this]( wxCommandEvent& evt )
357 if( evt.GetId() == wxID_EDIT )
359 else if( evt.GetId() == wxID_OPEN )
361 else if( evt.GetId() == wxID_COPY )
384 templatePath.RemoveLastDir();
386 wxDir dir( templatePath.GetPath() );
388 if( !dir.IsOpened() )
395 bool found = dir.GetFirst( &filename,
"*.kicad_pro", wxDIR_FILES );
403 wxFileName projectFile( templatePath.GetPath(), filename );
405 m_dialog->SetProjectToEdit( projectFile.GetFullPath() );
417 templatePath.RemoveLastDir();
419 if( !wxLaunchDefaultApplication( templatePath.GetPath() ) )
420 DisplayError(
this, wxString::Format(
_(
"Failed to open '%s'." ), templatePath.GetPath() ) );
430 templatePath.RemoveLastDir();
431 wxString srcTemplatePath = templatePath.GetPath();
435 wxTextEntryDialog nameDlg(
m_dialog,
_(
"Enter name for the new template:" ),
_(
"Duplicate Template" ),
436 srcTemplateName +
_(
"_copy" ) );
438 if( nameDlg.ShowModal() != wxID_OK )
441 wxString newTemplateName = nameDlg.GetValue();
443 if( newTemplateName.IsEmpty() )
449 wxString userTemplatesPath =
m_dialog->GetUserTemplatesPath();
451 if( userTemplatesPath.IsEmpty() )
457 wxFileName destPath( userTemplatesPath, wxEmptyString );
458 destPath.AppendDir( newTemplateName );
459 wxString newTemplatePath = destPath.GetPath();
461 if( destPath.DirExists() )
467 if( !destPath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
473 wxDir sourceDir( srcTemplatePath );
475 if( !sourceDir.IsOpened() )
481 PROJECT_TREE_TRAVERSER traverser(
nullptr, srcTemplatePath, srcTemplateName, newTemplatePath, newTemplateName );
483 sourceDir.Traverse( traverser );
491 wxFileName metaHtmlFile( newTemplatePath,
"info.html" );
492 metaHtmlFile.AppendDir(
"meta" );
494 if( metaHtmlFile.FileExists() )
496 wxTextFile htmlFile( metaHtmlFile.GetFullPath() );
498 if( htmlFile.Open() )
500 bool modified =
false;
502 for(
size_t i = 0; i < htmlFile.GetLineCount(); i++ )
504 wxString line = htmlFile.GetLine( i );
506 if( line.Contains( wxT(
"<title>" ) ) && line.Contains( wxT(
"</title>" ) ) )
508 int titleStart = line.Find( wxT(
"<title>" ) );
509 int titleEnd = line.Find( wxT(
"</title>" ) );
511 if( titleStart != wxNOT_FOUND && titleEnd != wxNOT_FOUND && titleEnd > titleStart )
513 wxString before = line.Left( titleStart + 7 );
514 wxString after = line.Mid( titleEnd );
515 line = before + newTemplateName + after;
536 [dlg, newTemplatePath]()
538 DisplayInfoMessage( dlg, wxString::Format(
_(
"Template duplicated successfully to '%s'." ),
546 const wxSize& aSize,
const wxString& aUserTemplatesPath,
547 const wxString& aSystemTemplatesPath,
548 const wxString& aDefaultTemplatesPath,
549 const std::vector<wxString>& aRecentTemplates,
550 const wxString& aBrowsedTemplatesPath ) :
577 m_scrolledTemplates->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
612 if( filterChoice >= 0 && filterChoice <
static_cast<int>(
m_filterChoice->GetCount() ) )
669 int width =
m_splitter->GetClientSize().GetWidth();
670 sashPos = width > 0 ? width / 3 : FromDIP( 300 );
726 wxFileName templateDir;
727 templateDir.AssignDir(
path );
729 if( !templateDir.DirExists() )
735 wxBitmap* icon = templ.
GetIcon();
739 if( icon && icon->IsOk() )
741 wxImage img = icon->ConvertToImage();
742 img.Rescale( 16, 16, wxIMAGE_QUALITY_HIGH );
743 scaledIcon = wxBitmap( img );
748 wxImage img = scaledIcon.ConvertToImage();
749 img.Rescale( 16, 16, wxIMAGE_QUALITY_HIGH );
750 scaledIcon = wxBitmap( img );
753 wxString displayTitle = title ? *title : templateDir.GetDirs().Last();
757 m_sizerMRU->Add( mruWidget, 0, wxEXPAND | wxBOTTOM, 2 );
784 if( aPath.IsEmpty() )
789 if( !dir.Open( aPath ) )
793 static_cast<int>( aCategory ) );
795 if( dir.HasSubDirs(
"meta" ) )
797 auto templ = std::make_unique<PROJECT_TEMPLATE>( aPath );
798 wxFileName htmlFile = templ->GetHtmlFile();
812 bool cont = dir.GetFirst( &subName, wxEmptyString, wxDIR_DIRS );
816 wxString subFull = aPath + wxFileName::GetPathSeparator() + subName;
822 metaTest.AssignDir( subFull );
825 if( metaTest.DirExists() )
827 auto templ = std::make_unique<PROJECT_TEMPLATE>( subFull );
828 wxFileName htmlFile = templ->GetHtmlFile();
829 wxString description = templ->GetError().IsEmpty() ?
ExtractDescription( htmlFile ) : templ->GetError();
841 cont = dir.GetNext( &subName );
879 return TemplateListLess( *a->GetTemplate()->GetTitle(), a->IsPinned(),
880 *b->GetTemplate()->GetTitle(), b->IsPinned() );
915 if( widget->GetTemplatePath() == aPath )
916 widget->SetPinned( aPinned );
930 bool matchesFilter =
true;
934 if( filterChoice == 1 && cat != TEMPLATE_WIDGET::CATEGORY::USER )
935 matchesFilter =
false;
936 else if( filterChoice == 2 && cat != TEMPLATE_WIDGET::CATEGORY::SYSTEM )
937 matchesFilter =
false;
938 else if( filterChoice == 3 && cat != TEMPLATE_WIDGET::CATEGORY::BROWSED )
939 matchesFilter =
false;
941 bool matchesSearch =
true;
943 if( !searchText.IsEmpty() )
945 wxString title = widget->GetTemplate()->GetTitle()->Lower();
946 wxString description = widget->GetDescription().Lower();
948 matchesSearch = title.Contains( searchText ) || description.Contains( searchText );
951 widget->Show( matchesFilter && matchesSearch );
1041 if( widget->GetTemplate() )
1043 wxFileName htmlFile = widget->GetTemplate()->GetHtmlFile();
1044 htmlFile.RemoveLastDir();
1046 if( htmlFile.GetPath() == aPath )
1048 if( aKeepMRUVisible )
1057 widget->SelectWithoutStateChange();
1062 widget->GetPosition(
nullptr, &y );
1065 if( scrollRate > 0 )
1092 wxBoxSizer* sizer =
new wxBoxSizer( wxVERTICAL );
1116 if( htmlFile.FileExists() && htmlFile.IsFileReadable() )
1119 wxString url = wxFileName::FileNameToURL( htmlFile );
1154 [
this](
const wxString& aPath,
const char* aLabel )
1156 if( aPath.IsEmpty() )
1160 dir.AssignDir( aPath );
1170 if( dir.DirExists() )
1192 [](
const wxString& a,
const wxString& b )
1194 if( a.IsEmpty() || b.IsEmpty() )
1205 return lhs.SameAs( rhs );
1242 m_scrolledTemplates->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
1247 if( widget->IsSelected() )
1248 widget->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
1250 widget->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
1258 widget->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
1274 if( clientWidth <= 0 )
1280 wxSizeEvent sizeEvt( wxSize( clientWidth, -1 ) );
1281 widget->GetEventHandler()->ProcessEvent( sizeEvt );
1299 wxString script = wxString::Format( wxS( R
"(
1302 var style = document.createElement( 'style' );
1303 style.textContent = `
1306 document.head.appendChild( style );
1308 )" ), GetCommonStyles() );
1310#if !defined( __MINGW32__ )
1320 wxString selectedPath;
1325 htmlFile.RemoveLastDir();
1326 selectedPath = htmlFile.GetPath();
1335 if( !selectedPath.IsEmpty() )
1342 if( !aHtmlFile.FileExists() || !aHtmlFile.IsFileReadable() )
1343 return wxEmptyString;
1345 wxTextFile file( aHtmlFile.GetFullPath() );
1348 return wxEmptyString;
1352 for( wxString line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine() )
1354 content += line + wxT(
" " );
1360 wxRegEx reMetaDesc( wxT(
"<meta[^>]*name=[\"']description[\"'][^>]*content=[\"']([^\"']*)[\"']" ),
1363 if( reMetaDesc.Matches( content ) )
1364 return reMetaDesc.GetMatch( content, 1 );
1367 wxRegEx reParagraph( wxT(
"<p[^>]*>(.*?)</p>" ), wxRE_ICASE );
1369 if( reParagraph.Matches( content ) )
1371 wxString desc = reParagraph.GetMatch( content, 1 );
1374 wxRegEx reTags( wxT(
"<[^>]*>" ) );
1375 reTags.ReplaceAll( &desc, wxT(
" " ) );
1378 desc.Replace( wxT(
" " ), wxT(
" " ) );
1379 desc.Replace( wxT(
"&" ), wxT(
"&" ) );
1380 desc.Replace( wxT(
"<" ), wxT(
"<" ) );
1381 desc.Replace( wxT(
">" ), wxT(
">" ) );
1382 desc.Replace( wxT(
""" ), wxT(
"\"" ) );
1385 wxRegEx reWhitespace( wxT(
"\\s+" ) );
1386 reWhitespace.ReplaceAll( &desc, wxT(
" " ) );
1388 desc = desc.Trim().Trim(
false );
1390 if( !desc.IsEmpty() )
1395 wxRegEx reBody( wxT(
"<body[^>]*>(.*)</body>" ), wxRE_ICASE );
1397 if( reBody.Matches( content ) )
1399 wxString body = reBody.GetMatch( content, 1 );
1402 wxRegEx reTags( wxT(
"<[^>]*>" ) );
1403 reTags.ReplaceAll( &body, wxT(
" " ) );
1406 body.Replace( wxT(
" " ), wxT(
" " ) );
1407 body.Replace( wxT(
"&" ), wxT(
"&" ) );
1408 body.Replace( wxT(
"<" ), wxT(
"<" ) );
1409 body.Replace( wxT(
">" ), wxT(
">" ) );
1410 body.Replace( wxT(
""" ), wxT(
"\"" ) );
1413 wxRegEx reWhitespace( wxT(
"\\s+" ) );
1414 reWhitespace.ReplaceAll( &body, wxT(
" " ) );
1416 body = body.Trim().Trim(
false );
1419 if( body.Length() > 250 )
1420 return body.Left( 250 ) + wxS(
"..." );
1425 return wxEmptyString;
1439 if( startPath.IsEmpty() )
1442 wxDirDialog dirDialog(
this,
_(
"Select Templates Directory" ), startPath,
1443 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
1445 if( dirDialog.ShowModal() != wxID_OK )
1448 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
1459 if( filterChoice != 0 && filterChoice != 3 )
1498 parentSizer->Layout();
wxBitmapBundle KiBitmapBundleDef(BITMAPS aBitmap, int aDefHeight)
Construct and return a bitmap bundle for the given icon ID, with the default bitmap size being aDefHe...
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
DIALOG_TEMPLATE_SELECTOR * m_dialog
REFRESH_GUARD(DIALOG_TEMPLATE_SELECTOR *aDialog)
wxSearchCtrl * m_searchCtrl
wxStaticText * m_browsedPathLabel
wxBoxSizer * m_sizerTemplateList
wxScrolledWindow * m_scrolledTemplates
wxPanel * m_panelTemplates
wxChoice * m_filterChoice
wxScrolledWindow * m_scrolledMRU
DIALOG_TEMPLATE_SELECTOR_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Project Template Selector"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(900, 600), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
wxButton * m_clearBrowseButton
wxSplitterWindow * m_splitter
wxPanel * m_webviewPlaceholder
bool m_loadingExternalHtml
wxString ExtractDescription(const wxFileName &aHtmlFile)
void SetState(DialogState aState)
wxString m_systemTemplatesPath
void SelectTemplateByPath(const wxString &aPath)
void OnBackClicked(wxCommandEvent &event) override
PROJECT_TEMPLATE * m_selectedTemplate
wxString m_browsedTemplatesPath
TEMPLATE_WIDGET * m_selectedWidget
void LoadTemplatePreview(PROJECT_TEMPLATE *aTemplate)
void OnSearchCtrl(wxCommandEvent &event) override
bool IsTemplatePinned(const wxString &aPath) const
void OnFileSystemEvent(wxFileSystemWatcherEvent &event)
void SortTemplateWidgets()
void onClearBrowsedClicked(wxCommandEvent &aEvent) override
bool browsedPathIsDuplicate() const
Returns true if the browsed path is empty or duplicates one of the standard template directories (aft...
void OnSearchCtrlCancel(wxCommandEvent &event) override
void OnWebViewLoaded(wxWebViewEvent &event)
wxString m_defaultTemplatesPath
wxString m_userTemplatesPath
WEBVIEW_PANEL * m_webviewPanel
void OnFilterChanged(wxCommandEvent &event) override
void onBrowseClicked(wxCommandEvent &aEvent) override
void SetTemplatePinned(const wxString &aPath, bool aPinned)
PROJECT_TEMPLATE * GetSelectedTemplate()
void EnsureWebViewCreated()
void RefreshTemplateList()
~DIALOG_TEMPLATE_SELECTOR()
std::vector< TEMPLATE_WIDGET * > m_templateWidgets
void OnRefreshTimer(wxTimerEvent &event)
void OnScrolledTemplatesSize(wxSizeEvent &event)
void SetWidget(TEMPLATE_WIDGET *aWidget)
std::vector< TEMPLATE_MRU_WIDGET * > m_mruWidgets
void OnSysColourChanged(wxSysColourChangedEvent &event)
void updateBrowsedPathLabel()
void OnSearchTimer(wxTimerEvent &event)
DIALOG_TEMPLATE_SELECTOR(wxWindow *aParent, const wxPoint &aPos, const wxSize &aSize, const wxString &aUserTemplatesPath, const wxString &aSystemTemplatesPath, const wxString &aDefaultTemplatesPath, const std::vector< wxString > &aRecentTemplates, const wxString &aBrowsedTemplatesPath=wxEmptyString)
std::vector< wxString > m_pinnedTemplates
wxFileSystemWatcher * m_watcher
void EnsurePreviewSplit()
friend class TEMPLATE_WIDGET
std::vector< std::unique_ptr< PROJECT_TEMPLATE > > m_templates
std::vector< wxString > m_recentTemplates
int m_TemplateFilterChoice
std::vector< wxString > m_PinnedTemplates
static const wxString GetPinningSymbol()
virtual SETTINGS_MANAGER & GetSettingsManager() const
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
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
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.
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
This file is part of the common library.
static const wxChar * traceTemplateSelector
static constexpr int ID_TOGGLE_PINNED
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
PGM_BASE & Pgm()
The global program "get" accessor.
#define METADIR
A directory which contains information about the project template and does not get copied.
#define wxFileSystemWatcher
T * GetAppSettings(const char *aFilename)
wxString GetWelcomeHtml(bool aDarkMode)
wxString GetTemplateInfoHtml(const wxString &aTemplateName, bool aDarkMode)
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().