def _handleBadExps(self, s, locals=None, globals=None): ' Handle expressions that do not evaluate ' if self.dbg: self.dbgOuf.write('!!! ERROR: failed to evaluate expression: %s \n' % s) res="<!-- EXCEPTION: \nExpression: "+s+"\n" res+=common.formatexception()+"\n-->" print common.formatexception() return res+('***! %s !***' % s)
def _wrap(func): try: return func() except: print common.formatexception() sys.exit(1)
def _handleBadExps(self, s, locals=None, globals=None): ' Handle expressions that do not evaluate ' if self.dbg: self.dbgOuf.write( '!!! ERROR: failed to evaluate expression: %s \n' % s) res = "<!-- EXCEPTION: \nExpression: " + s + "\n" res += common.formatexception() + "\n-->" print common.formatexception() return res + ('***! %s !***' % s)
def OpenFile(self,filesystem,location): try: res=self._OpenFile(filesystem,location) except: res=None print "Exception in getting image file - you can't do that!" print common.formatexception() if res is not None: res[0].seek(0) args=(wx.InputStream(res[0]),)+res[1:] res=wx.FSFile(*args) return res
def OpenFile(self, filesystem, location): try: res = self._OpenFile(filesystem, location) except: res = None print "Exception in getting image file - you can't do that!" print common.formatexception() if res is not None: # we have to seek the file object back to the begining and make a new # wx.FSFile each time as wxPython doesn't do the reference counting # correctly res[0].seek(0) args = (wx.InputStream(res[0]), ) + res[1:] res = wx.FSFile(*args) return res
def OpenFile(self,filesystem,location): try: res=self._OpenFile(filesystem,location) except: res=None print "Exception in getting image file - you can't do that!" print common.formatexception() if res is not None: # we have to seek the file object back to the begining and make a new # wx.FSFile each time as wxPython doesn't do the reference counting # correctly res[0].seek(0) args=(wx.InputStream(res[0]),)+res[1:] res=wx.FSFile(*args) return res
def errorme(self, desc, exception=None): "Put exception information into the hex pane and output traceback to console" if exception is not None: x = StringIO.StringIO() print >> x, exception.__str__(), self.errorinfo += x.getvalue() + " : " print >> sys.stderr, common.formatexception() self.errorinfo += desc + "\n"
def errorme(self, desc, exception=None): "Put exception information into the hex pane and output traceback to console" if exception is not None: x=StringIO.StringIO() print >>x,exception.__str__(), self.errorinfo+=x.getvalue()+" : " print >>sys.stderr, common.formatexception() self.errorinfo+=desc+"\n"
self.dbgOuf.write('%sIntermediate YAPTU format:\n%s\n%s' % (_sep, xStr, _sep)) return xStr def _handleBadExps(self, s, locals=None, globals=None): ' Handle expressions that do not evaluate ' if self.dbg: self.dbgOuf.write('!!! ERROR: failed to evaluate expression: %s \n' % s) res="<!-- EXCEPTION: \nExpression: "+s+"\n" res+=common.formatexception()+"\n-->" print common.formatexception() return res+('***! %s !***' % s) def _preProcess(self, s, why): ' Preprocess embedded python statements and expressions ' return self._xmlDecode(s) def _preProcessDbg(self, s, why): ' Preprocess embedded python statements and expressions '
def OpenFile(self,filesystem,location): try: res=self._OpenFile(filesystem,location) except: res=None print "Exception in getting image file - you can't do that!" print common.formatexception() if res is not None: res[0].seek(0) args=(wx.InputStream(res[0]),)+res[1:] res=wx.FSFile(*args) return res def _OpenFile(self, filesystem, location): proto=self.GetProtocol(location) r=self.GetRightLocation(location) params=r.split(';') r=params[0] params=params[1:] p={} for param in params: x=param.find('=') key=str(param[:x]) value=param[x+1:] if key=='width' or key=='height': p[key]=int(value) else: p[key]=value if proto=="bpimage": return self.OpenBPImageFile(location, r, **p) elif proto=="bpuserimage": return self.OpenBPUserImageFile(location, r, **p) return None def OpenBPUserImageFile(self, location, name, **kwargs): si=self.wpm.GetImageStatInformation(name) res=self._GetCache(location, si) if res is not None: return res file,cons=self.wpm.GetImageConstructionInformation(name) if cons == wx.Image: res=BPFSImageFile(self, location, file, **kwargs) else: res=BPFSImageFile(self, location, img=cons(file), **kwargs) self._AddCache(location, si, res) return res def OpenBPImageFile(self, location, name, **kwargs): f=guihelper.getresourcefile(name) if not os.path.isfile(f): print f,"doesn't exist" return None si=statinfo(f) res=self._GetCache(location, si) if res is not None: return res res=BPFSImageFile(self, location, name=f, **kwargs) self._AddCache(location, si, res) return res def BPFSImageFile(fshandler, location, name=None, img=None, width=-1, height=-1, valign="center", bgcolor=None): """Handles image files If we have to do any conversion on the file then we return PNG data. This used to be a class derived from wx.FSFile, but due to various wxPython bugs it instead returns the parameters to make a wx.FSFile since a new one has to be made every time. """ if img is None and width<0 and height<0: mime=guihelper.getwxmimetype(name) if mime not in (None, "image/x-bmp"): return (open(name, "rb"), location, mime, "", wx.DateTime_Now()) if img is None: img=wx.Image(name) if width>0 and height>0: b=ScaleImageIntoBitmap(img, width, height, bgcolor, valign) else: b=img.ConvertToBitmap() f=common.gettempfilename("png") if not b.SaveFile(f, wx.BITMAP_TYPE_PNG): raise Exception, "Saving to png failed" data=open(f, "rb").read() os.remove(f) return (cStringIO.StringIO(data), location, "image/png", "", wx.DateTime_Now()) class ImageCropSelect (wx.ScrolledWindow) : def __init__(self, parent, image, previewwindow=None, id=1, resultsize=(100,100), size=wx.DefaultSize, pos=wx.DefaultPosition, style=0): wx.ScrolledWindow.__init__(self, parent, id=id, size=size, pos=pos, style=style|wx.FULL_REPAINT_ON_RESIZE) self.previewwindow=previewwindow self.bg=wx.Brush(wx.WHITE) self.parentbg=wx.Brush(parent.GetBackgroundColour()) self._bufbmp=None self.anchors=None wx.EVT_ERASE_BACKGROUND(self, lambda evt: None) wx.EVT_PAINT(self, self.OnPaint) self.image=image self.origimage=image self.setresultsize(resultsize) self.cursors=[wx.StockCursor(c) for c in (wx.CURSOR_ARROW, wx.CURSOR_HAND, wx.CURSOR_SIZING, wx.CURSOR_NO_ENTRY)] self.clickpoint=None wx.EVT_MOTION(self, self.OnMotion) wx.EVT_LEFT_DOWN(self, self.OnLeftDown) wx.EVT_LEFT_UP(self, self.OnLeftUp) def SetPreviewWindow(self, previewwindow): self.previewwindow=previewwindow def OnPaint(self, _): sz=self.thebmp.GetWidth(), self.thebmp.GetHeight() sz2=self.GetClientSize() sz=max(sz[0],sz2[0])+32,max(sz[1],sz2[1])+32 if self._bufbmp is None or self._bufbmp.GetWidth()<sz[0] or self._bufbmp.GetHeight()<sz[1]: self._bufbmp=wx.EmptyBitmap((sz[0]+64)&~8, (sz[1]+64)&~8) dc=wx.BufferedPaintDC(self, self._bufbmp, style=wx.BUFFER_VIRTUAL_AREA) if sz2[0]<sz[0] or sz2[1]<sz[1]: dc.SetBackground(self.parentbg) dc.Clear() dc.DrawBitmap(self.thebmp, 0, 0, False) l,t,r,b=self.anchors points=(l,t), (r,t), (r,b), (l,b) dc.DrawLines( points+(points[0],) ) for x,y in points: dc.DrawRectangle(x-5, y-5, 10, 10) OUTSIDE=0 INSIDE=1 HANDLE_LT=2 HANDLE_RT=3 HANDLE_RB=4 HANDLE_LB=5 def _hittest(self, evt): l,t,r,b=self.anchors within=lambda x,y,l,t,r,b: l<=x<=r and t<=y<=b x,y=self.CalcUnscrolledPosition(evt.GetX(), evt.GetY()) for i,(ptx,pty) in enumerate(((l,t), (r,t), (r,b), (l,b))): if within(x,y,ptx-5, pty-5, ptx+5,pty+5): return self.HANDLE_LT+i if within(x,y,l,t,r,b): return self.INSIDE return self.OUTSIDE def OnMotion(self, evt): if evt.Dragging(): return self.OnMotionDragging(evt) self.UpdateCursor(evt) def UpdateCursor(self, evt): ht=self._hittest(evt) self.SetCursor(self.cursors[min(2,ht)]) def OnMotionDragging(self, evt): if not evt.LeftIsDown() or self.clickpoint is None: self.SetCursor(self.cursors[3]) return xx,yy=self.CalcUnscrolledPosition(evt.GetX(), evt.GetY()) deltax=xx-self.origevtpos[0] deltay=yy-self.origevtpos[1] if self.clickpoint==self.INSIDE: newanchors=self.origanchors[0]+deltax, self.origanchors[1]+deltay, \ self.origanchors[2]+deltax, self.origanchors[3]+deltay iw=self.dimensions[0] ih=self.dimensions[1] if newanchors[0]<0: newanchors=0,newanchors[1], self.origanchors[2]-self.origanchors[0], newanchors[3] if newanchors[1]<0: newanchors=newanchors[0], 0, newanchors[2], self.origanchors[3]-self.origanchors[1] if newanchors[2]>iw: newanchors=iw-(self.origanchors[2]-self.origanchors[0]),newanchors[1],iw, newanchors[3] if newanchors[3]>ih: newanchors=newanchors[0],ih-(self.origanchors[3]-self.origanchors[1]), newanchors[2],ih self.anchors=newanchors self.Refresh(False) self.updatepreview() return if self.clickpoint==self.HANDLE_LT: aa=0,1,-1,-1 elif self.clickpoint==self.HANDLE_RT: aa=2,1,+1,-1 elif self.clickpoint==self.HANDLE_RB: aa=2,3,+1,+1 elif self.clickpoint==self.HANDLE_LB: aa=0,3,-1,+1 else: assert False, "can't get here" na=[self.origanchors[0],self.origanchors[1],self.origanchors[2],self.origanchors[3]] na[aa[0]]=na[aa[0]]+deltax na[aa[1]]=na[aa[1]]+deltay neww=na[2]-na[0] newh=na[3]-na[1] ar=float(neww)/newh if ar<self.aspectratio: na[aa[0]]=na[aa[0]]+(self.aspectratio*newh-neww)*aa[2] elif ar>self.aspectratio: na[aa[1]]=na[aa[1]]+(neww/self.aspectratio-newh)*aa[3] if neww<10 or newh<10: return if na[0]<0: xdiff=-na[0] ydiff=xdiff/self.aspectratio na[0]=0 na[1]+=ydiff if na[1]<0: ydiff=-na[1] xdiff=ydiff*self.aspectratio na[1]=0 na[0]-=xdiff if na[2]>self.dimensions[0]: xdiff=na[2]-self.dimensions[0] ydiff=xdiff/self.aspectratio na[2]=na[2]-xdiff na[3]=na[3]-ydiff if na[3]>self.dimensions[1]: ydiff=na[3]-self.dimensions[1] xdiff=ydiff*self.aspectratio na[2]=na[2]-xdiff na[3]=na[3]-ydiff if na[0]<0 or na[1]<0 or na[2]>self.dimensions[0] or na[3]>self.dimensions[1]: print "offscreen fixup not written yet" return self.anchors=na self.Refresh(False) self.updatepreview() return def OnLeftDown(self, evt): ht=self._hittest(evt) if ht==self.OUTSIDE: self.SetCursor(self.cursors[3]) return self.clickpoint=ht xx,yy=self.CalcUnscrolledPosition(evt.GetX(), evt.GetY()) self.origevtpos=xx,yy self.origanchors=self.anchors def OnLeftUp(self, evt): self.clickpoint=None self.UpdateCursor(evt) def setlbcolour(self, colour): self.bg=wx.Brush(colour) self.remakebitmap() def SetZoom(self, factor): curzoom=float(self.image.GetWidth())/self.origimage.GetWidth() self.anchors=[a*factor/curzoom for a in self.anchors] self.image=self.origimage.Scale(self.origimage.GetWidth()*factor, self.origimage.GetHeight()*factor) self.setresultsize(self.resultsize) def setresultsize(self, (w,h)): self.resultsize=w,h self.aspectratio=ratio=float(w)/h imgratio=float(self.image.GetWidth())/self.image.GetHeight() neww=self.image.GetWidth() newh=self.image.GetHeight() if imgratio<ratio: neww*=ratio/imgratio elif imgratio>ratio: newh*=imgratio/ratio neww=max(neww, 50) newh=max(newh, 50) if self.anchors==None: self.anchors=0.1 * neww, 0.1 * newh, 0.9 * neww, 0.9 * newh l,t,r,b=self.anchors l=min(neww-40, l) r=min(neww-10, r) if r-l<20: r=40 t=min(newh-40, t) b=min(newh-10, b) if b-t<20: b=40 aratio=float(r-l)/(b-t) if aratio<ratio: b=t+(r-l)/ratio elif aratio>ratio: r=l+(b-t)*ratio self.anchors=l,t,r,b self.dimensions=neww,newh self.thebmp=wx.EmptyBitmap(neww, newh) self.remakebitmap() def remakebitmap(self): w,h=self.dimensions dc=wx.MemoryDC() dc.SelectObject(self.thebmp) dc.SetBackground(self.bg) dc.Clear() dc.DrawBitmap(self.image.ConvertToBitmap(), w/2-self.image.GetWidth()/2, h/2-self.image.GetHeight()/2, True) dc.SelectObject(wx.NullBitmap) self.imageofthebmp=None self.SetVirtualSize( (w, h) ) self.SetScrollRate(1,1) self.updatepreview() self.Refresh(False) def updatepreview(self): if self.previewwindow: self.previewwindow.SetUpdated(self.GetPreview) def GetPreview(self): w,h=self.resultsize l,t,r,b=self.anchors scale=max(float(w+0.99999)/(r-l), float(h+0.99999)/(b-t)) if True and scale<1: sub=wx.EmptyBitmap(w,h) mdcsub=wx.MemoryDC() mdcsub.SelectObject(sub) mdcsub.SetUserScale(scale, scale) mdc=wx.MemoryDC() mdc.SelectObject(self.thebmp) mdcsub.Blit(0,0,r-l,b-t,mdc,l,t) mdc.SelectObject(wx.NullBitmap) mdcsub.SelectObject(wx.NullBitmap) return sub sub=self.thebmp.GetSubBitmap( (l,t,(r-l),(b-t)) ) sub=sub.ConvertToImage() sub.Rescale(w,h) return sub.ConvertToBitmap() class ImagePreview (wx.PyWindow) : def __init__(self, parent): wx.PyWindow.__init__(self, parent) wx.EVT_ERASE_BACKGROUND(self, lambda evt: None) wx.EVT_PAINT(self, self.OnPaint) self.bmp=wx.EmptyBitmap(1,1) self.updater=None def SetUpdated(self, updater): self.updater=updater self.Refresh(True) def OnPaint(self, _): if self.updater is not None: self.bmp=self.updater() self.updater=None dc=wx.PaintDC(self) dc.DrawBitmap(self.bmp, 0, 0, False) class ImagePreviewDialog (wx.Dialog) : SCALES=[ (0.25, "1/4"), (0.5, "1/2"), (1, "1"), (2, "2"), (4, "4")] def __init__(self, parent, image, filename, phoneprofile): wx.Dialog.__init__(self, parent, -1, "Image Preview", style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.SYSTEM_MENU|wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX) self.phoneprofile=phoneprofile self.filename=filename self.image=image vbsouter=wx.BoxSizer(wx.VERTICAL) hbs=wx.BoxSizer(wx.HORIZONTAL) self.cropselect=ImageCropSelect(self, image) vbs=wx.BoxSizer(wx.VERTICAL) self.colourselect=wx.lib.colourselect.ColourSelect(self, wx.NewId(), "Background ...", (255,255,255)) vbs.Add(self.colourselect, 0, wx.ALL|wx.EXPAND, 5) wx.lib.colourselect.EVT_COLOURSELECT(self, self.colourselect.GetId(), self.OnBackgroundColour) vbs.Add(wx.StaticText(self, -1, "Origin"), 0, wx.ALL, 5) self.originbox=wx.ListBox(self, size=(-1, 100)) vbs.Add(self.originbox, 0, wx.ALL|wx.EXPAND, 5) vbs.Add(wx.StaticText(self, -1, "Target"), 0, wx.ALL, 5) self.targetbox=wx.ListBox(self, size=(-1,100)) vbs.Add(self.targetbox, 0, wx.EXPAND|wx.ALL, 5) vbs.Add(wx.StaticText(self, -1, "Scale"), 0, wx.ALL, 5) for one,(s,_) in enumerate(self.SCALES): if s==1: break self.slider=wx.Slider(self, -1, one, 0, len(self.SCALES)-1, style=wx.HORIZONTAL|wx.SL_AUTOTICKS) wx.EVT_SCROLL(self, self.SetZoom) vbs.Add(self.slider, 0, wx.ALL|wx.EXPAND, 5) self.zoomlabel=wx.StaticText(self, -1, self.SCALES[one][1]) vbs.Add(self.zoomlabel, 0, wx.ALL|wx.ALIGN_CENTRE_HORIZONTAL, 5) vbs.Add(wx.StaticText(self, -1, "Preview"), 0, wx.ALL, 5) self.imagepreview=ImagePreview(self) self.cropselect.SetPreviewWindow(self.imagepreview) vbs.Add(self.imagepreview, 0, wx.ALL, 5) hbs.Add(vbs, 0, wx.ALL, 5) hbs.Add(self.cropselect, 1, wx.ALL|wx.EXPAND, 5) vbsouter.Add(hbs, 1, wx.EXPAND|wx.ALL, 5) vbsouter.Add(wx.StaticLine(self, -1, style=wx.LI_HORIZONTAL), 0, wx.EXPAND|wx.ALL, 5) vbsouter.Add(self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.HELP), 0, wx.ALIGN_CENTRE|wx.ALL, 5) wx.EVT_LISTBOX(self, self.originbox.GetId(), self.OnOriginSelect) wx.EVT_LISTBOX_DCLICK(self, self.originbox.GetId(), self.OnOriginSelect) wx.EVT_LISTBOX(self, self.targetbox.GetId(), self.OnTargetSelect) wx.EVT_LISTBOX_DCLICK(self, self.targetbox.GetId(), self.OnTargetSelect) wx.EVT_BUTTON(self, wx.ID_HELP, lambda _: wx.GetApp().displayhelpid(helpids.ID_DLG_IMAGEPREVIEW)) self.originbox.Set(phoneprofile.GetImageOrigins().keys()) self.originbox.SetSelection(0) self.OnOriginSelect(None) self.SetSizer(vbsouter) vbsouter.Fit(self) import guiwidgets guiwidgets.set_size("wallpaperpreview", self, 80, 1.0) def ShowModal(self): res=wx.Dialog.ShowModal(self) import guiwidgets guiwidgets.save_size("wallpaperpreview", self.GetRect()) return res def SetZoom(self, evt): self.cropselect.SetZoom(self.SCALES[evt.GetPosition()][0]) self.zoomlabel.SetLabel(self.SCALES[evt.GetPosition()][1]) return def OnBackgroundColour(self, evt): self.cropselect.setlbcolour(evt.GetValue()) def OnOriginSelect(self, _): v=self.originbox.GetStringSelection() assert v is not None t=self.targetbox.GetStringSelection() self.targets=self.phoneprofile.GetTargetsForImageOrigin(v) keys=self.targets.keys() keys.sort() self.targetbox.Set(keys) if t in keys: self.targetbox.SetSelection(keys.index(t)) else: self.targetbox.SetSelection(0) self.OnTargetSelect(None) def OnTargetSelect(self, _): v=self.targetbox.GetStringSelection() print "target is",v w,h=self.targets[v]['width'],self.targets[v]['height'] self.imagepreview.SetSize( (w,h) ) self.cropselect.setresultsize( (w, h) ) sz=self.GetSizer() if sz is not None: sz.Layout() self.Refresh(True) def GetResultImage(self): return self.imagepreview.bmp.ConvertToImage() def GetResultParams(self): return self.targets[self.targetbox.GetStringSelection()] def GetResultOrigin(self): return self.originbox.GetStringSelection() if __name__=='__main__': if __debug__: def profile(filename, command): import hotshot, hotshot.stats, os file=os.path.abspath(filename) profile=hotshot.Profile(file) profile.run(command) profile.close() del profile howmany=100 stats=hotshot.stats.load(file) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(100) stats.sort_stats('cum', 'calls') stats.print_stats(100) stats.sort_stats('calls', 'time') stats.print_stats(100) sys.exit(0) class FakeProfile: def GetImageOrigins(self): return {"images": {'description': 'General images'}, "mms": {'description': 'Multimedia Messages'}, "camera": {'description': 'Camera images'}} def GetTargetsForImageOrigin(self, origin): return {"wallpaper": {'width': 100, 'height': 200, 'description': 'Display as wallpaper'}, "photoid": {'width': 100, 'height': 150, 'description': 'Display as photo id'}, "outsidelcd": {'width': 90, 'height': 80, 'description': 'Display on outside screen'}} def run(): app=wx.PySimpleApp() dlg=ImagePreviewDialog(None, wx.Image("test.jpg"), "foobar.png", FakeProfile()) dlg.ShowModal() if __debug__ and True: profile("wp.prof", "run()") run() import fileview if __name__=='__main__': if __debug__: def profile(filename, command): import hotshot, hotshot.stats, os file=os.path.abspath(filename) profile=hotshot.Profile(file) profile.run(command) profile.close() del profile howmany=100 stats=hotshot.stats.load(file) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(100) stats.sort_stats('cum', 'calls') stats.print_stats(100) stats.sort_stats('calls', 'time') stats.print_stats(100) sys.exit(0) class FakeProfile: def GetImageOrigins(self): return {"images": {'description': 'General images'}, "mms": {'description': 'Multimedia Messages'}, "camera": {'description': 'Camera images'}} def GetTargetsForImageOrigin(self, origin): return {"wallpaper": {'width': 100, 'height': 200, 'description': 'Display as wallpaper'}, "photoid": {'width': 100, 'height': 150, 'description': 'Display as photo id'}, "outsidelcd": {'width': 90, 'height': 80, 'description': 'Display on outside screen'}} def run(): app=wx.PySimpleApp() dlg=ImagePreviewDialog(None, wx.Image("test.jpg"), "foobar.png", FakeProfile()) dlg.ShowModal() if __debug__ and True: profile("wp.prof", "run()") run()