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-2012 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, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ), NULL, this );
50  m_staticTitle->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ), NULL, this );
51 
52  // We're not selected until we're clicked
53  Unselect();
54 
55  // Start with template being NULL
56  m_currTemplate = NULL;
57 }
58 
59 
61 {
62  m_dialog->SetWidget( this );
63  SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNHIGHLIGHT ) );
64  m_selected = true;
65  Refresh();
66 }
67 
68 
70 {
71  SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
72  m_selected = false;
73  Refresh();
74 }
75 
76 
78 {
79  m_currTemplate = aTemplate;
80  m_staticTitle->SetLabel( *(aTemplate->GetTitle()) );
81  m_bitmapIcon->SetBitmap( *(aTemplate->GetIcon()) );
82 }
83 
84 
85 void TEMPLATE_WIDGET::OnMouse( wxMouseEvent& event )
86 {
87  // Toggle selection here
88  Select();
89  event.Skip();
90 }
91 
92 
94 {
95  for( size_t i=0; i < m_notebook->GetPageCount(); i++ )
96  {
97  m_panels[i]->SetSize( m_notebook->GetSize().GetWidth() - 6, 140 );
98  m_panels[i]->m_SizerBase->FitInside( m_panels[i] );
99  m_panels[i]->m_scrolledWindow->SetSize( m_panels[i]->GetSize().GetWidth() - 6,
100  m_panels[i]->GetSize().GetHeight() - 6 );
101  m_panels[i]->m_SizerChoice->FitInside( m_panels[i]->m_scrolledWindow );
102  }
103  m_notebook->Refresh();
104 
105  event.Skip();
106 }
107 
108 void DIALOG_TEMPLATE_SELECTOR::OnPageChange( wxNotebookEvent& event )
109 {
110  int page = m_notebook->GetSelection();
111 
112  if( page != wxNOT_FOUND && (unsigned)page < m_panels.size() )
113  m_tcTemplatePath->SetValue( m_panels[page]->GetPath() );
114 }
115 
116 
119 {
120  m_htmlWin->SetPage( _( "<html><h1>Template Selector</h1></html>" ) );
121  m_notebook->Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_TEMPLATE_SELECTOR::onNotebookResize ), NULL, this );
122  m_selectedWidget = NULL;
123 }
124 
125 
127 {
128  if( m_selectedWidget != NULL )
130 
131  m_selectedWidget = aWidget;
133 }
134 
136 {
137  TEMPLATE_WIDGET* w = new TEMPLATE_WIDGET( m_panels[aPage]->m_scrolledWindow, this );
138  w->SetTemplate( aTemplate );
139 
140  m_panels[aPage]->m_SizerChoice->Add( w );
141  m_panels[aPage]->m_SizerChoice->Layout();
142  m_panels[aPage]->SetSize( m_notebook->GetSize().GetWidth() - 6, 140 );
143  m_panels[aPage]->m_SizerBase->FitInside( m_panels[aPage] );
144  m_panels[aPage]->m_scrolledWindow->SetSize( m_panels[aPage]->GetSize().GetWidth() - 6,
145  m_panels[aPage]->GetSize().GetHeight() - 6 );
146  m_panels[aPage]->m_SizerChoice->FitInside( m_panels[aPage]->m_scrolledWindow );
147 
148  m_notebook->Refresh();
149 }
150 
151 
153 {
155 }
156 
157 void DIALOG_TEMPLATE_SELECTOR::AddTemplatesPage( const wxString& aTitle, wxFileName& aPath )
158 {
159  wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
160 
161  aPath.Normalize();
162  wxString path = aPath.GetFullPath(); // caller ensures this ends with file separator.
163 
164  TEMPLATE_SELECTION_PANEL* tpanel = new TEMPLATE_SELECTION_PANEL( newPage, path );
165  m_panels.push_back( tpanel );
166 
167  m_notebook->AddPage( newPage, aTitle );
168 
169  if( m_notebook->GetPageCount() == 1 )
170  m_tcTemplatePath->SetValue( path );
171 
172  buildPageContent( path, m_notebook->GetPageCount() - 1 );
173 }
174 
175 void DIALOG_TEMPLATE_SELECTOR::buildPageContent( const wxString& aPath, int aPage )
176 {
177  // Get a list of files under the template path to include as choices...
178  wxArrayString files;
179  wxDir dir;
180 
181  if( dir.Open( aPath ) )
182  {
183  wxDir sub_dir;
184  wxString sub_name;
185 
186  bool cont = dir.GetFirst( &sub_name, wxEmptyString, wxDIR_DIRS );
187  while( cont )
188  {
189  wxString sub_full = aPath + sub_name;
190  if( sub_dir.Open( sub_full ) )
191  {
192  files.Add( sub_name );
193 
194  PROJECT_TEMPLATE* pt = new PROJECT_TEMPLATE( sub_full );
195  AddTemplate( aPage, pt );
196  }
197 
198  cont = dir.GetNext( &sub_name );
199  }
200  }
201 }
202 
203 
205 {
206  wxFileName fn;
207  fn.AssignDir( m_tcTemplatePath->GetValue() );
208  fn.Normalize();
209  wxString currPath = fn.GetFullPath();
210 
211  wxDirDialog dirDialog( this, _( "Select Templates Directory" ),
212  currPath,
213  wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
214 
215  if( dirDialog.ShowModal() != wxID_OK )
216  return;
217 
218  wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
219 
220  m_tcTemplatePath->SetValue( dirName.GetFullPath() );
221 
222  if( currPath == m_tcTemplatePath->GetValue() )
223  return; // No change
224 
225  // Rebuild the page from the new templates path:
227 }
228 
229 
230 void DIALOG_TEMPLATE_SELECTOR::onValidatePath( wxCommandEvent& event )
231 {
232  int page = m_notebook->GetSelection();
233 
234  if( page < 0 )
235  return; // Should not happen
236 
237  wxString currPath = m_tcTemplatePath->GetValue();
238 
239  if( currPath == m_panels[page]->GetPath() ) // No change
240  return;
241 
242  wxFileName fn;
243  fn.AssignDir( m_tcTemplatePath->GetValue() );
244  fn.Normalize();
245  currPath = fn.GetFullPath();
246  m_tcTemplatePath->SetValue( currPath );
247 
249 }
250 
251 
253 {
254  // Rebuild the page from the new templates path:
255  int page = m_notebook->GetSelection();
256 
257  if( page < 0 )
258  return; // Should not happen
259 
260  wxString title = m_notebook->GetPageText( page );
261  wxString currPath = m_tcTemplatePath->GetValue();
262 
263  m_notebook->DeletePage( page );
264 
265  wxNotebookPage* newPage = new wxNotebookPage( m_notebook, wxID_ANY );
266  TEMPLATE_SELECTION_PANEL* tpanel = new TEMPLATE_SELECTION_PANEL( newPage, currPath );
267  m_panels[page] = tpanel;
268  m_notebook->InsertPage( page, newPage, title, true );
269 
270  buildPageContent( m_tcTemplatePath->GetValue(), page );
271 
272  m_selectedWidget = NULL;
273 }
274 
275 
276 void DIALOG_TEMPLATE_SELECTOR::OnHtmlLinkActivated( wxHtmlLinkEvent& event )
277 {
278  wxString url = event.GetLinkInfo().GetHref();
279  wxLaunchDefaultBrowser( url);
280 }
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()
void AddTemplate(int aPage, PROJECT_TEMPLATE *aTemplate)
void onValidatePath(wxCommandEvent &event) override
void SetHtml(wxFileName aFilename)
wxString * GetTitle()
Get the title of the project (extracted from the html title tag)
Class DIALOG_TEMPLATE_SELECTOR_BASE.
DIALOG_TEMPLATE_SELECTOR * m_dialog
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)
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.