~ubuntu-branches/ubuntu/raring/tegaki-pygtk/raring

« back to all changes in this revision

Viewing changes to tegakigtk/canvas.py

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing
  • Date: 2010-03-24 21:48:27 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100324214827-3p4qervs7jnvgpo7
Tags: 0.3.1-1
* new upstream release.
* debian/control: bump standards version to 3.8.4.
* debian/source/format: 1.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    """
33
33
    A character drawing canvas.
34
34
 
35
 
    A port of Takuro Ashie's TomoeCanvas to pygtk + additional features.
36
 
    Also based on a tutorial by Mark Mruss.
 
35
    This widget receives the input from the user and can return the
 
36
    corresponding L{tegaki.Writing} objects.
 
37
 
 
38
    It also has a "replay" method which can display a stroke-by-stroke
 
39
    animation of the current writing.
 
40
 
 
41
    The code was originally ported from Tomoe (C language).
 
42
    Since then many additional features were added.
37
43
    """
38
44
 
39
 
    # Default canvas size
 
45
    #: Default canvas size
40
46
    DEFAULT_WIDTH = 400
41
47
    DEFAULT_HEIGHT = 400
42
48
 
 
49
    #: Default canvas size
43
50
    DEFAULT_REPLAY_SPEED = 50 # msec
44
51
 
 
52
    #: - the stroke-added signal is emitted when the user has added a stroke
 
53
    #: - the drawing-stopped signal is emitted when the user has stopped drawing
