graphics/GraphicCommons.py
changeset 1169 53e4a2b775a7
parent 1166 2ed9675be08d
child 1170 074e46cdedbc
equal deleted inserted replaced
1168:7838595559ba 1169:53e4a2b775a7
    27 from math import *
    27 from math import *
    28 from types import *
    28 from types import *
    29 import datetime
    29 import datetime
    30 from threading import Lock,Timer
    30 from threading import Lock,Timer
    31 
    31 
       
    32 from controls.CustomToolTip import CustomToolTip, TOOLTIP_WAIT_PERIOD
       
    33 
    32 #-------------------------------------------------------------------------------
    34 #-------------------------------------------------------------------------------
    33 #                               Common constants
    35 #                               Common constants
    34 #-------------------------------------------------------------------------------
    36 #-------------------------------------------------------------------------------
    35 
    37 
    36 """
    38 """
    99 ERROR_HIGHLIGHT = (wx.Colour(255, 255, 0), wx.RED)
   101 ERROR_HIGHLIGHT = (wx.Colour(255, 255, 0), wx.RED)
   100 SEARCH_RESULT_HIGHLIGHT = (wx.Colour(255, 165, 0), wx.WHITE)
   102 SEARCH_RESULT_HIGHLIGHT = (wx.Colour(255, 165, 0), wx.WHITE)
   101 
   103 
   102 # Define highlight refresh inhibition period in second
   104 # Define highlight refresh inhibition period in second
   103 REFRESH_HIGHLIGHT_PERIOD = 0.1
   105 REFRESH_HIGHLIGHT_PERIOD = 0.1
   104 
       
   105 # Define tooltip wait for displaying period in second
       
   106 TOOLTIP_WAIT_PERIOD = 0.5
       
   107 
   106 
   108 HANDLE_CURSORS = {
   107 HANDLE_CURSORS = {
   109     (1, 1) : 2,
   108     (1, 1) : 2,
   110     (3, 3) : 2,
   109     (3, 3) : 2,
   111     (1, 3) : 3,
   110     (1, 3) : 3,
   551         if self.currentBox:
   550         if self.currentBox:
   552             # Draw current box
   551             # Draw current box
   553             dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, 
   552             dc.DrawRectangle(self.currentBox.x * scalex, self.currentBox.y * scaley, 
   554                              self.currentBox.width * scalex, self.currentBox.height * scaley)
   553                              self.currentBox.width * scalex, self.currentBox.height * scaley)
   555         dc.SetUserScale(scalex, scaley)
   554         dc.SetUserScale(scalex, scaley)
   556 
       
   557 #-------------------------------------------------------------------------------
       
   558 #                               Viewer ToolTip
       
   559 #-------------------------------------------------------------------------------
       
   560 
       
   561 """
       
   562 Class that implements a custom tool tip
       
   563 """
       
   564 
       
   565 if wx.Platform == '__WXMSW__':
       
   566     faces = { 'times': 'Times New Roman',
       
   567               'mono' : 'Courier New',
       
   568               'helv' : 'Arial',
       
   569               'other': 'Comic Sans MS',
       
   570               'size' : 10,
       
   571              }
       
   572 else:
       
   573     faces = { 'times': 'Times',
       
   574               'mono' : 'Courier',
       
   575               'helv' : 'Helvetica',
       
   576               'other': 'new century schoolbook',
       
   577               'size' : 12,
       
   578              }
       
   579 
       
   580 TOOLTIP_MAX_CHARACTERS = 30
       
   581 TOOLTIP_MAX_LINE = 5
       
   582 
       
   583 class ToolTip(wx.PopupWindow):
       
   584     
       
   585     def __init__(self, parent, tip, restricted=True):
       
   586         wx.PopupWindow.__init__(self, parent)
       
   587         
       
   588         self.CurrentPosition = wx.Point(0, 0)
       
   589         self.Restricted = restricted
       
   590         
       
   591         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
       
   592         self.SetTip(tip)
       
   593         
       
   594         self.Font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"])
       
   595         
       
   596         self.Bind(wx.EVT_PAINT, self.OnPaint)
       
   597     
       
   598     def SetFont(self, font):
       
   599         self.Font = font
       
   600         self.RefreshTip()
       
   601     
       
   602     def SetTip(self, tip):
       
   603         lines = []
       
   604         for line in tip.splitlines():
       
   605             if self.Restricted and line != "":
       
   606                 words = line.split()
       
   607                 new_line = words[0]
       
   608                 for word in words[1:]:
       
   609                     if len(new_line + " " + word) <= TOOLTIP_MAX_CHARACTERS:
       
   610                         new_line += " " + word
       
   611                     else:
       
   612                         lines.append(new_line)
       
   613                         new_line = word
       
   614                 lines.append(new_line)
       
   615             else:
       
   616                 lines.append(line)
       
   617         if self.Restricted and len(lines) > TOOLTIP_MAX_LINE:
       
   618             self.Tip = lines[:TOOLTIP_MAX_LINE]
       
   619             if len(self.Tip[-1]) < TOOLTIP_MAX_CHARACTERS - 3:
       
   620                 self.Tip[-1] += "..."
       
   621             else:
       
   622                 self.Tip[-1] = self.Tip[-1][:TOOLTIP_MAX_CHARACTERS - 3] + "..."
       
   623         else:
       
   624             self.Tip = lines
       
   625         wx.CallAfter(self.RefreshTip)
       
   626     
       
   627     def MoveToolTip(self, pos):
       
   628         screen_size = wx.GetDisplaySize()
       
   629         w, h = self.GetTipExtent()
       
   630         self.CurrentPosition = wx.Point(
       
   631             max(0, min(pos.x, screen_size[0] - w - 4)),
       
   632             max(0, min(pos.y, screen_size[1] - h - 4))) 
       
   633         self.SetPosition(pos)
       
   634     
       
   635     def GetTipExtent(self):
       
   636         max_width = 0
       
   637         max_height = 0
       
   638         for line in self.Tip:
       
   639             dc = wx.MemoryDC()
       
   640             dc.SetFont(self.Font)
       
   641             w, h = dc.GetTextExtent(line)
       
   642             max_width = max(max_width, w)
       
   643             max_height += h
       
   644         return max_width, max_height
       
   645     
       
   646     def RefreshTip(self):
       
   647         if self:
       
   648             w, h = self.GetTipExtent()
       
   649             self.SetSize(wx.Size(w + 4, h + 4))
       
   650             self.SetPosition(self.CurrentPosition)
       
   651             self.Refresh()
       
   652         
       
   653     def OnPaint(self, event):
       
   654         dc = wx.AutoBufferedPaintDC(self)
       
   655         dc.Clear()
       
   656         dc.SetPen(MiterPen(wx.BLACK))
       
   657         dc.SetBrush(wx.Brush(wx.Colour(255, 238, 170)))
       
   658         dc.SetFont(self.Font)
       
   659         dc.BeginDrawing()
       
   660         w, h = self.GetTipExtent()
       
   661         dc.DrawRectangle(0, 0, w + 4, h + 4)
       
   662         offset = 0
       
   663         for line in self.Tip:
       
   664             dc.DrawText(line, 2, offset + 2)
       
   665             w, h = dc.GetTextExtent(line)
       
   666             offset += h
       
   667         dc.EndDrawing()
       
   668         event.Skip()
       
   669 
   555 
   670 #-------------------------------------------------------------------------------
   556 #-------------------------------------------------------------------------------
   671 #                    Helpers for highlighting text
   557 #                    Helpers for highlighting text
   672 #-------------------------------------------------------------------------------
   558 #-------------------------------------------------------------------------------
   673 
   559 
  1105         return 0, 0
   991         return 0, 0
  1106     
   992     
  1107     def OnToolTipTimer(self, event):
   993     def OnToolTipTimer(self, event):
  1108         value = self.GetToolTipValue()
   994         value = self.GetToolTipValue()
  1109         if value is not None and self.ToolTipPos is not None:
   995         if value is not None and self.ToolTipPos is not None:
  1110             self.ToolTip = ToolTip(self.Parent, value)
   996             self.ToolTip = CustomToolTip(self.Parent, value)
  1111             self.ToolTip.MoveToolTip(self.ToolTipPos)
   997             self.ToolTip.MoveToolTip(self.ToolTipPos)
  1112             self.ToolTip.Show()
   998             self.ToolTip.Show()
  1113         
   999         
  1114     def GetToolTipValue(self):
  1000     def GetToolTipValue(self):
  1115         return None
  1001         return None