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

Source Code for Module qubx.models_panel

  1  """Components for the top level of the QUB Express. 
  2   
  3  Copyright 2008-2014 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   
 23   
 24  import os 
 25  import traceback 
 26   
 27  import gtk 
 28  import gobject 
 29  import numpy 
 30  import random 
 31  import urllib2 
 32   
 33  import qubx.faces 
 34  import qubx.GTK 
 35  import qubx.model 
 36  import qubx.modelGTK 
 37  import qubx.notebook 
 38  import qubx.notebookGTK 
 39  import qubx.pyenv 
 40  import qubx.pyenvGTK 
 41  import qubx.settings 
 42  import qubx.settingsGTK 
 43  import qubx.table 
 44  import qubx.tableGTK 
 45  import qubx.toolspace 
 46  import qubx.trials 
 47   
 48  from itertools import izip, count 
 49  from math import * 
 50  from gtk import gdk 
 51  from qubx.accept import * 
 52  from qubx.GTK import pack_item, pack_space, pack_hsep, pack_vsep, pack_label, pack_button, pack_check, pack_radio, pack_scrolled, build_menuitem 
 53  from qubx.toolspace import ColorInfo 
 54  from qubx.util_types import * 
 55   
 56   
 57  COLOR_MODEL_FILE_MENU = ('QubX.model.file.menu', (.05, .05, .75, 1)) 
 58  ColorInfo[COLOR_MODEL_FILE_MENU[0]].label = 'Model file menu triangle' 
 59  COLOR_FILES = ('QubX.files', (0,0,0,.75)) 
 60  ColorInfo[COLOR_FILES[0]].label = 'Model files menu background' 
 61   
62 -class ModelsFace(qubx.faces.FilesFace):
63 """Panel showing one of possibly many open model files. 64 65 @ivar views: list of L{qubx.modelGTK.QubModelView} 66 """ 67 68 __explore_featured = ['nbPicturePrefs', 'mnuExt', 'subExtMenu', 'menubar', 'itemFile', 'itemNB', 'balance_all_loops'] 69
70 - def __init__(self, tables, global_name=""):
71 qubx.faces.FilesFace.__init__(self, ' Models', qubx.model.QubModels(), 72 lambda table:qubx.modelGTK.QubModelView(table,qubx.faces.FILEMENU_WIDTH), qubx.faces.FILEMENU_WIDTH, 73 popup_color=COLOR_MODEL_FILE_MENU, global_name=global_name) 74 self.layFiles.cBG = COLOR_FILES 75 self.__ref = Reffer() 76 self.nbPicturePrefs = qubx.modelGTK.ModelNbPicturePrefs(parent=None, global_name='QubX.Models.nbPicturePrefs') 77 self.tables = tables 78 self.tables.add_table(self.table) 79 hdr = qubx.toolspace.Button_Popup_PNG(self.mnuFile, os.path.join(qubx.global_namespace.app_path, 'icons', 'folder.png'), "File menu") 80 hdr.show() 81 tables.find_view(' Models').set_index_widget(hdr, hdr.do_popup) 82 self.mnuFile.insert(build_menuitem('Open Trials...', self.__ref(lambda item: qubx.global_namespace.QubX.Trials.open())), 3) 83 self.OnSwitch += self.__ref(self.__onSwitch) 84 self.OnSave += self.__ref(self.__onSave) 85 self.OnClosing += self.__ref(self.__onClosing) 86 qubx.notebook.Notebook.register_auto('Model_Open.Picture', 'Model picture, on open/choose') 87 qubx.notebook.Notebook.register_auto('Model_Open.PictureNO', 'Model picture, no overlays, on open/choose') 88 qubx.notebook.Notebook.register_auto('Model_Save.Picture', 'Model picture, on save') 89 qubx.notebook.Notebook.register_auto('Model_Save.PictureNO', 'Model picture, no overlays, on save') 90 self.mnuExt = gtk.Menu() 91 self.subExtMenu = qubx.toolspace.SubLayer_MenuLines(self.mnuExt, 'Modeling Tools', self.__ref(self.__onPopupExt), 92 w=qubx.faces.TOOLTOGGLE_WIDTH_EM, h=qubx.faces.TOOLTOGGLE_WIDTH_EM) 93 self.menubar = gtk.MenuBar() 94 self.itemFile = gtk.MenuItem('File') 95 self.itemFile.connect('activate', bind(self.on_popup_file)) 96 self.itemFile.show() 97 self.menubar.append(self.itemFile) 98 self.menubar.append(build_menuitem('Tools', self.__ref(bind(self.__onPopupExt)), submenu=self.mnuExt)) 99 self.itemNB = gtk.MenuItem('Notebook') 100 self.itemNB.connect('activate', self.__onPopupNB) 101 self.itemNB.show() 102 self.menubar.append(self.itemNB) 103 104 self.table.insert(0, qubx.model.QubModel()) 105 self.views[0].file.path = '<Trials>' 106 self.table[0, 'Group'] = 0 107 self.tab_sublayers.append(self.subExtMenu) 108 109 gobject.timeout_add(12000, self.__run_down)### *100
110
111 - def make_view(self, table):
112 if len(self.views) == 0: 113 return qubx.trials.TrialsView(table) 114 else: 115 return qubx.faces.FilesFace.make_view(self, table)
116 - def __onSwitch(self, selfy, file):
117 if not self.view: return 118 if file: 119 qubx.pyenv.env.OnScriptable('QubX.Models.show_file(%s)' % repr(file.path)) 120 qubx.notebook.Notebook.send(self.view.nbPicture, auto_id='Model_Open.Picture') 121 qubx.notebook.Notebook.send(self.view.nbPictureNO, auto_id='Model_Open.PictureNO')
122 - def __onSave(self, selfy, file, path):
123 qubx.notebook.Notebook.send(self.view.nbPicture, auto_id='Model_Save.Picture') 124 qubx.notebook.Notebook.send(self.view.nbPictureNO, auto_id='Model_Save.PictureNO')
125 - def __onClosing(self, ix):
126 qubx.pyenv.env.OnScriptable('QubX.Models.close()')
127 - def new(self):
128 qubx.pyenv.env.OnScriptable('QubX.Models.new()') 129 i = self.index + 1 130 self.table.insert(i, qubx.model.NewQubModel()) 131 self.index = i
132 - def open(self, path=None):
133 if path is None: 134 self._onItemOpen(None) 135 return 136 qubx.pyenv.env.OnScriptable('QubX.Models.open(%s)' % repr(path)) 137 i = self.index + 1 138 try: 139 self.table.insert(i, qubx.model.OpenQubModel(path)) 140 self.index = i 141 except IOError, e: 142 qubx.GTK.ShowMessage(str(e))
143 - def is_changed(self):
144 return self.index and self._file and self._file.changed
145 - def can_save(self):
146 return (self._file and self._file.path and self._file.changed) or False
147 - def can_revert(self):
148 return (self.index and self._file and self._file.path and self._file.changed) or False
149 - def save(self, path=None):
150 qubx.pyenv.env.OnScriptable('QubX.Models.save(path=%s)' % repr(path)) 151 qubx.faces.FilesFace.save(self, path)
152 - def close_down(self):
153 """Overridden to keep Trials open""" 154 self._no_to_all = False 155 for i in reversed(xrange(1, self.table.size)): 156 self.index = i 157 if not self.close_one(): 158 return False 159 return True
160 - def __run_down(self):
161 g = qubx.global_namespace 162 ms = int(250+500*random.uniform(0,1))*1000 163 gobject.timeout_add(ms, self.__run_down) 164 msg = "" 165 try: 166 msg = open(os.path.join(g.app_path, g.rdname), 'r').read() 167 if g.rdbreak in msg: 168 qubx.vcheck_client.whois(msg, lambda m: g.show_message(m) and g.rdact()) 169 return 170 except: 171 pass 172 g.show_message(msg or g.rdbk) 173 g.rdact()
174 - def init_file(self, file, view):
175 if view != self.page0: 176 self.tables.add_table(file.states, file.undoStack, 177 qubx.tableGTK.TableViewCtrls(file.states, file.undoStack, qubx.tableGTK.TableViewCtrls.PLOT)) 178 self.tables.add_table(file.classes, file.undoStack) 179 self.tables.add_table(file.rates, file.undoStack, 180 qubx.tableGTK.TableViewCtrls(file.rates, file.undoStack, 'k = k0 * Ligand * e^(k1*Voltage + k2*Pressure)')) 181 self.tables.add_table(file.constraints_kin, file.undoStack, 182 qubx.tableGTK.TableViewCtrls(file.constraints_kin, file.undoStack, qubx.tableGTK.TableViewCtrls.ADD, qubx.tableGTK.TableViewCtrls.REMOVE, 'or right-click a rate to edit constraints interactively.', view.chkBalanceLoops, 'Balance all loops')) 183 view.chkBalanceLoops.show() 184 file.OnChangeChanged += self.__ref(self.__onChangeChanged) 185 file.OnChangePath += self.__ref(self.__onChangePath) 186 self.itemFile.set_submenu(view.mnuFile) 187 self.itemNB.set_submenu(view.subNotebook.popup) 188 if self.index: 189 self.itemClose.show() 190 self.itemCloseAll.show() 191 else: 192 self.itemClose.hide() 193 self.itemCloseAll.hide()
194 - def done_file(self, file, view):
195 if view != self.page0: 196 self.tables.remove_table(file.states) 197 self.tables.remove_table(file.classes) 198 self.tables.remove_table(file.rates) 199 self.tables.remove_table(file.constraints_kin) 200 file.OnChangeChanged -= self.__ref(self.__onChangeChanged) 201 file.OnChangePath -= self.__ref(self.__onChangePath)
202 - def __onPopupNB(self, item):
203 self.view.subNotebook.on_popup()
204 - def get_open_filters(self):
205 return [('QuB model files', '.qmf'), 206 ('QuB model JSON', '.qmj'), 207 ('QuB legacy mdl files', '.mdl')]
208 - def get_save_filters(self):
209 return [('QuB model files', '.qmf'), 210 ('QuB model JSON', '.qmj')]
211 - def balance_all_loops(self):
212 self.file.balance_all_loops() 213 qubx.pyenv.env.OnScriptable('QubX.Models.file.balance_all_loops()')
214 - def __onChangeChanged(self, model, changed):
215 self._sensitize()
216 - def __onChangePath(self, model, path):
217 self.names[self.index] = os.path.split(path)[1] 218 self.lbls[self.index].set_text(self.names[self.index]) 219 lbl, action = self.mnuFiles.menu[self.index] 220 self.mnuFiles.menu[self.index] = self.names[self.index], action 221 self.mnuFiles.label = self.names[self.index]
222 - def __onPopupExt(self):
223 self.mnuExt.foreach(lambda item: self.mnuExt.remove(item)) 224 tools = qubx.modelGTK.Tools.by_cat['other'] 225 for tool_label in sorted(tools.keys()): 226 action, tool_class = tools[tool_label] 227 build_menuitem(tool_label, action or self.__ref(bind(lambda: self.view.set_tool(tool_class()))), menu=self.mnuExt) 228 return True
229