! This method sets the parent anchor of the X coordinate to \a parentAnchor. For a detailed description of what a parent anchor is, see the documentation of \ref setParentAnchor. \see setParentAnchor, setParentAnchorY */
| 12376 | \see setParentAnchor, setParentAnchorY |
| 12377 | */ |
| 12378 | bool QCPItemPosition::setParentAnchorX(QCPItemAnchor *parentAnchor, bool keepPixelPosition) |
| 12379 | { |
| 12380 | // make sure self is not assigned as parent: |
| 12381 | if (parentAnchor == this) |
| 12382 | { |
| 12383 | qDebug() << Q_FUNC_INFO << "can't set self as parent anchor" << reinterpret_cast<quintptr>(parentAnchor); |
| 12384 | return false; |
| 12385 | } |
| 12386 | // make sure no recursive parent-child-relationships are created: |
| 12387 | QCPItemAnchor *currentParent = parentAnchor; |
| 12388 | while (currentParent) |
| 12389 | { |
| 12390 | if (QCPItemPosition *currentParentPos = currentParent->toQCPItemPosition()) |
| 12391 | { |
| 12392 | // is a QCPItemPosition, might have further parent, so keep iterating |
| 12393 | if (currentParentPos == this) |
| 12394 | { |
| 12395 | qDebug() << Q_FUNC_INFO << "can't create recursive parent-child-relationship" << reinterpret_cast<quintptr>(parentAnchor); |
| 12396 | return false; |
| 12397 | } |
| 12398 | currentParent = currentParentPos->parentAnchorX(); |
| 12399 | } else |
| 12400 | { |
| 12401 | // is a QCPItemAnchor, can't have further parent. Now make sure the parent items aren't the |
| 12402 | // same, to prevent a position being child of an anchor which itself depends on the position, |
| 12403 | // because they're both on the same item: |
| 12404 | if (currentParent->mParentItem == mParentItem) |
| 12405 | { |
| 12406 | qDebug() << Q_FUNC_INFO << "can't set parent to be an anchor which itself depends on this position" << reinterpret_cast<quintptr>(parentAnchor); |
| 12407 | return false; |
| 12408 | } |
| 12409 | break; |
| 12410 | } |
| 12411 | } |
| 12412 | |
| 12413 | // if previously no parent set and PosType is still ptPlotCoords, set to ptAbsolute: |
| 12414 | if (!mParentAnchorX && mPositionTypeX == ptPlotCoords) |
| 12415 | setTypeX(ptAbsolute); |
| 12416 | |
| 12417 | // save pixel position: |
| 12418 | QPointF pixelP; |
| 12419 | if (keepPixelPosition) |
| 12420 | pixelP = pixelPosition(); |
| 12421 | // unregister at current parent anchor: |
| 12422 | if (mParentAnchorX) |
| 12423 | mParentAnchorX->removeChildX(this); |
| 12424 | // register at new parent anchor: |
| 12425 | if (parentAnchor) |
| 12426 | parentAnchor->addChildX(this); |
| 12427 | mParentAnchorX = parentAnchor; |
| 12428 | // restore pixel position under new parent: |
| 12429 | if (keepPixelPosition) |
| 12430 | setPixelPosition(pixelP); |
| 12431 | else |
| 12432 | setCoords(0, coords().y()); |
| 12433 | return true; |
| 12434 | } |
| 12435 |
no test coverage detected