Package qubx :: Module optimizeGTK
[hide private]
[frames] | no frames]

Source Code for Module qubx.optimizeGTK

  1  """GUI configuration for globally available pluggable optimizers. 
  2   
  3  Copyright 2008-2012 Research Foundation State University of New York  
  4  This file is part of QUB Express.                                           
  5   
  6  QUB Express is free software; you can redistribute it and/or modify           
  7  it under the terms of the GNU General Public License as published by  
  8  the Free Software Foundation, either version 3 of the License, or     
  9  (at your option) any later version.                                   
 10   
 11  QUB Express is distributed in the hope that it will be useful,                
 12  but WITHOUT ANY WARRANTY; without even the implied warranty of        
 13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 14  GNU General Public License for more details.                          
 15   
 16  You should have received a copy of the GNU General Public License,    
 17  named LICENSE.txt, in the QUB Express program directory.  If not, see         
 18  <http://www.gnu.org/licenses/>.                                       
 19   
 20  """ 
 21   
 22  import gtk 
 23  import numpy 
 24  import qubx.accept 
 25  import qubx.faces 
 26  import qubx.GTK 
 27  import qubx.optimize 
 28  import qubx.pyenv 
 29  import qubx.settings 
 30  import qubx.tree 
 31  import qubx.treeGTK 
 32  from qubx.util_types import Reffer, WeakEvent 
 33   
