-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.py
84 lines (67 loc) · 3.11 KB
/
menu.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
from PyQt5.QtWidgets import QMenuBar, QFileDialog, QMessageBox
class MenuBar(QMenuBar):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.create_menus()
def create_menus(self):
# File menu
file_menu = self.addMenu("File")
new_action = file_menu.addAction("New")
new_action.setShortcut("Ctrl+N")
new_action.triggered.connect(self.new_file) # Ensure method exists
open_action = file_menu.addAction("Open")
open_action.setShortcut("Ctrl+O")
open_action.triggered.connect(self.open_file) # Ensure method exists
save_action = file_menu.addAction("Save")
save_action.setShortcut("Ctrl+S")
save_action.triggered.connect(self.save_file) # Ensure method exists
file_menu.addSeparator()
exit_action = file_menu.addAction("Exit")
exit_action.setShortcut("Ctrl+Q")
exit_action.triggered.connect(self.parent.close)
# Theme menu
theme_menu = self.addMenu("Theme")
light_theme = theme_menu.addAction("Light")
light_theme.triggered.connect(lambda: self.apply_theme("light"))
dark_theme = theme_menu.addAction("Dark")
dark_theme.triggered.connect(lambda: self.apply_theme("dark"))
def new_file(self):
if self.parent.text_editor.document().isModified():
reply = QMessageBox.question(
self.parent, 'Save Changes?',
'Do you want to save your changes before creating a new file?',
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel,
QMessageBox.Save
)
if reply == QMessageBox.Save:
self.save_file()
elif reply == QMessageBox.Cancel:
return
self.parent.text_editor.clear()
self.parent.edit_tree.clear()
def open_file(self):
if self.parent.text_editor.document().isModified():
reply = QMessageBox.question(
self.parent, 'Save Changes?',
'Do you want to save your changes before opening another file?',
QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel,
QMessageBox.Save
)
if reply == QMessageBox.Save:
self.save_file()
elif reply == QMessageBox.Cancel:
return
file_name, _ = QFileDialog.getOpenFileName(self.parent, "Open File", "", "Text Files (*.txt);;All Files (*)")
if file_name:
with open(file_name, 'r') as file:
self.parent.text_editor.setPlainText(file.read())
def save_file(self):
file_name, _ = QFileDialog.getSaveFileName(self.parent, "Save File", "", "Text Files (*.txt);;All Files (*)")
if file_name:
with open(file_name, 'w') as file:
file.write(self.parent.text_editor.toPlainText())
self.parent.text_editor.document().setModified(False)
def apply_theme(self, theme_name):
"""Apply theme to MenuBar and other widgets."""
self.parent.theme_manager.apply_theme(theme_name)