-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_window.py
255 lines (206 loc) · 9.01 KB
/
save_window.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from PyQt5.QtWidgets import (QApplication, QWidget, QMessageBox, QGridLayout, QFormLayout,
QLabel, QLineEdit, QPushButton, QCheckBox, QComboBox, QFileSystemModel, QTreeView, QSizePolicy)
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QDir
from PyQt5.QtPrintSupport import QPrinter
from permissions import check_permission, add_permission
from encrypt_file import encrypt_file
import os
import re
# The window for saving a file
class SaveWindow(QWidget):
def __init__(self, mainWindow):
super().__init__()
self.setWindowTitle('Save As')
self.resize(1600, 800)
self.oldFile = ''
# Get MainWindow through constructor
self.mainWindow = mainWindow
self.closeOnSave = False
self.textEdit = mainWindow.centralWidget.textBox
layout = QFormLayout()
self.fileModel = QFileSystemModel()
self.fileModel.setRootPath(
QDir.currentPath() + '/users/' + self.mainWindow.user)
filters = ['*.txt']
self.fileModel.setNameFilters(filters)
self.fileTree = QTreeView()
self.fileTree.setModel(self.fileModel)
self.fileTree.setRootIndex(
self.fileModel.index(QDir.currentPath() + '/users/' + self.mainWindow.user))
self.fileTree.setColumnWidth(0, 500)
self.fileTree.doubleClicked.connect(self.doubleClickEvent)
self.fileTree.clicked.connect(self.selectionChanged)
layout.addRow(self.fileTree)
self.fnLineEdit = QLineEdit()
layout.addRow('File name: ', self.fnLineEdit)
self.typeComboBox = QComboBox()
self.typeComboBox.addItem('Text Files (*.txt)')
self.typeComboBox.addItem('PDF Files (*.pdf)')
layout.addRow('Save as type: ', self.typeComboBox)
gridLayout = QGridLayout()
saveButton = QPushButton('Save')
saveButton.clicked.connect(self.saveEvent)
saveButton.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum)
gridLayout.addWidget(saveButton, 0, 0)
cancelButton = QPushButton('Cancel')
cancelButton.clicked.connect(self.closeWindow)
cancelButton.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum)
gridLayout.addWidget(cancelButton, 0, 1)
layout.addRow(gridLayout)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setLayout(layout)
# Signal event when the user makes a selection in the QTreeView
def selectionChanged(self):
current = self.fileTree.currentIndex()
removePath = QDir.currentPath() + '/users/' + self.mainWindow.user + '/'
redata = re.compile(re.escape(removePath), re.IGNORECASE)
subPath = redata.sub('', self.fileModel.filePath(current))
if os.path.isdir(self.fileModel.filePath(current)):
subPath += '/'
self.fnLineEdit.setText(subPath)
# Displays confirm save message to the user
def confirmMessageBox(self, fileName):
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText(fileName + ' already exists.\nDo you want to replace it?')
msg.setWindowTitle('Confirm Save As')
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msg.buttonClicked.connect(self.confirmMessageEvent)
self.confirmResponse = False
msg.exec_()
# Displays generic save message to the user
def saveMessageBox(self, text):
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(text)
msg.setWindowTitle('Save Info')
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
# Handles the response to the confirm message box
def confirmMessageEvent(self, button):
if button.text() == '&Yes':
self.confirmResponse = True
# Skip saving the file
elif button.text() == '&No':
self.confirmResponse = False
else:
self.confirmResponse = False
# Closes the save window
def closeWindow(self):
# Restore old file from Save As event if window closed
if self.oldFile != '':
self.mainWindow.currentFile = self.oldFile
self.oldFile = ''
self.close()
# Overrides the QWidget show event
def showEvent(self, event):
filePath = self.oldFile.replace('users/', '')
self.fnLineEdit.setText(filePath)
# Overrides the QWidget close event
def closeEvent(self, event):
self.mainWindow.saveWindow = None
# Handles double clicking the QTreeView folders/files
def doubleClickEvent(self):
if self.fileModel.isDir(self.fileTree.currentIndex()):
return
self.saveEvent()
# Called when the user clicks the 'Save' button
def saveEvent(self):
# Save as new file
if self.mainWindow.currentFile == '':
fileName, extension = os.path.splitext(
os.path.basename(self.fnLineEdit.text().strip()))
subPath = self.fnLineEdit.text().strip().replace(fileName + extension, '')
filePath = 'users/' + self.mainWindow.user + '/' + subPath + fileName
# File name cannot be blank
if self.fnLineEdit.text().strip() == '' or fileName.strip() == '':
self.saveMessageBox('Please enter a file name.')
return
# Save text file
if self.typeComboBox.currentIndex() == 0:
filePath += '.txt'
# Check if file already exists
if os.path.exists(filePath):
self.confirmMessageBox(fileName + '.txt')
if not self.confirmResponse:
return
# Attempt to save file
if not self.saveFile(filePath):
self.saveMessageBox('Invalid file path.')
return
self.mainWindow.currentFile = filePath
self.mainWindow.statusBar().showMessage('File saved.')
self.mainWindow.needsSave = False
self.mainWindow.window_title = f'Notepad App - {os.path.basename(filePath)}'
self.mainWindow.setWindowTitle(self.mainWindow.window_title)
self.mainWindow.Edited = False
add_permission(self.mainWindow.user, filePath)
# Save PDF
else:
filePath += '.pdf'
# Check if file already exists
if os.path.exists(filePath):
self.confirmMessageBox(fileName + '.pdf')
if not self.confirmResponse:
return
# Run through Save As button; reset
if self.oldFile != '':
self.mainWindow.currentFile = self.oldFile
self.oldFile = ''
# Attempt to save file
if not self.savePDF(filePath):
self.saveMessageBox('Invalid file path.')
return
add_permission(self.mainWindow.user, filePath)
# Close save window
self.close()
# Save working file
else:
self.saveFile(self.mainWindow.currentFile)
self.mainWindow.statusBar().showMessage('File saved.')
self.mainWindow.needsSave = False
self.mainWindow.window_title = f'Notepad App - {os.path.basename(self.mainWindow.currentFile)}'
self.mainWindow.setWindowTitle(self.mainWindow.window_title)
self.mainWindow.Edited = False
add_permission(self.mainWindow.user, self.mainWindow.currentFile)
# Save before adding a new user to the current file
if self.mainWindow.addUserAfterSave:
self.mainWindow.addUserAfterSave = False
self.mainWindow.addUserEvent()
# Close the application after save if needed
if self.closeOnSave:
QtCore.QCoreApplication.exit(0)
# Called when the user clicks the 'Save As' button
def saveAsEvent(self):
self.oldFile = self.mainWindow.currentFile
self.mainWindow.currentFile = ''
self.show()
# Decides whether to open the save window or not when the user saves
def initSaveEvent(self):
if self.mainWindow.currentFile == '':
self.show()
else:
self.saveEvent()
self.close()
# Creates a new file or opens an existing one and saves the QTextEdit text
def saveFile(self, filePath):
try:
f = open(filePath, 'wb')
encrypted = encrypt_file(self.textEdit.toHtml())
f.write(encrypted)
f.close()
return True
except:
return False
# Saves the file as a PDF
def savePDF(self, filePath):
try:
printer = QPrinter(QPrinter.HighResolution)
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(filePath)
self.mainWindow.centralWidget.textBox.document().print_(printer)
return True
except:
return False