@@ -33,6 +33,8 @@ def get_version(rel_path: str) -> str:
33
33
34
34
class Dialog (dialog_text_base .DIALOG_TEXT_BASE ):
35
35
36
+ empty_config = {'Layers' :{},'Edges' :{}}
37
+ # Default config in JSON style
36
38
config_defaults = {
37
39
'Layers' :
38
40
{
@@ -47,10 +49,24 @@ class Dialog(dialog_text_base.DIALOG_TEXT_BASE):
47
49
'F.Silkscreen' : 'true' ,
48
50
'B.Silkscreen' : 'true' ,
49
51
'F.Mask' : 'true' ,
50
- 'B.Mask' : 'true'
52
+ 'B.Mask' : 'true' ,
53
+ 'Edge.Cuts' : 'false' ,
54
+ 'User.Comments' : 'false'
51
55
},
52
56
'Edges' :
53
57
{
58
+ 'F.Cu' : 'false' ,
59
+ 'In1.Cu' : 'false' ,
60
+ 'In2.Cu' : 'false' ,
61
+ 'In3.Cu' : 'false' ,
62
+ 'In4.Cu' : 'false' ,
63
+ 'B.Cu' : 'false' ,
64
+ 'F.Paste' : 'false' ,
65
+ 'B.Paste' : 'false' ,
66
+ 'F.Silkscreen' : 'false' ,
67
+ 'B.Silkscreen' : 'false' ,
68
+ 'F.Mask' : 'false' ,
69
+ 'B.Mask' : 'false' ,
54
70
'Edge.Cuts' : 'true' ,
55
71
'User.Comments' : 'true'
56
72
}
@@ -77,23 +93,58 @@ def __init__(self, parent, config, layertable, cammer, func):
77
93
self .SetTitle (_APP_NAME + " - " + _APP_VERSION )
78
94
79
95
80
- # Get config with defaults based on layertable
81
- self .config = self .config_defaults
82
- # Delete and defaults not present in layertable
96
+ # Load up last sessions config
97
+ self .config = self .empty_config
98
+ self .loadConfig ()
99
+
100
+ # Delete any config items not present in layertable - just in case layertable has changed
83
101
for key in self .config .keys ():
102
+ deleteThese = []
84
103
for layer in self .config [key ].keys ():
85
104
if layer not in layertable .keys ():
86
- self .config [key ].pop (layer , None )
87
-
88
- self .loadConfig ()
105
+ deleteThese .append (layer ) # Avoids "dictionary changed size during iteration"
106
+ for d in deleteThese :
107
+ self .config [key ].pop (d , None )
108
+
109
+ # Copy the configuration defaults
110
+ # Delete any layers present in the config from file - so we don't overwrite them
111
+ # Also delete those which are not in layertable
112
+ # Then update (copy) them into the config
113
+ defaults = self .config_defaults
114
+ for key in defaults .keys ():
115
+ deleteThese = []
116
+ for layer in defaults [key ].keys ():
117
+ hasKey = False
118
+ try :
119
+ hasKey = layer in self .config [key ].keys ()
120
+ except :
121
+ pass
122
+ if (layer not in layertable .keys ()) or hasKey :
123
+ deleteThese .append (layer )
124
+ for d in deleteThese :
125
+ defaults [key ].pop (d , None )
126
+ for key in defaults .keys ():
127
+ self .config [key ].update (defaults [key ])
128
+
129
+ # Add any extra layers which are present in layertable - default these to disabled
130
+ for key in self .config .keys ():
131
+ addThese = []
132
+ for layer in layertable .keys ():
133
+ if layer not in self .config [key ].keys ():
134
+ addThese .append (layer )
135
+ for a in addThese :
136
+ d = {a : 'false' } # JSON style
137
+ self .config [key ].update (d )
138
+
139
+ # Load the configuration into the Dialog grids
89
140
self .LoadSettings ()
90
141
91
142
# Autosize now grid is populated
92
143
self .LayersGrid .AutoSizeColumns ()
93
144
self .EdgesGrid .AutoSizeColumns ()
94
145
95
146
def loadConfig (self ):
96
- # Load up last sessions config
147
+ ''' Load the last sessions configuration from json file '''
97
148
try :
98
149
with open (self .config_file , 'r' ) as cf :
99
150
json_params = json .load (cf )
@@ -103,6 +154,7 @@ def loadConfig(self):
103
154
pass
104
155
105
156
def saveConfig (self ):
157
+ ''' Read the configuration from the Dialog grids and save to file '''
106
158
try :
107
159
with open (self .config_file , 'w' ) as cf :
108
160
json .dump (self .CurrentSettings (), cf , indent = 2 )
@@ -111,16 +163,18 @@ def saveConfig(self):
111
163
pass
112
164
113
165
def LoadSettings (self ):
166
+ ''' Load the settings from self.config into the Dialog grids '''
167
+
114
168
# Delete any existing rows in LayersGrid
115
169
if self .LayersGrid .NumberRows :
116
170
self .LayersGrid .DeleteRows (0 , self .LayersGrid .NumberRows )
117
171
# Append empty rows based on layertable
118
172
self .LayersGrid .AppendRows (len (self .layertable ))
119
173
# Initialize them
120
174
row = 0
121
- for layer in self .layertable . keys ():
122
- enabled = "1" if ( layer in self . config [ 'Layers' ]. keys ()) else "0"
123
- self .LayersGrid .SetCellValue (row , 0 , enabled )
175
+ for layer , enabled in self .config [ 'Layers' ]. items ():
176
+ e = "1" if enabled == 'true' else "0" # JSON style
177
+ self .LayersGrid .SetCellValue (row , 0 , e )
124
178
self .LayersGrid .SetCellRenderer (row , 0 , wx .grid .GridCellBoolRenderer ())
125
179
self .LayersGrid .SetCellValue (row , 1 , layer )
126
180
self .LayersGrid .SetReadOnly (row , 1 )
@@ -133,26 +187,28 @@ def LoadSettings(self):
133
187
self .EdgesGrid .AppendRows (len (self .layertable ))
134
188
# Initialize them
135
189
row = 0
136
- for layer in self .layertable . keys ():
137
- enabled = "1" if ( layer in self . config [ 'Edges' ]. keys ()) else "0"
138
- self .EdgesGrid .SetCellValue (row , 0 , enabled )
190
+ for layer , enabled in self .config [ 'Edges' ]. items ():
191
+ e = "1" if enabled == 'true' else "0" # JSON style
192
+ self .EdgesGrid .SetCellValue (row , 0 , e )
139
193
self .EdgesGrid .SetCellRenderer (row , 0 , wx .grid .GridCellBoolRenderer ())
140
194
self .EdgesGrid .SetCellValue (row , 1 , layer )
141
195
self .EdgesGrid .SetReadOnly (row , 1 )
142
196
row += 1
143
197
144
198
def CurrentSettings (self ):
145
- params = {}
199
+ params = self . empty_config
146
200
147
201
for row in range (self .LayersGrid .GetNumberRows ()):
148
- enabled = True if (self .LayersGrid .GetCellValue (row , 0 ) == "1" ) else False
202
+ enabled = 'true' if (self .LayersGrid .GetCellValue (row , 0 ) == "1" ) else 'false' # JSON style
149
203
layer = self .LayersGrid .GetCellValue (row , 1 )
150
- params ['Layers' ][layer ] = enabled
204
+ d = {layer : enabled }
205
+ params ['Layers' ].update (d )
151
206
152
207
for row in range (self .EdgesGrid .GetNumberRows ()):
153
- enabled = True if (self .EdgesGrid .GetCellValue (row , 0 ) == "1" ) else False
208
+ enabled = 'true' if (self .EdgesGrid .GetCellValue (row , 0 ) == "1" ) else 'false' # JSON style
154
209
layer = self .EdgesGrid .GetCellValue (row , 1 )
155
- params ['Edges' ][layer ] = enabled
210
+ d = {layer : enabled }
211
+ params ['Edges' ].update (d )
156
212
157
213
return params
158
214
0 commit comments