This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具v3/指定题号编译pdf.py

163 lines
7.1 KiB
Python

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_指定题号编译pdf import Ui_Form
import os
from database_tools_2 import *
class MyWindow(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
outputpath = os.path.join(os.getcwd(),"临时文件")
self.lineEdit_path.setText(outputpath)
self.TitleID_list = []
self.TeachersCheckBoxList = [self.checkBox_ans,self.checkBox_objs,self.checkBox_tags,self.checkBox_solution,self.checkBox_usages,self.checkBox_origin,self.checkBox_remark]
self.StudentsCheckBoxList = [self.checkBox_space,self.checkBox_ans]
self.ContnetCheckBoxList = [self.checkBox_ans,self.checkBox_objs,self.checkBox_tags,self.checkBox_solution,self.checkBox_usages,self.checkBox_origin,self.checkBox_remark,self.checkBox_space]
for widget in self.ContnetCheckBoxList:
widget.setDisabled(True)
self.studentlayout()
self.disableusagessetting()
self.pushButton_exec.clicked.connect(self.saveandbuild)
self.pushButton_add.clicked.connect(self.additems)
self.pushButton_delete.clicked.connect(self.deleteitem)
self.pushButton_SelectPath.clicked.connect(self.selectpath)
self.radioButton_teacher.clicked.connect(self.teacherlayout)
self.radioButton_student.clicked.connect(self.studentlayout)
self.checkBox_usages.clicked.connect(self.toggleusagessetting)
self.radioButton_student.clicked.connect(self.toggleusagessetting)
self.radioButton_teacher.clicked.connect(self.toggleusagessetting)
def selectpath(self):
outputpath = QFileDialog.getExistingDirectory(self,"选择文件夹")
self.lineEdit_path.setText(outputpath)
def additems(self):
toAddString = self.plainTextEdit_newitems.toPlainText()
try:
toAddList = TitleIDStringtoTupleList(toAddString)
self.TitleID_list += toAddList.copy()
except:
print("待添加字符串格式有误, 请检查")
self.plainTextEdit_allitems.setPlainText(TitleIDTupleListtoString(self.TitleID_list))
def deleteitem(self):
enum = int(self.lineEdit_index.text())-1
self.TitleID_list.pop(enum)
self.plainTextEdit_allitems.setPlainText(TitleIDTupleListtoString(self.TitleID_list))
def disableusagessetting(self):
self.lineEdit_high.setDisabled(True)
self.lineEdit_low.setDisabled(True)
self.lineEdit_grades.setDisabled(True)
def enableusagessetting(self):
self.lineEdit_high.setEnabled(True)
self.lineEdit_low.setEnabled(True)
self.lineEdit_grades.setEnabled(True)
def toggleusagessetting(self):
if self.checkBox_usages.isChecked() and self.radioButton_teacher.isChecked():
self.enableusagessetting()
else:
self.disableusagessetting()
def studentlayout(self):
for widget in self.TeachersCheckBoxList:
widget.setDisabled(True)
for widget in self.StudentsCheckBoxList:
widget.setEnabled(True)
def teacherlayout(self):
for widget in self.StudentsCheckBoxList:
widget.setDisabled(True)
for widget in self.TeachersCheckBoxList:
widget.setEnabled(True)
def generate_usages(self):
high = self.lineEdit_high.text().strip()
low = self.lineEdit_low.text().strip()
if self.checkBox_usages.isChecked() == False:
return [-2,-2]
elif self.checkBox_usages.isChecked() and high == "" and low == "":
return [-1,-1]
else:
if high == "":
high = "0"
if low == "":
low = "0"
return [int(high),int(low)]
def saveandbuild(self):
if self.radioButton_teacher.isChecked() and self.checkBox_usages.isChecked() and not self.lineEdit_grades.text().strip() == "":
grades = self.lineEdit_grades.text().strip().split(",")
else:
grades = []
# prodictpath = "../题库0.3/Problems.json"
# objdictpath = "../题库0.3/LessonObj.json"
# raw_pro_dict = load_dict(prodictpath)
# pro_dict = select_grade_from_pro_dict(raw_pro_dict,grades)
# obj_dict = load_dict(objdictpath)
configjson = {
"教师版": self.radioButton_teacher.isChecked(),
"字段显示设置": {
"题后空间": self.checkBox_space.isChecked(),
"课时目标": self.checkBox_objs.isChecked(),
"题目标签": self.checkBox_tags.isChecked(),
"答案": self.checkBox_ans.isChecked(),
"解答与提示": self.checkBox_solution.isChecked(),
"使用记录": self.generate_usages(),
"来源": self.checkBox_origin.isChecked(),
"备注": self.checkBox_remark.isChecked(),
"届别": grades
}
}
notetitle = "选题编译"
outputdir = self.lineEdit_path.text() #输出文件的目录
outputfilepath = os.path.join(outputdir,notetitle+".tex")
print("输出文件目录: %s\n输出文件名: %s"%(outputdir,notetitle+".tex"))
latex_raw = ReadTextFile("模板文件/讲义模板.txt")
if configjson["教师版"] == True:
latex_raw = latex_raw.replace(r"学号\blank{50} \ 姓名\blank{80}","上海市控江中学")
if sys.platform != "win32": #非win系统用默认字体
latex_raw = re.sub(r"fontset[\s]*=[\s]*none","fontset = fandol",latex_raw)
latex_raw = re.sub(r"\\setCJKmainfont",r"% \\setCJKmainfont",latex_raw)
bodystring = "\\tableofcontents\n\n\\newpage\n\n"
bodylist = []
# problems_dict = configjson["标题与题号"]
starttime = time.time()
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = db_database)
mycursor = mydb.cursor()
for p,IDs in self.TitleID_list:
currentbodystring = f"\\section{{{p}}}\n\\begin{{enumerate}}\n\n"
print(f"正在从数据库获取数据, 生成源代码...")
for id in tqdm.tqdm(generate_number_set(IDs)):
currentbodystring += generateLaTeXBodyContentfromMariaDB(mycursor,id,configjson)
currentbodystring += "\\end{enumerate}"
bodylist.append(currentbodystring)
bodystring += "\n\n\\newpage\n\n".join(bodylist)
midtime = time.time()
print(f"生成LaTeX文件所花时间: {midtime-starttime:.3f}")
latex_data = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",latex_raw,(notetitle,bodystring)) #替换标题和bodystring
SaveTextFile(latex_data,outputfilepath) #保存.tex文件
if XeLaTeXCompile(outputdir,notetitle+".tex"):
print("编译成功")
else:
print("编译失败")
endtime = time.time()
print(f"生成pdf文件所花时间: {endtime-midtime:.3f}")
self.close()
startfile(self.lineEdit_path.text())
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()