Skip to content

Commit

Permalink
feat: Add presets functionality with save, rename, and delete options
Browse files Browse the repository at this point in the history
  • Loading branch information
LyubomirT committed Sep 22, 2024
1 parent 4ca81c6 commit 3987963
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ output/
dist/
dist.zip
language.txt
theme.txt
theme.txt
presets.json
38 changes: 37 additions & 1 deletion src/darkmode.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,40 @@ QComboBox {
border: 1px solid #555555;
padding: 5px;
color: #ffffff;
}
}

/* Presets Section */
#presetsGroupBox {
background-color: #2b2b2b;
border: 1px solid #555555;
border-radius: 5px;
padding: 10px;
color: #ffffff;
}

QListWidget#presetsList {
background-color: #1e1e1e;
border: 1px solid #555555;
color: #ffffff;
padding: 5px;
selection-background-color: #555555;
}

QPushButton#savePresetButton, QPushButton#renamePresetButton, QPushButton#deletePresetButton {
font-size: 9pt;
padding: 8px;
background-color: #666666;
color: #ffffff;
border: none;
border-radius: 5px;
font: bold;
margin-top: 5px;
}

QPushButton#savePresetButton:hover, QPushButton#renamePresetButton:hover, QPushButton#deletePresetButton:hover {
background-color: #444444;
}

QPushButton#savePresetButton:disabled, QPushButton#renamePresetButton:disabled, QPushButton#deletePresetButton:disabled {
background-color: #555555;
}
130 changes: 125 additions & 5 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import time
import random
import string
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
import json
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QListWidgetItem
from PyQt5.QtCore import QTimer, Qt, QThread, pyqtSignal
from PyQt5.QtWidgets import QInputDialog
from ui import Ui_MainWindow
from pynput.keyboard import Controller, Key

Expand Down Expand Up @@ -85,7 +88,17 @@ def __init__(self):
self.stopButton.clicked.connect(self.stop_typing)
self.languageComboBox.currentTextChanged.connect(self.change_language)
self.lightModeCheckBox.toggled.connect(self.toggleTheme)

# Presets Buttons
self.savePresetButton.clicked.connect(self.save_preset)
self.renamePresetButton.clicked.connect(self.rename_preset)
self.deletePresetButton.clicked.connect(self.delete_preset)
self.presetsList.itemDoubleClicked.connect(self.load_preset)

self.thread = None
self.presets = {}
self.presets_file = "presets.json"
self.load_presets()

def start_typing(self):
text = self.textEdit.toPlainText()
Expand Down Expand Up @@ -129,7 +142,7 @@ def stop_typing(self):
self.thread.wait()
self.thread = None
self.typing_finished()

def toggleTheme(self):
if self.lightModeCheckBox.isChecked():
self.setStyleSheet(open("style.css").read())
Expand All @@ -140,10 +153,117 @@ def toggleTheme(self):
def save_theme_based_on_last_choice(self):
with open("theme.txt", "w", encoding="utf-8") as f:
f.write("light" if self.lightModeCheckBox.isChecked() else "dark")


# Presets Methods
def load_presets(self):
if os.path.exists(self.presets_file):
try:
with open(self.presets_file, "r", encoding="utf-8") as f:
self.presets = json.load(f)
for preset_name in self.presets:
self.presetsList.addItem(preset_name)
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to load presets: {e}")
else:
self.presets = {}

def save_presets_to_file(self):
try:
with open(self.presets_file, "w", encoding="utf-8") as f:
json.dump(self.presets, f, indent=4)
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to save presets: {e}")

def save_preset(self):
preset_name, ok = self.get_text_input("Save Preset", "Enter a name for the preset:")
if ok and preset_name:
if preset_name in self.presets:
QMessageBox.warning(self, "Warning", "A preset with this name already exists.")
return
self.presets[preset_name] = self.current_settings()
self.presetsList.addItem(preset_name)
self.save_presets_to_file()
QMessageBox.information(self, "Success", f"Preset '{preset_name}' saved successfully.")

def rename_preset(self):
selected_item = self.presetsList.currentItem()
if not selected_item:
QMessageBox.warning(self, "Warning", "Please select a preset to rename.")
return
old_name = selected_item.text()
new_name, ok = self.get_text_input("Rename Preset", "Enter a new name for the preset:", old_name)
if ok and new_name:
if new_name in self.presets:
QMessageBox.warning(self, "Warning", "A preset with this name already exists.")
return
self.presets[new_name] = self.presets.pop(old_name)
selected_item.setText(new_name)
self.save_presets_to_file()
QMessageBox.information(self, "Success", f"Preset renamed to '{new_name}' successfully.")

def delete_preset(self):
selected_item = self.presetsList.currentItem()
if not selected_item:
QMessageBox.warning(self, "Warning", "Please select a preset to delete.")
return
preset_name = selected_item.text()
confirm = QMessageBox.question(self, "Confirm Delete", f"Are you sure you want to delete preset '{preset_name}'?", QMessageBox.Yes | QMessageBox.No)
if confirm == QMessageBox.Yes:
self.presets.pop(preset_name, None)
self.presetsList.takeItem(self.presetsList.row(selected_item))
self.save_presets_to_file()
QMessageBox.information(self, "Success", f"Preset '{preset_name}' deleted successfully.")

