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
34
36 const wxString& aPath ) :
38{
39 m_parent = aParent;
40 m_templatesPath = aPath;
41}
42
43
45{
46 m_SizerChoice->Add( aTemplateWidget );
47 Layout();
48}
49
50
52 TEMPLATE_WIDGET_BASE( aParent )
53{
54 m_parent = aParent;
55 m_dialog = aDialog;
56
57 // wxWidgets_3.xx way of doing the same...
58 // Bind(wxEVT_LEFT_DOWN, &TEMPLATE_WIDGET::OnMouse, this );
59
60 m_bitmapIcon->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ),
61 nullptr, this );
62 m_staticTitle->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( TEMPLATE_WIDGET::OnMouse ),
63 nullptr, this );
64
65 // We're not selected until we're clicked
66 Unselect();
67
68 // Start with template being NULL
69 m_currTemplate = nullptr;
70}
71
72
74{
75 m_dialog->SetWidget( this );
76 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNHIGHLIGHT ) );
77 m_selected = true;
78 Refresh();
79}
80
81
83{
84 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
85 m_selected = false;
86 Refresh();
87}
88
89
91{
92 m_currTemplate = aTemplate;
93 m_staticTitle->SetFont( KIUI::GetInfoFont( this ) );
94 m_staticTitle->SetLabel( *aTemplate->GetTitle() );
95 m_staticTitle->Wrap( 100 );
96 m_bitmapIcon->SetBitmap( *aTemplate->GetIcon() );
97}
98
99
100void TEMPLATE_WIDGET::OnMouse( wxMouseEvent& event )
101{
102 // Toggle selection here
103 Select();
104 event.Skip();
105}
106
107
108void DIALOG_TEMPLATE_SELECTOR::OnPageChange( wxNotebookEvent& event )
109{
110 int page = event.GetSelection();
111
112 if( page != wxNOT_FOUND && (unsigned)page < m_panels.size() )
113 m_tcTemplatePath->SetValue( m_panels[page]->GetPath() );
114
115 event.Skip();
116}
117
118
119DIALOG_TEMPLATE_SELECTOR::DIALOG_TEMPLATE_SELECTOR( wxWindow* aParent, const wxPoint& aPos,
120 const wxSize& aSize,
121 std::map<wxString, wxFileName> aTitleDirMap ) :
122 DIALOG_TEMPLATE_SELECTOR_BASE( aParent, wxID_ANY, _( "Project Template Selector" ), aPos,
123 aSize )
124{
125 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
126 m_reloadButton->SetBitmap( KiBitmapBundle( BITMAPS::small_refresh ) );
127
128 m_htmlWin->SetPage( _( "<h1>Template Selector</h1>" ) );
129 m_selectedWidget = nullptr;
130
131 for( auto& [title, pathFname] : aTitleDirMap )
132 {
133 pathFname.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
134 wxString path = pathFname.GetFullPath(); // caller ensures this ends with file separator.
135
137 m_panels.push_back( tpanel );
138
139 m_notebook->AddPage( tpanel, title );
140
141 if( m_notebook->GetPageCount() == 1 )
142 m_tcTemplatePath->SetValue( path );
143
144 buildPageContent( path, m_notebook->GetPageCount() - 1 );
145 }
146
147 // When all widgets have the size fixed, call finishDialogSettings to update sizers
149}
150
151
153{
154 if( m_selectedWidget != nullptr )
156
157 m_selectedWidget = aWidget;
159}
160
161
163{
164 TEMPLATE_WIDGET* w = new TEMPLATE_WIDGET( m_panels[aPage]->m_scrolledWindow, this );
165 w->SetTemplate( aTemplate );
166 m_panels[aPage]->AddTemplateWidget( w );
167}
168
169
171{
172 return m_selectedWidget? m_selectedWidget->GetTemplate() : nullptr;
173}
174
175
176void DIALOG_TEMPLATE_SELECTOR::buildPageContent( const wxString& aPath, int aPage )
177{
178 // Get a list of files under the template path to include as choices...
179 wxDir dir;
180
181 if( dir.Open( aPath ) )
182 {
183 if( dir.HasSubDirs( "meta" ) )
184 {
185 AddTemplate( aPage, new PROJECT_TEMPLATE( aPath ) );
186 }
187 else
188 {
189 wxString sub_name;
190 wxArrayString subdirs;
191
192 bool cont = dir.GetFirst( &sub_name, wxEmptyString, wxDIR_DIRS );
193
194 while( cont )
195 {
196 subdirs.Add( wxString( sub_name ) );
197 cont = dir.GetNext( &sub_name );
198 }
199
200 if( !subdirs.IsEmpty() )
201 subdirs.Sort();
202
203 for( const wxString& dir_name : subdirs )
204 {
205 wxDir sub_dir;
206 wxString sub_full = aPath + dir_name;
207
208 if( sub_dir.Open( sub_full ) )
209 AddTemplate( aPage, new PROJECT_TEMPLATE( sub_full ) );
210 }
211 }
212 }
213
214 Layout();
215}
216
217
219{
220 wxFileName fn;
221 fn.AssignDir( m_tcTemplatePath->GetValue() );
222 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
223 wxString currPath = fn.GetFullPath();
224
225 wxDirDialog dirDialog( this, _( "Select Templates Directory" ), 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
240void DIALOG_TEMPLATE_SELECTOR::onReload( 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( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
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
273 m_panels[page] = tpanel;
274 m_notebook->InsertPage( page, tpanel, title, true );
275
276 buildPageContent( m_tcTemplatePath->GetValue(), page );
277
278 m_selectedWidget = nullptr;
279
280 Layout();
281}
282
283
285{
286 wxString url = event.GetLinkInfo().GetHref();
287 wxLaunchDefaultBrowser( url );
288}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
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 SetHtml(const wxFileName &aFilename)
void onDirectoryBrowseClicked(wxCommandEvent &event) override
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)
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:155
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