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  event.Skip();
119 }
120 
121 
124 {
125  m_htmlWin->SetPage( _( "<html><h1>Template Selector</h1></html>" ) );
126  m_notebook->Connect( wxEVT_SIZE,
127  wxSizeEventHandler( DIALOG_TEMPLATE_SELECTOR::onNotebookResize ),
128  NULL, this );
129  m_selectedWidget = NULL;
130 }
131 
132 
134 {
135  if( m_selectedWidget != NULL )
137 
138  m_selectedWidget = aWidget;
140 }
141 
142 
144 {
145  TEMPLATE_WIDGET* w = new TEMPLATE_WIDGET( m_panels[aPage]->m_scrolledWindow, this );
146  w->SetTemplate( aTemplate );
147 
148  m_panels[aPage]->m_SizerChoice->Add( w );
149  m_panels[aPage]->m_SizerChoice->Layout();
150  m_panels[aPage]->SetSize( m_notebook->GetSize().GetWidth() - 6, 140 );
151  m_panels[aPage]->m_SizerBase->FitInside( m_panels[aPage] );
152  m_panels[aPage]->m_scrolledWindow->SetSize( m_panels[aPage]->GetSize().GetWidth() - 6,
153  m_panels[aPage]->GetSize().GetHeight() - 6 );
154  m_panels[aPage]->m_SizerChoice->FitInside( m_panels[aPage]->m_scrolledWindow );
155 
156  m_notebook->Refresh();
157 }
158 
159 
161 {
163 }
164 
165 
166 void DIALOG_TEMPLATE_SELECTOR::AddTemplatesPage( const wxString& aTitle, wxFileName& aPath )
167 {
168  wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
169 
170  aPath.Normalize();
171  wxString path = aPath.GetFullPath(); // caller ensures this ends with file separator.
172 
173  TEMPLATE_SELECTION_PANEL* tpanel = new TEMPLATE_SELECTION_PANEL( newPage, path );
174  m_panels.push_back( tpanel );
175 
176  m_notebook->AddPage( newPage, aTitle );
177 
178  if( m_notebook->GetPageCount() == 1 )
179  m_tcTemplatePath->SetValue( path );
180 
181  buildPageContent( path, m_notebook->GetPageCount() - 1 );
182 }
183 
184 
185 void DIALOG_TEMPLATE_SELECTOR::buildPageContent( const wxString& aPath, int aPage )
186 {
187  // Get a list of files under the template path to include as choices...
188  wxDir dir;
189 
190  if( dir.Open( aPath ) )
191  {
192  if( dir.HasSubDirs( "meta" ) )
193  {
194  AddTemplate( aPage, new PROJECT_TEMPLATE( aPath ) );
195  }
196  else
197  {
198  wxDir sub_dir;
199  wxString sub_name;
200 
201  bool cont = dir.GetFirst( &sub_name, wxEmptyString, wxDIR_DIRS );
202 
203  while( cont )
204  {
205  wxString sub_full = aPath + sub_name;
206 
207  if( sub_dir.Open( sub_full ) )
208  {
209  AddTemplate( aPage, new PROJECT_TEMPLATE( sub_full ) );
210  }
211 
212  cont = dir.GetNext( &sub_name );
213  }
214  }
215  }
216 }
217 
218 
220 {
221  wxFileName fn;
222  fn.AssignDir( m_tcTemplatePath->GetValue() );
223  fn.Normalize();
224  wxString currPath = fn.GetFullPath();
225 
226  wxDirDialog dirDialog( this, _( "Select Templates Directory" ),
227  currPath,
228  wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
229 
230  if( dirDialog.ShowModal() != wxID_OK )
231  return;
232 
233  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
234 
235  m_tcTemplatePath->SetValue( dirName.GetFullPath() );
236 
237  // Rebuild the page from the new templates path:
239 }
240 
241 
242 void DIALOG_TEMPLATE_SELECTOR::onValidatePath( wxCommandEvent& event )
243 {
244  int page = m_notebook->GetSelection();
245 
246  if( page < 0 )
247  return; // Should not happen
248 
249  wxString currPath = m_tcTemplatePath->GetValue();
250 
251  wxFileName fn;
252  fn.AssignDir( m_tcTemplatePath->GetValue() );
253  fn.Normalize();
254  currPath = fn.GetFullPath();
255  m_tcTemplatePath->SetValue( currPath );
256 
258 }
259 
260 
262 {
263  // Rebuild the page from the new templates path:
264  int page = m_notebook->GetSelection();
265 
266  if( page < 0 )
267  return; // Should not happen
268 
269  wxString title = m_notebook->GetPageText( page );
270  wxString currPath = m_tcTemplatePath->GetValue();
271 
272  m_notebook->DeletePage( page );
273 
274  wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
275  TEMPLATE_SELECTION_PANEL* tpanel = new TEMPLATE_SELECTION_PANEL( newPage, currPath );
276  m_panels[page] = tpanel;
277  m_notebook->InsertPage( page, newPage, title, true );
278 
279  buildPageContent( m_tcTemplatePath->GetValue(), page );
280 
281  m_selectedWidget = NULL;
282 }
283 
284 
285 void DIALOG_TEMPLATE_SELECTOR::OnHtmlLinkActivated( wxHtmlLinkEvent& event )
286 {
287  wxString url = event.GetLinkInfo().GetHref();
288  wxLaunchDefaultBrowser( url );
289 }
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
#define _(s)
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.