def __setControlAreaVisible(self, visible): # type: (bool) -> None if self.__splitter is None or self.__splitter.count() < 2: return self.controlAreaVisible = visible splitter = self.__splitter # type: QSplitter w = splitter.widget(0) # Set minimum width to 1 (overrides minimumSizeHint) when control area # is not visible to allow the main area to shrink further. Reset the # minimum width with a 0 if control area is visible. w.setMinimumWidth(int(not visible)) sizes = splitter.sizes() current_size = sizes[0] if bool(current_size) == visible: return current_width = w.width() geom = self.geometry() frame = self.frameGeometry() framemargins = QMargins( frame.left() - geom.left(), frame.top() - geom.top(), frame.right() - geom.right(), frame.bottom() - geom.bottom() ) splitter.setSizes([int(visible), QWIDGETSIZE_MAX]) if not self.isWindow() or \ self.windowState() not in [Qt.WindowNoState, Qt.WindowActive]: # not a window or not in state where we can move move/resize return # force immediate resize recalculation splitter.refresh() self.layout().invalidate() self.layout().activate() if visible: # move left and expand by the exposing widget's width diffx = -w.width() diffw = w.width() else: # move right and shrink by the collapsing width diffx = current_width diffw = -current_width newgeom = QRect( geom.x() + diffx, geom.y(), geom.width() + diffw, geom.height() ) # bound/move by available geometry bounds = QApplication.desktop().availableGeometry(self) bounds = bounds.adjusted( framemargins.left(), framemargins.top(), -framemargins.right(), -framemargins.bottom() ) newsize = newgeom.size().boundedTo(bounds.size()) newgeom = QRect(newgeom.topLeft(), newsize) newgeom.moveLeft(max(newgeom.left(), bounds.left())) newgeom.moveRight(min(newgeom.right(), bounds.right())) self.setGeometry(newgeom)
def setControlMargins(self, *margins): """Set the controls points on the margins around `rect` """ if len(margins) > 1: margins = QMargins(*margins) else: margins = margins[0] if isinstance(margins, int): margins = QMargins(margins, margins, margins, margins) if self.__margins != margins: self.__margins = margins self.__pointsLayout()
def setControlMargins(self, *margins): # type: (int) -> None """Set the controls points on the margins around `rect` """ if len(margins) > 1: margins = QMargins(*margins) elif len(margins) == 1: margin = margins[0] margins = QMargins(margin, margin, margin, margin) else: raise TypeError if self.__margins != margins: self.__margins = margins self.__pointsLayout()
def qwidget_margin_within(widget, ancestor): # type: (QWidget, QWidget) -> QMargins """ Return the 'margins' of widget within its 'ancestor' Ancestor must be within the widget's parent hierarchy and both widgets must share the same top level window. Parameters ---------- widget : QWidget ancestor : QWidget Returns ------- margins: QMargins """ assert ancestor.isAncestorOf(widget) assert ancestor.window() is widget.window() r1 = widget.geometry() r2 = ancestor.geometry() topleft = r1.topLeft() bottomright = r1.bottomRight() topleft = widget.mapTo(ancestor, topleft) bottomright = widget.mapTo(ancestor, bottomright) return QMargins(topleft.x(), topleft.y(), r2.right() - bottomright.x(), r2.bottom() - bottomright.y())
def __init__(self, parent=None, rect=QRectF(), constraints=Free, **kwargs): # type: (Optional[QGraphicsItem], QRectF, Constraint, Any) -> None super().__init__(parent, **kwargs) self.setFlag(QGraphicsItem.ItemHasNoContents) self.setFlag(QGraphicsItem.ItemIsFocusable) self.__rect = QRectF(rect) if rect is not None else QRectF() self.__margins = QMargins() points = \ [ControlPoint(self, ControlPoint.Left, constraint=Qt.Horizontal), ControlPoint(self, ControlPoint.Top, constraint=Qt.Vertical), ControlPoint(self, ControlPoint.TopLeft), ControlPoint(self, ControlPoint.Right, constraint=Qt.Horizontal), ControlPoint(self, ControlPoint.TopRight), ControlPoint(self, ControlPoint.Bottom, constraint=Qt.Vertical), ControlPoint(self, ControlPoint.BottomLeft), ControlPoint(self, ControlPoint.BottomRight) ] assert (points == sorted(points, key=lambda p: p.anchor())) self.__points = dict((p.anchor(), p) for p in points) if self.scene(): self.__installFilter() for p in points: p.setFlag(QGraphicsItem.ItemIsFocusable) p.setFocusProxy(self) self.__constraints = constraints self.__activeControl = None # type: Optional[ControlPoint] self.__pointsLayout()
def init_form(self): self._fig = Figure((5.0, 4.0), dpi=100) self.canvas = FigureCanvas(self._fig) self.canvas.setParent(self) self.mpl_toolbar = NavigationToolbar(self.canvas, self) vbox = QVBoxLayout() vbox.addWidget(self.canvas) vbox.addWidget(self.mpl_toolbar) vbox.setContentsMargins(QMargins(0, 0, 0, 0)) self.setLayout(vbox) super(ControlMatplotlib, self).init_form()
def __setControlAreaVisible(self, visible): # type: (bool) -> None if self.__splitter is None or self.__splitter.count() < 2: return self.controlAreaVisible = visible splitter = self.__splitter # type: QSplitter w = splitter.widget(0) # Set minimum width to 1 (overrides minimumSizeHint) when control area # is not visible to allow the main area to shrink further. Reset the # minimum width with a 0 if control area is visible. w.setMinimumWidth(int(not visible)) sizes = splitter.sizes() current_size = sizes[0] if bool(current_size) == visible: return current_width = w.width() geom = self.geometry() frame = self.frameGeometry() framemargins = QMargins( frame.left() - geom.left(), frame.top() - geom.top(), frame.right() - geom.right(), frame.bottom() - geom.bottom() ) splitter.setSizes([int(visible), QWIDGETSIZE_MAX]) if not self.isWindow() or \ self.windowState() not in [Qt.WindowNoState, Qt.WindowActive]: # not a window or not in state where we can move move/resize return # force immediate resize recalculation splitter.refresh() self.layout().invalidate() self.layout().activate() if visible: # move left and expand by the exposing widget's width diffx = -w.width() diffw = w.width() else: # move right and shrink by the collapsing width diffx = current_width diffw = -current_width newgeom = QRect( geom.x() + diffx, geom.y(), geom.width() + diffw, geom.height() ) # bound/move by available geometry bounds = QApplication.desktop().availableGeometry(self) bounds = bounds.adjusted( framemargins.left(), framemargins.top(), -framemargins.right(), -framemargins.bottom() ) newsize = newgeom.size().boundedTo(bounds.size()) newgeom = QRect(newgeom.topLeft(), newsize) newgeom.moveLeft(max(newgeom.left(), bounds.left())) newgeom.moveRight(min(newgeom.right(), bounds.right())) self.setGeometry(newgeom)
def test_controlpointrect(self): control = ControlPointRect() rect = QGraphicsRectItem(QRectF(10, 10, 100, 200)) self.scene.addItem(rect) self.scene.addItem(control) control.setRect(rect.rect()) control.setFocus() control.rectChanged.connect(rect.setRect) control.setRect(QRectF(20, 20, 100, 200)) self.assertEqual(control.rect(), rect.rect()) self.assertEqual(control.rect(), QRectF(20, 20, 100, 200)) control.setControlMargins(5) self.assertEqual(control.controlMargins(), QMargins(5, 5, 5, 5)) control.rectEdited.connect(rect.setRect) self.view.show() self.app.exec_() self.assertEqual(rect.rect(), control.rect())
def __init__(self, parent=None, rect=None, constraints=0, **kwargs): QGraphicsObject.__init__(self, parent, **kwargs) self.setFlag(QGraphicsItem.ItemHasNoContents) self.setFlag(QGraphicsItem.ItemIsFocusable) self.__rect = rect if rect is not None else QRectF() self.__margins = QMargins() points = [ ControlPoint(self, ControlPoint.Left), ControlPoint(self, ControlPoint.Top), ControlPoint(self, ControlPoint.TopLeft), ControlPoint(self, ControlPoint.Right), ControlPoint(self, ControlPoint.TopRight), ControlPoint(self, ControlPoint.Bottom), ControlPoint(self, ControlPoint.BottomLeft), ControlPoint(self, ControlPoint.BottomRight), ] assert points == sorted(points, key=lambda p: p.anchor()) self.__points = dict((p.anchor(), p) for p in points) if self.scene(): self.__installFilter() for p in points: p.setFlag(QGraphicsItem.ItemIsFocusable) p.setFocusProxy(self) self.controlPoint(ControlPoint.Top).setConstraint(Qt.Vertical) self.controlPoint(ControlPoint.Bottom).setConstraint(Qt.Vertical) self.controlPoint(ControlPoint.Left).setConstraint(Qt.Horizontal) self.controlPoint(ControlPoint.Right).setConstraint(Qt.Horizontal) self.__constraints = constraints self.__activeControl = None self.__pointsLayout()
def framemargins(widget): frame, geom = widget.frameGeometry(), widget.geometry() return QMargins(geom.left() - frame.left(), geom.top() - frame.top(), geom.right() - frame.right(), geom.bottom() - frame.bottom())
def controlMargins(self): # type: () -> QMargins return QMargins(self.__margins)