-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_machine.py
58 lines (52 loc) · 2.29 KB
/
create_machine.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
import os
import shutil
import utils
from ui_util import QtCore, QtGui, QtWidgets
from py_ui.create import Ui_MainWindow
class CreateMachine:
def __init__(self, manager: any) -> None:
super(CreateMachine, self).__init__()
self.manager = manager
self.window = QtWidgets.QMainWindow()
self.hwnd = 0
self.ui = Ui_MainWindow()
self.ui.setupUi(self.window)
self.setup_events()
def run(self) -> None:
self.hwnd = int(self.window.winId())
self.window.setWindowFlags(QtCore.Qt.WindowType.WindowCloseButtonHint)
self.window.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
utils.update_style(self, False)
self.ui.nameEdit.clear()
self.ui.applyButton.setEnabled(False)
self.ui.presetBox.clear()
for preset in utils.get_all_presets():
self.ui.presetBox.addItem(preset)
self.window.show()
def setup_events(self) -> None:
self.ui.cancelButton.clicked.connect(self.window.close)
self.ui.applyButton.clicked.connect(self.apply)
self.ui.nameEdit.textChanged.connect(self.check_ability)
def check_ability(self, edit_text: str) -> None:
edit_text = edit_text.strip()
if not edit_text:
return self.ui.applyButton.setEnabled(False)
if edit_text.lower() in tuple(x.lower() for x in utils.get_all_machines()):
return self.ui.applyButton.setEnabled(False)
for elem in ('/', '\\', ':', '*', '?', '"', '<', '>', '|'):
if elem in edit_text:
return self.ui.applyButton.setEnabled(False)
return self.ui.applyButton.setEnabled(True)
def apply(self) -> None:
all_machines = list(utils.get_machines_list())
current_text = self.manager.current_text
current_index = (all_machines.index(current_text) + 1) if current_text else len(all_machines)
all_machines.insert(current_index, self.ui.nameEdit.text())
shutil.copy(
os.path.join(utils.presets_dir, self.ui.presetBox.currentText() + '.json'),
os.path.join(utils.machines_dir, self.ui.nameEdit.text() + '.json')
)
utils.set_machines_list(all_machines)
utils.check_all_machines()
self.manager.update_machines_list()
self.window.close()