KiCad PCB EDA Suite
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 <jon@craftyjon.com>
5  * Copyright (C) 2020 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>
27 #include <settings/json_settings.h>
28 #include <settings/parameters.h>
29 
30 using KIGFX::COLOR4D;
31 
52 {
53 public:
59  std::vector<COLOR4D> m_Palette;
60 
61  explicit COLOR_SETTINGS( wxString aFilename = "user" );
62 
63  virtual ~COLOR_SETTINGS() {}
64 
69  COLOR_SETTINGS( const COLOR_SETTINGS& aOther );
70 
71  COLOR_SETTINGS& operator=( const COLOR_SETTINGS &aOther );
72 
73  bool MigrateFromLegacy( wxConfigBase* aCfg ) override;
74 
75  COLOR4D GetColor( int aLayer ) const;
76 
77  COLOR4D GetDefaultColor( int aLayer );
78 
79  void SetColor( int aLayer, COLOR4D aColor );
80 
81  const wxString& GetName() const { return m_displayName; }
82  void SetName( const wxString& aName ) { m_displayName = aName; }
83 
85  void SetOverrideSchItemColors( bool aFlag ) { m_overrideSchItemColors = aFlag; }
86 
94  static std::vector<COLOR_SETTINGS*> CreateBuiltinColorSettings();
95 
96 private:
97  bool migrateSchema0to1();
98 
99  void initFromOther( const COLOR_SETTINGS& aOther );
100 
101  wxString m_displayName;
102 
104 
109  std::unordered_map<int, COLOR4D> m_colors;
110 
111  std::unordered_map<int, COLOR4D> m_defaultColors;
112 };
113 
115 {
116 public:
117  COLOR_MAP_PARAM( const std::string& aJsonPath, int aMapKey, COLOR4D aDefault,
118  std::unordered_map<int, COLOR4D>* aMap, bool aReadOnly = false ) :
119  PARAM_BASE( aJsonPath, aReadOnly ), m_key( aMapKey ), m_default( aDefault ),
120  m_map( aMap )
121  {}
122 
123  void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
124  {
125  if( m_readOnly )
126  return;
127 
128  if( OPT<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
129  ( *m_map )[ m_key ] = *optval;
130  else if( aResetIfMissing )
131  ( *m_map )[ m_key ] = m_default;
132  }
133 
134  void Store( JSON_SETTINGS* aSettings ) const override
135  {
136  aSettings->Set<COLOR4D>( m_path, ( *m_map )[ m_key ] );
137  }
138 
139  int GetKey() const
140  {
141  return m_key;
142  }
143 
145  {
146  return m_default;
147  }
148 
149  void SetDefault() override
150  {
151  ( *m_map )[ m_key ] = m_default;
152  }
153 
154  bool IsDefault() const override
155  {
156  return ( *m_map )[ m_key ] == m_default;
157  }
158 
159  bool MatchesFile( JSON_SETTINGS* aSettings ) const override
160  {
161  if( OPT<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
162  return m_map->count( m_key ) && ( *optval == m_map->at( m_key ) );
163 
164  // If the JSON doesn't exist, the map shouldn't exist either
165  return !m_map->count( m_key );
166  }
167 
168 private:
169  int m_key;
170 
172 
173  std::unordered_map<int, COLOR4D>* m_map;
174 };
175 
176 #endif
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...
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
virtual ~COLOR_SETTINGS()
bool m_overrideSchItemColors
wxString m_displayName
const wxString & GetName() const
COLOR4D GetDefault() const
int GetKey() const
COLOR_MAP_PARAM(const std::string &aJsonPath, int aMapKey, COLOR4D aDefault, std::unordered_map< int, COLOR4D > *aMap, bool aReadOnly=false)
void SetDefault() override
void SetOverrideSchItemColors(bool aFlag)
COLOR4D GetDefaultColor(int aLayer)
static std::vector< COLOR_SETTINGS * > CreateBuiltinColorSettings()
Constructs and returns a list of color settings objects based on the built-in color themes.
void SetName(const wxString &aName)
std::unordered_map< int, COLOR4D > m_defaultColors
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
std::vector< COLOR4D > m_Palette
m_Pallete stores a list of colors that are used, in order, when a list of colors needs to be generate...
COLOR4D GetColor(int aLayer) const
std::unordered_map< int, COLOR4D > m_colors
Map of all layer colors.
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
void initFromOther(const COLOR_SETTINGS &aOther)
COLOR_SETTINGS(wxString aFilename="user")
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:83
COLOR_SETTINGS & operator=(const COLOR_SETTINGS &aOther)
std::unordered_map< int, COLOR4D > * m_map
bool MigrateFromLegacy(wxConfigBase *aCfg) override
Migrates from wxConfig to JSON-based configuration.
boost::optional< T > OPT
Definition: optional.h:7
Color settings are a bit different than most of the settings objects in that there can be more than o...
bool GetOverrideSchItemColors() const
bool IsDefault() const override
Checks whether or not this param has been changed from its default value.
void SetColor(int aLayer, COLOR4D aColor)
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
void Load(JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.