Пример #1
0
class ShortcutEditor(QDialog):
    def __init__(self, load=False, parent=None, buttons=False):
        super(ShortcutEditor, self).__init__(parent)
        self._names = []
        self._hotkeys = []

        self._listbox = ListBox()

        listbuttons = ListButtons()
        listbuttons.insertStretch(0)
        self.connect(listbuttons, SIGNAL('add'), self._addShortcut)
        self.connect(listbuttons, SIGNAL('edit'), self._editShortcut)
        self.connect(listbuttons, SIGNAL('duplicate'), self._duplicate)
        self.connect(self._listbox,
                     SIGNAL('itemDoubleClicked (QListWidgetItem *)'),
                     self._editShortcut)

        self._listbox.connectToListButtons(listbuttons)

        hbox = QHBoxLayout()
        hbox.addLayout(create_buddy('Shortcuts', self._listbox, QVBoxLayout()))
        hbox.addLayout(listbuttons)

        okcancel = OKCancel()

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        if buttons:
            vbox.addLayout(okcancel)
        self.setLayout(vbox)

        self.connect(okcancel, SIGNAL('ok'), self.okClicked)
        self.connect(okcancel, SIGNAL('cancel'), self.close)

        if load:
            self.loadSettings()

    def _addShortcut(self):
        shortcuts = get_shortcuts().difference(self._hotkeys).union(
            i.shortcut for i in self._listbox.items() if i.shortcut)

        win = Editor('Add Shortcut', u'', self._actions, self.names(),
                     shortcuts, self)
        win.setModal(True)
        self.connect(win, SIGNAL('actionChanged'), self.addShortcut)
        win.show()

    def addShortcut(self, name, filenames, shortcut=u'', select=True):
        item = QListWidgetItem(name)
        item.actionName = name
        item.filenames = filenames[::]
        item.shortcut = shortcut
        self._listbox.addItem(item)
        if select:
            self._listbox.setCurrentItem(item,
                                         QItemSelectionModel.ClearAndSelect)

    def applySettings(self, control=None):
        from puddlestuff.puddletag import remove_shortcuts, add_shortcuts
        remove_shortcuts('&Actions', self._names)

        f = open(FILENAME, 'w')
        f.close()

        cparser = PuddleConfig(FILENAME)
        for i, item in enumerate(self._listbox.items()):
            section = SHORTCUT_SECTION + unicode(i)
            cparser.set(section, NAME, item.actionName)
            cparser.set(section, FILENAMES, item.filenames)

        from puddlestuff.mainwin.funcs import applyaction

        shortcuts = create_action_shortcuts(applyaction, control)
        for item, shortcut in zip(self._listbox.items(), shortcuts):
            if item.shortcut:
                shortcut.setShortcut(item.shortcut)
        add_shortcuts('&Actions', shortcuts, save=True)

    def okClicked(self):
        self.applySettings()
        self.close()

    def _duplicate(self):
        try:
            item = self._listbox.selectedItems()[0]
        except IndexError:
            return
        shortcuts = get_shortcuts().difference(self._hotkeys).union(
            i.shortcut for i in self._listbox.items() if i.shortcut)
        win = Editor('Duplicate Shortcut', u'', self._actions, self.names(),
                     shortcuts, self)
        win.setAttrs(item.actionName, self._actions, item.filenames, u'')
        win.setModal(True)
        self.connect(win, SIGNAL('actionChanged'), self.addShortcut)
        win.show()

    def _editShortcut(self):
        try:
            item = self._listbox.selectedItems()[0]
        except IndexError:
            return
        shortcuts = get_shortcuts().difference(self._hotkeys).union(
            i.shortcut for i in self._listbox.items() if i.shortcut)

        names = self.names()
        names.remove(item.actionName)
        win = Editor('Edit Shortcut', item.shortcut, self._actions, names,
                     shortcuts, self)
        win.setAttrs(item.actionName, self._actions, item.filenames,
                     item.shortcut)
        win.setModal(True)
        self.connect(win, SIGNAL('actionChanged'),
                     partial(self.editShortcut, item))
        win.show()

    def editShortcut(self, item, name, filenames, shortcut):
        item.actionName = name
        item.filenames = filenames[::]
        item.shortcut = shortcut
        item.setText(name)

    def loadSettings(self, filename=None, actions=None):
        self._names = []
        self._hotkeys = []

        if filename is None:
            filename = os.path.join(ACTIONDIR, 'action_shortcuts')

        self._listbox.clear()
        cparser = PuddleConfig(filename)

        if actions is None:
            self._actions = load_actions()
        else:
            self._actions = actions

        from puddlestuff.puddletag import status
        if status['actions']:
            shortcuts = dict(
                (unicode(a.text()), unicode(a.shortcut().toString()))
                for a in status['actions'])
        else:
            shortcuts = {}

        for section in sorted(cparser.sections()):
            if section.startswith('Shortcut'):
                name = cparser.get(section, NAME, 'Default')
                self._names.append(name)
                filenames = cparser.get(section, FILENAMES, [])
                shortcut = shortcuts.get(name, u'')
                self.addShortcut(name, filenames, shortcut, select=False)
                self._hotkeys.append(shortcut)

    def names(self):
        return [item.actionName for item in self._listbox.items()]
