Package DejaVu :: Module Insert2d
[hide private]
[frames] | no frames]

Source Code for Module DejaVu.Insert2d

  1  ######################################################################## 
  2  # 
  3  # Date: Febuary 2006 Authors: Guillaume Vareille, Michel Sanner 
  4  # 
  5  #    vareille@scripps.edu 
  6  #    sanner@scripps.edu 
  7  # 
  8  #       The Scripps Research Institute (TSRI) 
  9  #       Molecular Graphics Lab 
 10  #       La Jolla, CA 92037, USA 
 11  # 
 12  # Copyright: Guillaume Vareille, Michel Sanner and TSRI 
 13  # 
 14  # Revision: 
 15  # 
 16  ######################################################################### 
 17  # 
 18  # $Header$ 
 19  # 
 20  # $Id$ 
 21  # 
 22   
 23  import Numeric 
 24  from copy import deepcopy 
 25  from opengltk.OpenGL import GL 
 26  from DejaVu.Common2d3dObject import Common2d3dObject 
 27   
 28   
29 -class Insert2d(Common2d3dObject):
30 """Base class for 2d inserts that can be displayed in a camera 31 """ 32 keywords = Common2d3dObject.keywords + [ 33 'anchor', 34 'position', 35 'size', 36 ] 37
38 - def __init__(self, name='insert2d', check=1, **kw):
39 #print "Insert2d.__init__" 40 41 self.anchor = [1, 0] # or 0 - left/top; .5 - center; 1 - right/bottom 42 self.position = [1, 0] # or 0 - left/top; .5 - center; 1 - right/bottom 43 44 self.size = [40, 30] # in pixels 45 46 self.polygonContour = None # the list of 2d points (pixels coordinates from bottom left) 47 # that defines the 2d polygon drawned by Draw() 48 49 self.lastPickEventTime = 0 50 self.isMoving = None # tells what is the current behavior 51 # the legend can be move (True) or resize (False) or nothing (None) 52 53 apply( Common2d3dObject.__init__, (self, name, check), kw) 54 55 self.initialAnchor = deepcopy(self.anchor) 56 self.initialPosition = deepcopy(self.position) 57 self.initialSize = deepcopy(self.size)
58 59
60 - def Set(self, check=1, redo=1, updateOwnGui=True, **kw):
61 """set data for this object 62 check=1 : verify that all the keywords present can be handle by this func 63 redo=1 : append self to viewer.objectsNeedingRedo 64 updateOwnGui=True : allow to update owngui at the end this func 65 """ 66 #print "Insert2d.Set" 67 redrawFlag, \ 68 updateOwnGuiFlag, \ 69 redoViewerDisplayListFlag, \ 70 redoDisplayListFlag, \ 71 redoTemplateFlag, \ 72 redoDisplayChildrenListFlag = apply( Common2d3dObject.Set, (self, check, 0), kw) 73 74 anchor = kw.get( 'anchor') 75 if anchor \ 76 and (anchor[0] >= 0.) and (anchor[0] <= 1.) \ 77 and (anchor[1] >= 0.) and (anchor[1] <= 1.): 78 self.anchor = anchor 79 redoDisplayListFlag = True 80 81 position = kw.get( 'position') 82 if position \ 83 and (position[0] >= 0.) and (position[0] <= 1.) \ 84 and (position[1] >= 0.) and (position[1] <= 1.): 85 self.position = position 86 redoDisplayListFlag = True 87 88 size = kw.get( 'size') 89 if size \ 90 and (size[0] >= 0.) \ 91 and (size[1] >= 0.): 92 self.size = size 93 redoDisplayListFlag = True 94 95 if redo and self.viewer: 96 if redoTemplateFlag is True: 97 self.redoTemplate() 98 redrawFlag = True 99 if redoDisplayListFlag is True: 100 if self not in self.viewer.objectsNeedingRedo.keys(): 101 self.viewer.objectsNeedingRedo[self] = None 102 redrawFlag = True 103 if redoDisplayChildrenListFlag is True: 104 lObjectsNeedingRedo = self.viewer.objectsNeedingRedo.keys() 105 for child in self.AllObjects(): 106 if child not in lObjectsNeedingRedo: 107 self.viewer.objectsNeedingRedo[child] = None 108 redrawFlag = True 109 if redoViewerDisplayListFlag is True: 110 self.viewer.deleteOpenglList() 111 redrawFlag = True 112 if updateOwnGui is True and updateOwnGuiFlag is True and self.ownGui is not None: 113 self.updateOwnGui() 114 if redrawFlag is True: 115 self.viewer.Redraw() 116 return redrawFlag, updateOwnGuiFlag, redoViewerDisplayListFlag, redoDisplayListFlag, redoTemplateFlag, redoDisplayChildrenListFlag
117 118 119
120 - def getState(self):
121 """return a dictionary describing this object's state 122 This dictionary can be passed to the Set method to restore the object's state 123 """ 124 state = Common2d3dObject.getState(self).copy() 125 state.update( { 126 'anchor': self.anchor, 127 'position': self.position, 128 'size': self.size, 129 } ) 130 return state
131 132
133 - def setViewer(self, viewer, buildDisplayList):
134 # could be renamed as 'onViewerAddition' 135 #print "Insert2d.setViewer" 136 Common2d3dObject.setViewer(self, viewer, buildDisplayList) 137 self.viewer.AddPickingCallback(self.processHit_cb)
138 139
140 - def Draw(self):
141 """set the projection matrices, the subclass must call this in their Draw func, 142 otherwise tile rendering and stereo won't work 143 """ 144 #print "Insert2d.Draw" 145 146 GL.glMatrixMode(GL.GL_PROJECTION) 147 if self.viewer.tileRender: 148 tile = self.getTile() 149 GL.glOrtho(float(tile[0]), float(tile[1]), float(tile[2]), float(tile[3]), -1, 1) 150 else: 151 GL.glOrtho(0, 152 float(self.viewer.currentCamera.width), 153 0, 154 float(self.viewer.currentCamera.height), 155 -1, 1) 156 GL.glMatrixMode(GL.GL_MODELVIEW)
157 158
159 - def pickDraw(self):
160 """called by the picking process to operate the selection 161 """ 162 #print "Insert2d.pickDraw", self 163 if self.polygonContour is not None: 164 # we draw just flat quad of the insert2d 165 GL.glMatrixMode(GL.GL_PROJECTION) 166 GL.glPushMatrix() 167 #GL.glLoadIdentity() 168 GL.glLoadMatrixf(self.viewer.currentCamera.pickMatrix) 169 GL.glOrtho(0, float(self.viewer.currentCamera.width), 170 0, float(self.viewer.currentCamera.height), -1, 1) 171 GL.glMatrixMode(GL.GL_MODELVIEW) 172 GL.glPushMatrix() 173 GL.glLoadIdentity() 174 GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL) 175 GL.glPushName(0) 176 GL.glBegin(GL.GL_QUADS) 177 GL.glVertex2fv(self.polygonContour[0]) 178 GL.glVertex2fv(self.polygonContour[1]) 179 GL.glVertex2fv(self.polygonContour[2]) 180 GL.glVertex2fv(self.polygonContour[3]) 181 GL.glEnd() 182 GL.glPopName() 183 184 GL.glMatrixMode(GL.GL_PROJECTION) 185 GL.glPopMatrix() 186 GL.glMatrixMode(GL.GL_MODELVIEW) 187 GL.glPopMatrix()
188 189
190 - def setSize(self, event, redo=1):
191 """the trackball transmit the translation info 192 """ 193 self.size[0] = event.x - self.coord2d[0] 194 self.size[1] = self.coord2d[1] - event.y 195 196 if self.size[0] < 1: 197 self.size[0] = 1 198 if self.size[1] < 1: 199 self.size[1] = 1 200 201 if self.size[0] > self.viewer.currentCamera.width: 202 self.size[0] = self.viewer.currentCamera.width 203 if self.size[1] > self.viewer.currentCamera.height: 204 self.size[1] = self.viewer.currentCamera.height 205 206 if self.viewer: 207 self.viewer.objectsNeedingRedo[self] = None
208 209
210 - def setPosition(self, event, redo=1):
211 """the trackball transmit the translation info 212 """ 213 #print "Insert2d.setPosition", event.x, event.y 214 self.position = [ event.x / float(self.viewer.currentCamera.width), 215 event.y / float(self.viewer.currentCamera.height) 216 ] 217 218 if self.position[0] < 0: 219 self.position[0] = 0 220 elif self.position[0] > 1: 221 self.position[0] = 1 222 223 if self.position[1] < 0: 224 self.position[1] = 0 225 elif self.position[1] > 1: 226 self.position[1] = 1 227 228 self.viewer.objectsNeedingRedo[self] = None
229 230
231 - def calculateAnchorAndPosition(self, event):
232 insert2dTopLeftFromScreenTopleft0 = self.position[0] * self.viewer.currentCamera.width \ 233 - self.anchor[0] * self.size[0] 234 insert2dTopLeftFromScreenTopleft1 = self.position[1] * self.viewer.currentCamera.height \ 235 - self.anchor[1] * self.size[1] 236 #print "insert2dTopLeftFromScreenTopleft", insert2dTopLeftFromScreenTopleft0, insert2dTopLeftFromScreenTopleft1 237 self.anchor = [ (event.x - insert2dTopLeftFromScreenTopleft0) / float(self.size[0]), 238 (event.y - insert2dTopLeftFromScreenTopleft1) / float(self.size[1]) 239 ] 240 #print "self.anchor", self.anchor 241 self.position = [ event.x / float(self.viewer.currentCamera.width), 242 event.y / float(self.viewer.currentCamera.height) 243 ]
244 245
246 - def ResetPosition(self):
247 self.anchor = deepcopy(self.initialAnchor) 248 self.position = deepcopy(self.initialPosition) 249 if self.viewer: 250 self.viewer.objectsNeedingRedo[self] = None
251 252
253 - def ResetSize(self):
254 #print "ResetSize", self.initialSize 255 self.size = deepcopy(self.initialSize) 256 if self.viewer: 257 self.viewer.objectsNeedingRedo[self] = None
258 259
260 - def ResetTransformation(self):
261 """Reset the tranformations 262 """ 263 self.ResetPosition() 264 self.ResetSize()
265 266
267 - def respondToMouseMove(self, event, redo=1):
268 """the trackball transmit the event info 269 """ 270 if self.isMoving is not None: 271 if self.isMoving is True: 272 self.setPosition(event) 273 else: 274 self.setSize(event) 275 self.viewer.Redraw()
276 277
278 - def respondToDoubleClick(self, event):
279 """to be overidden 280 """ 281 pass
282 283
284 - def processHit_cb(self, pick):
285 #print "Insert2d.processHit_cb", self, (self.viewer==None) 286 #print "pick",pick 287 #print "pick.event",dir(pick) 288 #print "pick.type",pick.type 289 #print "pick.event",dir(pick.event) 290 #print "pick.event",pick.event 291 #print "pick.event.type",pick.event.type 292 #print "pick.event.state",pick.event.state 293 #print "pick.event.time",pick.event.time 294 #print "pick.hits",pick.hits 295 296 if ( len(pick.hits) == 1) and pick.hits.has_key(self): 297 if self.viewer.currentObject != self: 298 # if the only hit is the legend, 299 # it becomes the current object 300 self.viewer.SetCurrentObject(self) 301 self.isMoving = True 302 self.calculateAnchorAndPosition(pick.event) 303 elif pick.event.time - self.lastPickEventTime < 200: #double click 304 self.viewer.SetCurrentObject(self.viewer.rootObject) 305 self.respondToDoubleClick(pick.event) 306 elif pick.hits[self][0][0] == 1: 307 # the click in inside the resize button 308 #print "resize" 309 self.isMoving = False 310 elif pick.hits[self][0][0] == 0: 311 # the click in inside the legend but outside 312 # the resize button 313 self.isMoving = True 314 #print "pick.event", pick.event.x, pick.event.y 315 self.calculateAnchorAndPosition(pick.event) 316 #print "self.position", self.position 317 if self.viewer: 318 self.viewer.objectsNeedingRedo[self] = None 319 320 elif self.viewer.currentObject == self: 321 #print "the insert2d is selected, but picking is outside" 322 self.isMoving = None 323 self.viewer.SetCurrentObject(self.viewer.rootObject) 324 325 self.lastPickEventTime = pick.event.time
326