consolidator
/
TodoWindow.py
145 строк · 6.3 Кб
1import os
2import re
3import sys
4from PySide6.QtCore import (Slot,QRect)
5from PySide6.QtWidgets import (
6QDialog,
7QDialogButtonBox,
8QTableWidget,
9QTableWidgetItem,
10QCheckBox,
11QPushButton,
12QMessageBox,
13QLabel,
14QLineEdit
15)
16from PySide6.QtUiTools import QUiLoader
17import pandas as pd
18from ModuleMaker import ModuleMaker
19import codecs
20
21class TodoWindow(QDialog):
22
23def __init__(self,data:pd.DataFrame) -> None:
24super().__init__()
25loader = QUiLoader()
26try:
27self.cur_path = sys._MEIPASS
28except:
29self.cur_path = os.path.dirname(os.path.realpath(__file__))
30self.TodoDialog:QDialog = loader.load(f"{self.cur_path}\\TodoWindow.ui", None)
31self.data:pd.DataFrame = data
32
33self.new_modules = []
34self.deleted_modules = []
35
36# init components
37self.buttonBox:QDialogButtonBox = self.TodoDialog.buttonBox
38self.mainTable:QTableWidget = self.TodoDialog.mainTable
39self.delModuleButton:QPushButton = self.TodoDialog.delModuleButton
40self.newModuleButton:QPushButton = self.TodoDialog.newModuleButton
41self.buttonBox.button(QDialogButtonBox.StandardButton.Save).setDisabled(True)
42
43# init handlers
44self.newModuleButton.clicked.connect(self.make_new_model)
45self.delModuleButton.clicked.connect(self.unbind_model)
46
47# init table data
48self.mainTable.setColumnWidth(0,400)
49self.refresh_table()
50
51
52def refresh_table(self):
53try:
54self.mainTable.clear()
55self.mainTable.setRowCount(0)
56for r,record in self.data.iterrows():
57self.mainTable.setRowCount(self.mainTable.rowCount()+1)
58self.mainTable.setItem(r,0,QTableWidgetItem(record["key"]))
59self.mainTable.setItem(r,1,QTableWidgetItem(record["module"]))
60chb = QCheckBox(self.mainTable)
61chb.setProperty("key",record["key"])
62chb.setChecked(record["done"])
63chb.stateChanged.connect(self.item_state_changed)
64self.mainTable.setCellWidget(r,2,chb)
65except: raise
66
67def show(self)->int:
68result = self.TodoDialog.exec()
69if result==1:
70if self.deleted_modules.__len__()>0:
71for f_ in self.deleted_modules:
72f:str = f_
73os.rename(f,f.replace(".py","-deleted.py"))
74else:
75if self.new_modules.__len__()>0:
76for f in self.new_modules: os.remove(f)
77return result
78
79@Slot() # декоратор нужен для того, чтобы получить доступ к методу sender()
80def item_state_changed(self):
81chb:QCheckBox = self.sender()
82key:str=chb.property("key")
83checked = chb.isChecked()
84self.buttonBox.button(QDialogButtonBox.StandardButton.Save).setDisabled(False)
85self.data.loc[self.data["key"]==key,"done"]=checked
86
87def make_new_model(self):
88try:
89dialog=QDialog(self.TodoDialog)
90dialog.setWindowTitle("Новый модуль")
91dialog.setFixedSize(670,150)
92label1= QLabel(dialog)
93label2= QLabel(dialog)
94moduleName = QLineEdit(dialog)
95handlerName = QLineEdit(dialog)
96okButton = QPushButton(dialog)
97label1.setText("Модуль")
98label2.setText("Обработчик")
99okButton.setText("Ok")
100label1.setGeometry(QRect(10, 10, 51, 16))
101label2.setGeometry(QRect(10, 60, 81, 16))
102moduleName.setGeometry(QRect( 10, 30, 651, 22))
103handlerName.setGeometry(QRect(10, 80, 651, 22))
104okButton.setGeometry(QRect(20,120,60,22))
105okButton.clicked.connect(dialog.accept)
106if dialog.exec()==1:
107
108mName = moduleName.text() #.encode("utf-8").decode("utf-8")
109hName = handlerName.text()
110mm:list =re.findall("(?!([a-zA-Z_0-9]+)).",hName)
111if mm.__len__()>0 : raise Exception("Имя обработчика должно содержать a-zA-Z_0-9")
112if mName is None or mName.__len__()==0: raise Exception("Имя модуля должно содержать хоть один символ")
113hPath = f"{self.cur_path}\\handlers2\\{hName}.py"
114if os.path.isfile(hPath): raise Exception(f"Обработчик '{hPath}' уже существует.")
115mess = ModuleMaker.make_from_file(f"{self.cur_path}\\handlers2\\module_pattern.py",mName,hName,f"{self.cur_path}\\handlers2")
116print(mess)
117self.new_modules.append(f"{self.cur_path}\\handlers2\\{hName}.py")
118self.data.loc[len(self.data)] = [mName,hName,False]
119r = self.mainTable.rowCount()
120self.mainTable.setRowCount(r+1)
121self.mainTable.setItem(r,0,QTableWidgetItem(mName))
122self.mainTable.setItem(r,1,QTableWidgetItem(hName))
123chb = QCheckBox(self.mainTable)
124chb.setProperty("key",mName)
125chb.setChecked(False)
126chb.stateChanged.connect(self.item_state_changed)
127self.mainTable.setCellWidget(r,2,chb)
128self.buttonBox.button(QDialogButtonBox.StandardButton.Save).setDisabled(False)
129except: raise
130
131def unbind_model(self):
132try:
133key = self.mainTable.selectedItems()[0].data(0)
134hName = self.mainTable.selectedItems()[1].data(0)
135result = QMessageBox.warning(self.TodoDialog, "Удаление модуля",
136f"Вы действительно хотите отвязать обработчик от модуля <b>'{key}'</b>?",
137QMessageBox.Ok | QMessageBox.Cancel,
138QMessageBox.Cancel)
139if result==QMessageBox.Ok:
140self.deleted_modules.append(f"{self.cur_path}\\handlers2\\{hName}.py")
141self.data.drop(self.data[self.data.key==key].index,inplace=True)
142self.refresh_table()
143self.buttonBox.button(QDialogButtonBox.StandardButton.Save).setDisabled(False)
144
145except: raise
146
147
148
149
150
151