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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22
25
30#include <trace_helpers.h>
32#include <wx/dirdlg.h>
33#include <wx/log.h>
34
35
37{
38public:
39 PANEL_STARTWIZARD_SETTINGS( std::shared_ptr<STARTWIZARD_PROVIDER_SETTINGS_MODEL> aModel,
40 wxWindow* aParent ) :
42 m_model( aModel )
43 {
45 };
46
47 bool TransferDataFromWindow() override
48 {
49 if( m_btnUseDefaults->GetValue() )
50 {
52 }
53 else
54 {
56 // Round-trip through a wxFileName object to remove any trailing separators
57 wxFileName path( m_cbPath->GetValue(), wxEmptyString );
58 m_model->import_path = path.GetPath();
59 }
60
61 return true;
62 }
63
64 bool TransferDataToWindow() override
65 {
67 std::vector<wxString> paths;
68
70 {
71 m_btnUseDefaults->SetValue( true );
72
73 if( !manager.GetPreviousVersionPaths( &paths ) )
74 {
75 m_btnPrevVer->SetLabelText( _( "Import settings from a previous version (none found)" ) );
76 }
77 else
78 {
79 m_cbPath->Clear();
80
81 for( const auto& path : paths )
82 m_cbPath->Append( path );
83
84 m_cbPath->SetSelection( 0 );
85 }
86
87 // SetValue does not fire the "OnRadioButton" event, so have to fabricate this
88 wxCommandEvent dummy;
90 }
91 else
92 {
93 m_btnPrevVer->SetValue( true );
94 m_cbPath->SetStringSelection( m_model->import_path );
95
96 wxCommandEvent dummy;
98 }
99
100 return true;
101 }
102
103protected:
104 void OnPrevVerSelected( wxCommandEvent& event ) override;
105
106 void OnPathChanged( wxCommandEvent& event ) override;
107
108 void OnPathDefocused( wxFocusEvent& event ) override;
109
110 void OnChoosePath( wxCommandEvent& event ) override;
111
112 void OnDefaultSelected( wxCommandEvent& event ) override;
113
114private:
115 bool validatePath();
116
117 void showPathError( bool aShow = true );
118
119 std::shared_ptr<STARTWIZARD_PROVIDER_SETTINGS_MODEL> m_model;
120};
121
122
124{
125 m_cbPath->Enable();
126 m_btnCustomPath->Enable();
127 validatePath();
128}
129
130
131void PANEL_STARTWIZARD_SETTINGS::OnPathChanged( wxCommandEvent& event )
132{
133 validatePath();
134}
135
136
138{
139 validatePath();
140}
141
142
143void PANEL_STARTWIZARD_SETTINGS::OnChoosePath( wxCommandEvent& event )
144{
145 wxDirDialog dlg( nullptr, _( "Select Settings Path" ), m_cbPath->GetValue(),
146 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
147
148 if( dlg.ShowModal() == wxID_OK )
149 {
150 m_cbPath->SetValue( dlg.GetPath() );
151 validatePath();
152 }
153}
154
155
157{
158 m_cbPath->Disable();
159 m_btnCustomPath->Disable();
160 showPathError( false );
161}
162
163
165{
167 wxString path = m_cbPath->GetValue();
168 bool valid = manager.IsSettingsPathValid( path );
169
170 showPathError( !valid );
171
172 return valid;
173}
174
175
177{
178 m_lblPathError->Show( aShow );
179 Layout();
180 Fit();
181}
182
183
188
189
194
195
196wxPanel* STARTWIZARD_PROVIDER_SETTINGS::GetWizardPanel( wxWindow* aParent, STARTWIZARD* aWizard )
197{
198 m_model = std::make_shared<STARTWIZARD_PROVIDER_SETTINGS_MODEL>();
199 return new PANEL_STARTWIZARD_SETTINGS( m_model, aParent );
200}
201
202
204{
206 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
207
209 {
210 settings->SaveToFile( mgr.GetPathForSettingsFile( settings ) );
211 return;
212 }
213
214 // Else, perform migration. First copy the old files in, then reload the in-memory copies.
215 mgr.MigrateFromPreviousVersion( m_model->import_path );
216 mgr.Load();
217
218 // The wizard defaulted to the system locale, so re-apply the just-imported language from
219 // common settings to avoid requiring a restart.
220 wxString languageErr;
221
222 if( !Pgm().SetLanguage( languageErr, true ) )
223 wxLogTrace( traceLocale, wxT( "Unable to apply imported language: %s" ), languageErr );
224}
225
226
228{
230 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
231
232 // We will already have created default settings in memory; make sure they are persisted
233 // so we don't get first-run wizard restarts
234 settings->SaveToFile( mgr.GetPathForSettingsFile( settings ) );
235}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
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:528
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
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)
const wxChar *const traceLocale
Flag to enable locale debug output.
PGM_BASE & Pgm()
The global program "get" accessor.
std::vector< FAB_LAYER_COLOR > dummy
std::string path
wxLogTrace helper definitions.