Пример #2
0
class ShortcutEditor(QDialog):
    def __init__(self, load=False, parent=None, buttons=False):
        super(ShortcutEditor, self).__init__(parent)
        self._names = []
        self._hotkeys = []

        self._listbox = ListBox()

        listbuttons = ListButtons()
        listbuttons.insertStretch(0)
        self.connect(listbuttons, SIGNAL('add'), self._addShortcut)
        self.connect(listbuttons, SIGNAL('edit'), self._editShortcut)
        self.connect(listbuttons, SIGNAL('duplicate'), self._duplicate)
        self.connect(self._listbox,
            SIGNAL('itemDoubleClicked (QListWidgetItem *)'), self._editShortcut)
        
        self._listbox.connectToListButtons(listbuttons)

        hbox = QHBoxLayout()
        hbox.addLayout(create_buddy('Shortcuts', self._listbox, QVBoxLayout()))
        hbox.addLayout(listbuttons)

        okcancel = OKCancel()

        vbox = QVBoxLayout()
        vbox.addLayout(hbox)
        if buttons:
            vbox.addLayout(okcancel)
        self.setLayout(vbox)

        self.connect(okcancel, SIGNAL('ok'), self.okClicked)
        self.connect(okcancel, SIGNAL('cancel'), self.close)

        if load:
            self.loadSettings()

    def _addShortcut(self):
        shortcuts = get_shortcuts().difference(self._hotkeys).union(
            i.shortcut for i in self._listbox.items() if i.shortcut)
            
        win = Editor('Add Shortcut', u'', self._actions, self.names(), shortcuts, self)
        win.setModal(True)
        self.connect(win, SIGNAL('actionChanged'), self.addShortcut)
        win.show()

    def addShortcut(self, name, filenames, shortcut=u'', select=True):
        item = QListWidgetItem(name)
        item.actionName = name
        item.filenames = filenames[::]
        item.shortcut = shortcut
        self._listbox.addItem(item)
        if select:
            self._listbox.setCurrentItem(item, QItemSelectionModel.ClearAndSelect)

    def applySettings(self, control = None):
        from puddlestuff.puddletag import remove_shortcuts, add_shortcuts
        remove_shortcuts('&Actions', self._names)

        f = open(FILENAME, 'w')
        f.close()
        
        cparser = PuddleConfig(FILENAME)
        for i, item in enumerate(self._listbox.items()):
            section = SHORTCUT_SECTION + unicode(i)
            cparser.set(section, NAME, item.actionName)
            cparser.set(section, FILENAMES, item.filenames)

        from puddlestuff.mainwin.funcs import applyaction

        shortcuts = create_action_shortcuts(applyaction, control)
        for item, shortcut in zip(self._listbox.items(), shortcuts):
            if item.shortcut:
                shortcut.setShortcut(item.shortcut)
        add_shortcuts('&Actions', shortcuts, save=True)

    def okClicked(self):
        self.applySettings()
        self.close()

    def _duplicate(self):
        try:
            item = self._listbox.selectedItems()[0]
        except IndexError:
            return
        shortcuts = get_shortcuts().difference(self._hotkeys).union(
            i.shortcut for i in self._listbox.items() if i.shortcut)
        win = Editor('Duplicate Shortcut', u'', self._actions, self.names(), shortcuts, self)
        win.setAttrs(item.actionName, self._actions, item.filenames, u'')
        win.setModal(True)
        self.connect(win, SIGNAL('actionChanged'), self.addShortcut)
        win.show()

    def _editShortcut(self):
        try:
            item = self._listbox.selectedItems()[0]
        except IndexError:
            return
        shortcuts = get_shortcuts().difference(self._hotkeys).union(
            i.shortcut for i in self._listbox.items() if i.shortcut)

        names = self.names()
        names.remove(item.actionName)
        win = Editor('Edit Shortcut', item.shortcut, self._actions, names, shortcuts, self)
        win.setAttrs(item.actionName, self._actions, item.filenames, item.shortcut)
        win.setModal(True)
        self.connect(win, SIGNAL('actionChanged'),
            partial(self.editShortcut, item))
        win.show()

    def editShortcut(self, item, name, filenames, shortcut):
        item.actionName = name
        item.filenames = filenames[::]
        item.shortcut = shortcut
        item.setText(name)

    def loadSettings(self, filename=None, actions=None):
        self._names = []
        self._hotkeys = []

        if filename is None:
            filename = os.path.join(ACTIONDIR, 'action_shortcuts')

        self._listbox.clear()
        cparser = PuddleConfig(filename)

        if actions is None:
            self._actions = load_actions()
        else:
            self._actions = actions

        from puddlestuff.puddletag import status
        if status['actions']:
            shortcuts = dict((unicode(a.text()), unicode(a.shortcut().toString()))
                for a in status['actions'])
        else:
            shortcuts = {}

        for section in sorted(cparser.sections()):
            if section.startswith('Shortcut'):
                name = cparser.get(section, NAME, 'Default')
                self._names.append(name)
                filenames = cparser.get(section, FILENAMES, [])
                shortcut = shortcuts.get(name, u'')
                self.addShortcut(name, filenames, shortcut, select=False)
                self._hotkeys.append(shortcut)

    def names(self):
        return [item.actionName for item in self._listbox.items()]
