Title: [commits] (jeffrey) [15931] [APP] - Tweak drawing of one line lozenges
Revision
15931
Author
jeffrey
Date
2007-11-27 18:01:04 -0800 (Tue, 27 Nov 2007)

Log Message

[APP] - Tweak drawing of one line lozenges

Modified Paths

Diff

Modified: branches/rearchitecture/Chandler-Platform/ocap/wxui/calendar.py (15930 => 15931)

--- branches/rearchitecture/Chandler-Platform/ocap/wxui/calendar.py	2007-11-28 01:22:18 UTC (rev 15930)
+++ branches/rearchitecture/Chandler-Platform/ocap/wxui/calendar.py	2007-11-28 02:01:04 UTC (rev 15931)
@@ -17,14 +17,15 @@
 from math import radians
 from datetime import datetime, timedelta
 from colorsys import hsv_to_rgb
-from drawing import DrawWrappedText
+from drawing import DrawWrappedText, DrawClippedText
 
 SWATCH_BUFFER = (2,2)
 TEXT_BUFFER = (2,2)
 OUTLINE_WIDTH = 1
-HALO_WIDTH    = 3
+HALO_WIDTH = 1
+FULL_HALO_WIDTH = 2 * HALO_WIDTH + OUTLINE_WIDTH
 DEFAULT_TEXT_SIZE = 10
-ARC_RADIUS = 5.0
+SMALL_RADIUS = 5.0
 
 LEGEND_BORDER_WIDTH = 3.0
 LEGEND_TEXT_MARGIN = 2.0
@@ -53,17 +54,21 @@
 LEGEND_COLOR     = wx.Color(128, 128, 128)
 
 class InitializeSlots(object):
+    __slots__ = ()
     def __init__(self, **kw):
         for slot in self.__slots__:
             setattr(self, slot, kw.get(slot))
 
 class LozengeColors(InitializeSlots):
     __slots__ = ('text', 'outline', 'halo', 'gradient_left', 'gradient_right')
-    text = wx.WHITE
-    outline = wx.Color(0, 102, 204)
-    halo = wx.Color(255,255,255, 128)
-    gradient_left = wx.Color(0, 63, 127)
-    gradient_right = wx.Color(0, 102, 204)
+
+DefaultColors = LozengeColors(
+    text = wx.WHITE,
+    outline = wx.Color(0, 102, 204),
+    halo = wx.Color(255,255,255, 128),
+    gradient_left = wx.Color(0, 63, 127),
+    gradient_right = wx.Color(0, 102, 204),
+)
     
 class Event(InitializeSlots):
     __slots__ = ('id', 'title', 'start', 'duration', 'color', 'swatch_colors')
@@ -181,11 +186,20 @@
                 
         self.business_start = 9
         self.business_end   = 17
+        # CreateMeasuringContext has the wrong SWIG directive applied as of 11/2007,
+        # so this won't work
+        #measure = wx.GraphicsContext.CreateMeasuringContext()
+        measure = wx.GraphicsContext.Create(self)
+        
         self.legend_font = wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
+        self.legend_font_height = font_height(measure, self.legend_font)
         self.legend_strings = [str(i) for i in range(1,13)] * 2
 
