KiCad PCB EDA Suite
Loading...
Searching...
No Matches
toolbar_configuration.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Ian McInerney
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <magic_enum.hpp>
26#include <nlohmann/json.hpp>
27
28#include <tool/action_toolbar.h>
30
33
34void to_json( nlohmann::json& aJson, const TOOLBAR_ITEM& aItem )
35{
36 aJson = { { "type", magic_enum::enum_name( aItem.m_Type ) } };
37
38 switch( aItem.m_Type )
39 {
41 // Nothing to add for a separator
42 break;
43
45 aJson["size"] = aItem.m_Size;
46 break;
47
49 aJson["name"] = aItem.m_ControlName;
50 break;
51
53 aJson["name"] = aItem.m_ActionName;
54 break;
55
57 aJson["group_name"] = aItem.m_GroupName;
58
59 nlohmann::json grpItems = nlohmann::json::array();
60
61 for( const auto& it : aItem.m_GroupItems )
62 grpItems.push_back( it );
63
64 aJson["group_items"] = grpItems;
65
66 break;
67 }
68}
69
70
71void from_json( const nlohmann::json& aJson, TOOLBAR_ITEM& aItem )
72{
73 if( aJson.empty() )
74 return;
75
76 if( aJson.contains( "type" ) )
77 {
78 auto type = magic_enum::enum_cast<TOOLBAR_ITEM_TYPE>( aJson["type"].get<std::string>(),
79 magic_enum::case_insensitive );
80
81 if( type.has_value() )
82 aItem.m_Type = type.value();
83 }
84
85 switch( aItem.m_Type )
86 {
88 // Nothing to read for a separator
89 break;
90
92 if( aJson.contains( "size" ) )
93 aItem.m_Size = aJson["size"].get<int>();
94
95 break;
96
98 if( aJson.contains( "name" ) )
99 aItem.m_ControlName = aJson["name"].get<std::string>();
100
101 break;
102
104 if( aJson.contains( "name" ) )
105 aItem.m_ActionName = aJson["name"].get<std::string>();
106
107 break;
108
110 if( aJson.contains( "group_name" ) )
111 aItem.m_GroupName = aJson["group_name"].get<wxString>();
112
113 if( aJson.contains( "group_items" ) )
114 {
115 for( const nlohmann::json& it : aJson.at( "group_items" ) )
116 aItem.m_GroupItems.push_back( it.get<TOOLBAR_ITEM>() );
117 }
118 break;
119 }
120}
121
122
123void to_json( nlohmann::json& aJson, const TOOLBAR_CONFIGURATION& aConfig )
124{
125 aJson = nlohmann::json::array();
126
127 for( const TOOLBAR_ITEM& item : aConfig.m_toolbarItems )
128 aJson.push_back( item );
129}
130
131
132void from_json( const nlohmann::json& aJson, TOOLBAR_CONFIGURATION& aConfig )
133{
134 if( aJson.empty() )
135 return;
136
137 aConfig.m_toolbarItems.clear();
138
139 if( aJson.is_array() )
140 {
141 for( const nlohmann::json& item : aJson )
142 aConfig.m_toolbarItems.push_back( item.get<TOOLBAR_ITEM>() );
143 }
144}
145
146
147TOOLBAR_SETTINGS::TOOLBAR_SETTINGS( const wxString& aFullPath ) :
149{
150 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "toolbars",
151 [&]() -> nlohmann::json
152 {
153 // Serialize the toolbars
154 nlohmann::json js = nlohmann::json::array();
155
156 for( const auto& [loc, tb] : m_toolbars )
157 {
158 js.push_back( nlohmann::json( { { "name", magic_enum::enum_name( loc ) },
159 { "contents", tb } } ) );
160 }
161
162 return js;
163 },
164 [&]( const nlohmann::json& aObj )
165 {
166 // Deserialize the toolbars
167 m_toolbars.clear();
168
169 if( !aObj.is_array() )
170 return;
171
172 for( const auto& entry : aObj )
173 {
174 if( entry.empty() || !entry.is_object() )
175 continue;
176
177 auto loc = magic_enum::enum_cast<TOOLBAR_LOC>( entry["name"].get<std::string>(),
178 magic_enum::case_insensitive );
179
180 if( loc.has_value() )
181 {
182 m_toolbars.emplace(
183 std::make_pair( loc.value(),
184 entry["contents"].get<TOOLBAR_CONFIGURATION>() ) );
185 }
186 }
187 },
188 nlohmann::json::array() ) );
189}
190
191
192std::optional<TOOLBAR_CONFIGURATION> TOOLBAR_SETTINGS::GetToolbarConfig( TOOLBAR_LOC aToolbar, bool aAllowCustom )
193{
194 // If custom is allowed, look for if a toolbar exists
195 if( aAllowCustom )
196 {
197 auto tb = m_toolbars.find( aToolbar );
198
199 if( tb != m_toolbars.end() )
200 return tb->second;
201 }
202
203 return DefaultToolbarConfig( aToolbar );
204}
205
206
207std::optional<TOOLBAR_CONFIGURATION> TOOLBAR_SETTINGS::GetStoredToolbarConfig( TOOLBAR_LOC aToolbar )
208{
209 auto tb = m_toolbars.find( aToolbar );
210
211 if( tb != m_toolbars.end() )
212 return tb->second;
213
214 // Return a nullopt if no toolbar is configured
215 return std::nullopt;
216}
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:295
std::vector< TOOLBAR_ITEM > m_toolbarItems
std::vector< TOOLBAR_ITEM > m_GroupItems
std::string m_ActionName
TOOLBAR_ITEM_TYPE m_Type
std::string m_ControlName
TOOLBAR_SETTINGS(const wxString &aFilename)
virtual std::optional< TOOLBAR_CONFIGURATION > DefaultToolbarConfig(TOOLBAR_LOC aToolbar)
Get the default tools to show on the specified canvas toolbar.
std::optional< TOOLBAR_CONFIGURATION > GetStoredToolbarConfig(TOOLBAR_LOC aToolbar)
Get the stored configuration for the given toolbar.
std::optional< TOOLBAR_CONFIGURATION > GetToolbarConfig(TOOLBAR_LOC aToolbar, bool aAllowCustom=true)
Get the tools to show on the specified canvas toolbar.
std::map< TOOLBAR_LOC, TOOLBAR_CONFIGURATION > m_toolbars
SETTINGS_LOC
Definition: json_settings.h:54
@ TOOLBARS
The toolbar directory (e.g. ~/.config/kicad/toolbars/)
const int toolbarSchemaVersion
! Update the schema version whenever a migration is required
void to_json(nlohmann::json &aJson, const TOOLBAR_ITEM &aItem)
void from_json(const nlohmann::json &aJson, TOOLBAR_ITEM &aItem)