diff options
author | Adam Schreiber <sadam@gnome.org> | 2009-04-22 17:27:20 (GMT) |
---|---|---|
committer | Adam Schreiber <sadam@gnome.org> | 2009-04-22 17:27:20 (GMT) |
commit | 8dcea96c37ff4008b2b5a1a57eb137a4c267d1db (patch) | |
tree | a9dcbe2f71f5d963bc36f9c2cb98de658bbb20e5 | |
parent | 1b03eebf34cd8ab1083a761a749d24c0b1fc45bc (diff) |
Created animated frac box
-rw-r--r-- | deci.py | 131 |
1 files changed, 105 insertions, 26 deletions
@@ -20,6 +20,89 @@ import gtk import math +class DrawFractionalArea(gtk.DrawingArea): + def __init__(self): + gtk.DrawingArea.__init__(self) + self.connect_after("expose_event", self.expose) + self.ones = 0 + self.tenths = 0 + self.hundredths = 0 + + def expose(self, widget, event): + #print widget.window + self.context = widget.window.cairo_create() + #print self.context + # set a clip region for the expose event + self.context.rectangle(event.area.x, event.area.y, + event.area.width, event.area.height) + self.context.clip() + + self.draw(self.context) + + return False + + def draw (self, context): + rect = self.get_allocation() + + width = min(rect.width, rect.height) - 10 + + x = int(rect.x + round((rect.width - width)/2)) + y = int(rect.y + round((rect.height - width)/2)) + + context.rectangle(gtk.gdk.Rectangle(x, + y, + width, + width)) + + context.set_source_rgba(1.0, 0, 0, 0.25) + context.fill_preserve() + context.set_source_rgb(1, 0, 0) + context.stroke() + + if (self.ones != 0): + context.rectangle(gtk.gdk.Rectangle(x + 10, + y + 10, + width - 20, + width - 20)) + + context.set_source_rgba(0, 0, 1.0, 1) + context.fill_preserve() + + else: + tenthswidth = int(width/10) + hundredthswidth = int(width/100) + + for m in range(1, self.tenths + 1): + context.rectangle(gtk.gdk.Rectangle(x + 10, + y + width - m * tenthswidth + 5, + width - 20, + tenthswidth - 10)) + + context.set_source_rgba(0, 0, 1.0, 1) + context.fill_preserve() + + for m in range(1, self.hundredths + 1): + context.rectangle(gtk.gdk.Rectangle(x + 10, + y + width - self.tenths * tenthswidth - m * hundredthswidth + 1 - 5, + width - 20, + hundredthswidth - 1)) + + context.set_source_rgba(0, 0, 1.0, 1) + context.fill_preserve() + + def set_value (self, value): + val = round(value, 2) + + valt = val*100 + self.ones = int(math.floor(math.fmod(valt,math.pow(10,3))/math.pow(10,2))) + self.tenths = int(math.floor(math.fmod(valt,math.pow(10,2))/math.pow(10,1))) + self.hundredths = int(math.floor(math.fmod(valt,math.pow(10,1)))) + + self.queue_draw () + + def get_value (): + return (self.ones + self.tenths * 10**-1 + self.hundredths * 10**-2) + def main(): window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.connect("delete_event", delete_event) @@ -36,31 +119,33 @@ def main(): spinbut = gtk.SpinButton() spinbut.set_range(0.01,1) spinbut.set_increments(0.01,0.1) + spinbut.set_digits(2) spinbut.set_numeric(True) hbox.pack_start(spinbut, True, False) vbox.pack_start(hbox, False) # Scrollbar hscrollbar = gtk.HScrollbar() - hscrollbar.set_range(0,999999) - hscrollbar.set_increments(1,100) + hscrollbar.set_range(0.01,1) + hscrollbar.set_increments(0.01,0.1) + hscrollbar.set_value(0.01) vbox.pack_start(hscrollbar, False) # Equivalent Label - equivlab = gtk.Label("0 * 100000 + 0 * 10000 + 0 * 1000 + 0 * 100 + 0 * 10 + 0 * 1 = 0") + equivlab = gtk.Label("0 * 1 + 0 * 0.1 + 1 * 0.01 = 0.01") vbox.pack_start(equivlab, False) # Power of 10 Equivalent Label - powequivlab = gtk.Label("0 * 10<sup>5</sup> + 0 * 10<sup>4</sup> + 0 * 10<sup>3</sup> + 0 * 10<sup>2</sup> + 0 * 10<sup>1</sup> + 0 * 10<sup>0</sup> = 0") + powequivlab = gtk.Label("0 * 10<sup>0</sup> + 0 * 10<sup>-1</sup> + 1 * 10<sup>-2</sup> = 0.01") powequivlab.set_use_markup(True) vbox.pack_start(powequivlab, False) # Start Figure Box - drawshapesarea = DrawShapesArea() - vbox.add(drawshapesarea) + fractbox = DrawFractionalArea() + vbox.pack_start(fractbox) - spinbut.connect("value-changed", scrollbar_changed, [spinbut, hscrollbar, equivlab, powequivlab, drawshapesarea]) - hscrollbar.connect("value-changed", scrollbar_changed, [spinbut, hscrollbar, equivlab, powequivlab, drawshapesarea]) + spinbut.connect("value-changed", scrollbar_changed, [spinbut, hscrollbar, equivlab, powequivlab, fractbox]) + hscrollbar.connect("value-changed", scrollbar_changed, [spinbut, hscrollbar, equivlab, powequivlab, fractbox]) window.show_all() gtk.main() @@ -70,29 +155,23 @@ def scrollbar_changed(range, data=None): hscrollbar = data[1] equivlab = data[2] powequivlab = data[3] - drawshapesarea = data[4] - - val = round(range.get_value()) - if abs(val) < abs(1e-2): - val = 0 + fractbox = data[4] - val = int(val) + val = round(range.get_value(),2) - val5 = int(math.floor(val/math.pow(10,5))) - val4 = int(math.floor(math.fmod(val,math.pow(10,5))/math.pow(10,4))) - val3 = int(math.floor(math.fmod(val,math.pow(10,4))/math.pow(10,3))) - val2 = int(math.floor(math.fmod(val,math.pow(10,3))/math.pow(10,2))) - val1 = int(math.floor(math.fmod(val,math.pow(10,2))/math.pow(10,1))) - val0 = int(math.floor(math.fmod(val,math.pow(10,1)))) + valt = val*100 + val2 = int(math.floor(math.fmod(valt,math.pow(10,3))/math.pow(10,2))) + val1 = int(math.floor(math.fmod(valt,math.pow(10,2))/math.pow(10,1))) + val0 = int(math.floor(math.fmod(valt,math.pow(10,1)))) - spinbut.set_value(int(val)) - hscrollbar.set_value(int(val)) + spinbut.set_value(val) + hscrollbar.set_value(val) - equivlab.set_text("{0} * 100000 + {1} * 10000 + {2} * 1000 + {3} * 100 + {4} * 10 + {5} * 1 = {6}".format(val5, val4, val3, val2, val1, val0, val)) + equivlab.set_text("{0} * 1 + {1} * 0.1 + {2} * 0.01 = {3}".format(val2, val1, val0, val)) - powequivlab.set_markup("{0} * 10<sup>5</sup> + {1} * 10<sup>4</sup> + {2} * 10<sup>3</sup> + {3} * 10<sup>2</sup> + {4} * 10<sup>1</sup> + {5} * 10<sup>0</sup> = {6}".format(val5, val4, val3, val2, val1, val0, val)) - - drawshapesarea.set_count (val) + powequivlab.set_markup("{0} * 10<sup>0</sup> + {1} * 10<sup>-1</sup> + {2} * 10<sup>-2</sup> = {3}".format(val2, val1, val0, val)) + + fractbox.set_value(val) def delete_event(widget, event, data=None): return False |