Skip to content

Commit ab91837

Browse files
committed
Notepad Application
1 parent fe964b6 commit ab91837

File tree

3 files changed

+186
-28
lines changed

3 files changed

+186
-28
lines changed

Section-03/NotePad.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,32 @@ def setupUi(self, MainWindow):
5454
icon2.addPixmap(QtGui.QPixmap("images/save.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
5555
self.actionSave.setIcon(icon2)
5656
self.actionSave.setObjectName("actionSave")
57+
# self.actionPrint = QtGui.QAction(parent=MainWindow)
58+
# self.actionPrint.setObjectName("actionPrint")
59+
# self.actionPrint_Preview = QtGui.QAction(parent=MainWindow)
60+
# self.actionPrint_Preview.setObjectName("actionPrint_Preview")
61+
# self.actionQuit = QtGui.QAction(parent=MainWindow)
62+
# self.actionQuit.setObjectName("actionQuit")
5763
self.actionPrint = QtGui.QAction(parent=MainWindow)
58-
self.actionPrint.setObjectName("actionPrint")
59-
self.actionPrint_Preview = QtGui.QAction(parent=MainWindow)
60-
self.actionPrint_Preview.setObjectName("actionPrint_Preview")
61-
self.actionQuit = QtGui.QAction(parent=MainWindow)
62-
self.actionQuit.setObjectName("actionQuit")
63-
self.actionPrint_2 = QtGui.QAction(parent=MainWindow)
6464
icon3 = QtGui.QIcon()
6565
icon3.addPixmap(QtGui.QPixmap("images/print.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
66-
self.actionPrint_2.setIcon(icon3)
67-
self.actionPrint_2.setObjectName("actionPrint_2")
68-
self.actionPrint_Preview_2 = QtGui.QAction(parent=MainWindow)
66+
self.actionPrint.setIcon(icon3)
67+
self.actionPrint.setObjectName("actionPrint")
68+
self.actionPrint_Preview = QtGui.QAction(parent=MainWindow)
6969
icon4 = QtGui.QIcon()
7070
icon4.addPixmap(QtGui.QPixmap("images/printprev.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
71-
self.actionPrint_Preview_2.setIcon(icon4)
72-
self.actionPrint_Preview_2.setObjectName("actionPrint_Preview_2")
71+
self.actionPrint_Preview.setIcon(icon4)
72+
self.actionPrint_Preview.setObjectName("actionPrint_Preview")
7373
self.actionExport_PDF = QtGui.QAction(parent=MainWindow)
7474
icon5 = QtGui.QIcon()
7575
icon5.addPixmap(QtGui.QPixmap("images/pdf.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
7676
self.actionExport_PDF.setIcon(icon5)
7777
self.actionExport_PDF.setObjectName("actionExport_PDF")
78-
self.actionQuit_2 = QtGui.QAction(parent=MainWindow)
78+
self.actionQuit = QtGui.QAction(parent=MainWindow)
7979
icon6 = QtGui.QIcon()
8080
icon6.addPixmap(QtGui.QPixmap("images/exit.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
81-
self.actionQuit_2.setIcon(icon6)
82-
self.actionQuit_2.setObjectName("actionQuit_2")
81+
self.actionQuit.setIcon(icon6)
82+
self.actionQuit.setObjectName("actionQuit")
8383
self.actionUndo = QtGui.QAction(parent=MainWindow)
8484
icon7 = QtGui.QIcon()
8585
icon7.addPixmap(QtGui.QPixmap("images/undo.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
@@ -159,11 +159,11 @@ def setupUi(self, MainWindow):
159159
self.menuFile.addAction(self.actionOpen)
160160
self.menuFile.addAction(self.actionSave)
161161
self.menuFile.addSeparator()
162-
self.menuFile.addAction(self.actionPrint_2)
163-
self.menuFile.addAction(self.actionPrint_Preview_2)
162+
self.menuFile.addAction(self.actionPrint)
163+
self.menuFile.addAction(self.actionPrint_Preview)
164164
self.menuFile.addAction(self.actionExport_PDF)
165165
self.menuFile.addSeparator()
166-
self.menuFile.addAction(self.actionQuit_2)
166+
self.menuFile.addAction(self.actionQuit)
167167
self.menuEdit.addAction(self.actionUndo)
168168
self.menuEdit.addAction(self.actionRedo)
169169
self.menuEdit.addSeparator()
@@ -188,11 +188,15 @@ def setupUi(self, MainWindow):
188188
self.menubar.addAction(self.menuAbout.menuAction())
189189
self.toolBar.addAction(self.actionNew)
190190
self.toolBar.addAction(self.actionSave)
191+
self.toolBar.addAction(self.actionCut)
192+
self.toolBar.addAction(self.actionCopy)
193+
self.toolBar.addAction(self.actionPaste)
191194
self.toolBar.addAction(self.actionUndo)
192195
self.toolBar.addAction(self.actionRedo)
193-
self.toolBar.addAction(self.actionPrint_2)
194-
self.toolBar.addAction(self.actionPrint_Preview_2)
196+
self.toolBar.addAction(self.actionPrint)
197+
self.toolBar.addAction(self.actionPrint_Preview)
195198
self.toolBar.addAction(self.actionExport_PDF)
199+
196200

197201
self.retranslateUi(MainWindow)
198202
QtCore.QMetaObject.connectSlotsByName(MainWindow)
@@ -211,17 +215,17 @@ def retranslateUi(self, MainWindow):
211215
self.actionOpen.setShortcut(_translate("MainWindow", "Ctrl+O"))
212216
self.actionSave.setText(_translate("MainWindow", "Save"))
213217
self.actionSave.setShortcut(_translate("MainWindow", "Ctrl+S"))
218+
# self.actionPrint.setText(_translate("MainWindow", "Print"))
219+
# self.actionPrint_Preview.setText(_translate("MainWindow", "Print Preview"))
220+
# self.actionQuit.setText(_translate("MainWindow", "Quit"))
214221
self.actionPrint.setText(_translate("MainWindow", "Print"))
222+
self.actionPrint.setShortcut(_translate("MainWindow", "Ctrl+P"))
215223
self.actionPrint_Preview.setText(_translate("MainWindow", "Print Preview"))
216-
self.actionQuit.setText(_translate("MainWindow", "Quit"))
217-
self.actionPrint_2.setText(_translate("MainWindow", "Print"))
218-
self.actionPrint_2.setShortcut(_translate("MainWindow", "Ctrl+P"))
219-
self.actionPrint_Preview_2.setText(_translate("MainWindow", "Print Preview"))
220-
self.actionPrint_Preview_2.setShortcut(_translate("MainWindow", "Ctrl+Shift+P"))
224+
self.actionPrint_Preview.setShortcut(_translate("MainWindow", "Ctrl+Shift+P"))
221225
self.actionExport_PDF.setText(_translate("MainWindow", "Export PDF"))
222226
self.actionExport_PDF.setShortcut(_translate("MainWindow", "Ctrl+E"))
223-
self.actionQuit_2.setText(_translate("MainWindow", "Quit"))
224-
self.actionQuit_2.setShortcut(_translate("MainWindow", "Ctrl+Q"))
227+
self.actionQuit.setText(_translate("MainWindow", "Quit"))
228+
self.actionQuit.setShortcut(_translate("MainWindow", "Ctrl+Q"))
225229
self.actionUndo.setText(_translate("MainWindow", "Undo"))
226230
self.actionUndo.setShortcut(_translate("MainWindow", "Ctrl+Z"))
227231
self.actionRedo.setText(_translate("MainWindow", "Redo"))

Section-03/NotePadApp.py

Lines changed: 156 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
1-
from PyQt6.QtWidgets import QMainWindow, QApplication, QFileDialog, QMessageBox
2-
from PyQt6.QtPrintSupport import QPrinter
1+
from PyQt6.QtWidgets import QMainWindow, QApplication, QFileDialog, QMessageBox, QFontDialog, QColorDialog
2+
from PyQt6.QtPrintSupport import QPrinter, QPrintDialog, QPrintPreviewDialog
33
import sys
4+
from PyQt6.QtCore import QFileInfo, Qt
5+
from PyQt6.QtGui import QFont, QTextCharFormat
46
from NotePad import Ui_MainWindow
57

8+
69
class NotePadWindow(QMainWindow, Ui_MainWindow):
710
def __init__(self):
811
super().__init__()
912
self.setupUi(self)
1013
self.show()
1114

1215
#---------------Signal Connection Section-----------------
16+
#--File Menu--
1317
self.actionSave.triggered.connect(self.save_file)
1418
self.actionNew.triggered.connect(self.file_new)
1519
self.actionOpen.triggered.connect(self.open_file)
20+
self.actionPrint.triggered.connect(self.print_file)
21+
self.actionPrint_Preview.triggered.connect(self.preview_dialog)
22+
self.actionExport_PDF.triggered.connect(self.export_pdf)
23+
self.actionQuit.triggered.connect(self.exit_app)
24+
25+
#--Edit Menu--
26+
self.actionUndo.triggered.connect(self.textEdit.undo)
27+
self.actionRedo.triggered.connect(self.textEdit.redo)
28+
self.actionCopy.triggered.connect(self.textEdit.copy)
29+
self.actionCut.triggered.connect(self.textEdit.cut)
30+
self.actionPaste.triggered.connect(self.textEdit.paste)
31+
32+
#--Format Menu--
33+
self.actionBold.triggered.connect(self.text_bold)
34+
self.actionItalic.triggered.connect(self.text_italic)
35+
self.actionUnderline.triggered.connect(self.text_underline)
36+
37+
self.actionAlign_Left.triggered.connect(self.align_left)
38+
self.actionAlign_Right.triggered.connect(self.align_right)
39+
self.actionCenter.triggered.connect(self.align_center)
40+
self.actionAlign_Justify.triggered.connect(self.align_justify)
41+
42+
self.actionFont.triggered.connect(self.font_dialog)
43+
self.actionColor.triggered.connect(self.color_dialog)
44+
self.actionAbout_App.triggered.connect(self.about)
45+
1646

1747

1848
#------------Maybe Save Function----------------
@@ -45,6 +75,7 @@ def file_new(self):
4575
if self.maybe_save():
4676
self.textEdit.clear()
4777

78+
4879
#------------------Open File Function-----------------------
4980
def open_file(self):
5081
fname = QFileDialog.getOpenFileName(self, "Open File", '/home')
@@ -56,6 +87,129 @@ def open_file(self):
5687

5788
#-----------------Print Function--------------------
5889
# from PyQt6.QtPrintSupport import QPrinter --> inded libraries
90+
def print_file(self):
91+
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
92+
dialog = QPrintDialog(printer)
93+
94+
if dialog.exec() == QPrintDialog.DialogCode.Accepted:
95+
self.textEdit.print(printer)
96+
97+
#--------------Print Preview Function---------------------
98+
# from PyQt6.QtPrintSupport import QPrintPreviewDialog --> loaded libraries
99+
def preview_dialog(self):
100+
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
101+
previewDialog = QPrintPreviewDialog(printer, self)
102+
previewDialog.paintRequested.connect(self.print_preview)
103+
previewDialog.exec()
104+
105+
def print_preview(self, printer):
106+
self.textEdit.print(printer)
107+
108+
#----------------Export PDF Section-------------------
109+
# from PyQt6.QtCore import QFileInfo --> loaded libraries
110+
def export_pdf(self):
111+
fn, _ = QFileDialog.getSaveFileName(self, 'Export PDF', "PDF files (.pdf)")
112+
if fn != "":
113+
if QFileInfo(fn).suffix() == "" :fn += '.pdf'
114+
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
115+
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
116+
printer.setOutputFileName(fn)
117+
self.textEdit.document().print(printer)
118+
119+
#--------------Quit Section---------------------------
120+
def exit_app(self):
121+
self.close()
122+
123+
#--------------Bold Section---------------------------
124+
def text_bold(self):
125+
current_format_bold = self.textEdit.currentCharFormat()
126+
new_format_bold = QTextCharFormat()
127+
128+
# Toggle bold property
129+
new_format_bold.setFontWeight(QFont.Weight.Bold if current_format_bold.fontWeight() == QFont.Weight.Normal else QFont.Weight.Normal)
130+
131+
# Preserve the existing underline and italic properties
132+
new_format_bold.setFontItalic(current_format_bold.fontItalic())
133+
new_format_bold.setFontUnderline(current_format_bold.fontUnderline())
134+
135+
# Apply the modified format to the selected text
136+
cursor = self.textEdit.textCursor()
137+
cursor.mergeCharFormat(new_format_bold)
138+
self.textEdit.setCurrentCharFormat(new_format_bold)
139+
140+
#--------------Italic Section---------------------------
141+
def text_italic(self):
142+
current_format_italic = self.textEdit.currentCharFormat()
143+
new_format_italic = QTextCharFormat()
144+
145+
# Toggle italic property
146+
new_format_italic.setFontItalic(not current_format_italic.fontItalic())
147+
148+
# Preserve the existing underline and bold properties
149+
new_format_italic.setFontWeight(current_format_italic.fontWeight())
150+
new_format_italic.setFontUnderline(current_format_italic.fontUnderline())
151+
152+
# Apply the modified format to the selected text
153+
cursor = self.textEdit.textCursor()
154+
cursor.mergeCharFormat(new_format_italic)
155+
self.textEdit.setCurrentCharFormat(new_format_italic)
156+
157+
#--------------Underline Section---------------------------
158+
def text_underline(self):
159+
current_format_underline = self.textEdit.currentCharFormat()
160+
new_format_underline = QTextCharFormat()
161+
162+
# Toggle underline property
163+
new_format_underline.setFontUnderline(not current_format_underline.fontUnderline())
164+
165+
# Preserve the existing Italic and bold properties
166+
new_format_underline.setFontWeight(current_format_underline.fontWeight())
167+
new_format_underline.setFontItalic(current_format_underline.fontItalic())
168+
169+
170+
# Apply the modified format to the selected text
171+
cursor = self.textEdit.textCursor()
172+
cursor.mergeCharFormat(new_format_underline)
173+
self.textEdit.setCurrentCharFormat(new_format_underline)
174+
175+
#------------Alignment Section-----------------
176+
def align_left(self):
177+
self.textEdit.setAlignment(Qt.AlignmentFlag.AlignLeft)
178+
179+
def align_right(self):
180+
self.textEdit.setAlignment(Qt.AlignmentFlag.AlignRight)
181+
182+
def align_center(self):
183+
self.textEdit.setAlignment(Qt.AlignmentFlag.AlignCenter)
184+
185+
def align_justify(self):
186+
self.textEdit.setAlignment(Qt.AlignmentFlag.AlignJustify)
187+
188+
#----------Font Section------------
189+
def font_dialog(self):
190+
font, ok = QFontDialog.getFont()
191+
192+
if ok:
193+
cursor = self.textEdit.textCursor()
194+
195+
# Preserve the existing formatting of the selected text
196+
current_format = cursor.charFormat()
197+
198+
# Set the new font style
199+
current_format.setFont(font)
200+
201+
# Apply the modified format to the selected text
202+
cursor.mergeCharFormat(current_format)
203+
204+
#-----------Color Section--------------
205+
def color_dialog(self):
206+
color = QColorDialog.getColor()
207+
self.textEdit.setTextColor(color)
208+
209+
#--------About Section---------------
210+
def about(self):
211+
QMessageBox.about(self, "About App", "This is simple notepad app with PyQt6")
212+
59213

60214

61215
app = QApplication(sys.argv)
-184 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)