Debian Codegraph @

wxcrafter/src/wxc_settings.cpp

raw
1 #include "wxc_settings.h"
2
3 #include "wxgui_helpers.h"
4
5 #include <wx/filename.h>
6
7 wxcSettings::wxcSettings()
8 : m_flags(0)
9 , m_initCompleted(false)
10 {
11 Load();
12 }
13
14 wxcSettings::~wxcSettings() {}
15
16 wxcSettings& wxcSettings::Get()
17 {
18 static wxcSettings _settings;
19 return _settings;
20 }
21
22 void wxcSettings::Load()
23 {
24 wxFileName fn(wxCrafter::GetConfigFile());
25 JSONRoot root(fn);
26 if(root.isOk()) {
27 m_flags = root.toElement().namedObject("m_annoyDialogs").toInt(m_flags);
28 m_flags &= ~USE_TABBED_MODE;
29 m_sashPosition = root.toElement().namedObject("m_sashPosition").toInt(150);
30 m_secondarySashPos = root.toElement().namedObject("m_secondarySashPos").toInt(-1);
31 m_treeviewSashPos = root.toElement().namedObject("m_treeviewSashPos").toInt(-1);
32 m_history = root.toElement().namedObject("recentFiles").toArrayString();
33
34 JSONElement arr = root.toElement().namedObject("m_templateClasses");
35 m_templateClasses.clear();
36 for(int i = 0; i < arr.arraySize(); ++i) {
37 CustomControlTemplate templateControl;
38 templateControl.FromJSON(arr.arrayItem(i));
39 templateControl.SetControlId(::wxNewEventType());
40 m_templateClasses.insert(std::make_pair(templateControl.GetClassName(), templateControl));
41 }
42 }
43 }
44
45 void wxcSettings::Save()
46 {
47 wxFileName fn(wxCrafter::GetConfigFile());
48
49 JSONRoot root(cJSON_Object);
50 m_flags &= ~USE_TABBED_MODE;
51 root.toElement().addProperty("m_annoyDialogs", (int)m_flags);
52 root.toElement().addProperty("m_sashPosition", m_sashPosition);
53 root.toElement().addProperty("m_secondarySashPos", m_secondarySashPos);
54 root.toElement().addProperty("m_treeviewSashPos", m_treeviewSashPos);
55 root.toElement().addProperty("recentFiles", m_history);
56
57 JSONElement arr = JSONElement::createArray("m_templateClasses");
58 root.toElement().append(arr);
59
60 CustomControlTemplateMap_t::const_iterator iter = m_templateClasses.begin();
61 for(; iter != m_templateClasses.end(); ++iter) {
62 arr.append(iter->second.ToJSON());
63 }
64 root.save(fn);
65 }
66
67 void wxcSettings::RegisterCustomControl(CustomControlTemplate& cct)
68 {
69 CustomControlTemplateMap_t::iterator iter = m_templateClasses.find(cct.GetClassName());
70 if(iter != m_templateClasses.end()) {
71 m_templateClasses.erase(iter);
72 }
73 cct.SetControlId(::wxNewEventType());
74 m_templateClasses.insert(std::make_pair(cct.GetClassName(), cct));
75 }
76
77 CustomControlTemplate wxcSettings::FindByControlId(int controlId) const
78 {
79 CustomControlTemplateMap_t::const_iterator iter = m_templateClasses.begin();
80 for(; iter != m_templateClasses.end(); ++iter) {
81 if(iter->second.GetControlId() == controlId) {
82 return iter->second;
83 }
84 }
85 return CustomControlTemplate();
86 }
87
88 CustomControlTemplate wxcSettings::FindByControlName(const wxString& name) const
89 {
90 CustomControlTemplateMap_t::const_iterator iter = m_templateClasses.find(name);
91 if(iter == m_templateClasses.end()) {
92 return CustomControlTemplate();
93 }
94 return iter->second;
95 }
96
97 void wxcSettings::DeleteCustomControl(const wxString& name)
98 {
99 CustomControlTemplateMap_t::iterator iter = m_templateClasses.find(name);
100 if(iter == m_templateClasses.end()) {
101 return;
102 }
103
104 m_templateClasses.erase(iter);
105 }
106
107 void wxcSettings::MergeCustomControl(const JSONElement& arr)
108 {
109 for(int i = 0; i < arr.arraySize(); ++i) {
110 CustomControlTemplate templateControl;
111 templateControl.FromJSON(arr.arrayItem(i));
112 templateControl.SetControlId(::wxNewEventType());
113
114 if(m_templateClasses.count(templateControl.GetClassName())) {
115 // an instance with this name already exists, replace it
116 m_templateClasses.erase(templateControl.GetClassName());
117 }
118 m_templateClasses.insert(std::make_pair(templateControl.GetClassName(), templateControl));
119 }
120
121 // Apply the changes
122 Save();
123 }
124
125 JSONElement wxcSettings::GetCustomControlsAsJSON(const wxArrayString& controls) const
126 {
127 JSONElement arr = JSONElement::createArray("m_templateClasses");
128 CustomControlTemplateMap_t::const_iterator iter = m_templateClasses.begin();
129 for(; iter != m_templateClasses.end(); ++iter) {
130 if(controls.Index(iter->first) != wxNOT_FOUND) {
131 arr.append(iter->second.ToJSON());
132 }
133 }
134 return arr;
135 }
136
137 void wxcSettings::ShowNagDialogIfNeeded() {}
138
139 // ----------------------------------------------------------------------
140 // CustomControlTemplate
141 // ----------------------------------------------------------------------
142
143 CustomControlTemplate::CustomControlTemplate()
144 : m_controlId(wxNOT_FOUND)
145 {
146 }
147
148 CustomControlTemplate::~CustomControlTemplate() {}
149
150 void CustomControlTemplate::FromJSON(const JSONElement& json)
151 {
152 m_includeFile = json.namedObject("m_includeFile").toString();
153 m_allocationLine = json.namedObject("m_allocationLine").toString();
154 m_className = json.namedObject("m_className").toString();
155 m_xrcPreviewClass = json.namedObject("m_xrcPreviewClass").toString();
156 m_events = json.namedObject("m_events").toStringMap();
157 }
158
159 JSONElement CustomControlTemplate::ToJSON() const
160 {
161 JSONElement obj = JSONElement::createObject();
162 obj.addProperty("m_includeFile", m_includeFile);
163 obj.addProperty("m_allocationLine", m_allocationLine);
164 obj.addProperty("m_className", m_className);
165 obj.addProperty("m_xrcPreviewClass", m_xrcPreviewClass);
166 obj.addProperty("m_events", m_events);
167 return obj;
168 }