from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import os




# 定义询问字符文件
QUEST_FILE = "quest_info.txt"


def load_info():
    if os.path.exists(QUEST_FILE):
        with open(QUEST_FILE,"r") as f:
            return f.read()
    return ""
def save_info(text:str):
    with open(QUEST_FILE,'+w') as f:
        print("save:",text)
        f.write(text)

class quest_text(QObject):
    def __init__(self,title:str):
        QObject.__init__(self)
        self.app = QApplication(sys.argv)
        self.w=QDialog()
        self.w.resize(800,400)
        self.w.setWindowTitle(title)
        self.w.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
        self.w.setWindowModality(Qt.WindowModality.ApplicationModal)
        self.text=load_info()
        self.text_edit = QTextEdit(self.w)
        self.text_edit.setObjectName(u"text")
        self.text_edit.setGeometry(QRect(20, 20, 760, 320))
        self.text_edit.setFrameShape(QFrame.Shape.Box)
        self.text_edit.setMidLineWidth(1)
        self.text_edit.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
        self.text_edit.setPlainText(self.text)
        self.text_edit.moveCursor(QTextCursor.MoveOperation.End, QTextCursor.MoveMode.MoveAnchor)
        self.ok_but_init()
        self.cancel_but_init()

    def ok_but_init(self):
        self.ok_but=QPushButton(self.w)
        self.ok_but.setObjectName(u"ok_but")
        self.ok_but.setGeometry(QRect(700, 350, 93, 28))
        self.ok_but.clicked.connect(self.ok_but_clicked)
        self.ok_but.setText("确认")
        self.ok_but.setCheckable(True)
        self.ok_but.setChecked(True)
        self.ok_state=False

    def cancel_but_init(self):
        self.cancel_but=QPushButton(self.w)
        self.cancel_but.setObjectName(u"cancel_but")
        self.cancel_but.setGeometry(QRect(600, 350, 93, 28))
        self.cancel_but.clicked.connect(self.cancel_but_clicked)
        self.cancel_but.setText("取消")
        self.ok_but.setCheckable(True)
        self.ok_state=False

    def ok_but_clicked(self):
        self.text=self.text_edit.toPlainText()
        self.ok_state=True
        self.w.done(QDialog.DialogCode.Accepted)
        self.w.close()
    
    def cancel_but_clicked(self):
        self.text=self.text_edit.toPlainText()
        self.ok_state=False
        self.w.done(QDialog.DialogCode.Accepted)
        self.w.close()

    def item_clicked(self,item:QListWidgetItem ):
        self.select_item=item.text()
        self.w.done(QDialog.DialogCode.Accepted)
        self.w.close()
    def show(self):
        # if(self.w.exec()==QDialog.DialogCode.Accepted):
            # return self.ok_state,self.text
        self.w.show()
        a=self.app.exec()
        # sys.exit()
        save_info(self.text)
        return self.ok_state,self.text






if __name__=="__main__":
    q=quest_text("输入本次提交的描述")
    ack,text=q.show()
    print("ack=",ack,"text=",text)
