2020-01-13 01:44:19 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
|
2021-07-27 12:22:27 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-01-13 01:44:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _COLOR_SETTINGS_H
|
|
|
|
#define _COLOR_SETTINGS_H
|
|
|
|
|
2021-06-04 03:52:50 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <gal/color4d.h>
|
|
|
|
#include <settings/json_settings.h>
|
2020-10-15 01:57:36 +00:00
|
|
|
#include <settings/parameters.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
using KIGFX::COLOR4D;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Color settings are a bit different than most of the settings objects in that there
|
|
|
|
* can be more than one of them loaded at once.
|
|
|
|
*
|
|
|
|
* Each COLOR_SETTINGS object corresponds to a single color scheme file on disk.
|
|
|
|
* The default color scheme is called "default" and will be created on first run.
|
|
|
|
*
|
|
|
|
* When users change color settings, they have the option of modifying the default scheme
|
|
|
|
* or saving their changes to a new color scheme file.
|
|
|
|
*
|
|
|
|
* Each COLOR_SETTINGS defines all the settings used across all parts of KiCad, but it's not
|
|
|
|
* necessary for the underlying theme file to contain all of them. The color settings cascade,
|
|
|
|
* so if a user chooses a non-default scheme for a certain application, and that non-default
|
|
|
|
* scheme file is missing some color definitions, those will fall back to those from the "default"
|
|
|
|
* scheme (which is either loaded from disk or created if missing)
|
|
|
|
*
|
|
|
|
* Each application (eeschema, gerbview, pcbnew) can have a different active color scheme selected.
|
|
|
|
* The "child applications" (library editors) inherit from either eeschema or pcbnew.
|
|
|
|
*/
|
2024-03-20 01:53:21 +00:00
|
|
|
class KICOMMON_API COLOR_SETTINGS : public JSON_SETTINGS
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-11-10 03:11:59 +00:00
|
|
|
explicit COLOR_SETTINGS( const wxString& aFilename = wxT( "user" ),
|
|
|
|
bool aAbsolutePath = false );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
virtual ~COLOR_SETTINGS() {}
|
|
|
|
|
2020-07-04 02:34:16 +00:00
|
|
|
/**
|
|
|
|
* Copy ctor provided for temporary manipulation of themes in the theme editor.
|
|
|
|
* This will not copy the JSON_SETTINGS underlying data.
|
|
|
|
*/
|
|
|
|
COLOR_SETTINGS( const COLOR_SETTINGS& aOther );
|
|
|
|
|
|
|
|
COLOR_SETTINGS& operator=( const COLOR_SETTINGS &aOther );
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
bool MigrateFromLegacy( wxConfigBase* aCfg ) override;
|
|
|
|
|
|
|
|
COLOR4D GetColor( int aLayer ) const;
|
|
|
|
|
|
|
|
COLOR4D GetDefaultColor( int aLayer );
|
|
|
|
|
2021-07-26 17:28:37 +00:00
|
|
|
void SetColor( int aLayer, const COLOR4D& aColor );
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2020-04-09 16:15:57 +00:00
|
|
|
const wxString& GetName() const { return m_displayName; }
|
|
|
|
void SetName( const wxString& aName ) { m_displayName = aName; }
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2020-04-09 16:15:57 +00:00
|
|
|
bool GetOverrideSchItemColors() const { return m_overrideSchItemColors; }
|
|
|
|
void SetOverrideSchItemColors( bool aFlag ) { m_overrideSchItemColors = aFlag; }
|
2020-02-03 16:46:58 +00:00
|
|
|
|
2020-10-15 01:57:36 +00:00
|
|
|
/**
|
|
|
|
* Constructs and returns a list of color settings objects based on the built-in color themes.
|
|
|
|
* These color settings are not backed by a file and cannot be modified by the user.
|
|
|
|
* This is expected to be called by SETTINGS_MANAGER which will take ownership of the objects
|
|
|
|
* and handle freeing them at the end of its lifetime.
|
|
|
|
* @return a list of pointers COLOR_SETTINGS objects containing the default color theme(s)
|
|
|
|
*/
|
|
|
|
static std::vector<COLOR_SETTINGS*> CreateBuiltinColorSettings();
|
|
|
|
|
2023-02-18 22:42:36 +00:00
|
|
|
// Names for the built-in color settings
|
|
|
|
static const wxString COLOR_BUILTIN_DEFAULT;
|
|
|
|
static const wxString COLOR_BUILTIN_CLASSIC;
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
private:
|
2020-05-06 01:45:48 +00:00
|
|
|
bool migrateSchema0to1();
|
|
|
|
|
2020-07-08 16:19:10 +00:00
|
|
|
void initFromOther( const COLOR_SETTINGS& aOther );
|
|
|
|
|
2024-02-23 16:06:23 +00:00
|
|
|
private:
|
2020-01-13 01:44:19 +00:00
|
|
|
wxString m_displayName;
|
2020-04-09 16:15:57 +00:00
|
|
|
bool m_overrideSchItemColors;
|
2020-01-13 01:44:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of all layer colors.
|
2021-07-29 09:47:43 +00:00
|
|
|
* The key needs to be a valid layer ID, see layer_ids.h
|
2020-01-13 01:44:19 +00:00
|
|
|
*/
|
|
|
|
std::unordered_map<int, COLOR4D> m_colors;
|
|
|
|
|
|
|
|
std::unordered_map<int, COLOR4D> m_defaultColors;
|
|
|
|
};
|
|
|
|
|
|
|
|
class COLOR_MAP_PARAM : public PARAM_BASE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
COLOR_MAP_PARAM( const std::string& aJsonPath, int aMapKey, COLOR4D aDefault,
|
|
|
|
std::unordered_map<int, COLOR4D>* aMap, bool aReadOnly = false ) :
|
|
|
|
PARAM_BASE( aJsonPath, aReadOnly ), m_key( aMapKey ), m_default( aDefault ),
|
|
|
|
m_map( aMap )
|
|
|
|
{}
|
|
|
|
|
2020-05-31 21:42:04 +00:00
|
|
|
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
if( m_readOnly )
|
|
|
|
return;
|
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
if( std::optional<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
|
2020-05-31 21:42:04 +00:00
|
|
|
( *m_map )[ m_key ] = *optval;
|
|
|
|
else if( aResetIfMissing )
|
|
|
|
( *m_map )[ m_key ] = m_default;
|
2020-01-13 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 01:57:36 +00:00
|
|
|
void Store( JSON_SETTINGS* aSettings ) const override
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
aSettings->Set<COLOR4D>( m_path, ( *m_map )[ m_key ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetKey() const
|
|
|
|
{
|
|
|
|
return m_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
COLOR4D GetDefault() const
|
|
|
|
{
|
|
|
|
return m_default;
|
|
|
|
}
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
void SetDefault() override
|
2020-01-13 01:44:19 +00:00
|
|
|
{
|
|
|
|
( *m_map )[ m_key ] = m_default;
|
|
|
|
}
|
|
|
|
|
2020-05-30 16:56:16 +00:00
|
|
|
bool MatchesFile( JSON_SETTINGS* aSettings ) const override
|
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
if( std::optional<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
|
2020-05-30 16:56:16 +00:00
|
|
|
return m_map->count( m_key ) && ( *optval == m_map->at( m_key ) );
|
|
|
|
|
|
|
|
// If the JSON doesn't exist, the map shouldn't exist either
|
|
|
|
return !m_map->count( m_key );
|
|
|
|
}
|
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
private:
|
|
|
|
int m_key;
|
2020-05-30 16:56:16 +00:00
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
COLOR4D m_default;
|
2020-05-30 16:56:16 +00:00
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
std::unordered_map<int, COLOR4D>* m_map;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|