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()]
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()]
class ButtonsAndList(QFrame): def __init__(self, parent=None, title=u'', add_text=ADD_TEXT, help_text=u''): QFrame.__init__(self, parent) self.title = title connect = lambda c, signal, s: self.connect(c, SIGNAL(signal), s) self.setFrameStyle(QFrame.Box) self.listbox = ListBox() self.listbox.setSelectionMode(self.listbox.ExtendedSelection) buttons = ListButtons() hbox = QHBoxLayout() hbox.addWidget(self.listbox) vbox = QVBoxLayout() sortlistbox = QPushButton(translate("Defaults", '&Sort')) self._sortOrder = Qt.AscendingOrder connect(sortlistbox, 'clicked()', self._sortListBox) vbox.addWidget(sortlistbox) vbox.addLayout(buttons) vbox.addStretch() hbox.addLayout(vbox) if help_text: label = QLabel(help_text) layout = QVBoxLayout() layout.addWidget(label) layout.addLayout(hbox, 1) self.setLayout(layout) else: self.setLayout(hbox) connect(buttons, "add", self.addItem) connect(buttons, "edit", self.editItem) buttons.duplicate.setVisible(False) self.listbox.connectToListButtons(buttons) self.listbox.editButton = buttons.edit connect(self.listbox, 'itemDoubleClicked(QListWidgetItem *)', self._doubleClicked) self.addText = add_text def _doubleClicked(self, item): self.editItem() def _sortListBox(self): if self._sortOrder == Qt.AscendingOrder: self.listbox.sortItems(Qt.DescendingOrder) self._sortOrder = Qt.DescendingOrder else: self.listbox.sortItems(Qt.AscendingOrder) self._sortOrder = Qt.AscendingOrder def addItem(self): l = self.listbox.item patterns = [unicode(l(z).text()) for z in range(self.listbox.count())] row = self.listbox.currentRow() if row < 0: row = 0 (text, ok) = QInputDialog().getItem(self, 'puddletag', self.addText, patterns, row) if ok: self.listbox.clearSelection() self.listbox.addItem(text) self.listbox.setCurrentRow(self.listbox.count() - 1) def addItems(self, items): self.listbox.addItems(items) def editItem(self, row=None): if row is None: row = self.listbox.currentRow() l = self.listbox.item patterns = [unicode(l(z).text()) for z in range(self.listbox.count())] (text, ok) = QInputDialog().getItem(self, 'puddletag', self.addText, patterns, row) if ok: item = l(row) item.setText(text) self.listbox.setItemSelected(item, True) def getItems(self): return [item.text() for item in self.listbox.items()]
class ButtonsAndList(QFrame): def __init__(self, parent=None, title=u'', add_text=ADD_TEXT, help_text=u''): QFrame.__init__(self, parent) self.title = title connect = lambda c, signal, s: self.connect(c, SIGNAL(signal), s) self.setFrameStyle(QFrame.Box) self.listbox = ListBox() self.listbox.setSelectionMode(self.listbox.ExtendedSelection) buttons = ListButtons() hbox = QHBoxLayout() hbox.addWidget(self.listbox) vbox = QVBoxLayout() sortlistbox = QPushButton(translate("Defaults", '&Sort')) self._sortOrder = Qt.AscendingOrder connect(sortlistbox, 'clicked()', self._sortListBox) vbox.addWidget(sortlistbox) vbox.addLayout(buttons) vbox.addStretch() hbox.addLayout(vbox) if help_text: label = QLabel(help_text) layout = QVBoxLayout() layout.addWidget(label) layout.addLayout(hbox, 1) self.setLayout(layout) else: self.setLayout(hbox) connect(buttons, "add", self.addItem) connect(buttons, "edit", self.editItem) buttons.duplicate.setVisible(False) self.listbox.connectToListButtons(buttons) self.listbox.editButton = buttons.edit connect(self.listbox, 'itemDoubleClicked(QListWidgetItem *)', self._doubleClicked) self.addText = add_text def _doubleClicked(self, item): self.editItem() def _sortListBox(self): if self._sortOrder == Qt.AscendingOrder: self.listbox.sortItems(Qt.DescendingOrder) self._sortOrder = Qt.DescendingOrder else: self.listbox.sortItems(Qt.AscendingOrder) self._sortOrder = Qt.AscendingOrder def addItem(self): l = self.listbox.item patterns = [unicode(l(z).text()) for z in range(self.listbox.count())] row = self.listbox.currentRow() if row < 0: row = 0 (text, ok) = QInputDialog().getItem(self, 'puddletag', self.addText, patterns, row) if ok: self.listbox.clearSelection() self.listbox.addItem(text) self.listbox.setCurrentRow(self.listbox.count() - 1) def addItems(self, items): self.listbox.addItems(items) def editItem(self, row=None): if row is None: row = self.listbox.currentRow() l = self.listbox.item patterns = [unicode(l(z).text()) for z in range(self.listbox.count())] (text, ok) = QInputDialog().getItem (self, 'puddletag', self.addText, patterns, row) if ok: item = l(row) item.setText(text) self.listbox.setItemSelected(item, True) def getItems(self): return [item.text() for item in self.listbox.items()]