def __init__(self, parent, tool_bar, image_cache, item, controller, show_labels): """ Creates a new tool bar tool for an action item. """ self.item = item self.tool_bar = tool_bar action = item.action if action.style == 'widget': widget = action.create_control(tool_bar) self.control = tool_bar.addWidget(widget) elif action.image is None: self.control = tool_bar.addAction(action.name) else: size = tool_bar.iconSize() image = action.image.create_icon((size.width(), size.height())) self.control = tool_bar.addAction(image, action.name) self.control.triggered.connect(self._qt4_on_triggered) self.control.setToolTip(action.tooltip) self.control.setWhatsThis(action.description) self.control.setEnabled(action.enabled) self.control.setVisible(action.visible) if action.style == 'toggle': self.control.setCheckable(True) self.control.setChecked(action.checked) elif action.style == 'radio': # Create an action group if it hasn't already been done. try: ag = item.parent._qt4_ag except AttributeError: ag = item.parent._qt4_ag = QtGui.QActionGroup(parent) self.control.setActionGroup(ag) self.control.setCheckable(True) self.control.setChecked(action.checked) # Keep a reference in the action. This is done to make sure we live as # long as the action (and still respond to its signals) and don't die # if the manager that created us is garbage collected. self.control._tool_instance = self # Listen for trait changes on the action (so that we can update its # enabled/disabled/checked state etc). action.on_trait_change(self._on_action_enabled_changed, 'enabled') action.on_trait_change(self._on_action_visible_changed, 'visible') action.on_trait_change(self._on_action_checked_changed, 'checked') action.on_trait_change(self._on_action_name_changed, 'name') action.on_trait_change(self._on_action_accelerator_changed, 'accelerator') # Detect if the control is destroyed. self.control.destroyed.connect(self._qt4_on_destroyed) if controller is not None: self.controller = controller controller.add_to_toolbar(self)
def __init__(self, parent, menu, item, controller): """ Creates a new menu item for an action item. """ self.item = item action = item.action # FIXME v3: This is a wx'ism and should be hidden in the toolkit code. self.control_id = None if action.image is None: self.control = menu.addAction(action.name, self._qt4_on_triggered, action.accelerator) else: self.control = menu.addAction(action.image.create_icon(), action.name, self._qt4_on_triggered, action.accelerator) menu.menu_items.append(self) self.control.setToolTip(action.tooltip) self.control.setWhatsThis(action.description) self.control.setEnabled(action.enabled) self.control.setVisible(action.visible) if getattr(action, 'menu_role', False): if action.menu_role == "About": self.control.setMenuRole(QtGui.QAction.AboutRole) elif action.menu_role == "Preferences": self.control.setMenuRole(QtGui.QAction.PreferencesRole) if action.style == 'toggle': self.control.setCheckable(True) self.control.setChecked(action.checked) elif action.style == 'radio': # Create an action group if it hasn't already been done. try: ag = item.parent._qt4_ag except AttributeError: ag = item.parent._qt4_ag = QtGui.QActionGroup(parent) self.control.setActionGroup(ag) self.control.setCheckable(True) self.control.setChecked(action.checked) # Listen for trait changes on the action (so that we can update its # enabled/disabled/checked state etc). action.on_trait_change(self._on_action_enabled_changed, 'enabled') action.on_trait_change(self._on_action_visible_changed, 'visible') action.on_trait_change(self._on_action_checked_changed, 'checked') action.on_trait_change(self._on_action_name_changed, 'name') action.on_trait_change(self._on_action_accelerator_changed, 'accelerator') # Detect if the control is destroyed. self.control.destroyed.connect(self._qt4_on_destroyed) if controller is not None: self.controller = controller controller.add_to_menu(self)