KiCad PCB EDA Suite
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 <brian.sidebotham@gmail.com>
5  * Copyright (C) 1992-2018 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 
27 #include <wx/dir.h>
28 #include <wx/settings.h>
29 
30 
32  const wxString& aPath ) :
34 {
35  m_parent = aParent;
36  m_templatesPath = aPath;
37 }
38 
39 
41  TEMPLATE_WIDGET_BASE( aParent )
42 {
43  m_parent = aParent;
44  m_dialog = aDialog;
45 
46  // wxWidgets_3.xx way of doing the same...
47  // Bind(wxEVT_LEFT_DOWN, &TEMPLATE_WIDGET::OnMouse, this );
48 
49  m_bitmapIcon->Connect( wxEVT_LEFT_DOWN,
50  wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ), NULL, this );
51  m_staticTitle->Connect( wxEVT_LEFT_DOWN,
52  wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ), NULL, this );
53 
54  // We're not selected until we're clicked
55  Unselect();
56 
57  // Start with template being NULL
58  m_currTemplate = NULL;
59 }
60 
61 
63 {
64  m_dialog->SetWidget( this );
65  SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNHIGHLIGHT ) );
66  m_selected = true;
67  Refresh();
68 }
69 
70 
72 {
73  SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
74  m_selected = false;
75  Refresh();
76 }
77 
78 
80 {
81  m_currTemplate = aTemplate;
82  m_staticTitle->SetLabel( *(aTemplate->GetTitle()) );
83  m_bitmapIcon->SetBitmap( *(aTemplate->GetIcon()) );
84 }
85 
86 
87 void TEMPLATE_WIDGET::OnMouse( wxMouseEvent& event )
88 {
89  // Toggle selection here
90  Select();
91  event.Skip();
92 }
93 
94 
96 {
97  for( size_t i=0; i < m_notebook->GetPageCount(); i++ )
98  {
99  m_panels[i]->SetSize( m_notebook->GetSize().GetWidth() - 6, 140 );
100  m_panels[i]->m_SizerBase->FitInside( m_panels[i] );
101  m_panels[i]->m_scrolledWindow->SetSize( m_panels[i]->GetSize().GetWidth() - 6,
102  m_panels[i]->GetSize().GetHeight() - 6 );
103  m_panels[i]->m_SizerChoice->FitInside( m_panels[i]->m_scrolledWindow );
104  }
105  m_notebook->Refresh();
106 
107  event.Skip();
108 }
109 
110 
111 void DIALOG_TEMPLATE_SELECTOR::OnPageChange( wxNotebookEvent& event )
112 {
113  int page = event.GetSelection();
114 
115  if( page != wxNOT_FOUND && (unsigned)page < m_panels.size() )
116  m_tcTemplatePath->SetValue( m_panels[page]->GetPath() );
117 }
118 
119 
122 {
123  m_htmlWin->SetPage( _( "<html><h1>Template Selector</h1></html>" ) );
124  m_notebook->Connect( wxEVT_SIZE,
125  wxSizeEventHandler( DIALOG_TEMPLATE_SELECTOR::onNotebookResize ),
126  NULL, this );
127  m_selectedWidget = NULL;
128 }
129 
130 
132 {
133  if( m_selectedWidget != NULL )
135 
136  m_selectedWidget = aWidget;
138 }
139 
140 
142 {
143  TEMPLATE_WIDGET* w = new TEMPLATE_WIDGET( m_panels[aPage]->m_scrolledWindow, this );
144  w->SetTemplate( aTemplate );
145 
146  m_panels[aPage]->m_SizerChoice->Add( w );
147  m_panels[aPage]->m_SizerChoice->Layout();
148  m_panels[aPage]->SetSize( m_notebook->GetSize().GetWidth() - 6, 140 );
149  m_panels[aPage]->m_SizerBase->FitInside( m_panels[aPage] );
150  m_panels[aPage]->m_scrolledWindow->SetSize( m_panels[aPage]->GetSize().GetWidth() - 6,
151  m_panels[aPage]->GetSize().GetHeight() - 6 );
152  m_panels[aPage]->m_SizerChoice->FitInside( m_panels[aPage]->m_scrolledWindow );
153 
154  m_notebook->Refresh();
155 }
156 
157 
159 {
161 }
162 
163 
164 void DIALOG_TEMPLATE_SELECTOR::AddTemplatesPage( const wxString& aTitle, wxFileName& aPath )
165 {
166  wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
167 
168  aPath.Normalize();
169  wxString path = aPath.GetFullPath(); // caller ensures this ends with file separator.
170 
171  TEMPLATE_SELECTION_PANEL* tpanel = new TEMPLATE_SELECTION_PANEL( newPage, path );
172  m_panels.push_back( tpanel );
173 
174  m_notebook->AddPage( newPage, aTitle );
175 
176  if( m_notebook->GetPageCount() == 1 )
177  m_tcTemplatePath->SetValue( path );
178 
179  buildPageContent( path, m_notebook->GetPageCount() - 1 );
180 }
181 
182 
183 void DIALOG_TEMPLATE_SELECTOR::buildPageContent( const wxString& aPath, int aPage )
184 {
185  // Get a list of files under the template path to include as choices...
186  wxDir dir;
187 
188  if( dir.Open( aPath ) )
189  {
190  if( dir.HasSubDirs( "meta" ) )
191  {
192  AddTemplate( aPage, new PROJECT_TEMPLATE( aPath ) );
193  }
194  else
195  {
196  wxDir sub_dir;
197  wxString sub_name;
198 
199  bool cont = dir.GetFirst( &sub_name, wxEmptyString, wxDIR_DIRS );
200 
201  while( cont )
202  {
203  wxString sub_full = aPath + sub_name;
204 
205  if( sub_dir.Open( sub_full ) )
206  {
207  AddTemplate( aPage, new PROJECT_TEMPLATE( sub_full ) );
208  }
209 
210  cont = dir.GetNext( &sub_name );
211  }
212  }
213  }
214 }
215 
216 
218 {
219  wxFileName fn;
220  fn.AssignDir( m_tcTemplatePath->GetValue() );
221  fn.Normalize();
222  wxString currPath = fn.GetFullPath();
223 
224  wxDirDialog dirDialog( this, _( "Select Templates Directory" ),
225  currPath,
226  wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
227 
228  if( dirDialog.ShowModal() != wxID_OK )
229  return;
230 
231  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
232 
233  m_tcTemplatePath->SetValue( dirName.GetFullPath() );
234 
235  // Rebuild the page from the new templates path:
237 }
238 
239 
240 void DIALOG_TEMPLATE_SELECTOR::onValidatePath( wxCommandEvent& event )
241 {
242  int page = m_notebook->GetSelection();
243 
244  if( page < 0 )
245  return; // Should not happen
246 
247  wxString currPath = m_tcTemplatePath->GetValue();
248 
249  wxFileName fn;
250  fn.AssignDir( m_tcTemplatePath->GetValue() );
251  fn.Normalize();
252  currPath = fn.GetFullPath();
253  m_tcTemplatePath->SetValue( currPath );
254 
256 }
257 
258 
260 {
261  // Rebuild the page from the new templates path:
262  int page = m_notebook->GetSelection();
263 
264  if( page < 0 )
265  return; // Should not happen
266 
267  wxString title = m_notebook->GetPageText( page );
268  wxString currPath = m_tcTemplatePath->GetValue();
269 
270  m_notebook->DeletePage( page );
271 
272  wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
273  TEMPLATE_SELECTION_PANEL* tpanel = new TEMPLATE_SELECTION_PANEL( newPage, currPath );
274  m_panels[page] = tpanel;
275  m_notebook->InsertPage( page, newPage, title, true );
276 
277  buildPageContent( m_tcTemplatePath->GetValue(), page );
278 
279  m_selectedWidget = NULL;
280 }
281 
282 
283 void DIALOG_TEMPLATE_SELECTOR::OnHtmlLinkActivated( wxHtmlLinkEvent& event )
284 {
285  wxString url = event.GetLinkInfo().GetHref();
286  wxLaunchDefaultBrowser( url );
287 }
TEMPLATE_WIDGET(wxWindow *aParent, DIALOG_TEMPLATE_SELECTOR *aDialog)
void buildPageContent(const wxString &aPath, int aPage)
void OnPageChange(wxNotebookEvent &event) override
PROJECT_TEMPLATE * GetSelectedTemplate()
void onNotebookResize(wxSizeEvent &event)
wxFileName GetHtmlFile()
Get the full Html filename for the project template.
void OnHtmlLinkActivated(wxHtmlLinkEvent &event) override
DIALOG_TEMPLATE_SELECTOR(wxWindow *aParent)
void onDirectoryBrowseClicked(wxCommandEvent &event) override
A class which provides project template functionality.
std::vector< TEMPLATE_SELECTION_PANEL * > m_panels
PROJECT_TEMPLATE * GetTemplate()
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void AddTemplate(int aPage, PROJECT_TEMPLATE *aTemplate)
void onValidatePath(wxCommandEvent &event) override
wxString * GetTitle()
Get the title of the project (extracted from the html title tag)
Class DIALOG_TEMPLATE_SELECTOR_BASE.
DIALOG_TEMPLATE_SELECTOR * m_dialog
void SetHtml(const wxFileName &aFilename)
Class TEMPLATE_WIDGET_BASE.
Class TEMPLATE_SELECTION_PANEL_BASE.
PROJECT_TEMPLATE * m_currTemplate
TEMPLATE_SELECTION_PANEL(wxNotebookPage *aParent, const wxString &aPath)
void SetTemplate(PROJECT_TEMPLATE *aTemplate)
Set the project template for this widget, which will determine the icon and title associated with thi...
void SetWidget(TEMPLATE_WIDGET *aWidget)
size_t i
Definition: json11.cpp:597
void AddTemplatesPage(const wxString &aTitle, wxFileName &aPath)
Add a new page with aTitle, populated with templates from aPath.
wxString m_templatesPath
the path to access to the folder containing the templates (which are also folders) ...
void OnMouse(wxMouseEvent &event)
wxBitmap * GetIcon()
Get the 64px^2 icon for the project template.