-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
91 lines (77 loc) · 3.54 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
from PyQt5 import QtWidgets, sip
from PyQt5.QtWidgets import QDialog, QMessageBox
from mexui.settings_ui import Ui_Settings
import edit_command
from mex_functions import loadConfig, openDialog
class Window(QDialog, Ui_Settings):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.config = loadConfig()
self.createCommandLabelsAndButtons()
self.setupComboBox()
def setupComboBox(self):
lang = self.config["DEFAULT"]["voice_lang"]
if lang == "pl-PL":
self.comboBox.setCurrentIndex(1)
self.comboBox.activated.connect(self.changeLanguage)
def changeLanguage(self):
self.config["DEFAULT"]["voice_lang"] = self.comboBox.currentText()
with open(os.path.expanduser("~") + "/mexassistant/config.ini", "w") as f:
self.config.write(f)
def createCommandLabelsAndButtons(self):
self.commandsLayout = QtWidgets.QGridLayout()
self.commandsLayout.setObjectName("commandsLayout")
sections = self.config.sections()
for row, i in enumerate(sections):
label = QtWidgets.QLabel(self)
label.setText(i.capitalize())
label.setProperty("class", "command_label")
self.commandsLayout.addWidget(label, row + 2, 0)
self.createCommandButton("edit", "Edytuj", i, row + 2, 1)
self.createCommandButton("delete", "Usuń", i, row + 2, 2)
self.newCommandButton = QtWidgets.QPushButton(self)
self.newCommandButton.setText("Dodaj nową komendę")
self.newCommandButton.pressed.connect(lambda: self.buttonClicked("edit"))
self.commandsLayout.addWidget(self.newCommandButton, len(sections) + 2, 0, 1, 3)
self.gridLayout.addLayout(self.commandsLayout, 2, 0, 1, 3)
def buttonClicked(self, action, button=None):
if action == "edit":
if button:
editCommandDialog = edit_command.Window(button.property("command"))
else:
editCommandDialog = edit_command.Window()
editCommandDialog.exec()
self.refreshCommands()
elif action == "delete":
command = button.property("command")
self.deleteCommand(command)
def createCommandButton(self, action, actionName, command, row, column):
button = QtWidgets.QPushButton(self)
button.setText(actionName)
button.setProperty("class", "command_button")
button.setProperty("command", command)
button.pressed.connect(lambda button=button: self.buttonClicked(action, button))
self.commandsLayout.addWidget(button, row, column)
def deleteCommand(self, command):
dialogResult = openDialog("Na pewno? Tej czynności nie będzie można cofnąć.", "confirmation")
if dialogResult == QMessageBox.Yes:
self.config.remove_section(command)
with open(os.path.expanduser("~") + "/mexassistant/config.ini", "w") as f:
self.config.write(f)
self.refreshCommands()
def deleteLayout(self, layout):
if layout is not None:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget is not None:
widget.deleteLater()
else:
self.deleteLayout(item.layout())
sip.delete(layout)
def refreshCommands(self):
self.config = loadConfig()
self.deleteLayout(self.commandsLayout)
self.createCommandLabelsAndButtons()