# -*- 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 Lesser General Public # License along with this library; if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, 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 and simply override offset_function() and label_function(). The mark() and special_mark() methods are used to generate the marks along the rule as well as the text labels. The make_slide() method iterates across the domain of the offset_function(). 3. In window.py, you need to add methods to calculate values for your slide and stator. """ import math import traceback from gettext import gettext as _ from constants import SWIDTH, SHEIGHT, OFFSET, SCALE, HTOP1, HTOP2, HTOP3 log10 = 1 # math.log(10, 10) class C_slide(): """ Log scale for slide """ def __init__(self): self.name = 'C' self.slide1 = HTOP1 self.slide2 = HTOP2 self.slide3 = HTOP3 self.slide_offset1 = 5 self.slide_offset2 = 7 self.slide_offset3 = -12 def offset_function(x): return math.log(x, 10) def label_function(x): return x self.svg = self.make_slide(self.name, offset_function, label_function) def mark(self, offset, height3, height2, height1, string=None, flip=False, scale=1.0): """ Plot marks in a range from 1 to 10 along the length of the slide """ svg = '' if flip: log = (log10 - offset) * SCALE * scale + OFFSET else: log = offset * SCALE * scale + OFFSET if string is not None: 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 = _('Zero Division Error') + ' ' + 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(Custom_slide): """ user-defined scale for slide """ def __init__(self, offset_function, label_function, min, max, step): self.name = '' 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 self.error_msg = None self.svg = self.make_slide(self.name, offset_function, label_function, min, max, step) def main(): """ Log scale for slide and stator """ print C_slide().svg return 0 if __name__ == "__main__": main()