123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- """
- Module to easily group elements of a gui
- """
- from PyQt4 import QtGui
- import warnings
- from itertools import chain
- class GroupWarning(Warning):
- """
- General Warning Class for GroupedObjects
- """
- pass
- warnings.simplefilter('always', GroupWarning)
- class GroupedObjects:
- """
- This class enables grouping of objects to easily access them as groups throughout the gui.
- """
- def __init__(self):
- """
- Initialise this object
- :return:
- """
- self._objects = {}
- self._status = {}
- self.warn = False
- self.autoremove = False
- self.exception_on_deleted = False
- self.notify_deletion = False
- def setFlags(self, flagDict):
- """
- Set Flags that define the behaviour of GroupedObjects in various events.
- :param flagDict: Dictionary containing the Flags.
- Possible Flags are: (They are to be of type bool)
- * warn: If an element is deleted and the corresponding entry is encountered by setEnabled, a
- Warning is raised if warn is True
- * autoremove: If an element is deleted and the corresponding entry is encountered by setEnabled,
- the Element will be removed from GroupedObjects if autoremove is True
- * exception_on_deleted: If an element is deleted and the corresponding entry is encountered by
- setEnabled an Exception will be raised if exception_on_deleted is True
- * notify_deletion: If this is set to True a notification will be printed to STDOUT whenever an
- autoremove is performed (see above)
- :return: -
- """
- self.warn = flagDict.get('warn', False)
- self.autoremove = flagDict.get('autoremove', False)
- self.exception_on_deleted = flagDict.get('exception_on_deleted', False)
- self.notify_deletion = flagDict.get('notify_deletion', False)
- def createEmptyGroup(self, group):
- """
- Create an empty group
- :param group: (str) the name of the group
- :return: -
- """
- if group in self._objects:
- raise GroupWarning("Specified Group \""+group+"\" already in list")
- else:
- self._objects[group] = []
- def addItem(self, group, item):
- """
- Add a item or items to a group and thus register with the GroupedObjects object
- :param group: (list or str) List of groups or single group where the item is to be added
- :param item: (single item or list) What item/s
- :return: -
- """
- group = group if isinstance(group, list) else [group]
- item = item if isinstance(item, list) else [item]
- exclude = list(chain(*[self.getElements(i) for i in group]))
- for gr in group:
- if not (gr in self._status):
- self._status[gr] = True
- if gr in self._objects and isinstance(self._objects[gr], list):
- self._objects[gr].extend(item)
- else:
- self._objects[gr] = item
- if not self._status[gr]: # for the case when the status is set before the object is registered or the object is recreated
- self.setEnabled(gr, False, exclude=exclude)
- def setChecked(self, group, state):
- """
- Set the state of all the checkboxes in the group
- :param group: What group
- :param state: True for checked, False for unchecked
- :return: -
- """
- if group in self._objects:
- self._status[group] = state
- for obj in self._objects[group]:
- if isinstance(obj, QtGui.QCheckBox):
- obj.setChecked(state)
- else:
- warnings.warn("Specified Group \""+group+"\" not in list", GroupWarning, stacklevel=2)
- def setEnabled(self, group, state, exclude=None):
- """
- Set the state of all the items in the group
- :param group: What group
- :param state: True for enabled, False for disabled
- :param exclude: Exclude this item
- :return: -
- """
- to_remove = []
- if group in self._objects:
- self._status[group] = state
- if self._objects[group] == []: # untested if explicit test for empty list is necessary
- return
- for obj in self._objects[group]:
- try:
- if exclude and obj in exclude:
- continue
- if isinstance(obj, QtGui.QAction):
- obj.setEnabled(state)
- elif isinstance(obj, QtGui.QMenu):
- obj.menuAction().setVisible(state)
- else:
- obj.setEnabled(state)
- except RuntimeError as e:
- if "deleted" in str(e):
- # self.removeItem(group, obj)
- if self.autoremove:
- to_remove.append([group, obj])
- if self.warn:
- warnings.warn(str(e), GroupWarning, stacklevel=3)
- if self.exception_on_deleted:
- raise e
- else:
- raise e
- if to_remove and self.autoremove:
- for rm in to_remove:
- if self.notify_deletion:
- print("Autoremoving element from group '" + rm[0] + "'")
- self.removeItem(*rm)
- else:
- self._status[group] = state
- warnings.warn("Specified Group \""+group+"\" not in Elements", GroupWarning, stacklevel=2)
- def addMenuItem(self, group, item):
- """
- Deprecated. Use addItem.
- """
- self.addItem(group, item)
- def addButton(self, group, item):
- """
- Deprecated. Use addItem.
- """
- self.addItem(group, item)
- def addCheckbox(self, group, item):
- """
- Deprecated. Use addItem.
- """
- self.addItem(group, item)
- def removeItem(self, group, item):
- """
- Remove an element from a group
- :param group: (list or str) list of groups or groupname. if set to none: remove from all groups
- :param item: (list or item) list of items or item to remove
- :return: -
- """
- if group is None:
- group = list(self._objects.keys())
- groups = group if isinstance(group, list) else [group]
- items = item if isinstance(item, list) else [item]
- for gr in groups:
- #print(gr)
- if not gr in self._objects:
- # return
- continue
- for it in items:
- if it in self._objects[gr]:
- del self._objects[gr][self._objects[gr].index(it)]
- def removeGroup(self, group):
- """
- Remove a group from GroupedObjects. If the group is not registered, a warning will be raised.
- :param group: (str) the group to remove
- :return:
- """
- if group in self._objects:
- del self._objects[group]
- else:
- warnings.warn("Group was not registered - Nothing removed", GroupWarning, stacklevel=2)
- def emptyGroup(self, group):
- """
- Unregister all elements from group. (This will delete the group and recreate it.
- :param group: (str) the group to clean out.
- :return: -
- """
- self.removeGroup(group)
- self.createEmptyGroup(group)
- def getElements(self, group):
- """
- Get the elements of a group as list.
- :param group: (str) the gorup you want the elements of
- :return: (list) Elements in group
- """
- if group in self._objects:
- return self._objects[group]
- else:
- return []
- def isEnabled(self, group):
- """
- Check if a group is enabled.
- :param group: (str) the gorup to check.
- :return: (bool) State of group
- """
- if group in self._status:
- return self._status[group]
- else:
- return True # Default for elements is enabled
- class LivePlotWindows():
- """
- Container class to hold open LivePlotWindows.
- Added LivePlotWindows will automatically be plotted to on the event of new data.
- """
- def __init__(self):
- self.plotWindows = {}
- def addWindow(self, board_id, window):
- """
- Register a Window.
- :param window: (PlotWidget) The window to be added.
- :return: -
- """
- if board_id in self.plotWindows:
- self.plotWindows[board_id].append(window)
- else:
- self.plotWindows[board_id] = [window, ]
- def getWindows(self, board_id):
- """
- Get the list of registered plot windows.
- :return: (list) List of plotWindows
- """
- return self.plotWindows[board_id]
- def hasWindows(self, board_id):
- """
- Check if Windows are registered.
- :return: (bool) True if there are windows and False if not.
- """
- if board_id in self.plotWindows and len(self.plotWindows[board_id]) > 0:
- return True
- else:
- return False
- def removeWindow(self, board_id, window):
- """
- Remove a window from open plot windows
- :param window: the window to remove
- :return:
- """
- del self.plotWindows[board_id][self.plotWindows[board_id].index(window)]
- # Every Element is acessible through every Variable set here. Checkboxes, Buttons and MenuItems variables are set
- # to improve readability of the code. They refer to the same Object
- Elements = Checkboxes = Buttons = MenuItems = GroupedObjects()
- live_plot_windows = LivePlotWindows()
- cuda_windows = None
|