# -*- coding: utf-8 -*- #Copyright (c) 2009, 2010 Walter Bender # 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 3 of the License, or # (at your option) any later version. # # You should have received a copy of the GNU General Public License # along with this library; if not, write to the Free Software # Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA """ Modifying slide rule: The customization feature is intended to handle most cases where you require a specialized slide or stator. But if you would like to add a new slide to the toolbar, you need to make changes in three places: 1. In SlideruleActivity.py, you need to add new entries to the arrays that define the toolbars. 2. In genslides.py (this file), you need to add new class objects to generate the graphics associated with your slide and stator. In most cases, you can simply inherit from the C_slide and C_stator classes. The make_slide() method iterates across the domain of the offset_function(). NOTE that most of the make_slide() methods ignore min, max, and step. 3. In window.py, you need to add methods to calculate values for your slide and stator. """ from math import * import traceback from gettext import gettext as _ from constants import SWIDTH, SHEIGHT, OFFSET, SCALE, HTOP1, HTOP2, HTOP3 class C_slide_generator(): """ Log scale for slide """ def __init__(self, name, offset_text, label_text, min, max, step): self.name = name self.offset_text = offset_text.replace('import','') self.label_text = label_text.replace('import','') self.min = min self.max = max self.step = step self.error_msg = None self.precision = 1 self.setup_svg() def offset_function(x): my_offset = "def f(x): return " + self.offset_text userdefined = {} exec my_offset in globals(), userdefined return userdefined.values()[0](x) def label_function(x): my_label = "def f(x): return " + self.label_text userdefined = {} exec my_label in globals(), userdefined return userdefined.values()[0](x) self.svg = self.make_slide(self.name, offset_function, label_function, self.min, self.max, self.step) def setup_svg(self): self.setup_slide() def setup_slide(self): self.slide1 = HTOP1 self.slide2 = HTOP2 self.slide3 = HTOP3 self.slide_offset1 = 5 self.slide_offset2 = 7 self.slide_offset3 = -12 def setup_stator(self): self.slide1 = SHEIGHT - HTOP1 self.slide2 = SHEIGHT - HTOP2 self.slide3 = SHEIGHT - HTOP3 + 12 self.slide_offset1 = - 5 self.slide_offset2 = - 7 self.slide_offset3 = 12 def mark(self, offset, height3, height2, height1, value=None): """ Plot marks in a range from 1 to 10 along the length of the slide """ svg = '' log = offset * SCALE + OFFSET if value is not None: if type(value) == str: string = value else: format = '%0.' + str(self.precision) + 'f' string = format % (value) svg += ' \n' svg += ' max: i = max max = min min = i else: i = min while i < max + step: try: svg += self.mark(offset_function(i), self.slide3, self.slide2, self.slide1, label_function(i)) except OverflowError, e: self.error_msg = _('Overflow Error') + ': ' + str(e) except NameError, e: self.error_msg = _('Name Error') + ': ' + str(e) except ZeroDivisionError, e: self.error_msg = _('Can not divide by zero') + ' ' + str(e) except TypeError, e: self.error_msg = _('Type Error') + ': ' + str(e) except ValueError, e: self.error_msg = _('Value Error') + ': ' + str(e) except SyntaxError, e: self.error_msg = _('Syntax Error') + ': ' + str(e) except: traceback.print_exc() i += step svg += self.footer() return svg class Custom_stator_generator(Custom_slide_generator): """ user-defined scale for slide """ def setup_svg(self): self.precision = 2 self.setup_stator() def main(): """ Log scale for slide and stator """ print C_slide().svg return 0 if __name__ == "__main__": main()