forked from Esukhia/dakje-desktop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BasicEditor.py
178 lines (139 loc) · 5.64 KB
/
BasicEditor.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QKeySequence, QTextDocumentWriter, QFont
from PyQt5.QtWidgets import QMainWindow, QMessageBox, \
QFileDialog, QAction, QApplication, QMenuBar, QPlainTextEdit
class BasicEditor(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.createActions()
self.setupTextEdit()
self.initUI()
self.filename = None
def initUI(self):
self.setupMenus()
self.setupToolBar()
self.setupLayout()
self.setWindowTitle("Untitled - Editor")
self.setWindowIcon(QIcon("tab1.png"))
self.setWindowState(Qt.WindowMaximized)
self.resize(1200, 480)
def setupTextEdit(self):
font = QFont()
# font.setFamily('Noto Sans Tibetan')
font.setFixedPitch(True)
font.setPointSize(15)
self.textEdit = QPlainTextEdit()
self.textEdit.setFont(font)
self.textEdit.redoAvailable.connect(self.redoAction.setEnabled)
self.textEdit.undoAvailable.connect(self.undoAction.setEnabled)
def setupMenus(self):
self.menu = self.menuBar()
# File
fileMenu = self.menu.addMenu("&File")
fileMenu.addAction(self.newFileAction)
fileMenu.addAction(self.openFileAction)
fileMenu.addAction(self.saveFileAction)
fileMenu.addAction(self.actionQuit)
# Edit
editMenu = self.menu.addMenu("&Edit")
editMenu.addAction(self.undoAction)
editMenu.addAction(self.redoAction)
# Settings
helpMenu = self.menu.addMenu("&Help")
helpMenu.addAction("About &Qt", QApplication.instance().aboutQt)
self.menuBarRight = QMenuBar(self.menu)
self.menu.setCornerWidget(self.menuBarRight, Qt.TopRightCorner)
def setupToolBar(self):
self.toolbar = self.addToolBar("toolbar")
self.toolbar.addAction(self.newFileAction)
self.toolbar.addAction(self.openFileAction)
self.toolbar.addAction(self.saveFileAction)
self.toolbar.addSeparator()
self.toolbar.addAction(self.undoAction)
self.toolbar.addAction(self.redoAction)
def setupLayout(self):
self.setCentralWidget(self.textEdit)
def createActions(self, BASE_DIRECTORY):
### New File ###
self.newFileAction = QAction(
QIcon(os.path.join(BASE_DIRECTORY, 'files/filenew.png')),
"&New...", self, )
self.newFileAction.setShortcut(QKeySequence.New)
self.newFileAction.setStatusTip("Create a new file")
self.newFileAction.triggered.connect(self.newFile)
### Open File ###
self.openFileAction = QAction(
QIcon(os.path.join(BASE_DIRECTORY, 'files/fileopen.png')),
"&Open...", self)
self.openFileAction.setShortcut(QKeySequence.Open)
self.openFileAction.setStatusTip("Open a text file")
self.openFileAction.triggered.connect(self.openFile)
### Save File ###
self.saveFileAction = QAction(
QIcon(os.path.join(BASE_DIRECTORY, 'files/filesave.png')),
"&Save...", self, )
self.saveFileAction.setShortcut(QKeySequence.Save)
self.saveFileAction.setStatusTip("Save the current document")
self.saveFileAction.triggered.connect(self.saveFile)
### Undo ###
self.undoAction = QAction(
QIcon(os.path.join(BASE_DIRECTORY, 'files/editundo.png')),
"&Undo", self)
self.undoAction.setShortcut(QKeySequence.Undo)
self.undoAction.setStatusTip("Undo the last editing action")
self.undoAction.setEnabled(False)
self.undoAction.triggered.connect(self.undo)
### Redo ###
self.redoAction = QAction(
QIcon(os.path.join(BASE_DIRECTORY, 'files/editredo.png')),
"&Redo", self)
self.redoAction.setShortcut(QKeySequence.Redo)
self.redoAction.setStatusTip("Redo the last editing action")
self.redoAction.setEnabled(False)
self.redoAction.triggered.connect(self.redo)
### Quit ###
self.actionQuit = QAction("&Quit", self)
self.actionQuit.setShortcut(QKeySequence.Open)
self.actionQuit.setStatusTip("Ctrl+Q")
self.actionQuit.triggered.connect(self.close)
def about(self):
QMessageBox.about(self, "About PyTib Editor",
"rules using regular expressions.</p>")
def newFile(self):
self.textEdit.clear()
def openFile(self):
self.filename, ok = QFileDialog.getOpenFileName(
self, 'Open File', ".", "UTF-8 (*.txt)")
if self.filename:
with open(self.filename, "r", encoding='utf-8') as f:
self.textEdit.setPlainText(f.read())
def saveFile(self):
if not self.filename:
self.filename, ok = QFileDialog.getSaveFileName(
self, "Choose a file name", '.', "UTF-8 (*.txt)")
if not ok:
return
if not self.filename:
return
self.statusBar().showMessage("Saved '%s'" % self.filename, 2000)
writer = QTextDocumentWriter(self.filename)
success = writer.write(self.textEdit.document())
if not success:
QMessageBox.question(self, 'Cancel', 'Saving Failed',
QMessageBox.Yes)
return
def undo(self):
self.textEdit.document().undo()
def redo(self):
self.textEdit.document().redo()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = BasicEditor()
window.show()
try:
sys.exit(app.exec_())
except:
print("exiting")
sys.exit(1)