-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
127 lines (108 loc) · 3.88 KB
/
editor.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
#!/usr/bin/env python
# a minimal text editor to demo PyQt5
# GNU All-Permissive License
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
import sys
import os
import pickle
from PyQt5 import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class TextEdit(QMainWindow):
def __init__(self):
super(TextEdit, self).__init__()
#font = QFont("Courier", 11)
#self.setFont(font)
self.filename = False
self.Ui()
def Ui(self):
quitApp = QAction(QIcon('/usr/share/icons/breeze-dark/actions/32/application-exit.svg'), 'Quit', self)
saveFile = QAction(QIcon('/usr/share/icons/breeze-dark/actions/32/document-save.svg'), 'Save', self)
newFile = QAction('New', self)
openFile = QAction('Open', self)
copyText = QAction('Copy', self)
pasteText = QAction('Yank', self)
newFile.setShortcut('Ctrl+N')
newFile.triggered.connect(self.newFile)
openFile.setShortcut('Ctrl+O')
openFile.triggered.connect(self.openFile)
saveFile.setShortcut('Ctrl+S')
saveFile.triggered.connect(self.saveFile)
quitApp.setShortcut('Ctrl+Q')
quitApp.triggered.connect(self.close)
copyText.setShortcut('Ctrl+K')
copyText.triggered.connect(self.copyFunc)
pasteText.setShortcut('Ctrl+Y')
pasteText.triggered.connect(self.pasteFunc)
menubar = self.menuBar()
menubar.setNativeMenuBar(True)
menuFile = menubar.addMenu('&File')
menuFile.addAction(newFile)
menuFile.addAction(openFile)
menuFile.addAction(saveFile)
menuFile.addAction(quitApp)
menuEdit = menubar.addMenu('&Edit')
menuEdit.addAction(copyText)
menuEdit.addAction(pasteText)
toolbar = self.addToolBar('Toolbar')
toolbar.addAction(quitApp)
toolbar.addAction(saveFile)
self.text = QTextEdit(self)
self.setCentralWidget(self.text)
self.setMenuWidget(menubar)
self.setMenuBar(menubar)
self.setGeometry(200,200,480,320)
self.setWindowTitle('TextEdit')
self.show()
def copyFunc(self):
self.text.copy()
def pasteFunc(self):
self.text.paste()
def unSaved(self):
destroy = self.text.document().isModified()
print(destroy)
if destroy == False:
return False
else:
detour = QMessageBox.question(self,
"Hold your horses.",
"File has unsaved changes. Save now?",
QMessageBox.Yes|QMessageBox.No|
QMessageBox.Cancel)
if detour == QMessageBox.Cancel:
return True
elif detour == QMessageBox.No:
return False
elif detour == QMessageBox.Yes:
return self.saveFile()
return True
def saveFile(self):
self.filename = QFileDialog.getSaveFileName(self, 'Save File', os.path.expanduser('~'))
f = self.filename[0]
with open(f, "w") as CurrentFile:
CurrentFile.write(self.text.toPlainText() )
CurrentFile.close()
def newFile(self):
if not self.unSaved():
self.text.clear()
def openFile(self):
filename, _ = QFileDialog.getOpenFileName(self, "Open File", '', "All Files (*)")
try:
self.text.setText(open(filename).read())
except:
True
def closeEvent(self, event):
if self.unSaved():
event.ignore()
else:
exit
def main():
app = QApplication(sys.argv)
editor = TextEdit()
sys.exit(app.exec_())
if __name__ == '__main__':
main()