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 (C) 1992-2022 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
34
36 const wxString& aPath ) :
38{
39 m_parent = aParent;
40 m_templatesPath = aPath;
41 m_minHeight = 0;
42}
43
44
46{
47 m_SizerChoice->Add( aTemplateWidget );
48 int height = aTemplateWidget->GetBestSize().GetHeight();
49 m_minHeight = std::max( m_minHeight, height );
50}
51
52
54 TEMPLATE_WIDGET_BASE( aParent )
55{
56 m_parent = aParent;
57 m_dialog = aDialog;
58
59 // wxWidgets_3.xx way of doing the same...
60 // Bind(wxEVT_LEFT_DOWN, &TEMPLATE_WIDGET::OnMouse, this );
61
62 m_bitmapIcon->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ),
63 nullptr, this );
64 m_staticTitle->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ),
65 nullptr, this );
66
67 // We're not selected until we're clicked
68 Unselect();
69
70 // Start with template being NULL
71 m_currTemplate = nullptr;
72}
73
74
76{
77 m_dialog->SetWidget( this );
78 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNHIGHLIGHT ) );
79 m_selected = true;
80 Refresh();
81}
82
83
85{
86 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
87 m_selected = false;
88 Refresh();
89}
90
91
93{
94 m_currTemplate = aTemplate;
95 m_staticTitle->SetLabel( *aTemplate->GetTitle() );
96 m_staticTitle->SetFont( KIUI::GetInfoFont( this ) );
97 m_staticTitle->Wrap( 100 );
98 m_bitmapIcon->SetBitmap( *aTemplate->GetIcon() );
99}
100
101
102void TEMPLATE_WIDGET::OnMouse( wxMouseEvent& event )
103{
104 // Toggle selection here
105 Select();
106 event.Skip();
107}
108
109
111{
112 // Ensure all panels have the full available width:
113 for( size_t i = 0; i < m_notebook->GetPageCount(); i++ )
114 {
115 // Gives a little margin for panel horizontal size, especially to show the
116 // full scroll bars of wxScrolledWindow
117 // Fix me if a better way exists
118 const int h_margin = 10;
119 const int v_margin = 22;
120
121 int max_width = m_notebook->GetClientSize().GetWidth() - h_margin;
122 int min_height = m_panels[i]->GetMinHeight() + v_margin;
123 m_panels[i]->SetSize( max_width, std::max( m_panels[i]->GetSize().GetY(), min_height ) );
124 m_panels[i]->SetMinSize( wxSize( -1, min_height ) );
125 }
126
127 Refresh();
128
129 event.Skip();
130}
131
132
133void DIALOG_TEMPLATE_SELECTOR::OnPageChange( wxNotebookEvent& event )
134{
135 int page = event.GetSelection();
136
137 if( page != wxNOT_FOUND && (unsigned)page < m_panels.size() )
138 m_tcTemplatePath->SetValue( m_panels[page]->GetPath() );
139
140 event.Skip();
141}
142
143
144DIALOG_TEMPLATE_SELECTOR::DIALOG_TEMPLATE_SELECTOR( wxWindow* aParent, const wxPoint& aPos, const wxSize& aSize ) :
145 DIALOG_TEMPLATE_SELECTOR_BASE( aParent, wxID_ANY, wxEmptyString, aPos, aSize )
146{
147 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
148 m_reloadButton->SetBitmap( KiBitmapBundle( BITMAPS::small_refresh ) );
149
150 m_htmlWin->SetPage( _( "<h1>Template Selector</h1>" ) );
151 m_notebook->Connect( wxEVT_SIZE,
152 wxSizeEventHandler( DIALOG_TEMPLATE_SELECTOR::onNotebookResize ),
153 nullptr, this );
154 m_selectedWidget = nullptr;
155}
156
157
159{
160 if( m_selectedWidget != nullptr )
162
163 m_selectedWidget = aWidget;
165}
166
167
169{
170 TEMPLATE_WIDGET* w = new TEMPLATE_WIDGET( m_panels[aPage]->m_scrolledWindow, this );
171 w->SetTemplate( aTemplate );
172 m_panels[aPage]->AddTemplateWidget( w );
173
174 m_notebook->Refresh();
175}
176
177
179{
180 return m_selectedWidget? m_selectedWidget->GetTemplate() : nullptr;
181}
182
183
184void DIALOG_TEMPLATE_SELECTOR::AddTemplatesPage( const wxString& aTitle, wxFileName& aPath )
185{
186 wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
187
188 aPath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
189 wxString path = aPath.GetFullPath(); // caller ensures this ends with file separator.
190
192 m_panels.push_back( tpanel );
193
194 m_notebook->AddPage( newPage, aTitle );
195
196 if( m_notebook->GetPageCount() == 1 )
197 m_tcTemplatePath->SetValue( path );
198
199 buildPageContent( path, m_notebook->GetPageCount() - 1 );
200
201 // Ensure m_notebook has a minimal height to show the template widgets:
202 // and add a margin for scroll bars and decorations
203 // FIX ME: find a better way to allow space for these items: the value works on MSW
204 // but is too big on GTK. But I did not find a better way (JPC)
205 const int margin = 50;
206 int min_height = tpanel->GetMinHeight() + margin;
207
208 if( m_notebook->GetMinClientSize().GetHeight() < min_height )
209 m_notebook->SetMinClientSize( wxSize( -1, min_height ) );
210}
211
212
213void DIALOG_TEMPLATE_SELECTOR::buildPageContent( const wxString& aPath, int aPage )
214{
215 // Get a list of files under the template path to include as choices...
216 wxDir dir;
217
218 if( dir.Open( aPath ) )
219 {
220 if( dir.HasSubDirs( "meta" ) )
221 {
222 AddTemplate( aPage, new PROJECT_TEMPLATE( aPath ) );
223 }
224 else
225 {
226 wxDir sub_dir;
227 wxString sub_name;
228
229 bool cont = dir.GetFirst( &sub_name, wxEmptyString, wxDIR_DIRS );
230
231 while( cont )
232 {
233 wxString sub_full = aPath + sub_name;
234
235 if( sub_dir.Open( sub_full ) )
236 AddTemplate( aPage, new PROJECT_TEMPLATE( sub_full ) );
237
238 cont = dir.GetNext( &sub_name );
239 }
240 }
241 }
242
243 wxSizeEvent dummy;
245}
246
247
249{
250 wxFileName fn;
251 fn.AssignDir( m_tcTemplatePath->GetValue() );
252 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
253 wxString currPath = fn.GetFullPath();
254
255 wxDirDialog dirDialog( this, _( "Select Templates Directory" ), currPath,
256 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
257
258 if( dirDialog.ShowModal() != wxID_OK )
259 return;
260
261 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
262
263 m_tcTemplatePath->SetValue( dirName.GetFullPath() );
264
265 // Rebuild the page from the new templates path:
267}
268
269
270void DIALOG_TEMPLATE_SELECTOR::onReload( wxCommandEvent& event )
271{
272 int page = m_notebook->GetSelection();
273
274 if( page < 0 )
275 return; // Should not happen
276
277 wxString currPath = m_tcTemplatePath->GetValue();
278
279 wxFileName fn;
280 fn.AssignDir( m_tcTemplatePath->GetValue() );
281 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
282 currPath = fn.GetFullPath();
283 m_tcTemplatePath->SetValue( currPath );
284
286}
287
288
290{
291 // Rebuild the page from the new templates path:
292 int page = m_notebook->GetSelection();
293
294 if( page < 0 )
295 return; // Should not happen
296
297 wxString title = m_notebook->GetPageText( page );
298 wxString currPath = m_tcTemplatePath->GetValue();
299
300 m_notebook->DeletePage( page );
301
302 wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
303 TEMPLATE_SELECTION_PANEL* tpanel = new TEMPLATE_SELECTION_PANEL( newPage, currPath );
304 m_panels[page] = tpanel;
305 m_notebook->InsertPage( page, newPage, title, true );
306
307 buildPageContent( m_tcTemplatePath->GetValue(), page );
308
309 m_selectedWidget = nullptr;
310 PostSizeEvent(); // A easy way to force refresh displays
311}
312
313
315{
316 wxString url = event.GetLinkInfo().GetHref();
317 wxLaunchDefaultBrowser( url );
318}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Class DIALOG_TEMPLATE_SELECTOR_BASE.
void AddTemplate(int aPage, PROJECT_TEMPLATE *aTemplate)
void OnPageChange(wxNotebookEvent &event) override
void AddTemplatesPage(const wxString &aTitle, wxFileName &aPath)
Add a new page with aTitle, populated with templates from aPath.
void onReload(wxCommandEvent &event) override
void SetHtml(const wxFileName &aFilename)
void onDirectoryBrowseClicked(wxCommandEvent &event) override
void onNotebookResize(wxSizeEvent &event)
DIALOG_TEMPLATE_SELECTOR(wxWindow *aParent, const wxPoint &aPos, const wxSize &aSize)
PROJECT_TEMPLATE * GetSelectedTemplate()
void SetWidget(TEMPLATE_WIDGET *aWidget)
void buildPageContent(const wxString &aPath, int aPage)
void OnHtmlLinkActivated(wxHtmlLinkEvent &event) override
std::vector< TEMPLATE_SELECTION_PANEL * > m_panels
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:50
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)
int m_minHeight
minimal height to show templates (this is the height of the biggest template widget)
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
#define _(s)
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:154
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
std::vector< FAB_LAYER_COLOR > dummy
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