-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.py
More file actions
129 lines (106 loc) · 5 KB
/
File.py
File metadata and controls
129 lines (106 loc) · 5 KB
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
from PyQt6.QtCore import QFileInfo
from PyQt6.QtGui import QIcon, QAction
from PyQt6.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
from PyQt6.QtWidgets import QFileDialog
class File(object):
def __init__(self, window_f, text_widget, inits):
super().__init__()
self.inits = inits
self.supported_types = ['txt', 'html']
self.window = window_f
self.text_widget = text_widget
window_f.file_menu = window_f.menuBar().addMenu("File")
# Add option to save the file
window_f.save_file = QAction(QIcon("icons/diskette.png"), 'Save File', window_f)
window_f.save_file.setShortcut("Ctrl+S")
window_f.save_file.triggered.connect(self.save)
window_f.file_menu.addAction(window_f.save_file)
# Add option to open a file
window_f.open_file = QAction('Open File', window_f)
window_f.open_file.setIcon(QIcon('icons/document.png'))
window_f.open_file.setShortcut("Ctrl+O")
window_f.open_file.triggered.connect(self.open)
window_f.file_menu.addAction(window_f.open_file)
# Add option to open a new window
window_f.new_box = QAction('New Window', window_f)
window_f.new_box.setShortcut("Ctrl+N")
window_f.new_box.triggered.connect(self.new_box)
window_f.file_menu.addAction(window_f.new_box)
# Add option to print a file
window_f.print_file = QAction(QIcon("print.png"), 'Print', window_f)
window_f.print_file.setIcon(QIcon('icons/printer.png'))
window_f.print_file.setShortcut("Ctrl+P")
window_f.print_file.triggered.connect(self.print)
window_f.file_menu.addAction(window_f.print_file)
# Add option for print preview
window_f.print_preview = QAction(QIcon("printprev.png"), 'Print Preview', window_f)
window_f.print_preview.triggered.connect(self.preview)
window_f.file_menu.addAction(window_f.print_preview)
# Add option to export to pdf
window_f.exportPDF = QAction(QIcon("pdf.png"), 'PDF Export', window_f)
window_f.exportPDF.setIcon(QIcon('icons/export.png'))
window_f.exportPDF.triggered.connect(self.export_pdf)
window_f.file_menu.addAction(window_f.exportPDF)
def new_box(self):
self.window_f = self.inits.make_main_window()
self.window_f.show()
return self.window_f
def save(self):
buffer = self.text_widget.toHtml()
if buffer:
file_dialog = QFileDialog(self.window)
# convert list of supported types to a string that conforms to the search format
supported_types_string = '*.' + ';;*.'.join(self.supported_types)
file_name = file_dialog.getSaveFileName(self.window, 'Save File', '', supported_types_string)
if file_name != ('', ''):
f = open(file_name[0], 'w')
if f.closed:
return
f.write(buffer)
f.close()
def open(self, file_name=None):
# I don't really understand why file_name would be False
# instead of none, but apparently that's how it is
if file_name is False:
file_dialog = QFileDialog(self.window)
# convert list of supported types to a string that conforms to the search format
supported_types_string = '*.' + ';;*.'.join(self.supported_types)
file_name, _ = file_dialog.getOpenFileName(self.window, 'Open File', supported_types_string)
if file_name:
try:
file = open(file_name, 'r')
except:
return False
if file.closed:
return
self.editor()
with file:
text = file.read()
self.text_widget.setText(text)
return True
def editor(self):
self.text_widget = self.inits.make_text_widget(self.window)
self.window.setCentralWidget(self.text_widget)
def print(self):
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
print_dialog = QPrintDialog(printer)
if print_dialog.exec():
self.text_widget.document().print(print_dialog.printer())
def preview(self):
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
file_dialog = QPrintPreviewDialog(printer)
file_dialog.paintRequested.connect(self.paint_preview)
file_dialog.exec()
def paint_preview(self, printer):
self.text_widget.document().print(printer)
def export_pdf(self):
fn, _ = QFileDialog.getSaveFileName(self.text_widget, "Export PDF", None, "PDF Files (.pdf);;All Files()")
if fn != '':
if QFileInfo(fn).suffix() == "":
fn += '.pdf'
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
printer.setOutputFileName(fn)
print(self.text_widget.toHtml)
print(printer)
self.text_widget.document().print(printer)