KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_color_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 (C) 2020 Jon Evans <[email protected]>
5 * Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
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#include <bitmaps.h>
22#include <launch_ext.h>
23#include <layer_ids.h>
24#include <menus_helpers.h>
26#include <pgm_base.h>
30#include <validators.h>
32#include <widgets/wx_panel.h>
33
34#include <wx/msgdlg.h>
35#include <wx/textdlg.h>
36
37// Button ID starting point
38constexpr int FIRST_BUTTON_ID = 1800;
39
40
43 m_currentSettings( nullptr ),
44 m_swatches(),
45 m_copied( COLOR4D::UNSPECIFIED ),
46 m_validLayers(),
47 m_backgroundLayer( LAYER_PCB_BACKGROUND ),
48 m_colorNamespace()
49{
50#ifdef __APPLE__
51 m_btnOpenFolder->SetLabel( _( "Reveal Themes in Finder" ) );
52
53 // Simple border is too dark on OSX
54 m_colorsListWindow->SetWindowStyle( wxBORDER_SUNKEN|wxVSCROLL );
55#endif
56
57 m_panel1->SetBorders( false, false, true, false );
58}
59
60
62{
64 LaunchExternal( dir );
65}
66
67
69{
71 return;
72
73 for( const std::pair<const int, COLOR_SWATCH*>& pair : m_swatches )
74 {
75 int layer = pair.first;
76 COLOR_SWATCH* button = pair.second;
77
78 COLOR4D defaultColor = m_currentSettings->GetDefaultColor( layer );
79
80 m_currentSettings->SetColor( layer, defaultColor );
81 button->SetSwatchColor( defaultColor, false );
82 }
83}
84
85
87{
88 if( show )
89 {
90 // In case changes have been made to the current theme in another panel:
91 int idx = m_cbTheme->GetSelection();
92 COLOR_SETTINGS* settings = static_cast<COLOR_SETTINGS*>( m_cbTheme->GetClientData( idx ) );
93
94 if( settings )
95 *m_currentSettings = *settings;
96
99 }
100
101 return PANEL_COLOR_SETTINGS_BASE::Show( show );
102}
103
104
105void PANEL_COLOR_SETTINGS::OnLeftDownTheme( wxMouseEvent& event )
106{
107 // Lazy rebuild of theme menu to catch any colour theme changes made in other panels
109
110 event.Skip();
111}
112
113
114void PANEL_COLOR_SETTINGS::OnThemeChanged( wxCommandEvent& event )
115{
116 int idx = m_cbTheme->GetSelection();
117
118 if( idx == static_cast<int>( m_cbTheme->GetCount() ) - 2 )
119 {
120 m_cbTheme->SetStringSelection( GetSettingsDropdownName( m_currentSettings ) );
121 return;
122 }
123
124 if( idx == (int)m_cbTheme->GetCount() - 1 )
125 {
126 // New Theme...
127
128 if( !saveCurrentTheme( false ) )
129 return;
130
131 FOOTPRINT_NAME_VALIDATOR themeNameValidator;
132 wxTextEntryDialog dlg( this, _( "New theme name:" ), _( "Add Color Theme" ) );
133 dlg.SetTextValidator( themeNameValidator );
134
135 if( dlg.ShowModal() != wxID_OK )
136 return;
137
138 wxString themeName = dlg.GetValue();
139 wxFileName fn( themeName + wxT( ".json" ) );
141
142 if( fn.Exists() )
143 {
144 wxMessageBox( _( "Theme already exists!" ) );
145 return;
146 }
147
148 SETTINGS_MANAGER& settingsMgr = Pgm().GetSettingsManager();
149 COLOR_SETTINGS* newSettings = settingsMgr.AddNewColorSettings( themeName );
150 newSettings->SetName( themeName );
151 newSettings->SetReadOnly( false );
152
153 for( int layer : m_validLayers )
154 newSettings->SetColor( layer, m_currentSettings->GetColor( layer ) );
155
156 newSettings->SaveToFile( settingsMgr.GetPathForSettingsFile( newSettings ) );
157
158 idx = m_cbTheme->Insert( themeName, idx - 1, static_cast<void*>( newSettings ) );
159 m_cbTheme->SetSelection( idx );
160
161 m_optOverrideColors->SetValue( newSettings->GetOverrideSchItemColors() );
162
163 *m_currentSettings = *newSettings;
166 }
167 else
168 {
169 COLOR_SETTINGS* selected = static_cast<COLOR_SETTINGS*>( m_cbTheme->GetClientData( idx ) );
170
171 if( selected->GetFilename() != m_currentSettings->GetFilename() )
172 {
173 if( !saveCurrentTheme( false ) )
174 return;
175
176 m_optOverrideColors->SetValue( selected->GetOverrideSchItemColors() );
177
178 *m_currentSettings = *selected;
181 }
182 }
183}
184
185
187{
188 if( m_swatches.empty() )
189 {
191 }
192 else
193 {
194 bool isReadOnly = m_currentSettings->IsReadOnly();
196
197 for( std::pair<int, COLOR_SWATCH*> pair : m_swatches )
198 {
199 pair.second->SetSwatchBackground( background );
200 pair.second->SetSwatchColor( m_currentSettings->GetColor( pair.first ), false );
201 pair.second->SetReadOnly( isReadOnly );
202 }
203 }
204}
205
206
207void PANEL_COLOR_SETTINGS::createThemeList( const wxString& aCurrent )
208{
209 int width = 0;
210 int height = 0;
211
212 m_cbTheme->GetTextExtent( _( "New Theme..." ), &width, &height );
213 int minwidth = width;
214
215 m_cbTheme->Clear();
216
217 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
218 {
219 wxString name = GetSettingsDropdownName( settings );
220
221 int pos = m_cbTheme->Append( name, static_cast<void*>( settings ) );
222
223 if( settings->GetFilename() == aCurrent )
224 m_cbTheme->SetSelection( pos );
225
226 m_cbTheme->GetTextExtent( name, &width, &height );
227 minwidth = std::max( minwidth, width );
228 }
229
230 m_cbTheme->Append( wxT( "---" ) );
231 m_cbTheme->Append( _( "New Theme..." ) );
232
233 m_cbTheme->SetMinSize( wxSize( minwidth + 50, -1 ) );
234}
235
236
237void PANEL_COLOR_SETTINGS::createSwatch( int aLayer, const wxString& aName )
238{
239 wxStaticText* label = new wxStaticText( m_colorsListWindow, wxID_ANY, aName );
240
241 // The previously selected theme can be deleted and cannot be selected.
242 // so select the default theme (first theme of the list)
243 if( m_cbTheme->GetSelection() < 0 )
244 {
245 m_cbTheme->SetSelection( 0 );
247 }
248
249 void* clientData = m_cbTheme->GetClientData( m_cbTheme->GetSelection() );
250 COLOR_SETTINGS* selected = static_cast<COLOR_SETTINGS*>( clientData );
251
252 int id = FIRST_BUTTON_ID + aLayer;
253 COLOR4D defaultColor = selected->GetDefaultColor( aLayer );
256
257 COLOR_SWATCH* swatch = new COLOR_SWATCH( m_colorsListWindow, color, id, backgroundColor,
258 defaultColor, SWATCH_MEDIUM );
259 swatch->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) );
260
261 // Display the swatches in the left column and the layer name in the right column
262 // The right column is sometimes (depending on the translation) truncated for long texts.
263 // We cannot allow swatches to be truncated or not shown
264 m_colorsGridSizer->Add( swatch, 0, wxALIGN_CENTER_VERTICAL | wxALL, 3 );
265 m_colorsGridSizer->Add( label, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT | wxLEFT, 5 );
266
267 m_labels[aLayer] = label;
268 m_swatches[aLayer] = swatch;
269
270 swatch->Bind( wxEVT_RIGHT_DOWN,
271 [&, aLayer]( wxMouseEvent& aEvent )
272 {
273 ShowColorContextMenu( aEvent, aLayer );
274 } );
275
276 swatch->Bind( COLOR_SWATCH_CHANGED, &PANEL_COLOR_SETTINGS::OnColorChanged, this );
277}
278
279
280void PANEL_COLOR_SETTINGS::ShowColorContextMenu( wxMouseEvent& aEvent, int aLayer )
281{
282 auto selected =
283 static_cast<COLOR_SETTINGS*>( m_cbTheme->GetClientData( m_cbTheme->GetSelection() ) );
284
285 wxCHECK_RET( selected, wxT( "Invalid color theme selected" ) );
286
287 COLOR4D current = m_currentSettings->GetColor( aLayer );
288 COLOR4D saved = selected->GetColor( aLayer );
289 bool readOnly = m_currentSettings->IsReadOnly();
290
291 wxMenu menu;
292
293 AddMenuItem( &menu, ID_COPY, _( "Copy color" ), KiBitmap( BITMAPS::copy ) );
294
295 if( !readOnly && m_copied != COLOR4D::UNSPECIFIED )
296 AddMenuItem( &menu, ID_PASTE, _( "Paste color" ), KiBitmap( BITMAPS::paste ) );
297
298 if( !readOnly && current != saved )
299 AddMenuItem( &menu, ID_REVERT, _( "Revert to saved color" ), KiBitmap( BITMAPS::undo ) );
300
301 menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
302 [&]( wxCommandEvent& aCmd )
303 {
304 switch( aCmd.GetId() )
305 {
306 case ID_COPY:
307 m_copied = current;
308 break;
309
310 case ID_PASTE:
311 updateColor( aLayer, m_copied );
312 break;
313
314 case ID_REVERT:
315 updateColor( aLayer, saved );
316 break;
317
318 default:
319 aCmd.Skip();
320 }
321 } );
322
323 PopupMenu( &menu );
324}
325
326
327void PANEL_COLOR_SETTINGS::OnColorChanged( wxCommandEvent& aEvent )
328{
329 COLOR_SWATCH* swatch = static_cast<COLOR_SWATCH*>( aEvent.GetEventObject() );
330 COLOR4D newColor = swatch->GetSwatchColor();
331 int layer = static_cast<SCH_LAYER_ID>( swatch->GetId() - FIRST_BUTTON_ID );
332
333 updateColor( layer, newColor );
334}
335
336
337void PANEL_COLOR_SETTINGS::updateColor( int aLayer, const KIGFX::COLOR4D& aColor )
338{
340 m_currentSettings->SetColor( aLayer, aColor );
341
342 // Colors must be persisted when edited because multiple PANEL_COLOR_SETTINGS could be
343 // referring to the same theme.
344 saveCurrentTheme( false );
345
346 m_swatches[aLayer]->SetSwatchColor( aColor, false );
347
348 if( m_currentSettings && aLayer == m_backgroundLayer )
349 {
351
352 for( std::pair<int, COLOR_SWATCH*> pair : m_swatches )
353 pair.second->SetSwatchBackground( background );
354 }
355
357}
358
359
361{
363 return true;
364
365 if( aValidate && !validateSave() )
366 return false;
367
368 SETTINGS_MANAGER& settingsMgr = Pgm().GetSettingsManager();
369 COLOR_SETTINGS* selected = settingsMgr.GetColorSettings( m_currentSettings->GetFilename() );
370
371 selected->SetOverrideSchItemColors( m_optOverrideColors->GetValue() );
372
373 for( int layer : m_validLayers )
374 selected->SetColor( layer, m_currentSettings->GetColor( layer ) );
375
376 settingsMgr.SaveColorSettings( selected, m_colorNamespace );
377
378 return true;
379}
380
381
383{
384 wxString name = aSettings->GetName();
385
386 if( aSettings->IsReadOnly() )
387 name += wxS( " " ) + _( "(read-only)" );
388
389 return name;
390}
int color
Definition: DXF_plotter.cpp:57
const char * name
Definition: DXF_plotter.cpp:56
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:106
wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmap &aImage, wxItemKind aType=wxITEM_NORMAL)
Create and insert a menu item with an icon into aMenu.
Definition: bitmap.cpp:266
Color settings are a bit different than most of the settings objects in that there can be more than o...
void SetName(const wxString &aName)
void SetColor(int aLayer, const COLOR4D &aColor)
bool GetOverrideSchItemColors() const
COLOR4D GetColor(int aLayer) const
COLOR4D GetDefaultColor(int aLayer)
void SetOverrideSchItemColors(bool aFlag)
const wxString & GetName() const
A simple color swatch of the kind used to set layer colors.
Definition: color_swatch.h:57
void SetSwatchColor(const KIGFX::COLOR4D &aColor, bool aSendEvent)
Set the current swatch color directly.
KIGFX::COLOR4D GetSwatchColor() const
This class provides a custom wxValidator object for limiting the allowable characters when defining f...
Definition: validators.h:66
bool IsReadOnly() const
Definition: json_settings.h:84
void SetReadOnly(bool aReadOnly)
Definition: json_settings.h:85
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
wxString GetFilename() const
Definition: json_settings.h:73
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
Class PANEL_COLOR_SETTINGS_BASE.
void createThemeList(const wxString &aCurrent)
Builds the theme listbox and sets the selection to the current theme.
void createSwatch(int aLayer, const wxString &aName)
std::vector< int > m_validLayers
A list of layer IDs that are valid for the current color settings dialog.
void OnThemeChanged(wxCommandEvent &aEvent) override
virtual bool saveCurrentTheme(bool aValidate)
std::string m_colorNamespace
A namespace that will be passed to SETTINGS_MANAGER::SaveColorSettings.
virtual void ResetPanel() override
Reset the contents of this panel.
void OnBtnOpenThemeFolderClicked(wxCommandEvent &event) override
void OnColorChanged(wxCommandEvent &aEvent)
std::map< int, wxStaticText * > m_labels
virtual void onNewThemeSelected()
Event fired when a new theme is selected that can be overridden in children.
wxString GetSettingsDropdownName(COLOR_SETTINGS *aSettings)
Retrieves the drop down name to be displayed for a color setting.
void updateColor(int aLayer, const KIGFX::COLOR4D &aColor)
COLOR_SETTINGS * m_currentSettings
PANEL_COLOR_SETTINGS(wxWindow *aParent)
void ShowColorContextMenu(wxMouseEvent &aEvent, int aLayer)
void OnLeftDownTheme(wxMouseEvent &event) override
bool Show(bool show) override
virtual void createSwatches()=0
virtual void onColorChanged()
Event fired when the user changes any color.
virtual bool validateSave(bool aQuiet=false)
Performs a pre-save validation of the current color theme.
std::map< int, COLOR_SWATCH * > m_swatches
wxString GetPathForSettingsFile(JSON_SETTINGS *aSettings)
Returns the path a given settings file should be loaded from / stored to.
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
void SaveColorSettings(COLOR_SETTINGS *aSettings, const std::string &aNamespace="")
Safely saves a COLOR_SETTINGS to disk, preserving any changes outside the given namespace.
static wxString GetColorSettingsPath()
Returns the path where color scheme files are stored; creating it if missing (normally .
COLOR_SETTINGS * AddNewColorSettings(const wxString &aFilename)
Registers a new color settings object with the given filename.
void SetBorders(bool aLeft, bool aRight, bool aTop, bool aBottom)
Definition: wx_panel.h:38
@ SWATCH_MEDIUM
Definition: color_swatch.h:41
#define _(s)
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition: layer_ids.h:220
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:345
Macros and inline functions to create menus items in menubars or popup menus.
constexpr int FIRST_BUTTON_ID
SETTINGS_MANAGER * GetSettingsManager()
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
Custom text control validator definitions.