def show_shortcuts_dialog(self): # The dialog is shown in non-modal form, and consequently must exists # even after this method returns. So we bind the dialog to the # instance to keep pythons GC out of business and manually delete the # dialog once the user closed it self.shortcuts_dialog = KShortcutsDialog( KShortcutsEditor.GlobalAction, KShortcutsEditor.LetterShortcutsDisallowed) self.shortcuts_dialog.addCollection(self.actionCollection()) # delete the dialog manually once the user closed it, to avoid some # mysterious crashes when quitting the application self.shortcuts_dialog.finished.connect( self.shortcuts_dialog.deleteLater) self.shortcuts_dialog.configure()
class SynaptiksNotifierItem(KStatusNotifierItem): def __init__(self, parent=None): KStatusNotifierItem.__init__(self, parent) self.setTitle('synaptiks') self.setIconByName('synaptiks') self.setCategory(KStatusNotifierItem.Hardware) self.setStatus(KStatusNotifierItem.Passive) self.setup_actions() self._config = SynaptiksTrayConfiguration(self) try: self.touchpad = Touchpad.find_first(Display.from_qt()) except Exception as error: # show an error message from synaptiks.kde.error import get_localized_error_message error_message = get_localized_error_message(error) options = KMessageBox.Options(KMessageBox.Notify | KMessageBox.AllowLink) KMessageBox.error(None, error_message, '', options) # disable all touchpad related actions for act in (self.touchpad_on_action, self.preferences_action): act.setEnabled(False) # disable synaptiks autostart, the user can still start synaptiks # manually again, if the reason of the error is fixed self._config.findItem('Autostart').setProperty(False) self._config.writeConfig() else: self.activateRequested.connect(self.show_configuration_dialog) # setup the touchpad manager self.setup_manager(self.touchpad) def setup_actions(self): self.touchpad_on_action = KToggleAction( i18nc('@action:inmenu', 'Touchpad on'), self.actionCollection()) self.actionCollection().addAction( 'touchpadOn', self.touchpad_on_action) self.touchpad_on_action.setGlobalShortcut( KShortcut(i18nc('Touchpad toggle shortcut', 'Ctrl+Alt+T'))) self.contextMenu().addAction(self.touchpad_on_action) self.contextMenu().addSeparator() shortcuts = self.actionCollection().addAction( KStandardAction.KeyBindings, 'shortcuts') shortcuts.triggered.connect(self.show_shortcuts_dialog) self.contextMenu().addAction(shortcuts) self.preferences_action = self.actionCollection().addAction( KStandardAction.Preferences, 'preferences') self.preferences_action.triggered.connect( self.show_configuration_dialog) self.contextMenu().addAction(self.preferences_action) help_menu = KHelpMenu(self.contextMenu(), KCmdLineArgs.aboutData()) self.contextMenu().addMenu(help_menu.menu()) def setup_manager(self, touchpad): self.touchpad_manager = TouchpadManager(touchpad, self) ManagerConfiguration.load(self.touchpad_manager) # transition upon touchpad_on_action self.touchpad_manager.add_touchpad_switch_action( self.touchpad_on_action) # update checked state of touchpad_on_action self.touchpad_manager.states['on'].assignProperty( self.touchpad_on_action, 'checked', True) self.touchpad_manager.states['off'].assignProperty( self.touchpad_on_action, 'checked', False) # update the overlay icon self.touchpad_manager.states['on'].entered.connect( partial(self.setOverlayIconByName, 'touchpad-off')) self.touchpad_manager.states['on'].exited.connect( partial(self.setOverlayIconByName, '')) # only show notification if the touchpad is permanently switched # off self.touchpad_manager.states['off'].entered.connect( partial(self.notify_touchpad_state, True)) self.touchpad_manager.states['off'].exited.connect( partial(self.notify_touchpad_state, False)) # and eventually start managing the touchpad self.touchpad_manager.start() def notify_touchpad_state(self, is_off=None): if is_off is None: is_off = self.touchpad.off # show a notification if is_off: event_id = 'touchpadOff' text = i18nc('touchpad switched notification', 'Touchpad switched off') else: event_id = 'touchpadOn' text = i18nc('touchpad switched notification', 'Touchpad switched on') icon = KIconLoader.global_().loadIcon('synaptiks', KIconLoader.Panel) KNotification.event(event_id, text, icon) def show_shortcuts_dialog(self): # The dialog is shown in non-modal form, and consequently must exists # even after this method returns. So we bind the dialog to the # instance to keep pythons GC out of business and manually delete the # dialog once the user closed it self.shortcuts_dialog = KShortcutsDialog( KShortcutsEditor.GlobalAction, KShortcutsEditor.LetterShortcutsDisallowed) self.shortcuts_dialog.addCollection(self.actionCollection()) # delete the dialog manually once the user closed it, to avoid some # mysterious crashes when quitting the application self.shortcuts_dialog.finished.connect( self.shortcuts_dialog.deleteLater) self.shortcuts_dialog.configure() def show_configuration_dialog(self): self.config_dialog = SynaptiksConfigDialog( self.touchpad, self.touchpad_manager, self._config) self.config_dialog.finished.connect(self.config_dialog.deleteLater) self.config_dialog.show()