KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_template_selector.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2012 Brian Sidebotham <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26#include <bitmaps.h>
28#include <widgets/ui_common.h>
29#include <wx_filename.h>
30#include <wx/dir.h>
31#include <wx/dirdlg.h>
32#include <wx/settings.h>
33
34static const wxString GetWelcomeHtml()
35{
36 return wxString(
37 "<!DOCTYPE html>"
38 "<html lang=\"en\">"
39 "<head>"
40 "<meta charset=\"UTF-8\">"
41 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
42 "<title>KiCad Project Template Selector</title>"
43 "<style>"
44 "body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; margin: 0; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: #333; min-height: 100vh; box-sizing: border-box; }"
45 ".container { max-width: 800px; margin: 0 auto; background: rgba(255, 255, 255, 0.95); border-radius: 12px; padding: 30px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1); backdrop-filter: blur(10px); }"
46 ".header { text-align: center; margin-bottom: 30px; }"
47 ".logo { font-size: 2.5rem; font-weight: bold; color: #4a5568; margin-bottom: 10px; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1); }"
48 ".subtitle { font-size: 1.2rem; color: #666; margin-bottom: 20px; }"
49 ".welcome-card { "
50#if defined( __MINGW32__ )
51 "background: #4299e1;" // linear-gradient does not work with webview used on MSYS2
52#else
53 "background: linear-gradient(135deg, #4299e1, #3182ce);"
54#endif
55 "color: white; padding: 25px; border-radius: 10px; margin-bottom: 25px; box-shadow: 0 4px 15px rgba(66, 153, 225, 0.3); }"
56 ".welcome-card h2 { margin-top: 0; font-size: 1.8rem; margin-bottom: 15px; }"
57 ".instructions { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin-bottom: 25px; }"
58 ".instruction-card { background: #f7fafc; border: 2px solid #e2e8f0; border-radius: 8px; padding: 20px; transition: all 0.3s ease; position: relative; overflow: hidden; }"
59 ".instruction-card:hover { transform: translateY(-2px); box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); border-color: #4299e1; }"
60 ".instruction-card::before { content: ''; position: absolute; top: 0; left: 0; width: 4px; height: 100%; background: linear-gradient(135deg, #4299e1, #3182ce); }"
61 ".instruction-card h3 { color: #2d3748; margin-top: 0; margin-bottom: 10px; font-size: 1.3rem; }"
62 ".instruction-card p { color: #4a5568; line-height: 1.6; margin: 0; }"
63 ".features { background: #f0fff4; border: 2px solid #9ae6b4; border-radius: 8px; padding: 20px; margin-bottom: 25px; }"
64 ".features h3 { color: #22543d; margin-top: 0; margin-bottom: 15px; font-size: 1.4rem; }"
65 ".features ul { color: #2f855a; line-height: 1.8; margin: 0; padding-left: 20px; }"
66 ".features li { margin-bottom: 8px; }"
67 ".tips { background: #fffaf0; border: 2px solid #fbd38d; border-radius: 8px; padding: 20px; }"
68 ".tips h3 { color: #c05621; margin-top: 0; margin-bottom: 15px; font-size: 1.4rem; }"
69 ".tips p { color: #c05621; line-height: 1.6; margin: 0 0 10px 0; }"
70 ".highlight { background: linear-gradient(120deg, #a8edea 0%, #fed6e3 100%); padding: 2px 6px; border-radius: 4px; font-weight: 600; }"
71 "</style>"
72 "</head>"
73 "<body>"
74 "<div class=\"container\">"
75 "<div class=\"header\">"
76 "<div class=\"logo\">KiCad 📑</div>"
77 "<div class=\"subtitle\">" + _( "Project Template Selector" ) + "</div>"
78 "</div>"
79 "<div class=\"welcome-card\">"
80 "<h2>" + _( "Welcome to Template Selection!" ) + "</h2>"
81 "<p>" + _( "Choose from a variety of pre-configured project templates to jumpstart your PCB design. Templates provide ready-to-use project structures with common components, libraries, and design rules." ) + "</p>"
82 "</div>"
83 "<div class=\"instructions\">"
84 "<div class=\"instruction-card\">"
85 "<h3>→ " + _( "Browse Templates" ) + "</h3>"
86 "<p>" + _( "Navigate through the template tabs above to explore different categories of project templates. Each tab contains templates organized by type or complexity." ) + "</p>"
87 "</div>"
88 "<div class=\"instruction-card\">"
89 "<h3>→ " + _( "Select a Template" ) + "</h3>"
90 "<p>" + _( "Click on any template in the list to " ) + "<span class=\"highlight\">" + _( "preview its details" ) + "</span>. " + _( "The template information will appear in this panel, showing descriptions, included components, and project structure." ) + "</p>"
91 "</div>"
92 "<div class=\"instruction-card\">"
93 "<h3>→ " + _( "Customize Path" ) + "</h3>"
94 "<p>" + _( "Use the " ) + "<span class=\"highlight\">" + _( "folder path field" ) + "</span> " + _( "above to browse custom template directories. Click the folder icon to browse, or the refresh icon to reload templates." ) + "</p>"
95 "</div>"
96 "<div class=\"instruction-card\">"
97 "<h3>→ " + _( "Create Project" ) + "</h3>"
98 "<p>" + _( "Once you've found the right template, click " ) + "<span class=\"highlight\">" + _( "OK" ) + "</span> " + _( "to create a new project based on the selected template. Your project will inherit all template settings and files." ) + "</p>"
99 "</div>"
100 "</div>"
101 "<div class=\"features\">"
102 "<h3>" + _( "What You Get with Templates" ) + "</h3>"
103 "<ul>"
104 "<li><strong>" + _( "Pre-configured libraries" ) + "</strong> " + _( "- Common components and footprints already linked" ) + "</li>"
105 "<li><strong>" + _( "Design rules" ) + "</strong> " + _( "- Appropriate electrical and mechanical constraints" ) + "</li>"
106 "<li><strong>" + _( "Layer stackups" ) + "</strong> " + _( "- Optimized for the intended application" ) + "</li>"
107 "<li><strong>" + _( "Component placement" ) + "</strong> " + _( "- Basic layout and routing guidelines" ) + "</li>"
108 "<li><strong>" + _( "Documentation" ) + "</strong> " + _( "- README files and design notes" ) + "</li>"
109 "<li><strong>" + _( "Manufacturing files" ) + "</strong> " + _( "- Gerber and drill file configurations" ) + "</li>"
110 "</ul>"
111 "</div>"
112 "<div class=\"tips\">"
113 "<h3>" + _( "Pro Tips" ) + "</h3>"
114 "<p><strong>" + _( "Start Simple:" ) + "</strong> " + _( "Begin with basic templates and add more elements as you go." ) + "</p>"
115 "<p><strong>" + _( "Customize Later:" ) + "</strong> " + _( "Templates are starting points - you can modify libraries, rules, and layouts after project creation." ) + "</p>"
116 "<p><strong>" + _( "Save Your Own:" ) + "</strong> " + _( "Once you develop preferred settings, create a custom template for future projects." ) + "</p>"
117 "</div>"
118 "</div>"
119 "</body>"
120 "</html>"
121);
122}
123
125 const wxString& aPath ) :
127{
128 m_parent = aParent;
129 m_templatesPath = aPath;
130}
131
132
134{
135 m_SizerChoice->Add( aTemplateWidget );
136 Layout();
137}
138
139
141 TEMPLATE_WIDGET_BASE( aParent )
142{
143 m_parent = aParent;
144 m_dialog = aDialog;
145
146 // wxWidgets_3.xx way of doing the same...
147 // Bind(wxEVT_LEFT_DOWN, &TEMPLATE_WIDGET::OnMouse, this );
148
149 m_bitmapIcon->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ),
150 nullptr, this );
151 m_staticTitle->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ),
152 nullptr, this );
153
154 // We're not selected until we're clicked
155 Unselect();
156
157 // Start with template being NULL
158 m_currTemplate = nullptr;
159}
160
161
163{
164 m_dialog->SetWidget( this );
165 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNHIGHLIGHT ) );
166 m_selected = true;
167 Refresh();
168}
169
170
172{
173 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
174 m_selected = false;
175 Refresh();
176}
177
178
180{
181 m_currTemplate = aTemplate;
182 m_staticTitle->SetFont( KIUI::GetInfoFont( this ) );
183 m_staticTitle->SetLabel( *aTemplate->GetTitle() );
184 m_staticTitle->Wrap( 100 );
185 m_bitmapIcon->SetBitmap( *aTemplate->GetIcon() );
186}
187
188
189void TEMPLATE_WIDGET::OnMouse( wxMouseEvent& event )
190{
191 // Toggle selection here
192 Select();
193 event.Skip();
194}
195
196
197void DIALOG_TEMPLATE_SELECTOR::OnPageChange( wxNotebookEvent& event )
198{
199 int newPage = event.GetSelection();
200 int oldPage = event.GetOldSelection();
201
202 // Move webview panel from old page to new page
203 if( oldPage != wxNOT_FOUND && (unsigned)oldPage < m_panels.size() )
204 {
205 // Detach webview from old panel
206 m_panels[oldPage]->m_SizerBase->Detach( m_webviewPanel );
207 m_panels[oldPage]->Layout();
208 }
209
210 if( newPage != wxNOT_FOUND && (unsigned)newPage < m_panels.size() )
211 {
212 // Reparent the webview to the new panel
213 m_webviewPanel->Reparent( m_panels[newPage] );
214
215 // Attach webview to new panel
216 m_panels[newPage]->m_SizerBase->Add( m_webviewPanel, 1, wxEXPAND | wxALL, 5 );
217 m_panels[newPage]->Layout();
218
219 // Update template path
220 m_tcTemplatePath->SetValue( m_panels[newPage]->GetPath() );
221
222 // Reset to welcome page when switching tabs if no template selected
224 }
225
226 event.Skip();
227}
228
229
230DIALOG_TEMPLATE_SELECTOR::DIALOG_TEMPLATE_SELECTOR( wxWindow* aParent, const wxPoint& aPos,
231 const wxSize& aSize,
232 std::map<wxString, wxFileName> aTitleDirMap ) :
233 DIALOG_TEMPLATE_SELECTOR_BASE( aParent, wxID_ANY, _( "Project Template Selector" ), aPos,
234 aSize )
235{
236 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
237 m_reloadButton->SetBitmap( KiBitmapBundle( BITMAPS::small_refresh ) );
238
239 m_selectedWidget = nullptr;
240
241 for( auto& [title, pathFname] : aTitleDirMap )
242 {
243 pathFname.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
244 wxString path = pathFname.GetFullPath(); // caller ensures this ends with file separator.
245
247 m_panels.push_back( tpanel );
248
249 m_notebook->AddPage( tpanel, title );
250
251 if( m_notebook->GetPageCount() == 1 )
252 m_tcTemplatePath->SetValue( path );
253
254 buildPageContent( path, m_notebook->GetPageCount() - 1 );
255 }
256
257 // Move webview panel from dialog to first template selection panel
258 if( !m_panels.empty() )
259 {
260 // Find the sizer containing the webview and detach it
261 wxSizer* parentSizer = m_webviewPanel->GetContainingSizer();
262
263 if( parentSizer )
264 {
265 parentSizer->Detach( m_webviewPanel );
266 }
267
268 // Reparent the webview to the first panel
269 m_webviewPanel->Reparent( m_panels[0] );
270
271 // Add webview to first panel
272 m_panels[0]->m_SizerBase->Add( m_webviewPanel, 1, wxEXPAND | wxALL, 5 );
273 m_panels[0]->Layout();
274 }
275
276 // Set welcome HTML after dialog is fully constructed
277 CallAfter( [this]()
278 {
279 #if defined (_WIN32)
280 // For some reason the next calls need it to work fine on Windows, especially with MSYS2
281 wxSafeYield();
282 // Deselect the m_tcTemplatePath string (selected for some strange reason)
283 m_tcTemplatePath->SelectNone();
284 #endif
285
287 });
288
289 // When all widgets have the size fixed, call finishDialogSettings to update sizers
291}
292
293
295{
296 if( m_selectedWidget != nullptr )
298
299 m_selectedWidget = aWidget;
300 wxFileName htmlFile = aWidget->GetTemplate()->GetHtmlFile();
301
302 if( htmlFile.FileExists() && htmlFile.IsFileReadable() )
303 {
304 m_webviewPanel->LoadURL( wxFileName::FileNameToURL( htmlFile ) );
305 }
306 else
307 {
308 // Fallback to a simple template info page if no HTML file exists
309 wxString templateHtml = wxString::Format(
310 "<!DOCTYPE html>"
311 "<html><head><meta charset='UTF-8'><style>"
312 "body { font-family: Arial, sans-serif; margin: 20px; }"
313 ".template-info { background: #f0f8ff; padding: 20px; border-radius: 8px; }"
314 "h1 { color: #333; margin-top: 0; }"
315 "</style></head><body>"
316 "<div class='template-info'>"
317 "<h1>%s</h1>"
318 "<p>Template selected. Click OK to create a new project based on this template.</p>"
319 "</div></body></html>",
320 *aWidget->GetTemplate()->GetTitle()
321 );
322 m_webviewPanel->SetPage( templateHtml );
323 }
324}
325
326
328{
329 TEMPLATE_WIDGET* w = new TEMPLATE_WIDGET( m_panels[aPage]->m_scrolledWindow, this );
330 w->SetTemplate( aTemplate );
331 m_panels[aPage]->AddTemplateWidget( w );
332}
333
334
336{
337 return m_selectedWidget? m_selectedWidget->GetTemplate() : nullptr;
338}
339
340
341void DIALOG_TEMPLATE_SELECTOR::buildPageContent( const wxString& aPath, int aPage )
342{
343 // Get a list of files under the template path to include as choices...
344 wxDir dir;
345
346 if( dir.Open( aPath ) )
347 {
348 if( dir.HasSubDirs( "meta" ) )
349 {
350 AddTemplate( aPage, new PROJECT_TEMPLATE( aPath ) );
351 }
352 else
353 {
354 wxString sub_name;
355 wxArrayString subdirs;
356
357 bool cont = dir.GetFirst( &sub_name, wxEmptyString, wxDIR_DIRS );
358
359 while( cont )
360 {
361 subdirs.Add( wxString( sub_name ) );
362 cont = dir.GetNext( &sub_name );
363 }
364
365 if( !subdirs.IsEmpty() )
366 subdirs.Sort();
367
368 for( const wxString& dir_name : subdirs )
369 {
370 wxDir sub_dir;
371 wxString sub_full = aPath + dir_name;
372
373 if( sub_dir.Open( sub_full ) )
374 AddTemplate( aPage, new PROJECT_TEMPLATE( sub_full ) );
375 }
376 }
377 }
378
379 Layout();
380}
381
382
384{
385 wxFileName fn;
386 fn.AssignDir( m_tcTemplatePath->GetValue() );
387 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
388 wxString currPath = fn.GetFullPath();
389
390 wxDirDialog dirDialog( this, _( "Select Templates Directory" ), currPath,
391 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
392
393 if( dirDialog.ShowModal() != wxID_OK )
394 return;
395
396 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
397
398 m_tcTemplatePath->SetValue( dirName.GetFullPath() );
399
400 // Rebuild the page from the new templates path:
402}
403
404
405void DIALOG_TEMPLATE_SELECTOR::onReload( wxCommandEvent& event )
406{
407 int page = m_notebook->GetSelection();
408
409 if( page < 0 )
410 return; // Should not happen
411
412 wxString currPath = m_tcTemplatePath->GetValue();
413
414 wxFileName fn;
415 fn.AssignDir( m_tcTemplatePath->GetValue() );
416 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
417 currPath = fn.GetFullPath();
418 m_tcTemplatePath->SetValue( currPath );
419
421}
422
423
425{
426 // Rebuild the page from the new templates path:
427 int page = m_notebook->GetSelection();
428
429 if( page < 0 )
430 return; // Should not happen
431
432 wxString title = m_notebook->GetPageText( page );
433 wxString currPath = m_tcTemplatePath->GetValue();
434
435 // Block all events to the notebook and its children
436 wxEventBlocker blocker( m_notebook );
437
438 // Detach webview from current panel before deleting it
439 if( (unsigned)page < m_panels.size() )
440 {
441 m_panels[page]->m_SizerBase->Detach( m_webviewPanel );
442 m_webviewPanel->Reparent( this ); // Reparent to dialog
443 m_webviewPanel->Hide(); // Hide webview panel temporarily
444 }
445
446 m_notebook->DeletePage( page );
447
449 m_panels[page] = tpanel;
450 m_notebook->InsertPage( page, tpanel, title, true );
451
452 // Reparent and add webview back to the new panel
453 m_webviewPanel->Reparent( tpanel );
454 m_webviewPanel->Show();
455 tpanel->m_SizerBase->Add( m_webviewPanel, 1, wxEXPAND | wxALL, 5 );
456
457 buildPageContent( m_tcTemplatePath->GetValue(), page );
458
459 m_selectedWidget = nullptr;
460 // Reset to welcome page after rebuilding
462
463 Layout();
464}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Class DIALOG_TEMPLATE_SELECTOR_BASE.
void AddTemplate(int aPage, PROJECT_TEMPLATE *aTemplate)
DIALOG_TEMPLATE_SELECTOR(wxWindow *aParent, const wxPoint &aPos, const wxSize &aSize, std::map< wxString, wxFileName > aTitleDirMap)
void OnPageChange(wxNotebookEvent &event) override
void onReload(wxCommandEvent &event) override
void onDirectoryBrowseClicked(wxCommandEvent &event) override
PROJECT_TEMPLATE * GetSelectedTemplate()
void SetWidget(TEMPLATE_WIDGET *aWidget)
void buildPageContent(const wxString &aPath, int aPage)
std::vector< TEMPLATE_SELECTION_PANEL * > m_panels
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)
void SetBitmap(const wxBitmapBundle &aBmp)
Class TEMPLATE_SELECTION_PANEL_BASE.
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)
Class TEMPLATE_WIDGET_BASE.
PROJECT_TEMPLATE * GetTemplate()
void OnMouse(wxMouseEvent &event)
void SetTemplate(PROJECT_TEMPLATE *aTemplate)
Set the project template for this widget, which will determine the icon and title associated with thi...
DIALOG_TEMPLATE_SELECTOR * m_dialog
TEMPLATE_WIDGET(wxWindow *aParent, DIALOG_TEMPLATE_SELECTOR *aDialog)
PROJECT_TEMPLATE * m_currTemplate
void SetPage(const wxString &htmlContent)
void LoadURL(const wxString &url)
static const wxString GetWelcomeHtml()
#define _(s)
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:156
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
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().
Definition: wx_filename.h:39