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 <gal/color4d.h>
25 #include <settings/json_settings.h>
26 
27 using KIGFX::COLOR4D;
28 
49 #include <settings/json_settings.h>
50 #include <settings/parameters.h>
51 
52 
54 {
55 public:
61  std::vector<COLOR4D> m_Palette;
62 
63  explicit COLOR_SETTINGS( std::string aFilename = "user" );
64 
65  virtual ~COLOR_SETTINGS() {}
66 
67  bool MigrateFromLegacy( wxConfigBase* aCfg ) override;
68 
69  bool Migrate() override;
70 
71  COLOR4D GetColor( int aLayer ) const;
72 
73  COLOR4D GetDefaultColor( int aLayer );
74 
75  void SetColor( int aLayer, COLOR4D aColor );
76 
77  const wxString& GetName() const { return m_displayName; }
78  void SetName( const wxString& aName ) { m_displayName = aName; }
79 
81  void SetOverrideSchItemColors( bool aFlag ) { m_overrideSchItemColors = aFlag; }
82 
83 private:
84  bool migrateSchema0to1();
85 
86  wxString m_displayName;
87 
89 
94  std::unordered_map<int, COLOR4D> m_colors;
95 
96  std::unordered_map<int, COLOR4D> m_defaultColors;
97 };
98 
100 {
101 public:
102  COLOR_MAP_PARAM( const std::string& aJsonPath, int aMapKey, COLOR4D aDefault,
103  std::unordered_map<int, COLOR4D>* aMap, bool aReadOnly = false ) :
104  PARAM_BASE( aJsonPath, aReadOnly ), m_key( aMapKey ), m_default( aDefault ),
105  m_map( aMap )
106  {}
107 
108  void Load( JSON_SETTINGS* aSettings ) const override
109  {
110  if( m_readOnly )
111  return;
112 
113  COLOR4D val = m_default;
114 
115  if( OPT<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
116  val = *optval;
117 
118  ( *m_map )[ m_key ] = val;
119  }
120 
121  void Store( JSON_SETTINGS* aSettings) const override
122  {
123  aSettings->Set<COLOR4D>( m_path, ( *m_map )[ m_key ] );
124  }
125 
126  int GetKey() const
127  {
128  return m_key;
129  }
130 
132  {
133  return m_default;
134  }
135 
136  virtual void SetDefault() override
137  {
138  ( *m_map )[ m_key ] = m_default;
139  }
140 
141 private:
142  int m_key;
144  std::unordered_map<int, COLOR4D>* m_map;
145 };
146 
147 #endif
std::string m_path
the string used to store the param in json files
Definition: parameters.h:66
COLOR_SETTINGS(std::string aFilename="user")
virtual ~COLOR_SETTINGS()
bool m_overrideSchItemColors
wxString m_displayName
const wxString & GetName() const
OPT< ValueType > Get(std::string aPath) const
Fetches a value from within the JSON document.
COLOR4D GetDefault() const
void Set(std::string aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
int GetKey() const
COLOR_MAP_PARAM(const std::string &aJsonPath, int aMapKey, COLOR4D aDefault, std::unordered_map< int, COLOR4D > *aMap, bool aReadOnly=false)
void SetOverrideSchItemColors(bool aFlag)
void Load(JSON_SETTINGS *aSettings) const override
Loads the value of this parameter from JSON to the underlying storage.
COLOR4D GetDefaultColor(int aLayer)
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.
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.
virtual void SetDefault() override
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:69
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
bool Migrate() override
Migrates the schema of this settings from the version in the file to the latest version.
Color settings are a bit different than most of the settings objects in that there can be more than o...
bool GetOverrideSchItemColors() const
void SetColor(int aLayer, COLOR4D aColor)
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40