Пример #3
0
class Editor(QDialog):
    def __init__(self,
                 title='Add Action',
                 shortcut=u'',
                 actions=None,
                 names=None,
                 shortcuts=None,
                 parent=None):
        super(Editor, self).__init__(parent)
        self.setWindowTitle(title)

        self._items = {}

        self._name = QLineEdit('Name')

        if shortcut and shortcut in shortcuts:
            shortcuts.remove(shortcut)

        self._shortcut = puddleobjects.ShortcutEditor(shortcuts)
        self._shortcut.setText(shortcut)
        clear = QPushButton(translate('Shortcuts', '&Clear'))
        self.connect(clear, SIGNAL('clicked()'), self._shortcut.clear)

        if names is None:
            names = []
        self._names = names

        self._actionList = ListBox()
        self.connect(self._actionList,
                     SIGNAL('itemDoubleClicked (QListWidgetItem *)'),
                     self._addAction)
        self._newActionList = ListBox()
        listbuttons = ListButtons()
        listbuttons.duplicate.hide()
        listbuttons.insertStretch(0)
        self.connect(listbuttons, SIGNAL('add'), self._addAction)

        self._newActionList.connectToListButtons(listbuttons)

        okcancel = OKCancel()
        self.connect(okcancel, SIGNAL('ok'), self.okClicked)
        self.connect(okcancel, SIGNAL('cancel'), self.close)
        self._ok = okcancel.ok
        self.connect(self._name, SIGNAL('textChanged(const QString)'),
                     self.enableOk)
        scut_status = QLabel('')
        self.connect(
            self._shortcut, SIGNAL('validityChanged'),
            lambda v: scut_status.setText(u'')
            if v or (not self._shortcut.text()) else scut_status.setText(
                translate('Shortcuts', "Invalid shortcut sequence.")))
        okcancel.insertWidget(0, scut_status)

        hbox = QHBoxLayout()
        hbox.addLayout(
            create_buddy('Actions', self._actionList, QVBoxLayout()), 1)
        hbox.addLayout(listbuttons, 0)
        hbox.addLayout(
            create_buddy('Actions to run for shortcut', self._newActionList,
                         QVBoxLayout()), 1)

        layout = QVBoxLayout()
        layout.addLayout(create_buddy('Shortcut &Name: ', self._name))
        scut_layout = create_buddy('&Keyboard Shortcut: ', self._shortcut)
        scut_layout.addWidget(clear)
        layout.addLayout(scut_layout)
        layout.addLayout(hbox)
        layout.addLayout(okcancel)
        self.setLayout(layout)

        if actions:
            self.setActions(actions)

    def _addAction(self, item=None):
        if item is None:
            for item in self._actionList.selectedItems():
                self._addAction(item)
            return
        new_item = QListWidgetItem(item)
        new_item._action = item._action
        self._newActionList.addItem(new_item)
        self._newActionList.setCurrentItem(new_item,
                                           QItemSelectionModel.ClearAndSelect)

    def enableOk(self, text):
        if not text or text in self._names:
            self._ok.setEnabled(False)
        else:
            self._ok.setEnabled(True)

    def okClicked(self):
        alist = self._newActionList
        items = map(alist.item, xrange(alist.count()))
        actions = [item._action[1] for item in items]
        self.emit(
            SIGNAL('actionChanged'), unicode(self._name.text()), actions,
            unicode(self._shortcut.text()) if self._shortcut.valid else u'')
        self.close()

    def setActions(self, actions):
        self._actionList.clear()
        self._actions = []
        for funcs, name, filename in actions:
            item = QListWidgetItem(name)
            item.setToolTip(u'\n'.join([func.description() for func in funcs]))
            item._action = [name, filename]
            self._actionList.addItem(item)

    def setName(self, name):
        self._name.setText(name)

    def setAttrs(self, name, actions, filenames, shortcut=u''):
        names = dict([(z[2], z[1]) for z in actions])
        self.setActions(actions)
        self.setName(name)
        self._newActionList.clear()
        self.setShortcut(shortcut)
        if filenames:
            for filename in filenames:
                item = QListWidgetItem(
                    names.get(filename, translate('Shortcuts', '(Deleted)')))
                item._action = [names.get(filename, u''), filename]
                self._newActionList.addItem(item)

    def setShortcut(self, text):
        self._shortcut.setText(text)
