Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pygtk_chart/chart_object.py
blob: 8e4cd7e8203d6b95c3155ae99f9aaeabc460b29b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
#
#       chart_object.py
#       
#       Copyright 2009 Sven Festersen <sven@sven-festersen.de>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
"""
This module contains the ChartObject class.

Author: Sven Festersen (sven@sven-festersen.de)
"""
import cairo
import gobject

class ChartObject(gobject.GObject):
    """
    This is the base class for all things that can be drawn on a chart
    widget.
    It emits the signal 'appearance-changed' when it needs to be
    redrawn.
    
    Properties
    ==========
    ChartObject inherits properties from gobject.GObject.
    Additional properties:
     - visible (sets whether the object should be visible,
       type: boolean)
     - antialias (sets whether the object should be antialiased,
       type: boolean).
       
    Signals
    =======
    ChartObject inherits signals from gobject.GObject,
    Additional signals:
     - appearance-changed (emitted if the object needs to be redrawn).
    """
    
    __gsignals__ = {"appearance-changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [])}

    
    __gproperties__ = {"visible": (gobject.TYPE_BOOLEAN,
                                    "visibilty of the object",
                                    "Set whether to draw the object or not.",
                                    True, gobject.PARAM_READWRITE),
                        "antialias": (gobject.TYPE_BOOLEAN,
                                    "use antialiasing",
                                    "Set whether to use antialiasing when drawing the object.",
                                    True, gobject.PARAM_READWRITE)}
    
    def __init__(self):
        gobject.GObject.__init__(self)
        self._show = True
        self._antialias = True
        
    def do_get_property(self, property):
        if property.name == "visible":
            return self._show
        elif property.name == "antialias":
            return self._antialias
        else:
            raise AttributeError, "Property %s does not exist." % property.name

    def do_set_property(self, property, value):
        if property.name == "visible":
            self._show = value
        elif property.name == "antialias":
            self._antialias = value
        else:
            raise AttributeError, "Property %s does not exist." % property.name
        
    def _do_draw(self, context, rect):
        """
        A derived class should override this method. The drawing stuff
        should happen here.
        
        @type context: cairo.Context
        @param context: The context to draw on.
        @type rect: gtk.gdk.Rectangle
        @param rect: A rectangle representing the charts area.
        """
        pass
        
    def draw(self, context, rect, *args):
        """
        This method is called by the parent Chart instance. It
        calls _do_draw.
        
        @type context: cairo.Context
        @param context: The context to draw on.
        @type rect: gtk.gdk.Rectangle
        @param rect: A rectangle representing the charts area.
        """
        res = None
        if self._show:
            if not self._antialias:
                context.set_antialias(cairo.ANTIALIAS_NONE)
            res = self._do_draw(context, rect, *args)
            context.set_antialias(cairo.ANTIALIAS_DEFAULT)
        return res
        
    def set_antialias(self, antialias):
        """
        This method sets the antialiasing mode of the ChartObject. Antialiasing
        is enabled by default.
        
        @type antialias: boolean
        @param antialias: If False, antialiasing is disabled for this 
        ChartObject.
        """
        self.set_property("antialias", antialias)
        self.emit("appearance_changed")
        
    def get_antialias(self):
        """
        Returns True if antialiasing is enabled for the object.
        
        @return: boolean.
        """
        return self.get_property("antialias")
        
    def set_visible(self, visible):
        """
        Use this method to set whether the ChartObject should be visible or
        not.
        
        @type visible: boolean
        @param visible: If False, the PlotObject won't be drawn.
        """
        self.set_property("visible", visible)
        self.emit("appearance_changed")
        
    def get_visible(self):
        """
        Returns True if the object is visble.
        
        @return: boolean.
        """
        return self.get_property("visible")
        

gobject.type_register(ChartObject)