def test_horizontal_mouse_wheel(self): from pyface.qt import QtCore, QtGui is_qt4 = (QtCore.__version_info__[0] <= 4) # create and mock a mouse wheel event if is_qt4: qt_event = QtGui.QWheelEvent(QtCore.QPoint(0, 0), 200, QtCore.Qt.NoButton, QtCore.Qt.NoModifier, QtCore.Qt.Horizontal) else: qt_event = QtGui.QWheelEvent( QtCore.QPoint(0, 0), self.window.control.mapToGlobal(QtCore.QPoint(0, 0)), QtCore.QPoint(200, 0), QtCore.QPoint(200, 0), 200, QtCore.Qt.Vertical, QtCore.Qt.NoButton, QtCore.Qt.NoModifier, QtCore.Qt.ScrollUpdate) # dispatch event self.window._on_mouse_wheel(qt_event) # validate results self.assertEqual(self.tool.event.mouse_wheel_axis, 'horizontal') self.assertAlmostEqual(self.tool.event.mouse_wheel, 5.0 / 3.0) self.assertEqual(self.tool.event.mouse_wheel_delta, (200, 0))
def test_no_context_menu_if_outside_tabwidgets(self): # Check that the case of a position not in any of the tab widgets # is handled correctly. window = TaskWindow() task = SplitEditorAreaPaneTestTask() window.add_task(task) with event_loop(): window.open() editor_area = task.editor_area editor_area_widget = editor_area.control tab_widget, = editor_area_widget.tabwidgets() # Position is relative to the receiving widget, so (-1, -1) should be # reliably outside. pos = QtCore.QPoint(-1, -1) context_menu_event = QtGui.QContextMenuEvent( QtGui.QContextMenuEvent.Mouse, pos, ) global_pos = editor_area_widget.mapToGlobal(pos) self.assertIsNone(editor_area.get_context_menu(global_pos)) # Exercise the context menu code to make sure it doesn't raise. (It # should do nothing.) with event_loop(): tab_widget.contextMenuEvent(context_menu_event) with event_loop(): window.close()
def show(self, x=None, y=None): """ Show the menu at the specified location. """ if x is None or y is None: point = QtGui.QCursor.pos() else: point = QtCore.QPoint(x, y) self.popup(point)
def popup_empty_menu(self, sender): """ Displays the empty list editor popup menu. """ from traitsui.qt4.menu import MakeMenu from pyface.qt import QtCore, QtGui self._cur_control = control = sender menu = MakeMenu(self.empty_list_menu, self, True, control).menu menu.exec_(control.mapToGlobal(QtCore.QPoint(0, 0)))
def paintEvent(self, e): """ Copy the image from the buffer to the qt.drawable. In Qt, all drawing should be done inside of here when a widget is shown onscreen. """ if self.buffer is None: return logging.debug('FigureCanvasQtAggLocal.paintEvent: ' .format(self, self.get_width_height())) if self.blit_buffer is None: # convert the Agg rendered image -> qImage qImage = QtGui.QImage(self.buffer, self.buffer_width, self.buffer_height, QtGui.QImage.Format_ARGB32) # get the rectangle for the image rect = qImage.rect() p = QtGui.QPainter(self) # reset the image area of the canvas to be the back-ground color p.eraseRect(rect) # draw the rendered image on to the canvas p.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap.fromImage(qImage)) p.end() else: qImage = QtGui.QImage(self.blit_buffer, self.blit_width, self.blit_height, QtGui.QImage.Format_ARGB32) pixmap = QtGui.QPixmap.fromImage(qImage) p = QtGui.QPainter(self) p.drawPixmap(QtCore.QPoint(self.blit_left, self.buffer_height - self.blit_top), pixmap) p.end() self.blit_buffer = None
def __init__(self, root, tab_bar, tab, start_pos): """ Initialise the instance. """ self.dragging = False self._root = root self._tab_bar = tab_bar self._tab = tab self._start_pos = QtCore.QPoint(start_pos) self._clone = None
def get_context_menu(self, pos): """ Returns a context menu containing split/collapse actions pos : position (in global coordinates) where the context menu was requested """ menu = Menu() splitter = None splitter = None for tabwidget in self.tabwidgets(): # obtain tabwidget's bounding rectangle in global coordinates global_rect = QtCore.QRect(tabwidget.mapToGlobal(QtCore.QPoint(0, 0)), tabwidget.size()) if global_rect.contains(pos): splitter = tabwidget.parent() # no split/collapse context menu for positions outside any tabwidget # region if not splitter: return # add split actions (only show for non-empty tabwidgets) if not splitter.is_empty(): actions = [Action(id='split_hor', name='Create new pane to the right', on_perform=lambda : splitter.split(orientation= QtCore.Qt.Horizontal)), Action(id='split_ver', name='Create new pane to the bottom', on_perform=lambda : splitter.split(orientation= QtCore.Qt.Vertical))] splitgroup = Group(*actions, id='split') menu.append(splitgroup) # add collapse action (only show for collapsible splitters) if splitter.is_collapsible(): if splitter is splitter.parent().leftchild: if splitter.parent().orientation() is QtCore.Qt.Horizontal: text = 'Merge with right pane' else: text = 'Merge with bottom pane' else: if splitter.parent().orientation() is QtCore.Qt.Horizontal: text = 'Merge with left pane' else: text = 'Merge with top pane' actions = [Action(id='merge', name=text, on_perform=lambda : splitter.collapse())] collapsegroup = Group(*actions, id='collapse') menu.append(collapsegroup) # return QMenu object return menu
def test_vertical_mouse_wheel_without_pixel_delta(self): from pyface.qt import QtCore, QtGui is_qt4 = (QtCore.__version_info__[0] <= 4) if is_qt4: self.skipTest("Not directly applicable in Qt4") # create and mock a mouse wheel event qt_event = QtGui.QWheelEvent( QtCore.QPointF(0, 0), self.window.control.mapToGlobal(QtCore.QPoint(0, 0)), QtCore.QPoint(0, 0), QtCore.QPoint(0, 200), 200, QtCore.Qt.Vertical, QtCore.Qt.NoButton, QtCore.Qt.NoModifier, QtCore.Qt.ScrollUpdate) # dispatch event self.window._on_mouse_wheel(qt_event) # validate results self.assertEqual(self.tool.event.mouse_wheel_axis, 'vertical') self.assertEqual(self.tool.event.mouse_wheel, 5.0 / 3.0) self.assertEqual(self.tool.event.mouse_wheel_delta, (0, 200))
def position_window(window, width=None, height=None, parent=None): """Positions a window on the screen with a specified width and height so that the window completely fits on the screen if possible. """ # Get the available geometry of the screen containing the window. system_metrics = SystemMetrics() screen_dx = system_metrics.screen_width screen_dy = system_metrics.screen_height # Use the frame geometry even though it is very unlikely that the X11 frame # exists at this point. fgeom = window.frameGeometry() width = width or fgeom.width() height = height or fgeom.height() if parent is None: parent = window._parent if parent is None: # Center the popup on the screen. window.move((screen_dx - width) // 2, (screen_dy - height) // 2) return # Calculate the desired size of the popup control: if isinstance(parent, QtGui.QWidget): gpos = parent.mapToGlobal(QtCore.QPoint()) x = gpos.x() y = gpos.y() cdx = parent.width() cdy = parent.height() # Get the frame height of the parent and assume that the window will # have a similar frame. Note that we would really like the height of # just the top of the frame. pw = parent.window() fheight = pw.frameGeometry().height() - pw.height() else: # Special case of parent being a screen position and size tuple (used # to pop-up a dialog for a table cell): x, y, cdx, cdy = parent fheight = 0 x -= (width - cdx) / 2 y += cdy + fheight # Position the window (making sure it will fit on the screen). window.move( max(0, min(x, screen_dx - width)), max(0, min(y, screen_dy - height)) )
def _filter_tab_bar(self, tab_bar, event): """ Support 'tearing off' a tab. """ if event.type() == QtCore.QEvent.Type.MouseMove: if tab_bar.rect().contains(event.pos()): self.reset_drag() else: if not self._drag_widget: index = tab_bar.currentIndex() self._tear_widgets = self.get_dock_widgets_for_bar(tab_bar) self._drag_widget = widget = self._tear_widgets.pop(index) pos = QtCore.QPoint(0, 0) press_event = QtGui.QMouseEvent( QtCore.QEvent.Type.MouseButtonPress, pos, widget.mapToGlobal(pos), QtCore.Qt.MouseButton.LeftButton, QtCore.Qt.MouseButton.LeftButton, event.modifiers(), ) QtCore.QCoreApplication.sendEvent(widget, press_event) return True event = QtGui.QMouseEvent( QtCore.QEvent.Type.MouseMove, event.pos(), event.globalPos(), event.button(), event.buttons(), event.modifiers(), ) QtCore.QCoreApplication.sendEvent(self._drag_widget, event) return True elif event.type() == QtCore.QEvent.Type.ToolTip: # QDockAreaLayout forces the tooltips to be QDockWidget.windowTitle, # so we provide the tooltips manually. widgets = self.get_dock_widgets_for_bar(tab_bar) index = tab_bar.tabAt(event.pos()) tooltip = widgets[index].editor.tooltip if index >= 0 else "" if tooltip: QtGui.QToolTip.showText(event.globalPos(), tooltip, tab_bar) return True return False
def popup_menu(self, index): """Displays the list editor popup menu.""" self._cur_control = sender = self.buttons[index] proxy = sender.proxy menu = MakeMenu(self.list_menu, self, True, sender).menu len_list = len(proxy.list) not_full = len_list < self._trait_handler.maxlen self._menu_before.enabled(not_full) self._menu_after.enabled(not_full) self._menu_delete.enabled(len_list > self._trait_handler.minlen) self._menu_up.enabled(index > 0) self._menu_top.enabled(index > 0) self._menu_down.enabled(index < (len_list - 1)) self._menu_bottom.enabled(index < (len_list - 1)) menu.exec_(sender.mapToGlobal(QtCore.QPoint(4, 24)))
def test_horizontal_mouse_wheel(self): from pyface.qt import QtCore, QtGui, is_qt4, is_qt5 # create and mock a mouse wheel event if is_qt4: qt_event = QtGui.QWheelEvent( QtCore.QPoint(0, 0), # pos 200, # delta QtCore.Qt.NoButton, # buttons QtCore.Qt.NoModifier, # modifiers QtCore.Qt.Horizontal, # orient ) elif is_qt5: qt_event = QtGui.QWheelEvent( QtCore.QPoint(0, 0), # pos self.window.control.mapToGlobal(QtCore.QPoint(0, 0)), # globalPos QtCore.QPoint(200, 0), # pixelDelta QtCore.QPoint(200, 0), # angleDelta 200, # qt4Delta QtCore.Qt.Horizontal, # qt4Orietation QtCore.Qt.NoButton, # buttons QtCore.Qt.NoModifier, # modifiers QtCore.Qt.ScrollUpdate, # phase ) else: qt_event = QtGui.QWheelEvent( QtCore.QPointF(0, 0), # pos self.window.control.mapToGlobal(QtCore.QPointF( 0, 0)), # globalPos QtCore.QPoint(200, 0), # pixelDelta QtCore.QPoint(200, 0), # angleDelta QtCore.Qt.NoButton, # buttons QtCore.Qt.NoModifier, # modifiers QtCore.Qt.ScrollUpdate, # phase False, # inverted ) # dispatch event self.window._on_mouse_wheel(qt_event) # validate results self.assertEqual(self.tool.event.mouse_wheel_axis, "horizontal") self.assertAlmostEqual(self.tool.event.mouse_wheel, 5.0 / 3.0) self.assertEqual(self.tool.event.mouse_wheel_delta, (200, 0))
def _select(self, pos): tw, hs, hs_geom = self._hotspot(pos) # See if the hotspot has changed. if self._selected_tab_widget is not tw or self._selected_hotspot != hs: if self._selected_tab_widget is not None: self._rband.hide() if tw is not None and hs != self._HS_NONE: if self._rband: self._rband.deleteLater() position = QtCore.QPoint(*hs_geom[0:2]) window = tw.window() self._rband = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, window) self._rband.move(window.mapFromGlobal(position)) self._rband.resize(*hs_geom[2:4]) self._rband.show() self._selected_tab_widget = tw self._selected_hotspot = hs
def popup_menu(self, sender): """ Displays the list editor popup menu. """ from traitsui.qt4.menu import MakeMenu from pyface.qt import QtCore, QtGui self._cur_control = sender proxy = sender.proxy index = proxy.index menu = MakeMenu(self.list_menu, self, True, sender).menu len_list = len(proxy.list) not_full = (len_list < self._trait_handler.maxlen) self._menu_before.enabled(not_full) self._menu_after.enabled(not_full) self._menu_delete.enabled(len_list > self._trait_handler.minlen) self._menu_up.enabled(index > 0) self._menu_top.enabled(index > 0) self._menu_down.enabled(index < (len_list - 1)) self._menu_bottom.enabled(index < (len_list - 1)) menu.exec_(sender.mapToGlobal(QtCore.QPoint(0, 0)))
def popup_menu(self): """ Displays the list editor popup menu. """ layout = self._list_pane.layout() sender = layout.sender() self._cur_control = sender proxy = sender.proxy index = proxy.index menu = MakeMenu(self.list_menu, self, True, sender).menu len_list = len(proxy.list) not_full = (len_list < self._trait_handler.maxlen) self._menu_before.enabled(not_full) self._menu_after.enabled(not_full) self._menu_delete.enabled(len_list > self._trait_handler.minlen) self._menu_up.enabled(index > 0) self._menu_top.enabled(index > 0) self._menu_down.enabled(index < (len_list - 1)) self._menu_bottom.enabled(index < (len_list - 1)) menu.exec_(sender.mapToGlobal(QtCore.QPoint(0, 0)))
def popup_empty_menu(self, sender): """ Displays the empty list editor popup menu. """ self._cur_control = control = sender menu = MakeMenu(self.empty_list_menu, self, True, control).menu menu.exec_(control.mapToGlobal(QtCore.QPoint(0, 0)))
def popup_empty_menu(self, index): """ Displays the empty list editor popup menu. """ self._cur_control = control = self.buttons[index] menu = MakeMenu(self.empty_list_menu, self, True, control).menu menu.exec_(control.mapToGlobal(QtCore.QPoint(4, 24)))