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 if( m_lblPathError->IsShown() )
62 return false;
63
64 return true;
65 }
66
67 bool TransferDataToWindow() override
68 {
70 std::vector<wxString> paths;
71
73 {
74 m_btnUseDefaults->SetValue( true );
75
76 if( !manager.GetPreviousVersionPaths( &paths ) )
77 {
78 m_btnPrevVer->SetLabelText( _( "Import settings from a previous version (none found)" ) );
79 }
80 else
81 {
82 m_cbPath->Clear();
83
84 for( const auto& path : paths )
85 m_cbPath->Append( path );
86
87 m_cbPath->SetSelection( 0 );
88 }
89
90 // SetValue does not fire the "OnRadioButton" event, so have to fabricate this
91 wxCommandEvent dummy;
93 }
94 else
95 {
96 m_btnPrevVer->SetValue( true );
97 m_cbPath->SetStringSelection( m_model->import_path );
98
99 wxCommandEvent dummy;
101 }
102
103 return true;
104 }
105
106protected:
107 void OnPrevVerSelected( wxCommandEvent& event ) override;
108
109 void OnPathChanged( wxCommandEvent& event ) override;
110
111 void OnPathDefocused( wxFocusEvent& event ) override;
112
113 void OnChoosePath( wxCommandEvent& event ) override;
114
115 void OnDefaultSelected( wxCommandEvent& event ) override;
116
117private:
118 bool validatePath();
119
120 void showPathError( bool aShow = true );
121
122 std::shared_ptr<STARTWIZARD_PROVIDER_SETTINGS_MODEL> m_model;
123};
124
125
127{
128 m_cbPath->Enable();
129 m_btnCustomPath->Enable();
130 validatePath();
131}
132
133
134void PANEL_STARTWIZARD_SETTINGS::OnPathChanged( wxCommandEvent& event )
135{
136 validatePath();
137}
138
139
141{
142 validatePath();
143}
144
145
146void PANEL_STARTWIZARD_SETTINGS::OnChoosePath( wxCommandEvent& event )
147{
148 wxDirDialog dlg( nullptr, _( "Select Settings Path" ), m_cbPath->GetValue(),
149 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
150
151 if( dlg.ShowModal() == wxID_OK )
152 {
153 m_cbPath->SetValue( dlg.GetPath() );
154 validatePath();
155 }
156}
157
158
160{
161 m_cbPath->Disable();
162 m_btnCustomPath->Disable();
163 showPathError( false );
164}
165
166
168{
169 wxString path = m_cbPath->GetValue();
171
172 showPathError( !valid );
173
174 return valid;
175}
176
177
179{
180 m_lblPathError->Show( aShow );
181 Layout();
182 Fit();
183}
184
185
190
191
196
197
198wxPanel* STARTWIZARD_PROVIDER_SETTINGS::GetWizardPanel( wxWindow* aParent, STARTWIZARD* aWizard )
199{
200 m_model = std::make_shared<STARTWIZARD_PROVIDER_SETTINGS_MODEL>();
201 return new PANEL_STARTWIZARD_SETTINGS( m_model, aParent );
202}
203
204
206{
208 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
209
211 {
212 settings->SaveToFile( mgr.GetPathForSettingsFile( settings ) );
213 return;
214 }
215
216 // Else, perform migration. First copy the old files in, then reload the in-memory copies.
217 mgr.MigrateFromPreviousVersion( m_model->import_path );
218 mgr.Load();
219
220 // The wizard defaulted to the system locale, so re-apply the just-imported language from
221 // common settings to avoid requiring a restart.
222 wxString languageErr;
223
224 if( !Pgm().SetLanguage( languageErr, true ) )
225 wxLogTrace( traceLocale, wxT( "Unable to apply imported language: %s" ), languageErr );
226}
227
228
230{
232 COMMON_SETTINGS* settings = Pgm().GetCommonSettings();
233
234 // We will already have created default settings in memory; make sure they are persisted
235 // so we don't get first-run wizard restarts
236 settings->SaveToFile( mgr.GetPathForSettingsFile( settings ) );
237}
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:546
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:123
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.