def __init__(self, parent = None): QtGui.QDialog.__init__(self, parent) PMXBaseDialog.__init__(self) self.setupUi(self) self.filesTableModel = FilesTableModel(self) self.tableViewFiles.setModel(self.filesTableModel) self.tableViewFiles.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.tableViewFiles.customContextMenuRequested.connect(self.showTableViewFilesContextMenu) self.tableViewFiles.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) self.tableViewFiles.horizontalHeader().setResizeMode(1, QtGui.QHeaderView.Stretch) #Setup Context Menu selectMenu = { "title": "Select Menu", "items": [ { 'text': 'Choose All', 'callback': lambda dialog: dialog.chooseAll(), }, { 'text': 'Choose None', 'callback': lambda dialog: dialog.chooseNone(), }, { 'text': 'Revert to Default Choices', 'callback': lambda dialog: dialog.revertChoices(), }, ] } self.selectMenu, _ = create_menu(self, selectMenu, connectActions = True) self.toolButtonSelect.setMenu(self.selectMenu)
def test_create_menu(self): menus, actions = create_menu(None, MENU_SETTINGS, allMenus = True) self.assertEqual(len(menus), 3) self.assertEqual(len(actions), 11) for menu, name in zip(menus, MENU_NAMES): self.assertEqual(menu.objectName(), name) for action, name in zip(actions, ACTION_NAMES): self.assertEqual(action.objectName(), name)
def contributeToMainMenu(self, name, settings): actions = [] menuAttr = text2objectname(name, prefix = "menu") menu = getattr(self, menuAttr, None) if menu is None: if "text" not in settings: settings["text"] = name menu, actions = create_menu(self.menubar, settings) setattr(self, menuAttr, menu) actions.insert(0, self.menubar.insertMenu(self.menuNavigation.children()[0], menu)) elif 'items' in settings: actions = extend_menu(menu, settings['items']) return actions
def contributeToMainMenu(self, name, settings): actions = [] menuAttr = text2objectname(name, prefix="menu") menu = getattr(self, menuAttr, None) if menu is None: if "text" not in settings: settings["text"] = name menu, actions = create_menu(self.menubar, settings) setattr(self, menuAttr, menu) actions.insert( 0, self.menubar.insertMenu(self.menuNavigation.children()[0], menu)) elif 'items' in settings: actions = extend_menu(menu, settings['items']) return actions
def __init__(self, parent=None): QtGui.QDialog.__init__(self, parent) PMXBaseDialog.__init__(self) self.setupUi(self) self.filesTableModel = FilesTableModel(self) self.tableViewFiles.setModel(self.filesTableModel) self.tableViewFiles.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.tableViewFiles.customContextMenuRequested.connect( self.showTableViewFilesContextMenu) self.tableViewFiles.setSelectionBehavior( QtGui.QAbstractItemView.SelectRows) self.tableViewFiles.horizontalHeader().setResizeMode( 1, QtGui.QHeaderView.Stretch) #Setup Context Menu selectMenu = { "title": "Select Menu", "items": [ { 'text': 'Choose All', 'callback': lambda dialog: dialog.chooseAll(), }, { 'text': 'Choose None', 'callback': lambda dialog: dialog.chooseNone(), }, { 'text': 'Revert to Default Choices', 'callback': lambda dialog: dialog.revertChoices(), }, ] } self.selectMenu, _ = create_menu(self, selectMenu, connectActions=True) self.toolButtonSelect.setMenu(self.selectMenu)
def mousePressEvent(self, e): """ Reimplemented to handle mouse press events. """ if e.button() == QtCore.Qt.RightButton: widget = self.widgetAtPos(e.pos()) tabWidget = self.parent() tabSplitter = tabWidget._root tabMenu = { "text": "Tab Menu", "items": [ { "text": "Close", "icon": resources.get_icon("tab-close"), "triggered": partial(tabWidget._close_widget, widget) }, { "text": "Close all", "triggered": tabSplitter.closeAll }, { "text": "Close other", "icon": resources.get_icon("tab-close-other"), "triggered": partial(tabSplitter.closeAllExceptWidget, widget) } ] } if self.parent().count() > 1: tabMenu["items"].extend([ "-", { "text": "Split vertically", "icon": resources.get_icon("view-split-left-right"), "triggered": partial(tabSplitter.splitVertically, widget) }, { "text": "Split horizontally", "icon": resources.get_icon("view-split-top-bottom"), "triggered": partial(tabSplitter.splitHorizontally, widget) } ]) tabMenu["items"].append("-") # Create custom menu tabMenu["items"].extend(widget.contributeToTabMenu()) menu = create_menu(self, tabMenu) # Display menu menu.exec_(e.globalPos()) elif e.button() == QtCore.Qt.LeftButton: # There is something odd in the focus handling where focus temporarily # moves elsewhere (actually to a View) when switching to a different # tab page. We suppress the notification so that the workbench doesn't # temporarily make the View active. self._root._repeat_focus_changes = False QtGui.QTabBar.mousePressEvent(self, e) self._root._repeat_focus_changes = True # Update the current tab. self._root._set_current_tab(self.parent(), self.currentIndex()) self._root._set_focus() if e.button() != QtCore.Qt.LeftButton: return if self._drag_state is not None: return # Potentially start dragging if the tab under the mouse is the current # one (which will eliminate disabled tabs). tab = self._tab_at(e.pos()) if tab < 0 or tab != self.currentIndex(): return self._drag_state = _DragState(self._root, self, tab, e.pos())
def mousePressEvent(self, e): """ Reimplemented to handle mouse press events. """ if e.button() == QtCore.Qt.RightButton: widget = self.widgetAtPos(e.pos()) tabWidget = self.parent() tabSplitter = tabWidget.parent() tabMenu = { "text": "Tab Menu", "items": [{ "text": "Close", "icon": "tab-close", "callback": partial(tabWidget._close_widget, widget) }, { "text": "Close All", "callback": tabSplitter.closeAll }, { "text": "Close Other", "icon": "tab-close-other", "callback": partial(tabSplitter.closeAllExceptWidget, widget) }] } if self.parent().count() > 1: tabMenu["items"].append("-") tabMenu["items"].append({ "text": "Split Horizontally", "icon": "view-split-left-right" }) tabMenu["items"].append({ "text": "Split Vertically", "icon": "view-split-top-bottom" }) tabMenu["items"].append("-") # Create custom menu ( tabMenu["items"].extend(widget.contributeToTabMenu()) menu, actions = create_menu(self, tabMenu, connectActions=True) # Display menu menu.exec_(e.globalPos()) elif e.button() == QtCore.Qt.LeftButton: # There is something odd in the focus handling where focus temporarily # moves elsewhere (actually to a View) when switching to a different # tab page. We suppress the notification so that the workbench doesn't # temporarily make the View active. self._root._repeat_focus_changes = False QtGui.QTabBar.mousePressEvent(self, e) self._root._repeat_focus_changes = True # Update the current tab. self._root._set_current_tab(self.parent(), self.currentIndex()) self._root._set_focus() if e.button() != QtCore.Qt.LeftButton: return if self._drag_state is not None: return # Potentially start dragging if the tab under the mouse is the current # one (which will eliminate disabled tabs). tab = self._tab_at(e.pos()) if tab < 0 or tab != self.currentIndex(): return self._drag_state = _DragState(self._root, self, tab, e.pos())
def setupTreeViewFileSystem(self): self.treeViewFileSystem.setModel(self.fileSystemProxyModel) #self.treeViewFileSystem.setHeaderHidden(True) #self.treeViewFileSystem.setUniformRowHeights(False) #Setup Context Menu contextMenu = { "text": "File System", "items": [ { "text": "New", "items": [ self.actionNewFolder, self.actionNewFile, self.actionNewFromTemplate ] }, "-", self.actionOpen, { "text": "Open With", "items": [ self.actionOpenDefaultEditor, self.actionOpenSystemEditor ] }, self.actionSetInTerminal, "-", self.actionRename, self.actionCut, self.actionCopy, self.actionPaste, self.actionDelete, ] } self.fileSystemMenu = create_menu(self, contextMenu) #Setup Context Menu optionsMenu = { "text": "File System Options", "items": [ { "text": "Order", "items": [ (self.actionOrderByName, self.actionOrderBySize, self.actionOrderByDate, self.actionOrderByType), "-", self.actionOrderDescending, self.actionOrderFoldersFirst ] } ] } self.actionOrderFoldersFirst.setChecked(True) self.actionOrderByName.trigger() self.fileSystemOptionsMenu = create_menu(self, optionsMenu) self.pushButtonOptions.setMenu(self.fileSystemOptionsMenu) #Connect context menu self.treeViewFileSystem.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.treeViewFileSystem.customContextMenuRequested.connect(self.showTreeViewFileSystemContextMenu) # Drag and Drop (see the proxy model) self.treeViewFileSystem.setDragEnabled(True) self.treeViewFileSystem.setAcceptDrops(True) self.treeViewFileSystem.setDefaultDropAction(QtCore.Qt.MoveAction) self.treeViewFileSystem.setDropIndicatorShown(True) self.treeViewFileSystem.setAlternatingRowColors(True) self.treeViewFileSystem.setAnimated(True)
def setupTreeViewFileSystem(self): self.treeViewFileSystem.setModel(self.fileSystemProxyModel) #self.treeViewFileSystem.setHeaderHidden(True) #self.treeViewFileSystem.setUniformRowHeights(False) #Setup Context Menu contextMenu = { "title": "File System", "items": [ { "text": "New", "items": [ self.actionNewFolder, self.actionNewFile, self.actionNewFromTemplate ] }, "-", self.actionOpen, { "text": "Open With", "items": [ self.actionOpenDefaultEditor, self.actionOpenSystemEditor ] }, self.actionSetInTerminal, "-", self.actionRename, self.actionCut, self.actionCopy, self.actionPaste, self.actionDelete, ] } self.fileSystemMenu, self.fileSystemMenuActions = create_menu( self, contextMenu) #Setup Context Menu optionsMenu = { "title": "File System Options", "items": [{ "text": "Order", "items": [(self.actionOrderByName, self.actionOrderBySize, self.actionOrderByDate, self.actionOrderByType), "-", self.actionOrderDescending, self.actionOrderFoldersFirst] }] } self.actionOrderFoldersFirst.setChecked(True) self.actionOrderByName.trigger() self.fileSystemOptionsMenu, _ = create_menu(self, optionsMenu) self.pushButtonOptions.setMenu(self.fileSystemOptionsMenu) #Connect context menu self.treeViewFileSystem.setContextMenuPolicy( QtCore.Qt.CustomContextMenu) self.treeViewFileSystem.customContextMenuRequested.connect( self.showTreeViewFileSystemContextMenu) #======================================================================= # Drag and Drop (see the proxy model) #======================================================================= self.treeViewFileSystem.setDragEnabled(True) self.treeViewFileSystem.setAcceptDrops(True) self.treeViewFileSystem.setDefaultDropAction(QtCore.Qt.MoveAction) self.treeViewFileSystem.setDropIndicatorShown(True) self.treeViewFileSystem.setAlternatingRowColors(True) self.treeViewFileSystem.setAnimated(True)
def setupTreeViewFileSystem(self): self.treeViewFileSystem.setModel(self.fileSystemProxyModel) #self.treeViewFileSystem.setHeaderHidden(True) #self.treeViewFileSystem.setUniformRowHeights(False) #Setup Context Menu contextMenu = { "text": "File System", "items": [ { "text": "New", "items": [ ] }, "-", { "text": "Open With", "items": [ ] }, "-", ] } self.fileSystemMenu, objects = create_menu(self, contextMenu) #Setup Context Menu optionsMenu = { "text": "File System Options", "items": [ { "text": "Order", "items": [ { 'text': "By name", 'triggered': self.on_actionOrderByName_triggered }, { 'text': "By size", 'triggered': self.on_actionOrderBySize_triggered }, { 'text': "By date", 'triggered': self.on_actionOrderByDate_triggered }, { 'text': "By type", 'triggered': self.on_actionOrderByType_triggered }, "-", { 'text': "Descending", 'triggered': self.on_actionOrderDescending_triggered }, { 'text': "Folders first", 'triggered': self.on_actionOrderFoldersFirst_triggered } ] } ] } self.fileSystemOptionsMenu, objects = create_menu(self, optionsMenu) self.toolButtonOptions.setMenu(self.fileSystemOptionsMenu) #Connect context menu self.treeViewFileSystem.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.treeViewFileSystem.customContextMenuRequested.connect(self.showTreeViewFileSystemContextMenu) # Drag and Drop (see the proxy model) self.treeViewFileSystem.setDragEnabled(True) self.treeViewFileSystem.setAcceptDrops(True) self.treeViewFileSystem.setDefaultDropAction(QtCore.Qt.MoveAction) self.treeViewFileSystem.setDropIndicatorShown(True) self.treeViewFileSystem.setAlternatingRowColors(True) self.treeViewFileSystem.setAnimated(True)