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

Source Code for Module DejaVu.glfSticker

  1  ######################################################################## 
  2  # 
  3  # Date: August 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  ######################################################################### 
 15  # 
 16  # $Header: /opt/cvs/python/packages/share1.5/DejaVu/glfSticker.py,v 1.24 2007/07/23 20:38:58 vareille Exp $ 
 17  # 
 18  # $Id: glfSticker.py,v 1.24 2007/07/23 20:38:58 vareille Exp $ 
 19  # 
 20   
 21  import os 
 22   
 23  from math import floor, ceil 
 24  from opengltk.OpenGL import GL 
 25  from pyglf import glf 
 26  from DejaVu.Insert2d import Insert2d 
 27  from DejaVu.viewerFns import checkKeywords 
 28   
29 -class Sticker(Insert2d):
30 31 keywords = Insert2d.keywords + [ 32 'label', 33 'fontName', 34 'fontSpacing', 35 'position', 36 'anchor', 37 'fontColor', 38 'fontScales', 39 'wireFont', 40 'frameColor', 41 'framePolygonMode', 42 'frameSpace', 43 ] 44 45 glfVectorFontList = [ 46 'arial1.glf', 47 'courier1.glf', 48 'crystal1.glf', 49 'techno0.glf', 50 'techno1.glf', 51 'times_new1.glf', 52 'aksent1.glf', 53 'alpine1.glf', 54 'broadway1.glf', 55 'chicago1.glf', 56 'compact1.glf', 57 'cricket1.glf', 58 'garamond1.glf', 59 'gothic1.glf', 60 'penta1.glf', 61 'present_script1.glf' 62 ] 63
64 - def __init__(self, name='sticker', check=1, **kw):
65 66 # GLF FONTS Initialisations 67 glf.glfInit() 68 glf.glfEnable(glf.GLF_CONSOLE_MESSAGES) 69 lGlfModulePath = os.path.split(glf.__file__)[-2] 70 lPathToFonts = lGlfModulePath+os.sep+'fonts'+os.sep 71 self.glfVectorFontLoadIdDict = {} 72 for font in self.glfVectorFontList: 73 self.glfVectorFontLoadIdDict[font] = glf.glfLoadFont(lPathToFonts+font) 74 75 self.label = 'AaglfdgaSsdGreh' 76 self.fontName = 'arial1.glf' 77 self.glfFontID = self.glfVectorFontLoadIdDict[self.fontName] 78 self.fontSpacing = .2 79 self.fontColor = (1, 1, 1, .8) 80 self.fontScales = (10, 10) 81 self.wireFont = False 82 self.frameColor = (0, 1, 1, .5) 83 self.framePolygonMode = GL.GL_LINE # GL.GL_NONE for no frame 84 self.frameSpace = (.2, .2) 85 86 apply( Insert2d.__init__, (self, name), kw) 87 88 self.needsRedoDpyListOnResize = True
89 90
91 - def Set(self, check=1, redo=1, updateOwnGui=True, **kw):
92 """set data for this object: 93 check=1 : verify that all the keywords present can be handle by this func 94 redo=1 : append self to viewer.objectsNeedingRedo 95 updateOwnGui=True : allow to update owngui at the end this func 96 """ 97 #print "Sticker.Set" 98 redrawFlag, \ 99 updateOwnGuiFlag, \ 100 redoViewerDisplayListFlag, \ 101 redoDisplayListFlag, \ 102 redoTemplateFlag, \ 103 redoDisplayChildrenListFlag = apply( Insert2d.Set, (self, check, 0), kw) 104 105 label = kw.get('label') 106 if label: 107 kw.pop('label') 108 self.label = label 109 redoDisplayListFlag = True 110 111 fontName = kw.get('fontName') 112 if fontName: 113 kw.pop('fontName') 114 self.glfFontID = self.glfVectorFontLoadIdDict[fontName] 115 redoDisplayListFlag = True 116 117 fontSpacing = kw.get('fontSpacing') 118 if fontSpacing: 119 kw.pop('fontSpacing') 120 self.fontSpacing = fontSpacing 121 redoDisplayListFlag = True 122 123 position = kw.get('position') 124 if position: 125 kw.pop('position') 126 self.position = position 127 redoDisplayListFlag = True 128 129 fontScales = kw.get('fontScales') 130 if fontScales: 131 kw.pop('fontScales') 132 self.fontScales = fontScales 133 redoDisplayListFlag = True 134 135 wireFont = kw.get('wireFont') 136 if wireFont is not None: 137 kw.pop('wireFont') 138 self.wireFont = wireFont 139 redoDisplayListFlag = True 140 141 fontColor = kw.get('fontColor') 142 if fontColor: 143 kw.pop('fontColor') 144 self.fontColor = fontColor 145 redoDisplayListFlag = True 146 147 frameColor = kw.get('frameColor') 148 if frameColor: 149 kw.pop('frameColor') 150 self.frameColor = frameColor 151 redoDisplayListFlag = True 152 153 framePolygonMode = kw.get('framePolygonMode') 154 if framePolygonMode: 155 kw.pop('framePolygonMode') 156 self.framePolygonMode = framePolygonMode 157 redoDisplayListFlag = True 158 159 frameSpace = kw.get('frameSpace') 160 if frameSpace: 161 kw.pop('frameSpace') 162 self.frameSpace = frameSpace 163 redoDisplayListFlag = True 164 165 if redo and self.viewer: 166 if redoTemplateFlag is True: 167 self.redoTemplate() 168 redrawFlag = True 169 if redoDisplayListFlag is True: 170 if self not in self.viewer.objectsNeedingRedo.keys(): 171 self.viewer.objectsNeedingRedo[self] = None 172 redrawFlag = True 173 if redoDisplayChildrenListFlag is True: 174 lObjectsNeedingRedo = self.viewer.objectsNeedingRedo.keys() 175 for child in self.AllObjects(): 176 if child not in lObjectsNeedingRedo: 177 self.viewer.objectsNeedingRedo[child] = None 178 redrawFlag = True 179 if redoViewerDisplayListFlag is True: 180 self.viewer.deleteOpenglList() 181 redrawFlag = True 182 if updateOwnGui is True and updateOwnGuiFlag is True and self.ownGui is not None: 183 self.updateOwnGui() 184 if redrawFlag is True: 185 self.viewer.Redraw() 186 return redrawFlag, updateOwnGuiFlag, redoViewerDisplayListFlag, redoDisplayListFlag, redoTemplateFlag, redoDisplayChildrenListFlag
187 188 189
190 - def calculateSize(self):
191 #print "Sticker.calculateSize", self 192 glf.glfSetCurrentFont(self.glfFontID) 193 glf.glfSetSymbolSpace(self.fontSpacing) 194 195 lLabelbounds = glf.glfGetStringBounds(self.label) 196 197 # this is the correct way to use glfGetStringBounds to obtain the exact frame around the label 198 # (it still needs more correction when there are spaces in the label) 199 lFramebounds = ( -1, 200 lLabelbounds[1], 201 -1+(lLabelbounds[2]-lLabelbounds[0])-glf.glfGetSymbolSpace(), 202 lLabelbounds[3] ) 203 204 # we want the frame to be a bit larger than the label 205 lFramebounds = ( lFramebounds[0]-self.frameSpace[0], 206 lFramebounds[1]-self.frameSpace[1], 207 lFramebounds[2]+self.frameSpace[0], 208 lFramebounds[3]+self.frameSpace[1]) 209 210 self.size[0] = (lFramebounds[2] - lFramebounds[0]) * self.fontScales[0] 211 self.size[1] = (lFramebounds[3] - lFramebounds[1]) * self.fontScales[1] 212 self.size[0] = int(ceil( self.size[0] )) 213 self.size[1] = int(ceil( self.size[1] )) 214 #print "self.size", self.size 215 216 return lLabelbounds
217
218 - def drawSticker(self, lLabelbounds):
219 220 if self.framePolygonMode != GL.GL_NONE: 221 lenFrameColor = len(self.frameColor) 222 if lenFrameColor == 4: 223 GL.glColor4fv(self.frameColor) 224 elif lenFrameColor == 3: 225 GL.glColor3fv(self.frameColor) 226 GL.glPolygonMode(GL.GL_FRONT, self.framePolygonMode) 227 GL.glBegin(GL.GL_QUADS) 228 GL.glVertex2f(0, 0) 229 GL.glVertex2f(float(self.size[0]), 0) 230 GL.glVertex2f(float(self.size[0]), float(self.size[1])) 231 GL.glVertex2f(0, float(self.size[1])) 232 GL.glEnd() 233 234 GL.glScalef(float(self.fontScales[0]), 235 float(self.fontScales[1]), 236 0) 237 GL.glTranslatef(float(self.frameSpace[0]), 238 float(self.frameSpace[1]), 239 0) 240 241 # this corrects the glf draw function to start the label at the proper position 242 GL.glTranslatef(1, float(-lLabelbounds[1]), 0) 243 244 lenFontColor = len(self.fontColor) 245 if lenFontColor == 4: 246 GL.glColor4fv(self.fontColor) 247 elif lenFontColor == 3: 248 GL.glColor3fv(self.fontColor) 249 GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL) 250 if self.wireFont is False: 251 glf.glfDrawSolidString(self.label) 252 else: 253 glf.glfDrawWiredString(self.label) 254 255
256 - def Draw(self):
257 #print "Sticker.Draw", self 258 259 GL.glMatrixMode(GL.GL_PROJECTION) 260 GL.glPushMatrix() 261 GL.glLoadIdentity() 262 Insert2d.Draw(self) 263 GL.glMatrixMode(GL.GL_MODELVIEW) 264 GL.glPushMatrix() 265 GL.glLoadIdentity() 266 267 GL.glDisable(GL.GL_DEPTH_TEST) 268 GL.glDisable(GL.GL_LIGHTING); 269 270 lLabelbounds = self.calculateSize() 271 272 width = self.size[0] 273 height = self.size[1] 274 275 fullWidth = self.viewer.currentCamera.width 276 fullHeight = self.viewer.currentCamera.height 277 278 # we want the anchor of the image to be at the given position 279 posxFromLeft = self.position[0] * fullWidth - self.anchor[0] * width 280 posyFrombottom = (1.-self.position[1]) * fullHeight - (1.-self.anchor[1]) * height 281 posxFromLeft = int(floor( posxFromLeft ) ) 282 posyFrombottom = int(floor( posyFrombottom ) ) 283 284 if (self.position[1] == 0.): 285 posyFrombottom -= 1 286 # print "posxFromLeft, posyFrombottom", posxFromLeft, posyFrombottom 287 288 # used for picking 289 self.polygonContour = [ (posxFromLeft, posyFrombottom), 290 (posxFromLeft+width, posyFrombottom), 291 (posxFromLeft+width, posyFrombottom+height), 292 (posxFromLeft, posyFrombottom+height) 293 ] 294 295 GL.glTranslatef(float(posxFromLeft), float(posyFrombottom), 0) 296 297 self.drawSticker(lLabelbounds) 298 299 GL.glMatrixMode(GL.GL_PROJECTION) 300 GL.glPopMatrix() 301 GL.glMatrixMode(GL.GL_MODELVIEW) 302 GL.glPopMatrix() 303 304 return 1
305