Ticket #1111: fix_angle_scaling.patch

File fix_angle_scaling.patch, 3.3 KB (added by garycmartin, 13 years ago)

Patch from Reinier, fixes both the deg/rad issue and a bug in the sci/exp button

  • functions.py

    diff --git a/functions.py b/functions.py
    index ddc1088..c777af3 100644
    a b def _d(val): 
    8686    else:
    8787        return None
    8888
    89 _angle_scaling = 1
     89class ClassValue():
     90    """
     91    Class to share a value with the outside world.
     92    This is required because plain floats / integers are not asigned by
     93    reference, and can therefore not easily be changed in a different place.
     94    """
     95    def __init__(self, val):
     96        self.value = val
     97
     98angle_scaling = ClassValue(1.0)
     99
    90100def _scale_angle(x):
    91     return x * _angle_scaling
     101    return x * angle_scaling.value
    92102
    93103def _inv_scale_angle(x):
    94     return x / _angle_scaling
     104    return x / angle_scaling.value
    95105
    96106def abs(x):
    97107    return math.fabs(x)
    def mod(x, y): 
    289299        return x % y
    290300    else:
    291301        raise ValueError(_('Can only calculate x modulo <integer>'))
     302mod.__doc__ = _(
     303'mod(x, y), return the modulus of x with respect to y. This is the remainder \
     304after dividing x by y.')
    292305
    293306def mul(x, y):
    294307    if isinstance(x, _Decimal) or isinstance(y, _Decimal):
  • mathlib.py

    diff --git a/mathlib.py b/mathlib.py
    index 610bb07..c1ae273 100644
    a b class MathLib: 
    4040    FORMAT_SCIENTIFIC = 2
    4141
    4242    def __init__(self):
    43         self.set_angle_type(self.ANGLE_DEG)
    4443        self.set_format_type(self.FORMAT_SCIENTIFIC)
    4544        self.set_digit_limit(9)
    4645
    class MathLib: 
    6968        if len(self.div_sym) == 0 or len(self.div_sym) > 3:
    7069            self.div_sym = '/'
    7170
    72     def set_angle_type(self, type):
    73         self.angle_scaling = self.d(type)
    74         _logger.debug('Angle type set to %s', self.angle_scaling)
    75 
    7671    def set_format_type(self, fmt, digit_limit=9):
    7772        self.format_type = fmt
    7873        _logger.debug('Format type set to %s', fmt)
  • toolbars.py

    diff --git a/toolbars.py b/toolbars.py
    index 69cc940..221dbd7 100644
    a b class MiscToolbar(gtk.Toolbar): 
    290290        target_toolbar.insert(IconToggleToolButton(el,
    291291                    lambda x: self.update_angle_type(x, calc),
    292292                    _('Degrees / radians')), -1)
     293        self.update_angle_type('deg', calc)
    293294
    294295        el = [
    295296            {'icon': 'format-sci', 'html': 'sci'},
    class MiscToolbar(gtk.Toolbar): 
    312313        self.show_all()
    313314
    314315    def update_angle_type(self, text, calc):
     316        var = calc.parser.get_var('angle_scaling')
     317        if var is None:
     318            _logger.warning('Variable angle_scaling not defined.')
     319            return
     320
    315321        if text == 'deg':
    316             calc.ml.set_angle_type(MathLib.ANGLE_DEG)
     322            var.value = MathLib.ANGLE_DEG
    317323        elif text == 'rad':
    318             calc.ml.set_angle_type(MathLib.ANGLE_RAD)
    319         _logger.debug('Angle type: %s', calc.ml.angle_scaling)
     324            var.value = MathLib.ANGLE_RAD
     325        _logger.debug('Angle scaling: %s', var.value)
    320326
    321327    def update_format_type(self, text, calc):
    322328        if text == 'exp':
    323329            calc.ml.set_format_type(MathLib.FORMAT_EXPONENT)
    324330        elif text == 'sci':
    325             calc.ml.set_angle_type(MathLib.FORMAT_SCIENTIFIC)
     331            calc.ml.set_format_type(MathLib.FORMAT_SCIENTIFIC)
    326332        _logger.debug('Format type: %s', calc.ml.format_type)
    327333
    328334    def update_digits(self, text, calc):