KiCad PCB EDA Suite
Loading...
Searching...
No Matches
color_settings.h
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-2021 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#ifndef _COLOR_SETTINGS_H
22#define _COLOR_SETTINGS_H
23
24#include <unordered_map>
25
26#include <gal/color4d.h>
28#include <settings/parameters.h>
29
30using KIGFX::COLOR4D;
31
52{
53public:
54 explicit COLOR_SETTINGS( const wxString& aFilename = wxT( "user" ),
55 bool aAbsolutePath = false );
56
57 virtual ~COLOR_SETTINGS() {}
58
63 COLOR_SETTINGS( const COLOR_SETTINGS& aOther );
64
65 COLOR_SETTINGS& operator=( const COLOR_SETTINGS &aOther );
66
67 bool MigrateFromLegacy( wxConfigBase* aCfg ) override;
68
69 COLOR4D GetColor( int aLayer ) const;
70
71 COLOR4D GetDefaultColor( int aLayer );
72
73 void SetColor( int aLayer, const COLOR4D& aColor );
74
75 const wxString& GetName() const { return m_displayName; }
76 void SetName( const wxString& aName ) { m_displayName = aName; }
77
79 void SetOverrideSchItemColors( bool aFlag ) { m_overrideSchItemColors = aFlag; }
80
82 void SetUseBoardStackupColors( bool aFlag ) { m_useBoardStackupColors = aFlag; }
83
91 static std::vector<COLOR_SETTINGS*> CreateBuiltinColorSettings();
92
93 // Names for the built-in color settings
94 static const wxString COLOR_BUILTIN_DEFAULT;
95 static const wxString COLOR_BUILTIN_CLASSIC;
96
97private:
98 bool migrateSchema0to1();
99
100 void initFromOther( const COLOR_SETTINGS& aOther );
101
103
106
111 std::unordered_map<int, COLOR4D> m_colors;
112
113 std::unordered_map<int, COLOR4D> m_defaultColors;
114};
115
117{
118public:
119 COLOR_MAP_PARAM( const std::string& aJsonPath, int aMapKey, COLOR4D aDefault,
120 std::unordered_map<int, COLOR4D>* aMap, bool aReadOnly = false ) :
121 PARAM_BASE( aJsonPath, aReadOnly ), m_key( aMapKey ), m_default( aDefault ),
122 m_map( aMap )
123 {}
124
125 void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
126 {
127 if( m_readOnly )
128 return;
129
130 if( std::optional<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
131 ( *m_map )[ m_key ] = *optval;
132 else if( aResetIfMissing )
133 ( *m_map )[ m_key ] = m_default;
134 }
135
136 void Store( JSON_SETTINGS* aSettings ) const override
137 {
138 aSettings->Set<COLOR4D>( m_path, ( *m_map )[ m_key ] );
139 }
140
141 int GetKey() const
142 {
143 return m_key;
144 }
145
147 {
148 return m_default;
149 }
150
151 void SetDefault() override
152 {
153 ( *m_map )[ m_key ] = m_default;
154 }
155
156 bool MatchesFile( JSON_SETTINGS* aSettings ) const override
157 {
158 if( std::optional<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
159 return m_map->count( m_key ) && ( *optval == m_map->at( m_key ) );
160
161 // If the JSON doesn't exist, the map shouldn't exist either
162 return !m_map->count( m_key );
163 }
164
165private:
166 int m_key;
167
169
170 std::unordered_map<int, COLOR4D>* m_map;
171};
172
173#endif
void SetDefault() override
COLOR4D GetDefault() const
void Load(JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.
int GetKey() const
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
COLOR_MAP_PARAM(const std::string &aJsonPath, int aMapKey, COLOR4D aDefault, std::unordered_map< int, COLOR4D > *aMap, bool aReadOnly=false)
std::unordered_map< int, COLOR4D > * m_map
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)
std::unordered_map< int, COLOR4D > m_defaultColors
void initFromOther(const COLOR_SETTINGS &aOther)
void SetColor(int aLayer, const COLOR4D &aColor)
static std::vector< COLOR_SETTINGS * > CreateBuiltinColorSettings()
Constructs and returns a list of color settings objects based on the built-in color themes.
void SetUseBoardStackupColors(bool aFlag)
bool MigrateFromLegacy(wxConfigBase *aCfg) override
Migrates from wxConfig to JSON-based configuration.
static const wxString COLOR_BUILTIN_CLASSIC
static const wxString COLOR_BUILTIN_DEFAULT
bool m_useBoardStackupColors
bool GetOverrideSchItemColors() const
COLOR4D GetColor(int aLayer) const
virtual ~COLOR_SETTINGS()
COLOR4D GetDefaultColor(int aLayer)
wxString m_displayName
COLOR_SETTINGS & operator=(const COLOR_SETTINGS &aOther)
std::unordered_map< int, COLOR4D > m_colors
Map of all layer colors.
bool GetUseBoardStackupColors() const
bool m_overrideSchItemColors
void SetOverrideSchItemColors(bool aFlag)
const wxString & GetName() const
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:78
std::string m_path
the string used to store the param in json files
Definition: parameters.h:75