コード例 #1
0
ファイル: DebugObserver.py プロジェクト: ASayre/UCSBsketch
    def drawMyself( self ):
        # FIXME: does this tie us to the tk front-end?  If so, what should
        # we put into the GUI API to enable this sort of animation? is it mostly 
        # "update" that is needed?
        from SketchFramework import SketchGUI as gui
        canvas = gui.SketchGUISingleton()
        color_levels = { 0: "#FF6633", 1: "#FF00FF", 2: "#3366FF", 3: "#00CC00",}
        scale = 18  # pixels for text size

        # start with a list of all the annotations
        allAnnoSet = set([])
        logger.debug("Watch set %s" % (self.watchSet))
        for stroke in BoardSingleton().Strokes:
            logger.debug("Stroke annotations %s" % (stroke.findAnnotations(None)))

            for anno in stroke.findAnnotations(None):
                logger.debug ("%s in %s?" % (type(anno), list(self.watchSet)))
                if type(anno) in list(self.watchSet):
                    logger.debug("Adding %s to annoset" % (anno))
                    allAnnoSet.add( anno )

        # now make a map from the sets of strokes to the annotations on them
        annoMap = {} # dictionary of {frozenset(strokes):[annotations]}
        for anno in allAnnoSet:
            strokeset = frozenset(anno.Strokes)
            if strokeset not in annoMap:
                annoMap[strokeset] = []
            annoMap[strokeset].append( anno )

        # now assign a unique size for each anno on a given set of strokes
        sizeDict = {}
        for strokeset in annoMap.keys():
            depth = 0
            for anno in annoMap[strokeset]:
                sizeDict[anno] = depth
                depth += 1

        # sort the annotations based on the time at which they were added
        annoList = list(allAnnoSet)
        annoList.sort(key= (lambda x: x.Time))
        for anno in annoList:
            nestlevel = sizeDict[anno] # get the nesting level for this annotation
            # for the set of stroke this anno is annotating, find the bounding box
            
            tl = anno.Strokes[0].BoundTopLeft
            br = anno.Strokes[0].BoundBottomRight
            tlx = tl.X
            tly = tl.Y
            brx = br.X
            bry = br.Y
            
            bottomright_list = [s.BoundBottomRight for s in anno.Strokes]
            topleft_list = [s.BoundTopLeft for s in anno.Strokes]
            br, tl = _nestingBox(bottomright_list, topleft_list, scale = nestlevel*3)
            br.Y -= nestlevel * scale # save some space for text on bottom of box

            # if this is a "new" anno, wait a little before drawing
            if anno not in self.seenBefore:
                #time.sleep(0.5)
                self.seenBefore[anno] = True

            # now draw the actual boxes
            labeltext = anno.classname()
            tlx = tl.X
            tly = tl.Y
            brx = br.X
            bry = br.Y
            
            gui.drawBox(tl,br,color=color_levels[nestlevel % len(color_levels)])
            gui.drawText(tl.X, br.Y+scale, size = 12, InText=labeltext)