def ensure_on_screen(rect): """ Ensure that the given rect is contained on screen. If the origin of the rect is not contained within the closest desktop screen, the rect will be moved so that it is fully on the closest screen. If the rect is larger than the closest screen, the origin will never be less than the screen origin. Parameters ---------- rect : QRect The geometry rect of interest. Returns ------- result : QRect The potentially adjusted QRect which fits on the screen. """ d = QApplication.desktop() pos = rect.topLeft() drect = d.screenGeometry(pos) if not drect.contains(pos): x = pos.x() if x < drect.x() or x > drect.right(): dw = drect.width() - rect.width() x = max(drect.x(), drect.x() + dw) y = pos.y() if x < drect.top() or y > drect.bottom(): dh = drect.height() - rect.height() y = max(drect.y(), drect.y() + dh) rect = QRect(x, y, rect.width(), rect.height()) return rect
def mouse_over_widget(self, widget, pos, empty=False): """ Update the overlays based on the mouse position. This handler should be invoked when the mouse hovers over a single widget (such as a floating dock container) as opposed to an area of docked widgets. The guide rose will be displayed in the center of the widget with no border guides. Parameters ---------- widget : QWidget The widget under the mouse. pos : QPoint The hover position, expressed in the local coordinates of the widget. empty : bool, optional Whether the widget represents an empty widget. If this is True, a single center guide will be shown instead of the guide rose. """ Mode = QGuideRose.Mode rose = self._rose target_mode = Mode.AreaCenter if empty else Mode.CompassEx self._target_rose_mode = target_mode if rose.mode() != target_mode: rose.setMode(Mode.NoMode) self._rose_timer.start(self.rose_delay) self._band_timer.start(self.band_delay) origin = widget.mapToGlobal(QPoint(0, 0)) geo = QRect(origin, widget.size()) dirty = rose.geometry() != geo if dirty: rose.hide() rose.setMode(Mode.NoMode) rose.setGeometry(geo) guide = rose.guideAt(pos, target_mode) if dirty or guide != self._last_guide: self._last_guide = guide self._target_band_geo = self._band_geometry(widget, guide) self._band_timer.start(self.band_delay) rose.setCenterPoint(QPoint(geo.width() / 2, geo.height() / 2)) rose.mouseOver(pos) rose.show()