-        self.lozenge_font = wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
+        self.time_font = wx.Font(8, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
+        self.time_font_height = font_height(measure, self.time_font)
+        
         self.title_font = self.legend_font
+        self.title_font_height = font_height(measure, self.title_font)
 
         self.range_start = None
         self.range_mode = 'week'
@@ -285,9 +299,15 @@
             else:
                 rect.Inset(depth/(max_depth - 1)*BASE_DAY_WIDTH*0.2, 0, 0, 0)
                 rect.width = BASE_DAY_WIDTH * 0.8
-        
+
         rect = TransformRect(self.matrix, rect)
-        rect.Inset(1, 1)
+        rect.Inset(HALO_WIDTH, HALO_WIDTH)
+
+        line_height = self.title_font_height
+        min_height = 2*TEXT_Y_MARGIN + line_height
+        if rect.height < min_height:
+            rect.SetBottom(min(self.hour_height * 24, rect.y + min_height))
+            
         if column == 0 and depth == 0:
             # don't let the halo overlap the legend border by too much
             rect.Inset(.5, 0, 0, 0)
@@ -298,24 +318,41 @@
         if event.end > end:
             truncate |= wx.BOTTOM
         
-        DrawFilledLozenge(gc, rect, truncate = truncate)
+        if event.start < start:
+            # continuation lozenge, just draw a small-radius lozenge
+            DrawFilledLozenge(gc, rect, SMALL_RADIUS, truncate = truncate)
         
         # draw one line?  Draw time?  Draw title on first line?
-        # Room for how many lozenges (min 2)?
+        # Room for how many swatches (min 2)?
         
         if event.start >= start:
-            rect.Inset(TEXT_X_MARGIN - 1, TEXT_Y_MARGIN, TEXT_X_MARGIN - 1, 0)
+            radius = SMALL_RADIUS if rect.height != min_height else min_height/2
+            print radius, event.id
+            DrawFilledLozenge(gc, rect, radius, truncate = truncate)
+            
+            time_height = (TEXT_Y_MARGIN + TIME_BOTTOM_MARGIN +
+                           self.time_font_height)
+
+            rect.Inset(TEXT_X_MARGIN - 1, 0)
             gc.Clip(*rect)
             rect.Inset(1, 0)
-            time_text = self.time_formatter(start)
-            width, height = gc.GetTextExtent(time_text)
 
-            gc.SetFont(self.lozenge_font, LozengeColors.text)
-            gc.DrawText(time_text, rect.x, rect.y)
-            rect.Inset(0, TIME_BOTTOM_MARGIN + height, 0, 0)
-            gc.SetFont(self.title_font, LozengeColors.text)
-            # need to wrap text
-            DrawWrappedText(gc, event.title, rect)
+            if rect.height <= time_height + line_height:
+                # room for just one line of text
+                text_top = rect.GetCentre()[1] - line_height/2
+                gc.SetFont(self.title_font, DefaultColors.text)
+                DrawClippedText(gc, event.title, rect.x, text_top, rect.width)
+            else:
+                rect.Inset(0, TEXT_Y_MARGIN, 0, 0)
+                time_text = self.time_formatter(start)
+                gc.SetFont(self.time_font, DefaultColors.text)
+                gc.DrawText(time_text, rect.x, rect.y)
+                width, height = gc.GetTextExtent(time_text)
+                rect.Inset(0, TIME_BOTTOM_MARGIN + height, 0, 0)
+                gc.SetFont(self.title_font, DefaultColors.text)
+                # need to wrap text
+                DrawWrappedText(gc, event.title, rect)
+                
             gc.ResetClip()
 
 
@@ -414,9 +451,7 @@
     def OnSize(self, event):
         width, height = self.GetSize()
         self.SetVirtualSize((width, self.hour_height * 24))
-
         self.SetScrollRate(0, self.hour_height / 3)
-        
         self.Refresh(False)
 
     def GetDays(self):
@@ -465,11 +500,11 @@
     return gc.CreateLinearGradientBrush(offset, 0, offset + width, 0,
                                         gradient_left, gradient_right)
 
-def DrawFilledLozenge(gc, rect, colors=LozengeColors, truncate = 0):
+def DrawFilledLozenge(gc, rect, radius, colors=DefaultColors, truncate=0):
     """Draw a lozenge, truncated edges won't be rounded."""
-    path = GetLozengePath(gc, rect, truncate)
-    if HALO_WIDTH:
-        halo_pen = wx.Pen(colors.halo, HALO_WIDTH)
+    path = GetLozengePath(gc, rect, truncate, radius)
+    if FULL_HALO_WIDTH:
+        halo_pen = wx.Pen(colors.halo, FULL_HALO_WIDTH)
         gc.SetPen(halo_pen)
         gc.StrokePath(path)
 
@@ -479,7 +514,7 @@
     gc.SetPen(wx.Pen(colors.outline, OUTLINE_WIDTH))
     gc.DrawPath(path)
 
-def GetLozengePath(gc, rect, truncate):
+def GetLozengePath(gc, rect, truncate, radius):
     """
     Return a rounded rectangle path with certain edges truncated (not rounded).
     
@@ -490,11 +525,11 @@
     path = gc.CreatePath()
     x, y, width, height = rect
     if not truncate:
-        path.AddRoundedRectangle(x, y, width, height, ARC_RADIUS)
+        path.AddRoundedRectangle(x, y, width, height, radius)
         return path
         
     corners = wx.Rect2D(*rect)
-    adjust = wx.Point2D(0, ARC_RADIUS)
+    adjust = wx.Point2D(0, radius)
     
     start = corners.GetRightTop()
     if not (truncate & (wx.RIGHT | wx.TOP)):
@@ -515,12 +550,12 @@
         if truncate & (last_edge | edge):
             path.AddLineToPoint(point)
         else:
-            adjust.SetPolarCoordinates(90*(i - 1), ARC_RADIUS)
+            adjust.SetPolarCoordinates(90*(i - 1), radius)
             point += adjust
             path.AddLineToPoint(point)
-            adjust.SetPolarCoordinates(90*(i - 2), ARC_RADIUS)
+            adjust.SetPolarCoordinates(90*(i - 2), radius)
             point += adjust
-            path.AddArc(point, ARC_RADIUS, radians(i*90), radians((i + 1)*90))
+            path.AddArc(point, radius, radians(i*90), radians((i + 1)*90))
         
     return path
 
@@ -529,6 +564,10 @@
     width, height = matrix.TransformDistance(rect.width, rect.height)
     return wx.Rect2D(x, y, width, height)
 
+def font_height(gc, font):
+    gc.SetFont(font)
+    return gc.GetTextExtent('M')[1]
+
 def shortTZ(dt, tzinfo=None):
     """
     Return an empty string or the short timezone string for dt if dt.tzinfo
@@ -581,7 +620,7 @@
     calendar.SetRange(datetime(2007,11,25))
     calendar.ChangeEvent(id="foo0", title=lorem,
                          start=datetime(2007,11,25,10), 
-                         duration=timedelta(hours=4.97))
+                         duration=timedelta(hours=1.095))
     calendar.ChangeEvent(id="foo1", title=lorem,
                          start=datetime(2007,11,26,10), 
                          duration=timedelta(hours=18))
@@ -594,6 +633,19 @@
     calendar.ChangeEvent(id="foo4", title=lorem,
                          start=datetime(2007,11,27,18), 
                          duration=timedelta(hours=3))
+    calendar.ChangeEvent(id="foo5", title=lorem,
+                         start=datetime(2007,11,25,11,30), 
+                         duration=timedelta(hours=.8))
+    calendar.ChangeEvent(id="foo6", title=lorem,
+                         start=datetime(2007,11,25,12,30), 
+                         duration=timedelta(hours=.7))
+    calendar.ChangeEvent(id="foo7", title=lorem,
+                         start=datetime(2007,11,25,13,30), 
+                         duration=timedelta(hours=.6))
+    calendar.ChangeEvent(id="foo8", title=lorem,
+                         start=datetime(2007,11,25,23,45), 
+                         duration=timedelta(hours=1))
+
     frame.Show()
     wxApp.MainLoop()
 




_______________________________________________
Commits mailing list
[email protected]
http://lists.osafoundation.org/mailman/listinfo/commits

Reply via email to