-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.py
97 lines (81 loc) · 3.35 KB
/
MainWindow.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
92
93
94
95
96
from PyQt5.QtGui import QTextDocumentWriter
from MainWindowUi import Ui_MainWindow
from PyQt5.QtCore import QDate, pyqtSlot, QFile, qDebug, QTextStream, QByteArray
from PyQt5.QtWidgets import QMainWindow, QMessageBox
class MainWindow(QMainWindow):
DB_PATH = "db"
FILE_NAME_PATTERN = "yyyy-MM-dd"
FILE_FORMAT = "diary"
def __init__(self):
super().__init__()
self.current_document_name = ""
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.open_document(self.file_name_by_date(QDate.currentDate()))
@pyqtSlot(QDate)
def on_calendarView_clicked(self, date):
self.open_document(self.file_name_by_date(date))
def open_document(self, name):
self.close_document()
self.ui.documentView.clear()
self.current_document_name = name
if not QFile.exists(name):
return
reader = DocumentReader(name)
reader.read(self.ui.documentView.document())
if reader.is_error:
QMessageBox.warning(self, "Ошибка",
"При попытке сохранить файл дневника произошла ошибка: " + reader.errorString)
def close_document(self):
qDebug("MainWindow.close_document()")
qDebug("self.current_document_name is \"" + self.current_document_name + "\"")
if len(self.current_document_name) == 0:
qDebug("file not opened. finished")
return
doc = self.ui.documentView.document()
if doc.isEmpty():
qDebug("document is empty. finished")
self.current_document_name = ""
return
qDebug("writing document")
writer = DocumentWriter(self.current_document_name)
writer.write(doc)
if writer.is_error:
QMessageBox.warning(self, "Ошибка",
"При попытке сохранить файл дневника произошла ошибка: " + writer.errorString)
def file_name_by_date(self, date):
return self.DB_PATH + "/" + date.toString(self.FILE_NAME_PATTERN) + "." + self.FILE_FORMAT
class DocumentWriter:
def __init__(self, filename):
self.file = QFile()
self.file.setFileName(filename)
self.is_error = False
self.errorString = ""
if not self.file.open(QFile.WriteOnly):
error = self.file.errorString()
qDebug("cannot open file \"{1}\"".format(error))
self.is_error = True
self.errorString = error
def write(self, document):
if not self.file.isOpen():
return
writer = QTextDocumentWriter()
writer.setFormat(QByteArray().append("plaintext"))
writer.setDevice(self.file)
writer.write(document)
class DocumentReader:
def __init__(self, filename):
self.file = QFile()
self.file.setFileName(filename)
self.is_error = False
self.errorString = ""
if not self.file.open(QFile.ReadOnly):
error = self.file.errorString()
qDebug("cannot open file \"{1}\"".format(error))
self.is_error = True
self.errorString = error
def read(self, document):
if not self.file.isOpen():
return
text = str(self.file.readAll().data(), encoding='utf-8')
document.setPlainText(text)