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 <json_common.h>
27
28#include <tool/action_toolbar.h>
31
34
35void to_json( nlohmann::json& aJson, const TOOLBAR_ITEM& aItem )
36{
37 aJson = { { "type", magic_enum::enum_name( aItem.m_Type ) } };
38
39 switch( aItem.m_Type )
40 {
42 // Nothing to add for a separator
43 break;
44
46 aJson["size"] = aItem.m_Size;
47 break;
48
50 aJson["name"] = aItem.m_ControlName;
51 break;
52
54 aJson["name"] = aItem.m_ActionName;
55 break;
56
58 aJson["group_name"] = aItem.m_GroupName;
59
60 nlohmann::json grpItems = nlohmann::json::array();
61
62 for( const auto& it : aItem.m_GroupItems )
63 grpItems.push_back( it );
64
65 aJson["group_items"] = grpItems;
66
67 break;
68 }
69}
70
71
72void from_json( const nlohmann::json& aJson, TOOLBAR_ITEM& aItem )
73{
74 if( aJson.empty() )
75 return;
76
77 if( aJson.contains( "type" ) )
78 {
79 auto type = magic_enum::enum_cast<TOOLBAR_ITEM_TYPE>( aJson["type"].get<std::string>(),
80 magic_enum::case_insensitive );
81
82 if( type.has_value() )
83 aItem.m_Type = type.value();
84 }
85
86 switch( aItem.m_Type )
87 {
89 // Nothing to read for a separator
90 break;
91
93 if( aJson.contains( "size" ) )
94 aItem.m_Size = aJson["size"].get<int>();
95
96 break;
97
99 if( aJson.contains( "name" ) )
100 aItem.m_ControlName = aJson["name"].get<std::string>();
101
102 break;
103
105 if( aJson.contains( "name" ) )
106 aItem.m_ActionName = aJson["name"].get<std::string>();
107
108 break;
109
111 if( aJson.contains( "group_name" ) )
112 aItem.m_GroupName = aJson["group_name"].get<wxString>();
113
114 if( aJson.contains( "group_items" ) )
115 {
116 for( const nlohmann::json& it : aJson.at( "group_items" ) )
117 aItem.m_GroupItems.push_back( it.get<TOOLBAR_ITEM>() );
118 }
119 break;
120 }
121}
122
123
124void to_json( nlohmann::json& aJson, const TOOLBAR_CONFIGURATION& aConfig )
125{
126 aJson = nlohmann::json::array();
127
128 for( const TOOLBAR_ITEM& item : aConfig.m_toolbarItems )
129 aJson.push_back( item );
130}
131
132
133void from_json( const nlohmann::json& aJson, TOOLBAR_CONFIGURATION& aConfig )
134{
135 if( aJson.empty() )
136 return;
137
138 aConfig.m_toolbarItems.clear();
139
140 if( aJson.is_array() )
141 {
142 for( const nlohmann::json& item : aJson )
143 aConfig.m_toolbarItems.push_back( item.get<TOOLBAR_ITEM>() );
144 }
145}
146
147
148TOOLBAR_SETTINGS::TOOLBAR_SETTINGS( const wxString& aFullPath ) :
150{
151 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "toolbars",
152 [&]() -> nlohmann::json
153 {
154 // Serialize the toolbars
155 nlohmann::json js = nlohmann::json::array();
156
157 for( const auto& [loc, tb] : m_toolbars )
158 {
159 js.push_back( nlohmann::json( { { "name", magic_enum::enum_name( loc ) },
160 { "contents", tb } } ) );
161 }
162
163 return js;
164 },
165 [&]( const nlohmann::json& aObj )
166 {
167 // Deserialize the toolbars
168 m_toolbars.clear();
169
170 if( !aObj.is_array() )
171 return;
172
173 for( const auto& entry : aObj )
174 {
175 if( entry.empty() || !entry.is_object() )
176 continue;
177
178 auto loc = magic_enum::enum_cast<TOOLBAR_LOC>( entry["name"].get<std::string>(),
179 magic_enum::case_insensitive );
180
181 if( loc.has_value() )
182 {
183 m_toolbars.emplace(
184 std::make_pair( loc.value(),
185 entry["contents"].get<TOOLBAR_CONFIGURATION>() ) );
186 }
187 }
188 },
189 nlohmann::json::array() ) );
190}
191
192
193std::optional<TOOLBAR_CONFIGURATION> TOOLBAR_SETTINGS::GetToolbarConfig( TOOLBAR_LOC aToolbar, bool aAllowCustom )
194{
195 // If custom is allowed, look for if a toolbar exists
196 if( aAllowCustom )
197 {
198 auto tb = m_toolbars.find( aToolbar );
199
200 if( tb != m_toolbars.end() )
201 return tb->second;
202 }
203
204 return DefaultToolbarConfig( aToolbar );
205}
206
207
208std::optional<TOOLBAR_CONFIGURATION> TOOLBAR_SETTINGS::GetStoredToolbarConfig( TOOLBAR_LOC aToolbar )
209{
210 auto tb = m_toolbars.find( aToolbar );
211
212 if( tb != m_toolbars.end() )
213 return tb->second;
214
215 // Return a nullopt if no toolbar is configured
216 return std::nullopt;
217}
218
219
222{
223 // Register the factory globally so JSON configs get the same menu
224 TOOLBAR_CONTEXT_MENU_REGISTRY::RegisterMenuFactory( m_item.m_ActionName, std::move( aFactory ) );
225 return m_parent;
226}
227
228
229// Forwarding methods for TOOLBAR_ITEM_REF to enable chaining
230
231TOOLBAR_ITEM_REF TOOLBAR_ITEM_REF::AppendAction( const std::string& aActionName )
232{
233 return m_parent.AppendAction( aActionName );
234}
235
236
238{
239 return m_parent.AppendAction( aAction );
240}
241
242
244{
245 return m_parent.AppendSeparator();
246}
247
248
250{
251 return m_parent.AppendSpacer( aSize );
252}
253
254
256{
257 return m_parent.AppendGroup( aGroup );
258}
259
260
262{
263 return m_parent.AppendControl( aControlName );
264}
265
266
268{
269 return m_parent.AppendControl( aControl );
270}
Class to hold basic information about controls that can be added to the toolbars.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:296
std::vector< TOOLBAR_ITEM > m_toolbarItems
static void RegisterMenuFactory(const std::string &aActionName, MENU_FACTORY aFactory)
Register a context menu factory for an action.
std::function< std::unique_ptr< ACTION_MENU >(TOOL_MANAGER *)> MENU_FACTORY
Factory function type: takes TOOL_MANAGER, returns owned ACTION_MENU.
TOOLBAR_CONFIGURATION & AppendSeparator()
TOOLBAR_CONFIGURATION & WithContextMenu(TOOLBAR_CONTEXT_MENU_REGISTRY::MENU_FACTORY aFactory)
Associate a context menu factory with this action.
TOOLBAR_ITEM_REF(class TOOLBAR_CONFIGURATION &aParent, TOOLBAR_ITEM &aItem)
TOOLBAR_CONFIGURATION & AppendGroup(const TOOLBAR_GROUP_CONFIG &aGroup)
TOOLBAR_CONFIGURATION & m_parent
TOOLBAR_ITEM_REF AppendAction(const std::string &aActionName)
TOOLBAR_CONFIGURATION & AppendControl(const std::string &aControlName)
TOOLBAR_CONFIGURATION & AppendSpacer(int aSize)
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
Represent a single user action.
SETTINGS_LOC
@ 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)