""" qtgui/MGPickEvent.py: CCP4MG Molecular Graphics Program Copyright (C) 2001-2008 University of York, CCLRC Copyright (C) 2009-2010 University of York Copyright (C) 2012 STFC This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3, modified in accordance with the provisions of the license to address the requirements of UK law. You should have received a copy of the modified GNU Lesser General Public License along with this library. If not, copies may be downloaded from http://www.ccp4.ac.uk/ccp4license.php This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. """ import copy from PyQt4 import QtCore import displayTable,moleculargraphicsmodel import global_definitions import pygl_coord #--------------------------------------------------------------------- #--------------------------------------------------------------------- #--------------------------------------------------------------------- class MGPickEvent: #class MGPickEvent(QtCore.QObject): #--------------------------------------------------------------------- #--------------------------------------------------------------------- #--------------------------------------------------------------------- # Hold info on (probable mouse) pick of a graphical object # in parent MGGLWidget # In future there is possibility of initialising from script etc. # Feb08 - LP - currently only works for MolDisp object #--------------------------------------------------------------------- def __init__(self,glwidget,event): #--------------------------------------------------------------------- # Make this a child of MGGLWidget so should get cleaned up #QtCore.QObject.__init__(self,glwidget) self.glwidget = glwidget self.xyzbox = glwidget.GetXYZBox(event.pos().x(),event.pos().y()) # Initiallise self.atoms as -1 == untested # Subsequently an empty list implies tested and no atom found self.atoms = [] self.graphicsObject = -1 #print "initialising PickEvent",self self._pos = event.pos() self._modifiers = event.modifiers() def modifiers(self): return self._modifiers def pos(self): return self._pos #--------------------------------------------------------------------- def __del__(self): #--------------------------------------------------------------------- pass #print "deleting PickEvent",self #--------------------------------------------------------------------- def interpretPick(self): #--------------------------------------------------------------------- if self.graphicsObject != -1: return self.atoms = [] self.graphicsObject = None min_dist = 99999.9 #for go in self.parent().go: for go in self.glwidget.go: if go.obj.visible: # atoms = atom1,atom2,dist,sym # dist is actual dist-0.5 if have picked a single atom in order to # prioritise picking atoms over picking bonds atoms = go.GetClickedAtoms(self.xyzbox,self.glwidget.NEAR,self.glwidget.FAR) #print 'MGPickEvent.interpretPick',go,atoms if atoms and len(atoms)>2 and atoms[2] and atoms[2]-1 and atoms[3]0 and len(self.atoms)>1: index = 1 else: index = 0 return self.getAtomLabel(atom=self.atoms[index],format=format) #return self.atoms[index].GetAtomID()[0] else: return '' #--------------------------------------------------------------------- def getPickPosition(self): #--------------------------------------------------------------------- self.interpretPick() #print "in getPickPosition atoms",self.atoms,len(self.atoms) if len(self.atoms)==1: if hasattr(self,"transMat"): cart = self.transMat*pygl_coord.Cartesian(self.atoms[0].x,self.atoms[0].y,self.atoms[0].z) return (cart.get_x(),cart.get_y(),cart.get_z()) return (self.atoms[0].x,self.atoms[0].y,self.atoms[0].z) elif len(self.atoms)==2: if hasattr(self,"transMat"): cart = self.transMat*pygl_coord.Cartesian((self.atoms[0].x+self.atoms[1].x)/2.0, (self.atoms[0].y+self.atoms[1].y)/2.0, (self.atoms[0].z+self.atoms[1].z)/2.0) return (cart.get_x(),cart.get_y(),cart.get_z()) return ((self.atoms[0].x+self.atoms[1].x)/2.0, (self.atoms[0].y+self.atoms[1].y)/2.0, (self.atoms[0].z+self.atoms[1].z)/2.0) else: print "getPickPosition xyzbox",self.xyzbox[0] return ( (self.xyzbox[0].get_x()+ self.xyzbox[1].get_x() )/2.0 , (self.xyzbox[0].get_y()+ self.xyzbox[1].get_y() )/2.0 , (self.xyzbox[0].get_z()+ self.xyzbox[1].get_z() )/2.0 ) #--------------------------------------------------------------------- def getContextMenu(self): #--------------------------------------------------------------------- return self.glwidget.context_menu #--------------------------------------------------------------------- def getDispObj(self): #--------------------------------------------------------------------- self.interpretPick() if not self.graphicsObject: return None else: dispobj_list = global_definitions.get_dispobj(graphics_object=self.graphicsObject) if dispobj_list: return dispobj_list[0] else: return None #--------------------------------------------------------------------- def getDataObj(self): #--------------------------------------------------------------------- self.interpretPick() if not self.graphicsObject: return None else: dispobj_list = global_definitions.get_dispobj(graphics_object=self.graphicsObject) if dispobj_list: return dispobj_list[0].parent else: return None #--------------------------------------------------------------------- def getGuiObject(self): #--------------------------------------------------------------------- self.interpretPick() gui = None if self.graphicsObject: import MGApplication,global_definitions dispobj_list = global_definitions.get_dispobj(graphics_object=self.graphicsObject) if dispobj_list: gui = MGApplication.GetMainWindow().MGDisplayTable.findChild( displayTable.GDispObj,dispobj_list[0].name) return gui #----------------------------------------------------------------------- def getAtomLabel(self,atom=None,format=''): #----------------------------------------------------------------------- molobj = self.getDataObj() if not molobj: if atom: id = atom.GetAtomID() if type(id) is tuple and len(id)>0: return id[0] return id else: return '' if format == 'full': atom_label_style = [0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0] elif format == 'unique': atom_label_style = [0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0] else: atom_label_style = [0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0] truncate_mol_name=0 truncate_mol_name_length=6 mapping = [ [],[],[1],[2],[3,4],[5],[6,7],[8],[10],[11],[13],[15],[17]] mask = pygl_coord.inta(20) for j in range(0,20): mask[j] = 0 for j in range(0,len(atom_label_style)): if atom_label_style[j]: for k in mapping[j]: mask[k] = 1 #print "mapping",mapping,'atom_label_style',atom_label_style text_label = molobj.molHnd.AtomLabel(atom,mask) #print "getAtomLabel",text_label if atom_label_style[1]: if truncate_mol_name: moln = molobj.name_label[0:min(truncate_mol_name_length,len(molobj.name_label))] else: moln = molobj.name_label text_label=moln+':'+text_label if atom_label_style[0]: text_label = str(molobj.MolData_number)+')'+text_label return text_label