KiCad PCB EDA Suite
Loading...
Searching...
No Matches
startwizard_provider_settings.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22
25
31#include <wx/dirdlg.h>
32
33
35{
36public:
37 PANEL_STARTWIZARD_SETTINGS( std::shared_ptr<STARTWIZARD_PROVIDER_SETTINGS_MODEL> aModel,
38 wxWindow* aParent ) :
40 m_model( aModel )
41 {
43 };
44
45 bool TransferDataFromWindow() override
46 {
47 if( m_btnUseDefaults->GetValue() )
48 {
50 }
51 else
52 {
54 // Round-trip through a wxFileName object to remove any trailing separators
55 wxFileName path( m_cbPath->GetValue(), wxEmptyString );
56 m_model->import_path = path.GetPath();
57 }
58
59 return true;
60 }
61
62 bool TransferDataToWindow() override
63 {
65 std::vector<wxString> paths;
66
68 {
69 m_btnUseDefaults->SetValue( true );
70
71 if( !manager.GetPreviousVersionPaths( &paths ) )
72 {
73 m_btnPrevVer->SetLabelText( _( "Import settings from a previous version (none found)" ) );
74 }
75 else
76 {
77 m_cbPath->Clear();
78
79 for( const auto& path : paths )
80 m_cbPath->Append( path );
81
82 m_cbPath->SetSelection( 0 );
83 }
84
85 // SetValue does not fire the "OnRadioButton" event, so have to fabricate this
86 wxCommandEvent dummy;
88 }
89 else
90 {
91 m_btnPrevVer->SetValue( true );
92 m_cbPath->SetStringSelection( m_model->import_path );
93
94 wxCommandEvent dummy;
96 }
97
98 return true;
99 }
100
101protected:
102 void OnPrevVerSelected( wxCommandEvent& event ) override;
103
104 void OnPathChanged( wxCommandEvent& event ) override;
105
106 void OnPathDefocused( wxFocusEvent& event ) override;
107
108 void OnChoosePath( wxCommandEvent& event ) override;
109
110 void OnDefaultSelected( wxCommandEvent& event ) override;
111
112private:
113 bool validatePath();
114
115 void showPathError( bool aShow = true );
116
117 std::shared_ptr<STARTWIZARD_PROVIDER_SETTINGS_MODEL> m_model;
118};
119
120
122{
123 m_cbPath->Enable();
124 m_btnCustomPath->Enable();
125 validatePath();
126}
127
128
129void PANEL_STARTWIZARD_SETTINGS::OnPathChanged( wxCommandEvent& event )
130{
131 validatePath();
132}
133
134
136{
137 validatePath();
138}
139
140
141void PANEL_STARTWIZARD_SETTINGS::OnChoosePath( wxCommandEvent& event )
142{
143 wxDirDialog dlg( nullptr, _( "Select Settings Path" ), m_cbPath->GetValue(),
144 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
145
146 if( dlg.ShowModal() == wxID_OK )
147 {
148 m_cbPath->SetValue( dlg.GetPath() );
149 validatePath();
150 }
151}
152
153
155{
156 m_cbPath->Disable();
157 m_btnCustomPath->Disable();
158 showPathError( false );
159}
160
161
163{
165 wxString path = m_cbPath->GetValue();
166 bool valid = manager.IsSettingsPathValid( path );
167
168 showPathError( !valid );
169
170 return valid;
171}
172
173
175{
176 m_lblPathError->Show( aShow );
177 Layout();
178 Fit();
179}
180
181
186
187
192
193
194wxPanel* STARTWIZARD_PROVIDER_SETTINGS::GetWizardPanel( wxWindow* aParent, STARTWIZARD* aWizard )
195{
196 m_model = std::make_shared<STARTWIZARD_PROVIDER_SETTINGS_MODEL>();
197 return new PANEL_STARTWIZARD_SETTINGS( m_model, aParent );
198}
199
200
202{
204 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
205
207 {
208 settings->SaveToFile( mgr.GetPathForSettingsFile( settings ) );
209 return;
210 }
211
212 // Else, perform migration. First copy the old files in, then reload the in-memory copies.
213 mgr.MigrateFromPreviousVersion( m_model->import_path );
214 mgr.Load();
215}
216
217
219{
221 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
222
223 // We will already have created default settings in memory; make sure they are persisted
224 // so we don't get first-run wizard restarts
225 settings->SaveToFile( mgr.GetPathForSettingsFile( settings ) );
226}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
PANEL_STARTWIZARD_SETTINGS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(500, 300), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
void OnDefaultSelected(wxCommandEvent &event) override
void OnPathDefocused(wxFocusEvent &event) override
PANEL_STARTWIZARD_SETTINGS(std::shared_ptr< STARTWIZARD_PROVIDER_SETTINGS_MODEL > aModel, wxWindow *aParent)
std::shared_ptr< STARTWIZARD_PROVIDER_SETTINGS_MODEL > m_model
void OnPathChanged(wxCommandEvent &event) override
void OnPrevVerSelected(wxCommandEvent &event) override
void OnChoosePath(wxCommandEvent &event) override
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:537
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:128
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Return the path a given settings file should be loaded from / stored to.
bool MigrateFromPreviousVersion(const wxString &aSourcePath)
Handle migration of the settings from previous KiCad versions.
bool SettingsDirectoryValid() const
bool GetPreviousVersionPaths(std::vector< wxString > *aName=nullptr)
Retrieve the name of the most recent previous KiCad version that can be found in the user settings di...
static bool IsSettingsPathValid(const wxString &aPath)
Check if a given path is probably a valid KiCad configuration directory.
wxPanel * GetWizardPanel(wxWindow *aParent, STARTWIZARD *aWizard) override
void ApplyDefaults() override
Apply whatever actions and settings should happen if the user cancels the startup wizard.
std::shared_ptr< STARTWIZARD_PROVIDER_SETTINGS_MODEL > m_model
STARTWIZARD_PROVIDER(const wxString &aPageName)
#define _(s)
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:946
std::vector< FAB_LAYER_COLOR > dummy