def __menu_requested(self, point: QPoint): index = self.view.indexAt(point) model: QSortFilterProxyModel = index.model() source_index = model.mapToSource(index) menu = QMenu(self) for action in self._create_actions(source_index): menu.addAction(action) menu.popup(self.view.viewport().mapToGlobal(point))
def contextMenuEvent(self, event): if event.modifiers() & Qt.AltModifier: menu = QMenu(event.widget()) menu.setAttribute(Qt.WA_DeleteOnClose) formatmenu = menu.addMenu("Render as") group = QActionGroup(self, exclusive=True) def makeaction(text, parent, data=None, **kwargs): action = QAction(text, parent, **kwargs) if data is not None: action.setData(data) return action formatactions = [ makeaction( "Plain Text", group, checkable=True, toolTip=self.tr("Render contents as plain text"), data="text/plain", ), makeaction( "HTML", group, checkable=True, toolTip=self.tr("Render contents as HTML"), data="text/html", ), makeaction( "RST", group, checkable=True, toolTip=self.tr("Render contents as RST " "(reStructuredText)"), data="text/rst", ), makeaction( "Markdown", group, checkable=True, toolTip=self.tr("Render contents as Markdown"), data="text/markdown", ), ] for action in formatactions: action.setChecked(action.data() == self.__contentType.lower()) formatmenu.addAction(action) def ontriggered(action): mimetype = action.data() content = self.content() self.setContent(content, mimetype) self.editingFinished.emit() menu.triggered.connect(ontriggered) menu.popup(event.screenPos()) event.accept() else: event.ignore()
def context(pos): menu = QMenu(view) menu.addActions(view.actions()) a = menu.addAction("Aspect mode") am = QMenu(menu) am.addAction("Ignore", lambda: view.setAspectMode(Qt.IgnoreAspectRatio)) am.addAction("Keep", lambda: view.setAspectMode(Qt.KeepAspectRatio)) am.addAction("Keep by expanding", lambda: view.setAspectMode(Qt.KeepAspectRatioByExpanding)) a.setMenu(am) menu.popup(view.viewport().mapToGlobal(pos))
class GOTreeWidget(QTreeWidget): def contextMenuEvent(self, event): super().contextMenuEvent(event) term = self.itemAt(event.pos()).term self._currMenu = QMenu() self._currAction = self._currMenu.addAction("View term on AmiGO website") self._currAction.triggered.connect(lambda: self.BrowserAction(term)) self._currMenu.popup(event.globalPos()) def BrowserAction(self, term): if isinstance(term, go.Term): term = term.id webbrowser.open("http://amigo.geneontology.org/cgi-bin/amigo/term-details.cgi?term="+term)
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: if self.__popuptext: popup = QMenu(self) label = QLabel(self, textInteractionFlags=Qt.TextBrowserInteraction, openExternalLinks=self.__openExternalLinks, text=self.__popuptext) label.linkActivated.connect(self.linkActivated) label.linkHovered.connect(self.linkHovered) action = QWidgetAction(popup) action.setDefaultWidget(label) popup.addAction(action) popup.popup(event.globalPos(), action) event.accept() return else: super().mousePressEvent(event)
def contextMenuEvent(self, event): if event.modifiers() & Qt.AltModifier: menu = QMenu(event.widget()) menu.setAttribute(Qt.WA_DeleteOnClose) formatmenu = menu.addMenu("Render as") group = QActionGroup(self, exclusive=True) def makeaction(text, parent, data=None, **kwargs): action = QAction(text, parent, **kwargs) if data is not None: action.setData(data) return action formatactions = [ makeaction("Plain Text", group, checkable=True, toolTip=self.tr("Render contents as plain text"), data="text/plain"), makeaction("HTML", group, checkable=True, toolTip=self.tr("Render contents as HTML"), data="text/html"), makeaction("RST", group, checkable=True, toolTip=self.tr("Render contents as RST " "(reStructuredText)"), data="text/rst"), makeaction("Markdown", group, checkable=True, toolTip=self.tr("Render contents as Markdown"), data="text/markdown") ] for action in formatactions: action.setChecked(action.data() == self.__contentType.lower()) formatmenu.addAction(action) def ontriggered(action): mimetype = action.data() content = self.content() self.setContent(content, mimetype) self.editingFinished.emit() menu.triggered.connect(ontriggered) menu.popup(event.screenPos()) event.accept() else: event.ignore()
class GOTreeWidget(QTreeWidget): def __init__(self, parent=None): super().__init__(parent=parent) self._currMenu = QMenu() self._currAction = self._currMenu.addAction( "View term on AmiGO website") def contextMenuEvent(self, event): super().contextMenuEvent(event) def browser_action(_term): if isinstance(_term, go.Term): _term = _term.id webbrowser.open( f'http://amigo.geneontology.org/amigo/term/{_term}') term = self.itemAt(event.pos()).term self._currAction.triggered.connect(lambda: browser_action(term)) self._currMenu.popup(event.globalPos())
def _on_view_context_menu(self, pos): widget = self.scene.widget if widget is None: return assert isinstance(widget, HeatmapGridWidget) menu = QMenu(self.view.viewport()) menu.setAttribute(Qt.WA_DeleteOnClose) menu.addActions(self.view.actions()) menu.addSeparator() menu.addActions([self.__font_inc, self.__font_dec]) menu.addSeparator() a = QAction("Keep aspect ratio", menu, checkable=True) a.setChecked(self.keep_aspect) def ontoggled(state): self.keep_aspect = state self.__aspect_mode_changed() a.toggled.connect(ontoggled) menu.addAction(a) menu.popup(self.view.viewport().mapToGlobal(pos))
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: if self.__popuptext: popup = QMenu(self) label = QLabel( self, textInteractionFlags=Qt.TextBrowserInteraction, openExternalLinks=self.__openExternalLinks, ) label.setText(self.__styled(self.__defaultStyleSheet, self.__popuptext)) label.linkActivated.connect(self.linkActivated) label.linkHovered.connect(self.linkHovered) action = QWidgetAction(popup) action.setDefaultWidget(label) popup.addAction(action) popup.popup(event.globalPos(), action) event.accept() return else: super().mousePressEvent(event)
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowIcon(QIcon(':/res/box.png')) self._tray = QSystemTrayIcon() self._tray.setIcon(QIcon(':/res/box.png')) self._tray.activated.connect(self.show_tray_menu) self._tray.show() self._menu = None self._action_screen = None self._action_clip = None self._screenshot_wnd = None self.rebuild_menu() self._hotkey = hotkeys.initialize() if not settings['initialized']: settings['initialized'] = True settings['hotkey/screenshot'] = 'Print' settings['hotkey/clipboard'] = 'Ctrl+Alt+P' settings.save() self.register_hotkeys() def rebuild_menu(self): # Terrible way to clear hotkey shortcuts set to actions because Qt can not do that. self._menu = QMenu() self._action_screen = self._menu.addAction(self.tr('Make Screenshot')) self._action_screen.triggered.connect(self.capture_screen) self._action_clip = self._menu.addAction(self.tr('Share Clipboard')) self._action_clip.triggered.connect(self.share_clipboard) self._menu.addSeparator() self._menu.addAction(self.tr('Settings')).triggered.connect(self.show_settings) self._menu.addSeparator() self._menu.addAction(self.tr('Exit')).triggered.connect(lambda: QApplication.exit(0)) self._tray.setContextMenu(self._menu) def __del__(self): hotkey = getattr(self, '_hotkey', None) if hotkey: hotkey.destroy() def show_tray_menu(self): self._menu.popup(QCursor.pos()) def capture_screen(self): if self._screenshot_wnd: return self._screenshot_wnd = wnd = Screenshot() result = wnd.exec_() self._screenshot_wnd = None if result == QDialog.Accepted: wnd = ShareDialog(self, image=wnd.selected_image) wnd.show() wnd.exec_() def share_clipboard(self): mime = QApplication.clipboard().mimeData() try: wnd = ShareDialog(self, mime=mime) except ValueError as e: QMessageBox.critical(self, self.tr('Error'), str(e)) return else: wnd.show() wnd.exec_() def register_hotkeys(self): if self._hotkey is not None: self.rebuild_menu() self._hotkey.unregister(winid=self.winId()) hotkey_bindings = { settings['hotkey/clipboard']: (self.share_clipboard, self._action_clip), settings['hotkey/screenshot']: (self.capture_screen, self._action_screen) } for hotkey, (callback, action) in hotkey_bindings.items(): if hotkey: if self._hotkey.register(hotkey, callback, self.winId()): sequence = QKeySequence(hotkey) if hotkey else QKeySequence() action.setShortcut(sequence) else: QMessageBox.critical(self, 'Error', 'Could not bind {} hotkey!\n' 'Key combination {} is probably already in use.' .format(const.APP_NAME, hotkey)) else: qDebug('Hotkeys are not supported on this platform') def show_settings(self): self._hotkey.unregister(winid=self.winId()) dlg = SettingsDialog(self) dlg.show() dlg.exec_() # WORKAROUND: On windows calling register_hotkeys() directly often results in a crash. QTimer.singleShot(10, self.register_hotkeys)