def load_preset(self, item):
preset_name = item.text()
settings = self.presets.get(preset_name)
if settings:
self.apply_settings(settings)
QMessageBox.information(self, "Preset Loaded", f"Preset '{preset_name}' has been loaded.")
else:
QMessageBox.warning(self, "Warning", f"Preset '{preset_name}' not found.")

def current_settings(self):
return {
"delay": self.delaySpinBox.value(),
"interval": self.intervalSpinBox.value(),
"type_enter": self.enterCheckBox.isChecked(),
"chars_per_stroke": self.charPerStrokeSpinBox.value(),
"randomize_interval": self.randomizeIntervalCheckBox.isChecked(),
"mistake_percentage": self.mistakePercentageSpinBox.value(),
"light_mode": self.lightModeCheckBox.isChecked(),
"language": self.current_language
}

def apply_settings(self, settings):
self.delaySpinBox.setValue(settings.get("delay", 0))
self.intervalSpinBox.setValue(settings.get("interval", 0.0))
self.enterCheckBox.setChecked(settings.get("type_enter", False))
self.charPerStrokeSpinBox.setValue(settings.get("chars_per_stroke", 1))
self.randomizeIntervalCheckBox.setChecked(settings.get("randomize_interval", False))
self.mistakePercentageSpinBox.setValue(settings.get("mistake_percentage", 0))
self.lightModeCheckBox.setChecked(settings.get("light_mode", True))
self.toggleTheme()
language = settings.get("language", "English")
index = self.languageComboBox.findText(language + " - " + self.translations.get(language, {}).get("original", "Unknown"))
if index != -1:
self.languageComboBox.setCurrentIndex(index)

def get_text_input(self, title, label, default_text=""):
text, ok = QInputDialog.getText(self, title, label, text=default_text)
return text, ok

if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet(open("style.css").read())
if os.path.exists("theme.txt"):
with open("theme.txt", "r", encoding="utf-8") as f:
theme = f.read()
if theme == "light":
app.setStyleSheet(open("style.css").read())
else:
app.setStyleSheet(open("darkmode.css").read())
else:
app.setStyleSheet(open("style.css").read())
window = MainWindow()
window.show()
sys.exit(app.exec_())
sys.exit(app.exec_())
36 changes: 36 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,39 @@ QComboBox {
padding: 5px;
color: #000000;
}

/* Presets Section */
#presetsGroupBox {
background-color: #e0e0e0;
border: 1px solid #cccccc;
border-radius: 5px;
padding: 10px;
color: #000000;
}

QListWidget#presetsList {
background-color: #f9f9f9;
border: 1px solid #cccccc;
color: #000000;
padding: 5px;
selection-background-color: #d3d3d3;
}

QPushButton#savePresetButton, QPushButton#renamePresetButton, QPushButton#deletePresetButton {
font-size: 9pt;
padding: 8px;
background-color: #f9d039;
color: #ffffff;
border: none;
border-radius: 5px;
font: bold;
margin-top: 5px;
}

QPushButton#savePresetButton:hover, QPushButton#renamePresetButton:hover, QPushButton#deletePresetButton:hover {
background-color: #ffa500;
}

QPushButton#savePresetButton:disabled, QPushButton#renamePresetButton:disabled, QPushButton#deletePresetButton:disabled {
background-color: #e0e0e0;
}
14 changes: 13 additions & 1 deletion src/translations/Chinese.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"text": "停止打字"
},
{
"component": "lightModeCheckbox",
"component": "lightModeCheckBox",
"text": "轻模式"
},
{
Expand All @@ -36,6 +36,18 @@
{
"component": "mistakePercentageLabel",
"text": "错误百分比:"
},
{
"component": "savePresetButton",
"text": "保存预设"
},
{
"component": "renamePresetButton",
"text": "重命名预设"
},
{
"component": "deletePresetButton",
"text": "删除预设"
}
]
}
14 changes: 13 additions & 1 deletion src/translations/English.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
{
"component": "mistakePercentageLabel",
"text": "Mistake Percentage:"
},
{
"component": "savePresetButton",
"text": "Save Preset"
},
{
"component": "renamePresetButton",
"text": "Rename Preset"
},
{
"component": "deletePresetButton",
"text": "Delete Preset"
}
]
}
}
14 changes: 13 additions & 1 deletion src/translations/French.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
{
"component": "mistakePercentageLabel",
"text": "Pourcentage d'erreur :"
},
{
"component": "savePresetButton",
"text": "Enregistrer le préréglage"
},
{
"component": "renamePresetButton",
"text": "Renommer le préréglage"
},
{
"component": "deletePresetButton",
"text": "Supprimer le préréglage"
}
]
}
}
16 changes: 14 additions & 2 deletions src/translations/Norwegian.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
{
"component": "startButton",
"text": "Begynn Å Skrive "
"text": "Begynn Å Skrive"
},
{
"component": "stopButton",
Expand All @@ -36,6 +36,18 @@
{
"component": "mistakePercentageLabel",
"text": "Feilprosent:"
},
{
"component": "savePresetButton",
"text": "Lagre forhåndsinnstilling"
},
{
"component": "renamePresetButton",
"text": "Gi nytt navn på forhåndsinnstilling"
},
{
"component": "deletePresetButton",
"text": "Slett forhåndsinnstilling"
}
]
}
}
Loading

0 comments on commit 3987963

Please sign in to comment.