source: sasview/sansguiframe/src/sans/guiframe/CategoryManager.py @ 6034e16

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 6034e16 was 6034e16, checked in by Jae Cho <jhjcho@…>, 12 years ago

MAC: fixed the remove in cat dial

  • Property mode set to 100755
File size: 16.9 KB
Line 
1#!/usr/bin/python
2
3"""
4
5/**
6        This software was developed by Institut Laue-Langevin as part of
7        Distributed Data Analysis of Neutron Scattering Experiments (DANSE).
8
9        Copyright 2012 Institut Laue-Langevin
10
11**/
12
13"""
14
15
16import wx
17import sys
18import os
19from wx.lib.mixins.listctrl import CheckListCtrlMixin, ListCtrlAutoWidthMixin
20from collections import defaultdict
21import cPickle as pickle
22from sans.guiframe.events import ChangeCategoryEvent
23from sans.guiframe.CategoryInstaller import CategoryInstaller
24IS_MAC = (sys.platform == 'darwin')
25
26""" Notes
27The category manager mechanism works from 3 data structures used:
28- self.master_category_dict: keys are the names of categories,
29the values are lists of tuples,
30the first being the model names (the models belonging to that
31category), the second a boolean
32of whether or not the model is enabled
33- self.by_model_dict: keys are model names, values are a list
34of categories belonging to that model
35- self.model_enabled_dict: keys are model names, values are
36bools of whether the model is enabled
37use self._regenerate_model_dict() to create the latter two
38structures from the former
39use self._regenerate_master_dict() to create the first
40structure from the latter two
41
42The need for so many data structures comes from the fact
43sometimes we need fast access
44to all the models in a category (eg user selection from the gui)
45and sometimes we need access to all the categories
46corresponding to a model (eg user modification of model categories)
47
48"""
49
50
51
52class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin, 
53                    ListCtrlAutoWidthMixin):
54    """
55    Taken from
56    http://zetcode.com/wxpython/advanced/
57    """
58
59    def __init__(self, parent, callback_func):
60        """
61        Initialization
62        :param parent: Parent window
63        :param callback_func: A function to be called when
64        an element is clicked
65        """
66        wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT \
67                                 | wx.SUNKEN_BORDER)
68        CheckListCtrlMixin.__init__(self)
69        ListCtrlAutoWidthMixin.__init__(self)
70
71        self.callback_func = callback_func
72       
73    def OnCheckItem(self, index, flag):
74        """
75        When the user checks the item we need to save that state
76        """
77        self.callback_func(index, flag)
78   
79
80class CategoryManager(wx.Frame):
81    """
82    A class for managing categories
83    """
84    def __init__(self, parent, win_id, title):
85        """
86        Category Manager Dialog class
87        :param win_id: A new wx ID
88        :param title: Title for the window
89        """
90       
91        # make sure the category file is where it should be
92        self.performance_blocking = False
93
94        self.master_category_dict = defaultdict(list)
95        self.by_model_dict = defaultdict(list)
96        self.model_enabled_dict = defaultdict(bool)
97
98        wx.Frame.__init__(self, parent, win_id, title, size=(650, 400))
99
100        panel = wx.Panel(self, -1)
101        self.parent = parent
102
103        self._read_category_info()
104
105
106        vbox = wx.BoxSizer(wx.VERTICAL)
107        hbox = wx.BoxSizer(wx.HORIZONTAL)
108
109        left_panel = wx.Panel(panel, -1)
110        right_panel = wx.Panel(panel, -1)
111
112        self.cat_list = CheckListCtrl(right_panel, self._on_check)
113        self.cat_list.InsertColumn(0, 'Model', width = 280)
114        self.cat_list.InsertColumn(1, 'Category', width = 240)
115
116        self._fill_lists() 
117        self._regenerate_model_dict()
118        self._set_enabled()     
119
120        vbox2 = wx.BoxSizer(wx.VERTICAL)
121
122        sel = wx.Button(left_panel, -1, 'Enable All', size=(100, -1))
123        des = wx.Button(left_panel, -1, 'Disable All', size=(100, -1))
124        modify_button = wx.Button(left_panel, -1, 'Modify', 
125                                  size=(100, -1))
126        ok_button = wx.Button(left_panel, -1, 'OK', size=(100, -1))
127        cancel_button = wx.Button(left_panel, -1, 'Cancel', 
128                                  size=(100, -1))       
129
130       
131
132        self.Bind(wx.EVT_BUTTON, self._on_selectall, 
133                  id=sel.GetId())
134        self.Bind(wx.EVT_BUTTON, self._on_deselectall, 
135                  id=des.GetId())
136        self.Bind(wx.EVT_BUTTON, self._on_apply, 
137                  id = modify_button.GetId())
138        self.Bind(wx.EVT_BUTTON, self._on_ok, 
139                  id = ok_button.GetId())
140        self.Bind(wx.EVT_BUTTON, self._on_cancel, 
141                  id = cancel_button.GetId())
142
143        vbox2.Add(modify_button, 0, wx.TOP, 10)
144        vbox2.Add((-1, 20))
145        vbox2.Add(sel)
146        vbox2.Add(des)
147        vbox2.Add((-1, 20))
148        vbox2.Add(ok_button)
149        vbox2.Add(cancel_button)
150
151        left_panel.SetSizer(vbox2)
152
153        vbox.Add(self.cat_list, 1, wx.EXPAND | wx.TOP, 3)
154        vbox.Add((-1, 10))
155
156
157        right_panel.SetSizer(vbox)
158
159        hbox.Add(left_panel, 0, wx.EXPAND | wx.RIGHT, 5)
160        hbox.Add(right_panel, 1, wx.EXPAND)
161        hbox.Add((3, -1))
162
163        panel.SetSizer(hbox)
164        self.performance_blocking = True
165
166
167        self.Centre()
168        self.Show(True)
169
170        # gui stuff finished
171
172    def _on_check(self, index, flag):
173        """
174        When the user checks an item we need to immediately save that state.
175        :param index: The index of the checked item
176        :param flag: True or False whether the item was checked
177        """
178        if self.performance_blocking:
179            # for computational reasons we don't want to
180            # call this function every time the gui is set up
181            model_name = self.cat_list.GetItem(index, 0).GetText()
182            self.model_enabled_dict[model_name] = flag
183            self._regenerate_master_dict()
184
185
186    def _fill_lists(self):
187        """
188        Expands lists on the GUI
189        """
190        self.cat_list.DeleteAllItems()
191        model_name_list = [model for model in self.by_model_dict]
192        model_name_list.sort()
193
194        for model in model_name_list:
195            index = self.cat_list.InsertStringItem(sys.maxint, model)
196            self.cat_list.SetStringItem(index, 1, \
197                                            str(self.by_model_dict[model]).\
198                                            replace("'","").\
199                                            replace("[","").\
200                                            replace("]",""))
201
202
203           
204    def _set_enabled(self):
205        """
206        Updates enabled models from self.model_enabled_dict
207        """
208        num = self.cat_list.GetItemCount()
209        for i in range(num):
210            model_name = self.cat_list.GetItem(i, 0).GetText()
211            self.cat_list.CheckItem(i, 
212                                    self.model_enabled_dict[model_name] )
213                                   
214
215
216    def _on_selectall(self, event):
217        """
218        Callback for 'enable all'
219        """
220        self.performance_blocking = False
221        num = self.cat_list.GetItemCount()
222        for i in range(num):
223            self.cat_list.CheckItem(i)
224        for model in self.model_enabled_dict:
225            self.model_enabled_dict[model] = True
226        self._regenerate_master_dict()
227        self.performance_blocking = True
228
229    def _on_deselectall(self, event):
230        """
231        Callback for 'disable all'
232        """
233        self.performance_blocking = False
234        num = self.cat_list.GetItemCount()
235        for i in range(num):
236            self.cat_list.CheckItem(i, False)
237        for model in self.model_enabled_dict:
238            self.model_enabled_dict[model] = False
239        self._regenerate_master_dict()
240        self.performance_blocking = True
241
242    def _on_apply(self, event):
243        """
244        Call up the 'ChangeCat' dialog for category editing
245        """
246
247        if self.cat_list.GetSelectedItemCount() == 0:
248            wx.MessageBox('Please select a model', 'Error',
249                          wx.OK | wx.ICON_EXCLAMATION )
250
251        else:
252            selected_model = \
253                self.cat_list.GetItem(\
254                self.cat_list.GetFirstSelected(), 0).GetText()
255
256
257            modify_dialog = ChangeCat(self, selected_model, 
258                                      self._get_cat_list(),
259                                      self.by_model_dict[selected_model])
260           
261            if modify_dialog.ShowModal() == wx.ID_OK:
262                if not IS_MAC:
263                    self.dial_ok(modify_dialog, selected_model)
264
265    def dial_ok(self, dialog=None, model=None):
266        """
267        modify_dialog onclose
268        """
269        self.by_model_dict[model] = dialog.get_category()
270        self._regenerate_master_dict()
271        self._fill_lists()
272        self._set_enabled()
273
274
275    def _on_ok(self, event):
276        """
277        Close the manager
278        """
279        self._save_state()
280        evt = ChangeCategoryEvent()
281        wx.PostEvent(self.parent, evt)
282
283        self.Destroy()
284
285    def _on_cancel(self, event):
286        """
287        On cancel
288        """
289        self.Destroy()
290
291    def _save_state(self):
292        """
293        Serializes categorization info to file
294        """
295
296        self._regenerate_master_dict()
297
298        cat_file = open(CategoryInstaller.get_user_file(), 'wb')
299
300        pickle.dump( self.master_category_dict, cat_file )
301
302   
303    def _read_category_info(self):
304        """
305        Read in categorization info from file
306        """
307        try:
308                file = CategoryInstaller.get_user_file()
309                if os.path.isfile(file):
310                    cat_file = open(file, 'rb')
311                    self.master_category_dict = pickle.load(cat_file)
312                else:
313                        cat_file = open(CategoryInstaller.get_default_file(), 'rb')
314                        self.master_category_dict = pickle.load(cat_file)
315        except IOError:
316            print 'Problem reading in category file. Please review'
317
318
319        self._regenerate_model_dict()
320
321    def _get_cat_list(self):
322        """
323        Returns a simple list of categories
324        """
325        cat_list = list()
326        for category in self.master_category_dict.iterkeys():
327            if not category == 'Uncategorized':
328                cat_list.append(category)
329   
330        return cat_list
331
332    def _regenerate_model_dict(self):
333        """
334        regenerates self.by_model_dict which has each model
335        name as the key
336        and the list of categories belonging to that model
337        along with the enabled mapping
338        """
339        self.by_model_dict = defaultdict(list)
340        for category in self.master_category_dict:
341            for (model, enabled) in self.master_category_dict[category]:
342                self.by_model_dict[model].append(category)
343                self.model_enabled_dict[model] = enabled
344
345    def _regenerate_master_dict(self):
346        """
347        regenerates self.master_category_dict from
348        self.by_model_dict and self.model_enabled_dict
349        """
350        self.master_category_dict = defaultdict(list)
351        for model in self.by_model_dict:
352            for category in self.by_model_dict[model]:
353                self.master_category_dict[category].append\
354                    ((model, self.model_enabled_dict[model]))
355   
356
357
358class ChangeCat(wx.Dialog):
359    """
360    dialog for changing the categories of a model
361    """
362
363    def __init__(self, parent, title, cat_list, current_cats):
364        """
365        Actual editor for a certain category
366        :param parent: Window parent
367        :param title: Window title
368        :param cat_list: List of all categories
369        :param current_cats: List of categories applied to current model
370        """
371        wx.Dialog.__init__(self, parent, title = 'Change Category: '+title, size=(485, 425))
372
373        self.current_cats = current_cats
374        if str(self.current_cats[0]) == 'Uncategorized':
375            self.current_cats = []
376        self.parent = parent
377        self.selcted_model = title
378        vbox = wx.BoxSizer(wx.VERTICAL)
379        self.add_sb = wx.StaticBox(self, label = "Add Category")
380        self.add_sb_sizer = wx.StaticBoxSizer(self.add_sb, wx.VERTICAL)
381        gs = wx.GridSizer(3, 2, 5, 5)
382        self.cat_list = cat_list
383       
384        self.cat_text = wx.StaticText(self, label = "Current categories: ")
385        self.current_categories = wx.ListBox(self, 
386                                             choices = self.current_cats
387                                             , size=(300, 100))
388        self.existing_check = wx.RadioButton(self, 
389                                             label = 'Choose Existing')
390        self.new_check = wx.RadioButton(self, label = 'Create new')
391        self.exist_combo = wx.ComboBox(self, style = wx.CB_READONLY, 
392                                       size=(220,-1), choices = cat_list)
393        self.exist_combo.SetSelection(0)
394       
395       
396        self.remove_sb = wx.StaticBox(self, label = "Remove Category")
397       
398        self.remove_sb_sizer = wx.StaticBoxSizer(self.remove_sb, 
399                                                 wx.VERTICAL)
400
401        self.new_text = wx.TextCtrl(self, size=(220, -1))
402        self.ok_button = wx.Button(self, wx.ID_OK, "Done")
403        self.add_button = wx.Button(self, label = "Add")
404        self.add_button.Bind(wx.EVT_BUTTON, self.on_add)
405        self.remove_button = wx.Button(self, label = "Remove Selected")
406        self.remove_button.Bind(wx.EVT_BUTTON, self.on_remove)
407
408        self.existing_check.Bind(wx.EVT_RADIOBUTTON, self.on_existing)
409        self.new_check.Bind(wx.EVT_RADIOBUTTON, self.on_newcat)
410        self.existing_check.SetValue(True)
411
412        vbox.Add(self.cat_text, flag = wx.LEFT | wx.TOP | wx.ALIGN_LEFT, 
413                 border = 10)
414        vbox.Add(self.current_categories, flag = wx.ALL | wx.EXPAND, 
415                 border = 10  )
416
417        gs.AddMany( [ (self.existing_check, 5, wx.ALL),
418                      (self.exist_combo, 5, wx.ALL),
419                      (self.new_check, 5, wx.ALL),
420                      (self.new_text, 5, wx.ALL ),
421                      ((-1,-1)),
422                      (self.add_button, 5, wx.ALL | wx.ALIGN_RIGHT) ] )
423
424        self.add_sb_sizer.Add(gs, proportion = 1, flag = wx.ALL, border = 5)
425        vbox.Add(self.add_sb_sizer, flag = wx.ALL | wx.EXPAND, border = 10)
426
427        self.remove_sb_sizer.Add(self.remove_button, border = 5, 
428                                 flag = wx.ALL | wx.ALIGN_RIGHT)
429        vbox.Add(self.remove_sb_sizer, 
430                 flag = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, 
431                 border = 10)
432        vbox.Add(self.ok_button, flag = wx.ALL | wx.ALIGN_RIGHT, 
433                 border = 10)
434       
435        if self.current_categories.GetCount() > 0:
436                self.current_categories.SetSelection(0)
437        self.new_text.Disable()
438        self.SetSizer(vbox)
439        self.Centre()
440        self.Show(True)
441        if IS_MAC:
442            self.ok_button.Bind(wx.EVT_BUTTON, self.on_ok_mac)
443
444    def on_ok_mac(self, event):
445        """
446        On OK pressed (MAC only)
447        """
448        event.Skip()
449        self.parent.dial_ok(self, self.selcted_model)
450        self.Destroy()
451
452    def on_add(self, event):
453        """
454        Callback for new category added
455        """
456        new_cat = ''
457        if self.existing_check.GetValue():
458            new_cat = str(self.exist_combo.GetValue())
459        else:
460            new_cat = str(self.new_text.GetValue())
461            if new_cat in self.cat_list:
462                wx.MessageBox('%s is already a model' % new_cat, 'Error',
463                              wx.OK | wx.ICON_EXCLAMATION )
464                return
465
466        if new_cat in self.current_cats:
467            wx.MessageBox('%s is already included in this model' \
468                              % new_cat, 'Error',
469                          wx.OK | wx.ICON_EXCLAMATION )
470            return
471
472        self.current_cats.append(new_cat)
473        self.current_categories.SetItems(self.current_cats)
474           
475       
476    def on_remove(self, event):
477        """
478        Callback for a category removed
479        """
480        if self.current_categories.GetSelection() == wx.NOT_FOUND:
481            wx.MessageBox('Please select a category to remove', 'Error',
482                          wx.OK | wx.ICON_EXCLAMATION )
483        else:
484            self.current_categories.Delete( \
485                self.current_categories.GetSelection())
486            self.current_cats = self.current_categories.GetItems()
487
488       
489
490    def on_newcat(self, event):
491        """
492        Callback for new category added
493        """
494        self.new_text.Enable()
495        self.exist_combo.Disable()
496
497
498    def on_existing(self, event):   
499        """
500        Callback for existing category selected
501        """
502        self.new_text.Disable()
503        self.exist_combo.Enable()
504
505    def get_category(self):
506        """
507        Returns a list of categories applying to this model
508        """
509        if not self.current_cats:
510            self.current_cats.append("Uncategorized")
511
512        ret = list()
513        for cat in self.current_cats:
514            ret.append(str(cat))
515        return ret
516
517if __name__ == '__main__':
518       
519   
520    if(len(sys.argv) > 1):
521        app = wx.App()
522        CategoryManager(None, -1, 'Category Manager', sys.argv[1])
523        app.MainLoop()
524    else:
525        app = wx.App()
526        CategoryManager(None, -1, 'Category Manager', sys.argv[1])
527        app.MainLoop()
528
Note: See TracBrowser for help on using the repository browser.