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
3
3
import sys
4
+ from PyQt6 .QtCore import QFileInfo , Qt
5
+ from PyQt6 .QtGui import QFont , QTextCharFormat
4
6
from NotePad import Ui_MainWindow
5
7
8
+
6
9
class NotePadWindow (QMainWindow , Ui_MainWindow ):
7
10
def __init__ (self ):
8
11
super ().__init__ ()
9
12
self .setupUi (self )
10
13
self .show ()
11
14
12
15
#---------------Signal Connection Section-----------------
16
+ #--File Menu--
13
17
self .actionSave .triggered .connect (self .save_file )
14
18
self .actionNew .triggered .connect (self .file_new )
15
19
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
+
16
46
17
47
18
48
#------------Maybe Save Function----------------
@@ -45,6 +75,7 @@ def file_new(self):
45
75
if self .maybe_save ():
46
76
self .textEdit .clear ()
47
77
78
+
48
79
#------------------Open File Function-----------------------
49
80
def open_file (self ):
50
81
fname = QFileDialog .getOpenFileName (self , "Open File" , '/home' )
@@ -56,6 +87,129 @@ def open_file(self):
56
87
57
88
#-----------------Print Function--------------------
58
89
# 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
+
59
213
60
214
61
215
app = QApplication (sys .argv )
0 commit comments