34 -class OptMenu(qubx.GTK.DynamicComboBox):
35 - def __init__(self, caption, profile):
36 qubx.GTK.DynamicComboBox.__init__(self) 37 self.__ref = Reffer() 38 self.profile = profile 39 self.optTree = qubx.settings.SettingsMgr[profile].active 40 self.choose(str(self.optTree['Method'].data)) 41 self.OnPopulate += self.__ref(self.__onPopulate) 42 self.OnChanged += self.__ref(self.__onChanged) 43 self.options_face = OptOptions(caption, profile)
44 - def __onPopulate(self, add):
45 for opt in qubx.optimize.g_optimizers: 46 add(opt['name'])
47 - def __onChanged(self, selfy, active_text):
48 ### TODO: reflect scriptable opt change onscreen 49 qubx.pyenv.env.OnScriptable('SettingsMgr[%s].active["Method"].data = %s' % (repr(self.profile), repr(active_text))) 50 self.optTree['Method'].data = active_text 51 self.options_face.refresh()
52 53
54 -class OptOptions(qubx.faces.Face):
55 __explore_featured = ['profile', 'optTree', 'treeview', 'refresh']
56 - def __init__(self, caption, profile, global_name=""):
57 qubx.faces.Face.__init__(self, caption, global_name) 58 self.__ref = Reffer() 59 self.profile = profile 60 self.optTree = qubx.settings.SettingsMgr[profile].active 61 self.treeview = qubx.treeGTK.TreeView() 62 self.treeview.OnChange += self.__ref(self.__onChange) 63 self.treeview.show() 64 self.pack_start(self.treeview, True, True) 65 self.refresh()
66 - def refresh(self):
67 self.treeview.root = self.optTree[ str(self.optTree['Method'].data) ]
68 - def __onChange(self, view):
69 qubx.pyenv.env.OnScriptable('qubx.optimize.set_options(%s, %s)' % 70 (repr(self.profile), repr(qubx.tree.tree_to_dict(self.optTree[str(self.optTree['Method'].data)]))))
71 72
73 -class OptCheckList(qubx.GTK.OrderedCheckList):
74 - def __init__(self, profile):
75 qubx.GTK.OrderedCheckList.__init__(self) 76 self.profile = profile 77 self.optTree = qubx.settings.SettingsMgr[profile].active 78 self.__ref = Reffer() 79 self.__recs = [] 80 self.__priorities = [] 81 self.options_box = OptOptionsBox(profile) 82 for rec in qubx.optimize.g_optimizers: 83 self.__onOptimizerAdded(rec['name'], rec) 84 qubx.optimize.OnOptimizerAdded += self.__ref(self.__onOptimizerAdded) 85 qubx.optimize.OnOptOptions += self.__ref(self.__onOptOptions) 86 self.OnToggle += self.__ref(self.__onToggle) 87 self.OnSwap += self.__ref(self.__onSwap) 88 self.OnClick += self.__ref(self.__onClick)
89 - def __onOptimizerAdded(self, name, rec):
90 priority = qubx.tree.node_data_or_def(self.optTree.find('Methods').find(name), rec['priority']) 91 order = priority or rec['priority'] or 1 92 ix = len(self.__priorities) 93 while (ix > 0) and (self.__priorities[ix-1] > order): 94 ix -= 1 95 self.insert(ix, bool(priority), name) 96 self.__recs.insert(ix, rec) 97 self.__priorities.insert(ix, order)
98 - def __onOptOptions(self, name):
99 self.clear() 100 recs = self.__recs 101 self.__recs, self.__priorities = [], [] 102 for rec in recs: 103 self.__onOptimizerAdded(rec['name'], rec) 104 self.options_box.refresh()
105 - def __onToggle(self, ix, active):
106 rec = self.__recs[ix] 107 self.optTree['Methods'][rec['name']].data = (active and self.__priorities[ix] or 0) 108 qubx.pyenv.env.OnScriptable("qubx.optimize.set_opt_priority(%s, %s, %i)" % (repr(self.profile), repr(rec['name']), (active and self.__priorities[ix] or 0)))
109 - def __onSwap(self, a, b):
110 self.__recs[a], self.__recs[b] = self.__recs[b], self.__recs[a] 111 self.__priorities[a], self.__priorities[b] = self.__priorities[b], self.__priorities[a] 112 self.__reorder(min(a,b))
113 - def __reorder(self, ix0=0):
114 p = self.__priorities[ix0-1] if ix0 else 0 115 for i in xrange(ix0, len(self.__recs)): 116 p = self.__priorities[i] = p + 1 117 if self.get_active(i): 118 name = self.__recs[i]['name'] 119 node = self.optTree['Methods'][name] 120 if (not node.data) or (node.data[0] != p): 121 self.optTree['Methods'][name].data = p 122 qubx.pyenv.env.OnScriptable("qubx.optimize.set_method_priority(%s, %s, %i)" % (repr(self.profile), repr(name), p))
123 - def __onClick(self, ix):
124 self.optTree['Method'].data = self.__recs[ix]['name'] 125 self.options_box.refresh()
126 - def onStartOpt(self, rec, opts):
127 sel = self.get_selection() 128 sel.unselect_all() 129 ix = self.__recs.index(rec) 130 sel.select_path((ix,)) 131 self.__onClick(ix)
132 - def onEndOpt(self, rec, opts):
133 self.get_selection().unselect_all()
134 135 136 137
138 -class OptOptionsBox(gtk.VBox):
139 __explore_featured = ['profile', 'optTree', 'treeview', 'refresh']
140 - def __init__(self, profile):
141 gtk.VBox.__init__(self) 142 self.__ref = Reffer() 143 self.profile = profile 144 self.optTree = qubx.settings.SettingsMgr[profile].active 145 self.treeview = qubx.treeGTK.TreeView() 146 self.treeview.OnChange += self.__ref(self.__onChange) 147 self.treeview.show() 148 self.pack_start(self.treeview, True, True) 149 self.refresh()
150 - def refresh(self):
151 self.treeview.root = self.optTree[ str(self.optTree['Method'].data) ]
152 - def __onChange(self, view):
153 qubx.pyenv.env.OnScriptable('qubx.optimize.set_options(%s, %s)' % 154 (repr(self.profile), repr(qubx.tree.tree_to_dict(self.optTree[str(self.optTree['Method'].data)]))))
155
156 -class OptOptionsFace(qubx.faces.Face):
157 __explore_featured = ['optimizers']
158 - def __init__(self, caption, profile, global_name=""):
159 qubx.faces.Face.__init__(self, caption, global_name) 160 self.__ref = Reffer() 161 self.optimizers = OptCheckList(profile) 162 self.optimizers.show() 163 qubx.GTK.pack_scrolled(self.optimizers, self, size_request=(-1, 120)) 164 qubx.GTK.pack_item(self.optimizers.options_box, self, expand=True)
165