-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain_complete.py
310 lines (238 loc) · 10.1 KB
/
main_complete.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
""" Docx
author: ashraf minhaj
mail : ashraf_minhaj@yahoo.com
"""
"""
install -
$ pip install pyqt5
$ pip install docx2txt
"""
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtPrintSupport import *
import docx2txt
class MainApp(QMainWindow):
""" the main class of our app """
def __init__(self):
""" init things here """
super().__init__() # parent class initializer
# window title
self.title = "Google Doc Clone"
self.setWindowTitle(self.title)
# editor section
self.editor = QTextEdit(self)
self.setCentralWidget(self.editor)
# create menubar and toolbar first
self.create_menu_bar()
self.create_toolbar()
# after craeting toolabr we can call and select font size
font = QFont('Times', 12)
self.editor.setFont(font)
self.editor.setFontPointSize(12)
# stores path
self.path = ''
def create_menu_bar(self):
menuBar = QMenuBar(self)
""" add elements to the menubar """
# App icon will go here
app_icon = menuBar.addMenu(QIcon("doc_icon.png"), "icon")
# file menu **
file_menu = QMenu("File", self)
menuBar.addMenu(file_menu)
save_action = QAction('Save', self)
save_action.triggered.connect(self.file_save)
file_menu.addAction(save_action)
open_action = QAction('Open', self)
open_action.triggered.connect(self.file_open)
file_menu.addAction(open_action)
rename_action = QAction('Rename', self)
rename_action.triggered.connect(self.file_saveas)
file_menu.addAction(rename_action)
pdf_action = QAction("Save as PDF", self)
pdf_action.triggered.connect(self.save_pdf)
file_menu.addAction(pdf_action)
# edit menu **
edit_menu = QMenu("Edit", self)
menuBar.addMenu(edit_menu)
# paste
paste_action = QAction('Paste', self)
paste_action.triggered.connect(self.editor.paste)
edit_menu.addAction(paste_action)
# clear
clear_action = QAction('Clear', self)
clear_action.triggered.connect(self.editor.clear)
edit_menu.addAction(clear_action)
# select all
select_action = QAction('Select All', self)
select_action.triggered.connect(self.editor.selectAll)
edit_menu.addAction(select_action)
# view menu **
view_menu = QMenu("View", self)
menuBar.addMenu(view_menu)
# fullscreen
fullscr_action = QAction('Full Screen View', self)
fullscr_action.triggered.connect(lambda : self.showFullScreen())
view_menu.addAction(fullscr_action)
# normal screen
normscr_action = QAction('Normal View', self)
normscr_action.triggered.connect(lambda : self.showNormal())
view_menu.addAction(normscr_action)
# minimize
minscr_action = QAction('Minimize', self)
minscr_action.triggered.connect(lambda : self.showMinimized())
view_menu.addAction(minscr_action)
self.setMenuBar(menuBar)
def create_toolbar(self):
# Using a title
ToolBar = QToolBar("Tools", self)
# undo
undo_action = QAction(QIcon("undo.png"), 'Undo', self)
undo_action.triggered.connect(self.editor.undo)
ToolBar.addAction(undo_action)
# redo
redo_action = QAction(QIcon("redo.png"), 'Redo', self)
redo_action.triggered.connect(self.editor.redo)
ToolBar.addAction(redo_action)
# adding separator
ToolBar.addSeparator()
# copy
copy_action = QAction(QIcon("copy.png"), 'Copy', self)
copy_action.triggered.connect(self.editor.copy)
ToolBar.addAction(copy_action)
# cut
cut_action = QAction(QIcon("cut.png"), 'Cut', self)
cut_action.triggered.connect(self.editor.cut)
ToolBar.addAction(cut_action)
# paste
paste_action = QAction(QIcon("paste.png"), 'Paste', self)
paste_action.triggered.connect(self.editor.paste)
ToolBar.addAction(paste_action)
# adding separator
ToolBar.addSeparator()
ToolBar.addSeparator()
# fonts
self.font_combo = QComboBox(self)
self.font_combo.addItems(["Courier Std", "Hellentic Typewriter Regular", "Helvetica", "Arial", "SansSerif", "Helvetica", "Times", "Monospace"])
self.font_combo.activated.connect(self.set_font) # connect with function
ToolBar.addWidget(self.font_combo)
# font size
self.font_size = QSpinBox(self)
self.font_size.setValue(12)
self.font_size.valueChanged.connect(self.set_font_size) # connect with funcion
ToolBar.addWidget(self.font_size)
# separator
ToolBar.addSeparator()
# bold
bold_action = QAction(QIcon("bold.png"), 'Bold', self)
bold_action.triggered.connect(self.bold_text)
ToolBar.addAction(bold_action)
# underline
underline_action = QAction(QIcon("underline.png"), 'Underline', self)
underline_action.triggered.connect(self.underline_text)
ToolBar.addAction(underline_action)
# italic
italic_action = QAction(QIcon("italic.png"), 'Italic', self)
italic_action.triggered.connect(self.italic_text)
ToolBar.addAction(italic_action)
# separator
ToolBar.addSeparator()
# text alignment
right_alignment_action = QAction(QIcon("right-align.png"), 'Align Right', self)
right_alignment_action.triggered.connect(lambda : self.editor.setAlignment(Qt.AlignRight))
ToolBar.addAction(right_alignment_action)
left_alignment_action = QAction(QIcon("left-align.png"), 'Align Left', self)
left_alignment_action.triggered.connect(lambda : self.editor.setAlignment(Qt.AlignLeft))
ToolBar.addAction(left_alignment_action)
justification_action = QAction(QIcon("justification.png"), 'Center/Justify', self)
justification_action.triggered.connect(lambda : self.editor.setAlignment(Qt.AlignCenter))
ToolBar.addAction(justification_action)
# separator
ToolBar.addSeparator()
# zoom in
zoom_in_action = QAction(QIcon("zoom-in.png"), 'Zoom in', self)
zoom_in_action.triggered.connect(self.editor.zoomIn)
ToolBar.addAction(zoom_in_action)
# zoom out
zoom_out_action = QAction(QIcon("zoom-out.png"), 'Zoom out', self)
zoom_out_action.triggered.connect(self.editor.zoomOut)
ToolBar.addAction(zoom_out_action)
# separator
ToolBar.addSeparator()
self.addToolBar(ToolBar)
def italic_text(self):
# if already italic, change into normal, else italic
state = self.editor.fontItalic()
self.editor.setFontItalic(not(state))
def underline_text(self):
# if already underlined, change into normal, else underlined
state = self.editor.fontUnderline()
self.editor.setFontUnderline(not(state))
def bold_text(self):
# if already bold, make normal, else make bold
if self.editor.fontWeight() != QFont.Bold:
self.editor.setFontWeight(QFont.Bold)
return
self.editor.setFontWeight(QFont.Normal)
def set_font(self):
font = self.font_combo.currentText()
self.editor.setCurrentFont(QFont(font))
def set_font_size(self):
value = self.font_size.value()
self.editor.setFontPointSize(value)
# we can also make it one liner without writing such function.
# by using lamba function -
# self.font_size.valueChanged.connect(self.editor.setFontPointSize(self.font_size.value()))
def file_open(self):
self.path, _ = QFileDialog.getOpenFileName(self, "Open file", "", "Text documents (*.text);Text documents (*.txt);All files (*.*)")
try:
#with open(self.path, 'r') as f:
# text = f.read()
text = docx2txt.process(self.path) # docx2txt
#doc = Document(self.path) # if using docx
#text = ''
#for line in doc.paragraphs:
# text += line.text
except Exception as e:
print(e)
else:
self.editor.setText(text)
self.update_title()
def file_save(self):
print(self.path)
if self.path == '':
# If we do not have a path, we need to use Save As.
self.file_saveas()
text = self.editor.toPlainText()
try:
with open(self.path, 'w') as f:
f.write(text)
self.update_title()
except Exception as e:
print(e)
def file_saveas(self):
self.path, _ = QFileDialog.getSaveFileName(self, "Save file", "", "text documents (*.text);Text documents (*.txt);All files (*.*)")
if self.path == '':
return # If dialog is cancelled, will return ''
text = self.editor.toPlainText()
try:
with open(path, 'w') as f:
f.write(text)
self.update_title()
except Exception as e:
print(e)
def update_title(self):
self.setWindowTitle(self.title + ' ' + self.path)
def save_pdf(self):
f_name, _ = QFileDialog.getSaveFileName(self, "Export PDF", None, "PDF files (.pdf);;All files()")
print(f_name)
if f_name != '': # if name not empty
printer = QPrinter(QPrinter.HighResolution)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(f_name)
self.editor.document().print_(printer)
app = QApplication(sys.argv)
window = MainApp()
window.show()
sys.exit(app.exec_())