FlatCAMCommon.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. class LoudDict(dict):
  2. """
  3. A Dictionary with a callback for
  4. item changes.
  5. """
  6. def __init__(self, *args, **kwargs):
  7. dict.__init__(self, *args, **kwargs)
  8. self.callback = lambda x: None
  9. def __setitem__(self, key, value):
  10. """
  11. Overridden __setitem__ method. Will emit 'changed(QString)'
  12. if the item was changed, with key as parameter.
  13. """
  14. if key in self and self.__getitem__(key) == value:
  15. return
  16. dict.__setitem__(self, key, value)
  17. self.callback(key)
  18. def update(self, *args, **kwargs):
  19. if len(args) > 1:
  20. raise TypeError("update expected at most 1 arguments, got %d" % len(args))
  21. other = dict(*args, **kwargs)
  22. for key in other:
  23. self[key] = other[key]
  24. def set_change_callback(self, callback):
  25. """
  26. Assigns a function as callback on item change. The callback
  27. will receive the key of the object that was changed.
  28. :param callback: Function to call on item change.
  29. :type callback: func
  30. :return: None
  31. """
  32. self.callback = callback