# Copyright 2008 by Peter Moxhay and Wade Brainerd. # This file is part of Math. # # Math 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 3 of the License, or # (at your option) any later version. # # Math 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 Math. If not, see . from objectarea import Object from vector import Vector import gtk, rsvg CHECK_SVG = rsvg.Handle('check.svg') class AnswerObject(Object): """Rectangular area surrounding a correct answer.""" def __init__(self, pos, size, label): Object.__init__(self) self.pos = pos self.size = size self.label = label self.draggable = False self.selectable = False self.answer = 'equal' self.show_hint = False def draw(self, cr): cr.rectangle(self.pos.x, self.pos.y, self.size.x, self.size.y) cr.set_source_rgb(0.9, 0.9, 0.9) cr.fill_preserve() cr.set_line_width(2.0) cr.set_source_rgb(0, 0, 0) cr.stroke() cr.save() cr.translate(self.pos.x + self.size.x - 220, self.pos.y + self.size.y - 220) cr.scale(0.4, 0.4) CHECK_SVG.render_cairo(cr) cr.restore() if self.show_hint: cr.save() if self.container.problem_type =='amount': cr.translate(-500, 250) line_width = 30.0 cr.set_line_width(line_width) cr.set_source_rgb(0.5, 0.5, 0.5) if self.answer == 'equal': shift = 100 left_line_segment_top = Vector(850 + 50 - shift, 400 - 162 - 12 - 4) left_line_segment_bottom = Vector(850 + 50 - shift, 400 - 12 - 4) right_line_segment_top = Vector(1050 - 50 + shift, 400 - 162 - 12 - 4) right_line_segment_bottom = Vector(1050 - 50 + shift, 400 - 12 - 4) cr.move_to(left_line_segment_bottom.x, left_line_segment_bottom.y) cr.line_to(right_line_segment_bottom.x, left_line_segment_bottom.y) cr.stroke() cr.move_to(left_line_segment_bottom.x, left_line_segment_top.y) cr.line_to(right_line_segment_bottom.x, left_line_segment_top.y) cr.stroke() elif self.answer == 'less': shift = -20 left_line_segment_top = Vector(850 + 50, 400 - 100 - 12 - 4 - shift) left_line_segment_bottom = Vector(850 + 50, 400 - 12 - 4) right_line_segment_top = Vector(1050 - 50, 400 - 162 - 12 - 4 + shift) right_line_segment_bottom = Vector(1050 - 50, 400 - 12 - 4) x3 = left_line_segment_top.x y3 = left_line_segment_top.y x4 = right_line_segment_top.x y4 = right_line_segment_top.y y1 = left_line_segment_bottom.y x_max = 1100 m = (float(y4) - float(y3))/(float(x4) - float(x3)) cr.set_line_width(line_width) cr.set_source_rgb(0.5, 0.5, 0.5) cr.move_to(x_max, y1) cr.line_to(x3 + (y1 - y3)/m, y1) cr.line_to(x_max - 60, y3 + m * (x_max - 60 - x3)) cr.stroke() elif self.answer == 'greater': shift = -20 left_line_segment_bottom = Vector(850 + 50, 400 - 12 - 4) left_line_segment_top = Vector(850 + 50, 400 - 162 - 12 - 4 + shift) right_line_segment_bottom = Vector(1050 - 50, 400 - 12 - 4) right_line_segment_top = Vector(1050 - 50, 400 - 100 - 12 - 4 - shift) x3 = left_line_segment_top.x y3 = left_line_segment_top.y x4 = right_line_segment_top.x y4 = right_line_segment_top.y y1 = left_line_segment_bottom.y x_min = 800 m = (float(y4) - float(y3))/(float(x4) - float(x3)) cr.set_line_width(line_width) cr.set_source_rgb(0.5, 0.5, 0.5) cr.move_to(x_min, y1) cr.line_to(x3 + (y1 - y3)/m, y1) cr.line_to(x_min + 60, y3 + m * (x_min + 60 - x3)) cr.stroke() elif self.answer == 'equals_small': shift = 50 left_line_segment_top = Vector(850 + 50 - shift, 400 - 100 - 12 - 4) left_line_segment_bottom = Vector(850 + 50 - shift, 400 - 12 - 4) right_line_segment_top = Vector(1050 - 50 + shift, 400 - 100 - 12 - 4) right_line_segment_bottom = Vector(1050 - 50 + shift, 400 - 12 - 4) cr.move_to(left_line_segment_bottom.x, left_line_segment_bottom.y) cr.line_to(right_line_segment_bottom.x, left_line_segment_bottom.y) cr.stroke() cr.move_to(left_line_segment_bottom.x, left_line_segment_top.y) cr.line_to(right_line_segment_bottom.x, left_line_segment_top.y) cr.stroke() cr.restore() def get_bounds(self): return self.pos, self.pos + self.size