update format of the menu structure dict to allow for multiple separators in one menu, also rewrite the update_menus function to better reflect the spec for the menu dict

master
BodgeMaster 2022-03-20 10:01:00 +01:00
parent cb9770f57e
commit 7d3f606304
2 changed files with 29 additions and 34 deletions

View File

@ -26,32 +26,30 @@ class Window(QtWidgets.QMainWindow):
def set_size(self, size_x, size_y): def set_size(self, size_x, size_y):
self.resize(size_x, size_y) self.resize(size_x, size_y)
def update_menus(self, menu_dict): def update_menus(self, menu_dict, menu=None):
menu = self.menuBar() # if not a sub menu
if menu == None:
menu = self.menuBar()
#Looping through entire menu_dict #Looping through entire menu_dict
for topmenu, submenu in menu_dict.items(): for entry in menu_dict:
#making top level menu # inactive or separator
menu_item = menu.addMenu(topmenu) if menu_dict[entry] == None:
# determine if entry is a separator or an inactive menu item
#adding menu items (populating menu) if type(entry) == int:
for inner, data in submenu.items(): menu.addSeparator()
#Adding submenu and populating it else:
if type(data) == dict: menu_item = menu.addAction(entry)
sub = menu_item.addMenu(inner) menu_item.setEnabled(False)
for label, func in data.items(): # sub menus
sub.addAction(label) if type(menu_dict[entry]) == dict:
submenu = menu.addMenu(entry)
if func == None: # recurse because sub menus may have sub menus
sub.triggered.connect(lambda x: None) self.update_menus(menu_dict[entry], menu=submenu)
else: # ordinary menu entries
sub.triggered.connect(func) if callable(menu_dict[entry]):
#Adding separators menu_item = menu.addAction(entry)
elif inner == None: menu_item.triggered.connect(menu_dict[entry])
menu_item.addSeparator()
else:
item = menu_item.addAction(inner)
item.triggered.connect(data)
#TODO: This needs to run in a thread but Qt really doesn't want it to. There are two ways around this: #TODO: This needs to run in a thread but Qt really doesn't want it to. There are two ways around this:
# - create the QtWidgets.QApplication inside a thread and run all QT stuff inside that thread # - create the QtWidgets.QApplication inside a thread and run all QT stuff inside that thread

View File

@ -8,13 +8,13 @@ def not_implemented():
# "":{} -> menu or submenu # "":{} -> menu or submenu
# "":function -> menu entry # "":function -> menu entry
# "":None -> disabled menu entry # "":None -> disabled menu entry
# None:None -> separator # int:None -> separator
# #
# Entries with ... at the end are supposed to open dialogs whereas entries without dots are supposed to take effect immediately # Entries with ... at the end are supposed to open dialogs whereas entries without dots are supposed to take effect immediately
menu_structure = { menu_structure = {
"IDE": { "IDE": {
"Preferences...": not_implemented, "Preferences...": not_implemented,
None: None, 0: None,
"Quit": not_implemented "Quit": not_implemented
}, },
"Project": { "Project": {
@ -25,7 +25,7 @@ menu_structure = {
"Close": { "Close": {
"No open projects": None "No open projects": None
}, },
None: None, 0: None,
"Preferences...": not_implemented, "Preferences...": not_implemented,
"Search...": not_implemented, "Search...": not_implemented,
"Build": not_implemented "Build": not_implemented
@ -35,7 +35,7 @@ menu_structure = {
"Open...": not_implemented, "Open...": not_implemented,
"Save": not_implemented, "Save": not_implemented,
"Close": not_implemented, "Close": not_implemented,
None: None, 0: None,
"Rename...": not_implemented, "Rename...": not_implemented,
"Move...": not_implemented, "Move...": not_implemented,
"View in File Explorer...": not_implemented "View in File Explorer...": not_implemented
@ -45,9 +45,9 @@ menu_structure = {
"Copy": not_implemented, "Copy": not_implemented,
"Paste": not_implemented, "Paste": not_implemented,
"Move code...": not_implemented, "Move code...": not_implemented,
None: None, 0: None,
"Search and Replace...": not_implemented, "Search and Replace...": not_implemented,
None: None, 1: None,
"Format": not_implemented, "Format": not_implemented,
"Indent": not_implemented, "Indent": not_implemented,
"Unindent": not_implemented, "Unindent": not_implemented,
@ -63,6 +63,3 @@ menu_structure = {
"About IDE...": not_implemented, "About IDE...": not_implemented,
} }
} }
#FIXME: come up with a way to uniquely declare separators so they don't become the same element of a dict