CurrencyConvBinding
===================

* :download:`Download example <PyObjCExample-CurrencyConvBinding.zip>`

A rewrite of the :doc:`CurrencyConverter example </examples/Cocoa/AppKit/CurrencyConverter/index>` using Cocoa Bindings.

Originally from `Introduction to Developing Cocoa Applications Using Bindings`, converted to PyObjC by u.fiedler.

.. http://developer.apple.com/documentation/Cocoa/Conceptual/CurrencyConverterBindings/index.html

   NOTE: The link above is dead


.. rst-class:: tabber

Sources
-------

.. rst-class:: tabbertab

Converter.py
............

.. sourcecode:: python

    import objc
    from Foundation import NSObject
    from objc import super  # noqa: A004
    
    
    class Converter(NSObject):
        exchangeRate = objc.ivar.double()
        dollarsToConvert = objc.ivar.double()
    
        def init(self):
            self = super().init()
            self.exchangeRate = 3
            self.dollarsToConvert = 4
            return self
    
        def amountInOtherCurrency(self):
            return self.dollarsToConvert * self.exchangeRate
    
    
    Converter.setKeys_triggerChangeNotificationsForDependentKey_(
        ["dollarsToConvert", "exchangeRate"], "amountInOtherCurrency"
    )

.. rst-class:: tabbertab

CurrencyConvBinding.py
......................

.. sourcecode:: python

    # import classes required to start application
    import Converter  # noqa: F401
    import CurrencyConvBindingDocument  # noqa: F401
    from PyObjCTools import AppHelper
    
    if __name__ == "__main__":
        AppHelper.runEventLoop()

.. rst-class:: tabbertab

CurrencyConvBindingDocument.py
..............................

.. sourcecode:: python

    from Cocoa import NSDocument
    
    
    class CurrencyConvBindingDocument(NSDocument):
        def windowNibName(self):
            return "CurrencyConvBindingDocument"

.. rst-class:: tabbertab

setup.py
........

.. sourcecode:: python

    """
    Script for building the example:
    
    Usage:
        python3 setup.py py2app
    """
    
    from setuptools import setup
    
    plist = {
        "CFBundleDocumentTypes": [
            {
                "CFBundleTypeExtensions": ["CurrencyConvBinding", "*"],
                "CFBundleTypeName": "CurrencyConvBinding File",
                "CFBundleTypeRole": "Editor",
                "NSDocumentClass": "CurrencyConvBindingDocument",
            }
        ]
    }
    
    setup(
        name="CurrencyConvBinding",
        app=["CurrencyConvBinding.py"],
        data_files=["English.lproj"],
        options={"py2app": {"plist": plist}},
        setup_requires=["py2app", "pyobjc-framework-Cocoa"],
    )