Пример #4
0
class Editor(QDialog):
    def __init__(self, title='Add Action', shortcut=u'', actions=None, names=None, shortcuts=None, parent=None):
        super(Editor, self).__init__(parent)
        self.setWindowTitle(title)

        self._items = {}

        self._name = QLineEdit('Name')
        
        if shortcut and shortcut in shortcuts:
            shortcuts.remove(shortcut)

        self._shortcut = puddleobjects.ShortcutEditor(shortcuts)
        self._shortcut.setText(shortcut)
        clear = QPushButton(translate('Shortcuts', '&Clear'))
        self.connect(clear, SIGNAL('clicked()'), self._shortcut.clear)
        
        if names is None:
            names = []
        self._names = names
        
        self._actionList = ListBox()
        self.connect(self._actionList,
            SIGNAL('itemDoubleClicked (QListWidgetItem *)'), self._addAction)
        self._newActionList = ListBox()
        listbuttons = ListButtons()
        listbuttons.duplicate.hide()
        listbuttons.insertStretch(0)
        self.connect(listbuttons, SIGNAL('add'), self._addAction)

        self._newActionList.connectToListButtons(listbuttons)

        okcancel = OKCancel()
        self.connect(okcancel, SIGNAL('ok'), self.okClicked)
        self.connect(okcancel, SIGNAL('cancel'), self.close)
        self._ok = okcancel.ok
        self.connect(self._name, SIGNAL('textChanged(const QString)'), self.enableOk)
        scut_status = QLabel('')
        self.connect(self._shortcut, SIGNAL('validityChanged'),
            lambda v: scut_status.setText(u'') if v or (not self._shortcut.text()) else
                scut_status.setText(translate('Shortcuts', "Invalid shortcut sequence.")))
        okcancel.insertWidget(0, scut_status)

        hbox = QHBoxLayout()
        hbox.addLayout(
            create_buddy('Actions', self._actionList, QVBoxLayout()), 1)
        hbox.addLayout(listbuttons, 0)
        hbox.addLayout(create_buddy('Actions to run for shortcut',
            self._newActionList, QVBoxLayout()), 1)

        layout = QVBoxLayout()
        layout.addLayout(create_buddy('Shortcut &Name: ', self._name))
        scut_layout = create_buddy('&Keyboard Shortcut: ', self._shortcut)
        scut_layout.addWidget(clear)
        layout.addLayout(scut_layout)
        layout.addLayout(hbox)
        layout.addLayout(okcancel)
        self.setLayout(layout)

        if actions:
            self.setActions(actions)

    def _addAction(self, item=None):
        if item is None:
            for item in self._actionList.selectedItems():
                self._addAction(item)
            return
        new_item = QListWidgetItem(item)
        new_item._action = item._action
        self._newActionList.addItem(new_item)
        self._newActionList.setCurrentItem(new_item,
            QItemSelectionModel.ClearAndSelect)

    def enableOk(self, text):
        if not text or text in self._names:
            self._ok.setEnabled(False)
        else:
            self._ok.setEnabled(True)

    def okClicked(self):
        alist = self._newActionList
        items = map(alist.item, xrange(alist.count()))
        actions = [item._action[1] for item in items]
        self.emit(SIGNAL('actionChanged'), unicode(self._name.text()), actions,
            unicode(self._shortcut.text()) if self._shortcut.valid else u'')
        self.close()

    def setActions(self, actions):
        self._actionList.clear()
        self._actions = []
        for funcs, name, filename in actions:
            item = QListWidgetItem(name)
            item.setToolTip(u'\n'.join([func.description() for func in funcs]))
            item._action = [name, filename]
            self._actionList.addItem(item)

    def setName(self, name):
        self._name.setText(name)

    def setAttrs(self, name, actions, filenames, shortcut=u''):
        names = dict([(z[2], z[1]) for z in actions])
        self.setActions(actions)
        self.setName(name)
        self._newActionList.clear()
        self.setShortcut(shortcut)
        if filenames:
            for filename in filenames:
                item = QListWidgetItem(names.get(filename, translate('Shortcuts', '(Deleted)')))
                item._action = [names.get(filename, u''), filename]
                self._newActionList.addItem(item)

    def setShortcut(self, text):
        self._shortcut.setText(text)