qgraphicsitem.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,852 行 · 第 1/5 页

CPP
1,852
字号
}/*!    Gives keyboard input focus to this item. The \a focusReason argument will    be passed into any focus event generated by this function; it is used to    give an explanation of what caused the item to get focus.    Only items that set the ItemIsFocusable flag can accept keyboard focus.    If this item is not visible (i.e., isVisible() returns false), not    enabled, not associated with a scene, or if it already has input focus,    this function will do nothing.    As a result of calling this function, this item will receive a focus in    event with \a focusReason. If another item already has focus, that item    will first receive a focus out event indicating that it has lost input    focus.    \sa clearFocus(), hasFocus()*/void QGraphicsItem::setFocus(Qt::FocusReason focusReason){    if (!d_ptr->scene || !isVisible() || !isEnabled() || hasFocus())        return;    d_ptr->scene->setFocusItem(this, focusReason);}/*!    Takes keyboard input focus from the item.    If it has focus, a focus out event is sent to this item to tell it that it    is about to lose the focus.    Only items that set the ItemIsFocusable flag can accept keyboard focus.    \sa setFocus()*/void QGraphicsItem::clearFocus(){    if (!d_ptr->scene || !hasFocus())        return;    d_ptr->scene->setFocusItem(0);}/*!    Returns the position of the item in parent coordinates. If the item has no    parent, its position is given in scene coordinates.    The position of the item describes its origin (local coordinate    (0, 0)) in parent coordinates; this function returns the same as    mapToParent(0, 0).    For convenience, you can also call scenePos() to determine the    item's position in scene coordinates, regardless of its parent.    \sa x(), y(), setPos(), matrix(), {The Graphics View Coordinate System}*/QPointF QGraphicsItem::pos() const{    return d_ptr->pos;}/*!    \fn QGraphicsItem::x() const    This convenience function is equivalent to calling pos().x().    \sa y()*//*!    \fn QGraphicsItem::y() const    This convenience function is equivalent to calling pos().y().    \sa x()*//*!    Returns the item's position in scene coordinates. This is    equivalent to calling \c mapToScene(0, 0).    \sa pos(), sceneTransform(), {The Graphics View Coordinate System}*/QPointF QGraphicsItem::scenePos() const{    return mapToScene(0, 0);}/*!    Sets the position of the item to \a pos, which is in parent    coordinates.  For items with no parent, \a pos is in scene    coordinates.    The position of the item describes its origin (local coordinate    (0, 0)) in parent coordinates.    \sa pos(), scenePos(), {The Graphics View Coordinate System}*/void QGraphicsItem::setPos(const QPointF &pos){    if(d_ptr->pos == pos)        return;    // Notify the item that the position is changing.    QPointF newPos = itemChange(ItemPositionChange, pos).toPointF();    if (newPos == d_ptr->pos)        return;    // Update and repositition.    if (d_ptr->scene) {        qt_graphicsItem_fullUpdate(this);        prepareGeometryChange();    }    d_ptr->pos = newPos;    if (d_ptr->scene)        qt_graphicsItem_fullUpdate(this);    // Send post-notification.    itemChange(ItemPositionHasChanged, newPos);}/*!    \fn void QGraphicsItem::setPos(qreal x, qreal y)    \overload    This convenience function is equivalent to calling setPos(QPointF(\a x, \a    y)).*//*!    \fn void QGraphicsItem::moveBy(qreal dx, qreal dy)    Moves the item by \a dx points horizontally, and \a dy point    vertically. This function is equivalent to calling setPos(pos() +    QPointF(\a dx, \a dy)).*//*!    If this item is part of a scene that is viewed by a QGraphicsView, this    convenience function will attempt to scroll the view to ensure that \a    rect is visible inside the view's viewport. If \a rect is a null rect (the    default), QGraphicsItem will default to the item's bounding rect. \a xmargin    and \a ymargin are the number of pixels the view should use for margins.    If the specified rect cannot be reached, the contents are scrolled to the    nearest valid position.    If this item is not viewed by a QGraphicsView, this function does nothing.    \sa QGraphicsView::ensureVisible()*/void QGraphicsItem::ensureVisible(const QRectF &rect, int xmargin, int ymargin){    if (d_ptr->scene) {        QRectF sceneRect;        if (!rect.isNull())            sceneRect = sceneTransform().mapRect(rect);        else            sceneRect = sceneBoundingRect();        foreach (QGraphicsView *view, d_ptr->scene->d_func()->views)            view->ensureVisible(sceneRect, xmargin, ymargin);    }}/*!    \fn void QGraphicsItem::ensureVisible(qreal x, qreal y, qreal w, qreal h,    int xmargin = 50, int ymargin = 50)    This convenience function is equivalent to calling    ensureVisible(QRectF(\a x, \a y, \a w, \a h), \a xmargin, \a ymargin):*//*!    \obsolete    Returns the item's affine transformation matrix. This is a subset or the    item's full transformation matrix, and might not represent the item's full    transformation.    Use transform() instead.    \sa setTransform(), sceneTransform()*/QMatrix QGraphicsItem::matrix() const{    return transform().toAffine();}/*!    \since 4.3    Returns this item's transformation matrix. If no matrix has been set, the    identity matrix is returned.    \sa setTransform(), sceneTransform()*/QTransform QGraphicsItem::transform() const{    if (!d_ptr->hasTransform)        return QTransform();    return qVariantValue<QTransform>(d_ptr->extra(QGraphicsItemPrivate::ExtraTransform));}/*!    \obsolete    Use sceneTransform() instead.    \sa transform(), setTransform(), scenePos(), {The Graphics View Coordinate System}*/QMatrix QGraphicsItem::sceneMatrix() const{    return sceneTransform().toAffine();}/*!    \since 4.3    Returns this item's scene transformation matrix. This matrix can be used    to map coordinates and geometrical shapes from this item's local    coordinate system to the scene's coordinate system. To map coordinates    from the scene, you must first invert the returned matrix.    Example:    \code        QGraphicsRectItem rect;        rect.setPos(100, 100);        rect.sceneTransform().map(QPointF(0, 0));        // returns QPointF(100, 100);        rect.sceneTransform().inverted().map(QPointF(100, 100));        // returns QPointF(0, 0);    \endcode    Unlike transform(), which returns only an item's local transformation, this    function includes the item's (and any parents') position.    \sa transform(), setTransform(), scenePos(), {The Graphics View Coordinate System}*/QTransform QGraphicsItem::sceneTransform() const{    QTransform m = transform() * QTransform().translate(d_ptr->pos.x(), d_ptr->pos.y());    if (d_ptr->parent)        return m * d_ptr->parent->sceneTransform();    return m;}/*!    \since 4.3    Returns this item's device transformation matrix, using \a    viewportTransform to map from scene to device coordinates. This matrix can    be used to map coordinates and geometrical shapes from this item's local    coordinate system to the viewport's (or any device's) coordinate    system. To map coordinates from the viewport, you must first invert the    returned matrix.    Example:    \code        QGraphicsRectItem rect;        rect.setPos(100, 100);        rect.deviceTransform(view->viewportTransform()).map(QPointF(0, 0));        // returns the item's (0, 0) point in view's viewport coordinates        rect.deviceTransform(view->viewportTransform()).inverted().map(QPointF(100, 100));        // returns view's viewport's (100, 100) coordinate in item coordinates    \endcode    This function is the same as combining this item's scene transform with    the view's viewport transform, but is also understands    ItemIgnoresTransformations.    \sa transform(), setTransform(), scenePos(), {The Graphics View Coordinate System}*/QTransform QGraphicsItem::deviceTransform(const QTransform &viewportTransform) const{    // Find the topmost item that ignores view transformations.    const QGraphicsItem *untransformedAncestor = this;    QList<const QGraphicsItem *> parents;    while (untransformedAncestor && ((untransformedAncestor->d_ptr->ancestorFlags                                     & QGraphicsItemPrivate::AncestorIgnoresTransformations))) {        parents.prepend(untransformedAncestor);        untransformedAncestor = untransformedAncestor->parentItem();    }    if (!untransformedAncestor) {        // Assert in debug mode, continue in release.        Q_ASSERT_X(untransformedAncestor, "QGraphicsItem::deviceTransform",                   "Invalid object structure!");        return QTransform();    }    // First translate the base untransformable item.    QPointF mappedPoint = (untransformedAncestor->sceneTransform() * viewportTransform).map(QPointF(0, 0));    QTransform matrix;    matrix.translate(mappedPoint.x(), mappedPoint.y());    matrix = untransformedAncestor->transform() * matrix;    // Then transform and translate all children.    for (int i = 0; i < parents.size(); ++i) {        const QGraphicsItem *parent = parents.at(i);        QPointF pos = parent->pos();        QTransform moveMatrix;        moveMatrix.translate(pos.x(), pos.y());        matrix = (parent->transform() * moveMatrix) * matrix;    }    return matrix;}/*!    \obsolete    Sets the item's affine transformation matrix. This is a subset or the    item's full transformation matrix, and might not represent the item's full    transformation.    Use setTransform() instead.    \sa transform(), rotate(), scale(), shear(), translate(), {The Graphics View Coordinate System}*/void QGraphicsItem::setMatrix(const QMatrix &matrix, bool combine){    QTransform oldTransform = this->transform();    QTransform newTransform;    if (!combine)        newTransform = QTransform(matrix);    else        newTransform = QTransform(matrix) * oldTransform;    if (oldTransform == newTransform)        return;    // Notify the item that the matrix is changing.    QVariant variant;    qVariantSetValue<QMatrix>(variant, newTransform.toAffine());    newTransform = QTransform(qVariantValue<QMatrix>(itemChange(ItemMatrixChange, variant)));    if (oldTransform == newTransform)        return;    // Update and set the new transformation.    qt_graphicsItem_fullUpdate(this);    prepareGeometryChange();    d_ptr->hasTransform = !newTransform.isIdentity();    d_ptr->setExtra(QGraphicsItemPrivate::ExtraTransform, newTransform);    qt_graphicsItem_fullUpdate(this);    // Send post-notification.    itemChange(ItemTransformHasChanged, newTransform);}/*!    \since 4.3    Sets the item's current transformation matrix to \a matrix.    If \a combine is true, then \a matrix is combined with the current matrix;    otherwise, \a matrix \e replaces the current matrix. \a combine is false    by default.    To simplify interation with items using a transformed view, QGraphicsItem    provides mapTo... and mapFrom... functions that can translate between    items' and the scene's coordinates. For example, you can call mapToS

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?