def __init__(self, parent=None, name=None): global shortcutDict super(CWShortcutDialog, self).__init__(parent) self.ui = Ui_ShortcutDialog() self.ui.setupUi(self) self.ui.saveShortcutButton.clicked.connect(self.saveShortcut) self.ui.okayButton.clicked.connect(self.close) self.ui.shortcutTable.setRowCount(len(shortcutDict.keys())) for but in butList: self.ui.functionDropdown.addItem(but.name) for i, key in enumerate(shortcutDict.keys()): k = QTableWidgetItem("Ctrl+" + key) fn = QTableWidgetItem(shortcutDict[key]) self.ui.shortcutTable.setItem(i, 0, k) self.ui.shortcutTable.setItem(i, 1, fn)
class CWShortcutDialog(QDialog): # ------------------------------------------- def __init__(self, parent=None, name=None): global shortcutDict super(CWShortcutDialog, self).__init__(parent) self.ui = Ui_ShortcutDialog() self.ui.setupUi(self) self.ui.saveShortcutButton.clicked.connect(self.saveShortcut) self.ui.okayButton.clicked.connect(self.close) self.ui.shortcutTable.setRowCount(len(shortcutDict.keys())) for but in butList: self.ui.functionDropdown.addItem(but.name) for i, key in enumerate(shortcutDict.keys()): k = QTableWidgetItem("Ctrl+" + key) fn = QTableWidgetItem(shortcutDict[key]) self.ui.shortcutTable.setItem(i, 0, k) self.ui.shortcutTable.setItem(i, 1, fn) def saveShortcut(self): global shortcutDict, window key = self.ui.keyEntry.text().lower() if key == "a" or key == "u" or key == "c" or key == "v" or key == "z" or key == "x": self.errorPopup = CWErrorDialog("Error: the '" + key + "' key is reserved for OS text editing functions") self.errorPopup.show() elif key == "": self.errorPopup = CWErrorDialog("Error: Please enter a key") self.errorPopup.show() elif len(key) > 1: self.errorPopup = CWErrorDialog("Error: Please use only one character.") self.errorPopup.show() else: isIn = False fn = self.ui.functionDropdown.currentText() if fn in shortcutDict.values(): for k in shortcutDict.keys(): if fn == shortcutDict[k]: isIn = True if k != key: del shortcutDict[k] break for act in window.ui.menuCommands.actions(): if act.text() == fn: window.ui.menuCommands.removeAction(act) break for x in shortcutDict.keys(): if x == key: for y in range(len(shortcutDict.keys())): if self.ui.shortcutTable.item(y, 0).text() == "Ctrl+" + key: self.ui.shortcutTable.removeRow(y) break del shortcutDict[x] break k = QTableWidgetItem("Ctrl+" + key) f = QTableWidgetItem(fn) shortcutDict[key] = fn self.ui.shortcutTable.setRowCount(len(shortcutDict.keys())) pos = len(shortcutDict.keys()) - 1 if not isIn: self.ui.shortcutTable.setItem(pos, 0, k) self.ui.shortcutTable.setItem(pos, 1, f) else: for i in range(pos+1): if self.ui.shortcutTable.item(i, 1).text() == f.text(): self.ui.shortcutTable.setItem(i, 0, k) break seq = QKeySequence("Ctrl+" + key) if fn == "Save": window.ui.actionSave.setShortcut(seq) elif fn == "Save As": window.ui.actionSave_As.setShortcut(seq) elif fn == "Open": window.ui.actionOpen.setShortcut(seq) elif fn == "Edit Shortcuts": window.ui.actionShortcuts.setShortcut(seq) elif fn == "New Button": window.ui.actionButton.setShortcut(seq) elif fn == "New Command Box": window.ui.actionCommand_Box.setShortcut(seq) elif fn == "Beginning of Line": window.ui.actionBoL.setShortcut(seq) elif fn == "End of Line": window.ui.actionEoL.setShortcut(seq) elif fn == "Delete Line": window.ui.actionDL.setShortcut(seq) elif fn == "Beginning of File": window.ui.actionBoF.setShortcut(seq) elif fn == "End of File": window.ui.actionEoF.setShortcut(seq) else: act = QAction(window) act.setText(fn) act.triggered.connect(butDict[fn].buttonObj.click) act.setShortcut(seq) window.ui.menuCommands.addAction(act) self.ui.keyEntry.setText("") def closeEvent(self, event): if self.ui.keyEntry.text() != "": self.saveShortcut()