-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedactorModel.py
134 lines (109 loc) · 4.31 KB
/
RedactorModel.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from PyQt5.QtGui import QFont, QTextCursor, QTextCharFormat
from T9 import T9
from RedactorUtility import FileOpener
class RedactorModel:
def __init__(self):
self.text = None
self.font = None
self.font_size = 14
self.current_char_format = QTextCharFormat()
self.view = None
self.file_name = "Безымянный.red"
self.file_path = ""
self.is_saved = True
def open_file(self, new_file_path):
with FileOpener(new_file_path, 'r') as file:
self.view.text_edit.clear()
self.view.text_edit.setText(file.read())
self.is_saved = True
self.file_name = new_file_path.split('/')[-1]
self.file_path = new_file_path
if not new_file_path.endswith(".red"):
self.is_saved = False
self.file_path = ''
self.file_name += '.red'
def save_current_file(self):
if self.is_saved:
return
try:
with open(self.file_path, 'w') as file:
file.write(self.view.text_edit.toHtml())
self.is_saved = True
except FileNotFoundError:
self.save_as_current_file()
def save_as_current_file(self):
new_file_path = self.view.get_save_file_name()
if new_file_path:
self.file_path = new_file_path
self.file_name = new_file_path.split('/')[-1]
try:
with open(self.file_path, 'w') as file:
file.write(self.view.text_edit.toHtml())
self.is_saved = True
except FileNotFoundError:
self.view.show_error()
def set_text_changed(self):
self.is_saved = False
self.text = self.view.text_edit.toPlainText()
if len(self.text) == 0:
self._change_default_font()
def change_font(self, new_font):
self.font = new_font
self.view.text_edit.setCurrentFont(self.font)
self.view.text_edit.setFontPointSize(self.font_size)
def change_font_size(self, size):
self.font_size = int(size)
self.view.text_edit.setFontPointSize(self.font_size)
def _change_default_font(self):
if self.font is None:
self.font = self.view.text_edit.font()
self.view.text_edit.setStyleSheet("font: {0}pt \"{1}\";"
.format(self.font_size,
self.font.family()))
def change_font_color(self, color):
self.view.text_edit.setTextColor(color)
def set_font_italic(self):
self.view.text_edit.setFontItalic(not self.view.text_edit.fontItalic())
def set_font_underline(self):
self.view.text_edit.setFontUnderline(
not self.view.text_edit.fontUnderline())
def set_font_bold(self):
self.view.text_edit.setFontWeight(
QFont.Normal
if self.view.text_edit.fontWeight() == QFont.Bold
else QFont.Bold)
def set_font_strike(self):
tmp = self.view.text_edit.currentCharFormat()
tmp.setFontStrikeOut(not tmp.fontStrikeOut())
self.view.text_edit.setCurrentCharFormat(tmp)
def update_t9_buttons(self):
cursor = self.view.text_edit.textCursor()
cursor.select(QTextCursor.WordUnderCursor)
word = cursor.selectedText()
matches = T9.get_similar_words(word, self.view.t9_buttons_count)
while len(matches) < self.view.t9_buttons_count:
matches.append('')
self.view.main_form.update_buttons(matches)
def change_word_under_cursor(self, word):
cursor = self.view.text_edit.textCursor()
self.current_char_format = cursor.charFormat()
cursor.select(QTextCursor.WordUnderCursor)
cursor.removeSelectedText()
cursor.insertText(word, self.current_char_format)
self.view.text_edit.setFocus()
def cut(self):
self.view.text_edit.cut()
def copy(self):
self.view.text_edit.copy()
def paste(self):
self.view.text_edit.paste()
def undo(self):
self.view.text_edit.undo()
def redo(self):
self.view.text_edit.redo()
def add_observer(self, observer):
self.view = observer
self._change_default_font()
def notify_observers(self):
for x in self.view:
x.modelIsChanged()