45
54
    __gsignals__ = {
46
55
        "stroke_added" :     (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
47
56
        "drawing_stopped" :  (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [])
63
72
        self._drawing_stopped_time = 0
64
73
        self._drawing_stopped_id = 0
65
74
        self._draw_annotations = True
 
75
        self._need_draw_axis = True
66
76
 
67
77
        self._handwriting_line_gc = None
68
78
        self._annotation_gc = None
338
348
        """
339
349
        Converts window coordinates to internal coordinates.
340
350
        """
341
 
        sx = float(Writing.WIDTH) / self._width
342
 
        sy = float(Writing.HEIGHT) / self._height
 
351
        sx = float(self._writing.get_width()) / self._width
 
352
        sy = float(self._writing.get_height()) / self._height
343
353
        
344
354
        return (int(x * sx), int(y * sy))
345
355
    
347
357
        """
348
358
        Converts internal coordinates to window coordinates.
349
359
        """
350
 
        sx = float(self._width) / Writing.WIDTH
351
 
        sy = float(self._height) / Writing.WIDTH
 
360
        sx = float(self._width) / self._writing.get_width()
 
361
        sy = float(self._height) / self._writing.get_height()
352
362
        
353
363
        return (int(x * sx), int(y * sy))
354
364
 
436
446
        if force_draw:
437
447
            self.queue_draw_area(x-2, y-2, width+4, height+4)
438
448
 
439
 
    def _draw_axis(self):
 
449
    def _draw_axis(self):        
440
450
        self._pixmap.draw_line(self._axis_gc,
441
451
                               self._width / 2, 0,
442
452
                               self._width / 2, self._height)
452
462
                                    self.allocation.width,
453
463
                                    self.allocation.height)
454
464
 
455
 
        self._draw_axis()
 
465
        if self._need_draw_axis:
 
466
            self._draw_axis()
456
467
 
457
468
 
458
469
    def _draw_background_character(self):
580
591
    # Public...
581
592
 
582
593
    def get_drawing_stopped_time(self):
 
594
        """
 
595
        Get the inactivity time after which a character is considered drawn.
 
596
 
 
597
        @rtype: int
 
598
        @return: time in milliseconds
 
599
        """
583
600
        return self._drawing_stopped_time
584
601
 
585
602
    def set_drawing_stopped_time(self, time_msec):
 
603
        """
 
604
        Set the inactivity time after which a character is considered drawn.
 
605
 
 
606
        @type time_msec: int
 
607
        @param time_msec: time in milliseconds
 
608
        """
586
609
        self._drawing_stopped_time = time_msec
587
610
 
588
611
    def set_draw_annotations(self, draw_annotations):
 
612
        """
 
613
        Set whether to display stroke-number annotations or not.
 
614
 
 
615
        @type draw_annotations: boolean
 
616
        """
589
617
        self._draw_annotations = draw_annotations
590
618
 
591
619
    def get_draw_annotations(self):
 
620
        """
 
621
        Return whether stroke-number annotations are displayed or not.
 
622
        """
592
623
        return self._draw_annotations
593
624
 
 
625
    def set_draw_axis(self, draw_axis):
 
626
        self._need_draw_axis = draw_axis
 
627
 
 
628
    def get_draw_axis(self):
 
629
        return self._need_draw_axis
 
630
 
594
631
    def refresh(self, n_strokes=None, force_draw=False):
 
632
        """
 
633
        Update the screen.
 
634
        """
595
635
        if self._writing:
596
636
            self._refresh(self._writing,
597
637
                         n_strokes=n_strokes,
604
644
 
605
645
        If speed is None, uses the writing original speed when available or
606
646
        DEFAULT_REPLAY_SPEED when not available.
 
647
 
 
648
        @type speed: int
 
649
        @type speed: time between each point in milliseconds
607
650
        """
608
651
        self._draw_background()
609
652
        self._redraw()
623
666
        gobject.timeout_add(speed, self._on_animate)
624
667
 
625
668
    def get_writing(self, writing_width=None, writing_height=None):
 
669
        """
 
670
        Return a L{tegaki.Writing} object for the current handwriting.
 
671
 
 
672
        @type writing_width: int
 
673
        @param writing_width: the width that the writing should have or \
 
674
                              None if default
 
675
        @type writing_height: int
 
676
        @param writing_height: the height that the writing should have or \
 
677
                              None if default
 
678
        @rtype: Writing
 
679
 
 
680
        """
626
681
 
627
682
        if writing_width and writing_height:
628
683
            # Convert to requested size
629
 
            xratio = float(writing_width) / Writing.WIDTH
630
 
            yratio = float(writing_height) / Writing.HEIGHT
 
684
            xratio = float(writing_width) / self._writing.get_width()
 
685
            yratio = float(writing_height) / self._writing.get_height()
631
686
 
632
687
            return self._writing.resize(xratio, yratio)
633
688
        else:
637
692
 
638
693
        if writing_width and writing_height:
639
694
            # Convert to internal size
640
 
            xratio = float(Writing.WIDTH) / writing_width
641
 
            yratio = float(Writing.HEIGHT) / writing_height
 
695
            xratio = float(self._writing.get_width()) / writing_width
 
696
            yratio = float(self._writing.get_height()) / writing_height
642
697
           
643
698
            self._writing = self._writing.resize(xratio, yratio)
644
699
        else:
648
703
        self.refresh(force_draw=True)
649
704
 
650
705
    def clear(self):
 
706
        """
 
707
        Erase the current writing.
 
708
        """
651
709
        self._writing.clear()
652
710
 
653
711
        self.refresh(force_draw=True)
654
712
 
655
713
    def revert_stroke(self):
 
714
        """
 
715
        Undo the latest stroke
 
716
        """
656
717
        n = self._writing.get_n_strokes()
657
718
 
658
719
        if n > 0:
660
721
            self.refresh(force_draw=True)
661
722
 
662
723
    def normalize(self):
 
724
        """
 
725
        Normalize the current writing. (See L{tegaki.normalize})
 
726
        """
663
727
        self._writing.normalize()
664
728
        self.refresh(force_draw=True)
665
729
 
666
730
    def smooth(self):
 
731
        """
 
732
        Smooth the current writing. (See L{tegaki.smooth})
 
733
        """
667
734
        self._writing.smooth()
668
735
        self.refresh(force_draw=True)
669
736
 
670
737
    def set_background_character(self, character):
 
738
        """
 
739
        Set a character as background.
 
740
 
 
741
        @type character: str
 
742
        """
671
743
        self._background_character = character
672
744
 
673
745
    def get_background_writing(self):
674
746
        return self._background_writing
675
747
    
676
748
    def set_background_writing(self, writing, speed=25):
 
749
        """
 
750
        Set a writing as background. 
 
751
 
 
752
        Strokes of the background writing are displayed one at a time. 
 
753
        This is intended to let users "follow" the background writing like a
 
754
        template.
 
755
 
 
756
        @type writing: L{tegaki.Writing}
 
757
        """
677
758
        self.clear()
678
759
        self._background_writing = writing
679
760
        self._speed = speed
682
763
        self.refresh(force_draw=True)
683
764
 
684
765
    def set_background_color(self, r, g, b):
 
766
        """
 
767
        Set background color.
 
768
 
 
769
        @type r: int
 
770
        @param r: red
 
771
        @type g: int
 
772
        @param g: green
 
773
        @type b: int
 
774
        @param b: blue
 
775
        """
685
776
        self._background_color = (r, g, b)
686
777
        
687
778
        if self._background_gc: