def playMorse(self, text): if self.mp is not None: self.mp.shutdown() wpm = int(self.settings.value("wpm")) effectiveWpm = int(self.settings.value("effectiveWpm")) frequency = int(self.settings.value("frequency")) self.mp = MorsePlayer(text, wpm, effectiveWpm, frequency) self.mp.start()
def playExercise(self): if self.requireNewExercise == True: self.generateExercise() wpm = int(self.wpmLineEdit.text()) effectiveWpm = int(self.ewpmLineEdit.text()) frequency = int(self.freqLineEdit.text()) self.mp = MorsePlayer(self.morse_solution, wpm, effectiveWpm, frequency) self.mp.start()
class MainWindow(QMainWindow): def __init__(self): self.settings = QSettings("yayachiken", "PyMorsetrainer") if not self.settings.allKeys(): print("Initializing application settings...") self.settings.setValue("currentLesson", "1") self.settings.setValue("wpm", "20") self.settings.setValue("effectiveWpm", "15") self.settings.setValue("frequency", "800") self.settings.setValue("duration", "1") self.requireNewExercise = False self.mp = None self.lessonButtons = [] self.debug = False super().__init__() self.initUI() self.generateExercise() def initUI(self): self.centralWidget = QWidget() self.setCentralWidget(self.centralWidget) self.receivedTextEdit = QTextEdit() self.receivedTextEdit.setAcceptRichText(False) monospaceFont = QFont("Monospace") monospaceFont.setStyleHint(QFont.Monospace) self.receivedTextEdit.setFont(monospaceFont) playExerciseButton = QPushButton("Play exercise text") playExerciseButton.clicked.connect(self.playExercise) stopButton = QPushButton("Stop playing") stopButton.clicked.connect(self.stopPlaying) validateButton = QPushButton("Check input / Generate next exercise") validateButton.clicked.connect(self.checkInput) self.wpmLineEdit = QLineEdit(self.settings.value("wpm")) self.wpmLineEdit.textChanged.connect(functools.partial(self.saveChangedText, self.wpmLineEdit, "wpm")) wpmLabel = QLabel("WPM") self.ewpmLineEdit = QLineEdit(self.settings.value("effectiveWpm")) self.ewpmLineEdit.textChanged.connect(functools.partial(self.saveChangedText, self.ewpmLineEdit, "effectiveWpm")) ewpmLabel = QLabel("effective WPM") self.freqLineEdit = QLineEdit(self.settings.value("frequency")) self.freqLineEdit.textChanged.connect(functools.partial(self.saveChangedText, self.freqLineEdit, "frequency")) freqLabel = QLabel("Frequency (Hz)") self.durationLineEdit = QLineEdit(self.settings.value("duration")) self.durationLineEdit.textChanged.connect(functools.partial(self.saveChangedText, self.durationLineEdit, "duration")) durationLabel = QLabel("Duration (min)") self.lessonGrid = QGridLayout() lessonCombo = QComboBox() lessonCombo.setStyleSheet("combobox-popup: 0;") lessonCombo.addItem("1 - K M") for lesson in range(2, len(KOCH_LETTERS)): lessonCombo.addItem(str(lesson) + " - " + KOCH_LETTERS[lesson]) lessonCombo.setCurrentIndex(int(self.settings.value("currentLesson"))-1) lessonCombo.currentIndexChanged.connect(self.newLessonSelected) lessonIdLabel = QLabel("Lesson:") lessonBox = QHBoxLayout() lessonBox.addWidget(lessonIdLabel) lessonBox.addWidget(lessonCombo) lessonBox.addStretch(-1) self.createLessonLetterButtons(self.lessonGrid) mainLayout = QVBoxLayout() inputAndParameters = QHBoxLayout() parameterField = QVBoxLayout() inputAndParameters.addWidget(self.receivedTextEdit, stretch=1) self.receivedTextEdit.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.MinimumExpanding)) inputAndParameters.addLayout(parameterField, stretch=0) parameterField.addWidget(playExerciseButton) parameterField.addWidget(stopButton) parameterField.addWidget(validateButton) parameterGrid = QGridLayout() parameterGrid.addWidget(self.wpmLineEdit, 0, 0) parameterGrid.addWidget(wpmLabel, 0, 1) parameterGrid.addWidget(self.ewpmLineEdit, 1, 0) parameterGrid.addWidget(ewpmLabel, 1, 1) parameterGrid.addWidget(self.freqLineEdit, 2, 0) parameterGrid.addWidget(freqLabel, 2, 1) parameterGrid.addWidget(self.durationLineEdit, 3, 0) parameterGrid.addWidget(durationLabel, 3, 1) parameterField.addLayout(parameterGrid) parameterField.insertSpacing(-1, 15) parameterField.addLayout(lessonBox) parameterField.insertStretch(-1) mainLayout.addLayout(inputAndParameters) mainLayout.addLayout(self.lessonGrid) self.centralWidget.setLayout(mainLayout) self.setWindowTitle('PyMorsetrainer') self.show() def closeEvent(self, event): self.stopPlaying() def createLessonLetterButtons(self, parentGrid): newButtonCount = int(self.settings.value("currentLesson")) + 1 oldButtonCount = len(self.lessonButtons) if oldButtonCount > newButtonCount: for button in self.lessonButtons[newButtonCount:]: parentGrid.removeWidget(button) button.deleteLater() self.lessonButtons = self.lessonButtons[:newButtonCount] else: for idx, letter in enumerate(KOCH_LETTERS[oldButtonCount:newButtonCount]): idx = idx + oldButtonCount button = QToolButton() button.setText(letter) button.clicked.connect(functools.partial(self.playMorse, letter)) buttonPolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed, QSizePolicy.PushButton) buttonPolicy.setHorizontalStretch(0) button.setSizePolicy(buttonPolicy) button.setMinimumWidth(5) parentGrid.addWidget(button, 1 + int(idx / 12), int(idx % 12)) self.lessonButtons.append(button) def playMorse(self, text): if self.mp is not None: self.mp.shutdown() wpm = int(self.settings.value("wpm")) effectiveWpm = int(self.settings.value("effectiveWpm")) frequency = int(self.settings.value("frequency")) self.mp = MorsePlayer(text, wpm, effectiveWpm, frequency) self.mp.start() def playExercise(self): if self.requireNewExercise == True: self.generateExercise() self.playMorse(self.morse_solution) def stopPlaying(self): if self.mp is not None: self.mp.shutdown() def newLessonSelected(self, comboId): newLesson = comboId + 1 self.settings.setValue("currentLesson", newLesson) self.createLessonLetterButtons(self.lessonGrid) self.requireNewExercise = True def generateExercise(self): lesson = int(self.settings.value("currentLesson")) letters = KOCH_LETTERS[:lesson+1] wpm = int(self.wpmLineEdit.text()) effectiveWpm = int(self.ewpmLineEdit.text()) frequency = int(self.freqLineEdit.text()) duration = int(self.durationLineEdit.text()) mc = MorseCode("") while mc.tally_length_in_seconds(wpm, effectiveWpm) < duration * 60: new_word = "" for _ in range(0, 5): new_word += (random.choice(letters)) mc.set_morse_text(mc.get_morse_text() + " " + new_word) self.requireNewExercise = False self.morse_solution = mc.get_morse_text() if self.debug: print(self.morse_solution) def checkInput(self): self.evalWindow = EvaluationWindow(self.receivedTextEdit.toPlainText().upper(), self.morse_solution) self.evalWindow.setModal(True) self.evalWindow.show() self.requireNewExercise = True self.receivedTextEdit.clear() def saveChangedText(self, inputField, settingName): self.settings.setValue(settingName, inputField.text()) def enableDebugMode(): self.debug = True
class MainWindow(QMainWindow): def __init__(self): self.settings = QSettings("yayachiken", "PyMorsetrainer") if self.settings.value("currentLesson") is not None: self.lesson = int(self.settings.value("currentLesson")) else: self.lesson = 1 self.settings.setValue("currentLesson", 1) self.requireNewExercise = False self.mp = None self.thread = None super().__init__() self.initUI() self.generateExercise() def initUI(self): self.centralWidget = QWidget() self.setCentralWidget(self.centralWidget) self.receivedTextEdit = QTextEdit() self.receivedTextEdit.setAcceptRichText(False) monospaceFont = QFont("Monospace") monospaceFont.setStyleHint(QFont.Monospace) self.receivedTextEdit.setFont(monospaceFont) playExerciseButton = QPushButton("Play exercise text") playExerciseButton.clicked.connect(self.playExercise) stopButton = QPushButton("Stop playing") stopButton.clicked.connect(self.stopPlaying) validateButton = QPushButton("Check input / Generate next exercise") validateButton.clicked.connect(self.checkInput) self.wpmLineEdit = QLineEdit("20") wpmLabel = QLabel("WPM") self.ewpmLineEdit = QLineEdit("15") ewpmLabel = QLabel("effective WPM") self.freqLineEdit = QLineEdit("800") freqLabel = QLabel("Frequency (Hz)") self.durationLineEdit = QLineEdit("1") durationLabel = QLabel("Duration (min)") lessonBox = QHBoxLayout() lessonBox.setAlignment(Qt.AlignLeft) lessonCombo = QComboBox() lessonCombo.setMaximumWidth(75) lessonCombo.addItem("1 - K M") for lesson in range(2, len(KOCH_LETTERS)): lessonCombo.addItem(str(lesson) + " - " + KOCH_LETTERS[lesson]) lessonCombo.setCurrentIndex(self.lesson-1) lessonCombo.currentIndexChanged.connect(self.newLessonSelected) lessonIdLabel = QLabel("Lesson:") lessonIdLabel.setMaximumWidth(50) self.lessonLabel = QLabel(' '.join(KOCH_LETTERS[:self.lesson+1])) lessonBox.addWidget(lessonIdLabel) lessonBox.addWidget(lessonCombo) lessonBox.addWidget(self.lessonLabel) grid = QGridLayout() grid.setSpacing(10) grid.addWidget(self.receivedTextEdit, 1, 1, 7, 1) grid.addWidget(playExerciseButton, 1, 2, 1, 2) grid.addWidget(stopButton, 2, 2, 1, 2) grid.addWidget(validateButton, 3, 2, 1, 2) grid.addWidget(self.wpmLineEdit, 4, 2) grid.addWidget(wpmLabel, 4, 3) grid.addWidget(self.ewpmLineEdit, 5, 2) grid.addWidget(ewpmLabel, 5, 3) grid.addWidget(self.freqLineEdit, 6, 2) grid.addWidget(freqLabel, 6, 3) grid.addWidget(self.durationLineEdit, 7, 2) grid.addWidget(durationLabel, 7, 3) grid.addLayout(lessonBox, 8, 1, 1, 3) self.centralWidget.setLayout(grid) self.setWindowTitle('PyMorsetrainer') self.show() def closeEvent(self, event): self.stopPlaying() def playExercise(self): if self.requireNewExercise == True: self.generateExercise() wpm = int(self.wpmLineEdit.text()) effectiveWpm = int(self.ewpmLineEdit.text()) frequency = int(self.freqLineEdit.text()) self.mp = MorsePlayer(self.morse_solution, wpm, effectiveWpm, frequency) self.mp.start() def stopPlaying(self): if self.mp is not None: self.mp.shutdown() def newLessonSelected(self, comboId): self.lesson = comboId+1 self.settings.setValue("currentLesson", self.lesson) self.lessonLabel.setText(' '.join(KOCH_LETTERS[:self.lesson+1])) self.requireNewExercise = True def generateExercise(self): letters = KOCH_LETTERS[:self.lesson+1] wpm = int(self.wpmLineEdit.text()) effectiveWpm = int(self.ewpmLineEdit.text()) frequency = int(self.freqLineEdit.text()) duration = int(self.durationLineEdit.text()) mc = MorseCode("") while mc.tally_length_in_seconds(wpm, effectiveWpm) < duration * 60: new_word = "" for _ in range(0, 5): new_word += (random.choice(letters)) mc.set_morse_text(mc.get_morse_text() + " " + new_word) self.requireNewExercise = False self.morse_solution = mc.get_morse_text() print(self.morse_solution) def checkInput(self): self.evalWindow = EvaluationWindow(self.receivedTextEdit.toPlainText().upper(), self.morse_solution) self.evalWindow.setModal(True) self.evalWindow.show() self.requireNewExercise = True self.receivedTextEdit.clear()