@@ -33,6 +33,8 @@ def get_version(rel_path: str) -> str:
3333
3434class Dialog (dialog_text_base .DIALOG_TEXT_BASE ):
3535
36+ empty_config = {'Layers' :{},'Edges' :{}}
37+ # Default config in JSON style
3638 config_defaults = {
3739 'Layers' :
3840 {
@@ -47,10 +49,24 @@ class Dialog(dialog_text_base.DIALOG_TEXT_BASE):
4749 'F.Silkscreen' : 'true' ,
4850 'B.Silkscreen' : 'true' ,
4951 'F.Mask' : 'true' ,
50- 'B.Mask' : 'true'
52+ 'B.Mask' : 'true' ,
53+ 'Edge.Cuts' : 'false' ,
54+ 'User.Comments' : 'false'
5155 },
5256 'Edges' :
5357 {
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' ,
5470 'Edge.Cuts' : 'true' ,
5571 'User.Comments' : 'true'
5672 }
@@ -77,23 +93,58 @@ def __init__(self, parent, config, layertable, cammer, func):
7793 self .SetTitle (_APP_NAME + " - " + _APP_VERSION )
7894
7995
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
83101 for key in self .config .keys ():
102+ deleteThese = []
84103 for layer in self .config [key ].keys ():
85104 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
89140 self .LoadSettings ()
90141
91142 # Autosize now grid is populated
92143 self .LayersGrid .AutoSizeColumns ()
93144 self .EdgesGrid .AutoSizeColumns ()
94145
95146 def loadConfig (self ):
96- # Load up last sessions config
147+ ''' Load the last sessions configuration from json file '''
97148 try :
98149 with open (self .config_file , 'r' ) as cf :
99150 json_params = json .load (cf )
@@ -103,6 +154,7 @@ def loadConfig(self):
103154 pass
104155
105156 def saveConfig (self ):
157+ ''' Read the configuration from the Dialog grids and save to file '''
106158 try :
107159 with open (self .config_file , 'w' ) as cf :
108160 json .dump (self .CurrentSettings (), cf , indent = 2 )
@@ -111,16 +163,18 @@ def saveConfig(self):
111163 pass
112164
113165 def LoadSettings (self ):
166+ ''' Load the settings from self.config into the Dialog grids '''
167+
114168 # Delete any existing rows in LayersGrid
115169 if self .LayersGrid .NumberRows :
116170 self .LayersGrid .DeleteRows (0 , self .LayersGrid .NumberRows )
117171 # Append empty rows based on layertable
118172 self .LayersGrid .AppendRows (len (self .layertable ))
119173 # Initialize them
120174 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 )
124178 self .LayersGrid .SetCellRenderer (row , 0 , wx .grid .GridCellBoolRenderer ())
125179 self .LayersGrid .SetCellValue (row , 1 , layer )
126180 self .LayersGrid .SetReadOnly (row , 1 )
@@ -133,26 +187,28 @@ def LoadSettings(self):
133187 self .EdgesGrid .AppendRows (len (self .layertable ))
134188 # Initialize them
135189 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 )
139193 self .EdgesGrid .SetCellRenderer (row , 0 , wx .grid .GridCellBoolRenderer ())
140194 self .EdgesGrid .SetCellValue (row , 1 , layer )
141195 self .EdgesGrid .SetReadOnly (row , 1 )
142196 row += 1
143197
144198 def CurrentSettings (self ):
145- params = {}
199+ params = self . empty_config
146200
147201 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
149203 layer = self .LayersGrid .GetCellValue (row , 1 )
150- params ['Layers' ][layer ] = enabled
204+ d = {layer : enabled }
205+ params ['Layers' ].update (d )
151206
152207 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
154209 layer = self .EdgesGrid .GetCellValue (row , 1 )
155- params ['Edges' ][layer ] = enabled
210+ d = {layer : enabled }
211+ params ['Edges' ].update (d )
156212
157213 return params
158214
0 commit comments