Skip to content

Commit

Permalink
hiding the fill/centerIn implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
uwerat committed Oct 13, 2024
1 parent d2b12bc commit 5d9bf64
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 103 deletions.
240 changes: 172 additions & 68 deletions src/controls/QskItemAnchors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ namespace
}
}

bool isCenterAnchorPoint( Qt::AnchorPoint edge )
{
return ( edge == Qt::AnchorHorizontalCenter )
|| ( edge == Qt::AnchorVerticalCenter );
}

struct AnchorOperators
{
QQuickAnchorLine ( QQuickAnchors::*line ) () const;
Expand Down Expand Up @@ -116,18 +122,27 @@ QQuickItem* QskItemAnchors::attachedItem() const

QMarginsF QskItemAnchors::margins() const
{
if ( const auto anchors = qskGetAnchors( m_attachedItem ) )
{
return QMarginsF( anchors->leftMargin(), anchors->topMargin(),
anchors->rightMargin(), anchors->bottomMargin() );
}
const auto anchors = qskGetAnchors( m_attachedItem );
if ( anchors == nullptr )
return QMarginsF();

const auto d = QQuickAnchorsPrivate::get( anchors );

return QMarginsF();
const auto left = d->leftMarginExplicit ? d->leftMargin : d->margins;
const auto right = d->rightMarginExplicit ? d->rightMargin : d->margins;
const auto top = d->rightMarginExplicit ? d->rightMargin : d->margins;
const auto bottom = d->bottomMarginExplicit ? d->bottomMargin : d->margins;

return QMarginsF( left, top, right, bottom );
}

void QskItemAnchors::setMargins( const QMarginsF& margins )
{
if ( const auto anchors = qskGetOrCreateAnchors( m_attachedItem ) )
auto anchors = qskGetOrCreateAnchors( m_attachedItem );
if ( anchors == nullptr )
return;

if ( margins != this->margins() )
{
anchors->setLeftMargin( margins.left() );
anchors->setRightMargin( margins.right() );
Expand All @@ -136,17 +151,28 @@ void QskItemAnchors::setMargins( const QMarginsF& margins )
}
}

void QskItemAnchors::setCenterOffset( Qt::Orientation orientation, qreal offset )
void QskItemAnchors::setCenterOffsets(
qreal horizontalOffset, qreal verticalOffset )
{
if ( const auto anchors = qskGetOrCreateAnchors( m_attachedItem ) )
if ( auto anchors = qskGetOrCreateAnchors( m_attachedItem ) )
{
if ( orientation == Qt::Horizontal )
anchors->setHorizontalCenterOffset( offset );
else
anchors->setVerticalCenterOffset( offset );
anchors->setHorizontalCenterOffset( horizontalOffset );
anchors->setVerticalCenterOffset( verticalOffset );
}
}

void QskItemAnchors::setCenterOffset( Qt::Orientation orientation, qreal offset )
{
auto anchors = qskGetOrCreateAnchors( m_attachedItem );
if ( anchors == nullptr )
return;

if ( orientation == Qt::Horizontal )
anchors->setHorizontalCenterOffset( offset );
else
anchors->setVerticalCenterOffset( offset );
}

qreal QskItemAnchors::centerOffset( Qt::Orientation orientation )
{
if ( const auto anchors = qskGetOrCreateAnchors( m_attachedItem ) )
Expand All @@ -160,6 +186,22 @@ qreal QskItemAnchors::centerOffset( Qt::Orientation orientation )
return 0.0;
}

QQuickItem* QskItemAnchors::baseItem( Qt::AnchorPoint edge ) const
{
const auto anchors = qskGetAnchors( m_attachedItem );
if ( anchors == nullptr )
return nullptr;

if ( auto fill = anchors->fill() )
return !isCenterAnchorPoint( edge ) ? fill : nullptr;

if ( auto centerIn = anchors->centerIn() )
return isCenterAnchorPoint( edge ) ? centerIn : nullptr;

const auto& ops = operators( edge );
return ( ( anchors->*ops.line ) () ).item;
}

void QskItemAnchors::addAnchors( Qt::Corner corner,
QQuickItem* baseItem, Qt::Corner baseCorner )
{
Expand All @@ -179,24 +221,12 @@ void QskItemAnchors::addAnchors( Qt::Corner corner,
baseItem, anchorPoint( baseCorner, Qt::Vertical ) );
}

void QskItemAnchors::addAnchors( QQuickItem* baseItem, Qt::Orientations orientations )
{
if ( orientations & Qt::Horizontal )
{
addAnchor( Qt::AnchorLeft, baseItem, Qt::AnchorLeft );
addAnchor( Qt::AnchorRight, baseItem, Qt::AnchorRight );
}

if ( orientations & Qt::Vertical )
{
addAnchor( Qt::AnchorTop, baseItem, Qt::AnchorTop );
addAnchor( Qt::AnchorBottom, baseItem, Qt::AnchorBottom );
}
}

void QskItemAnchors::addAnchor( Qt::AnchorPoint edge, QQuickItem* baseItem,
Qt::AnchorPoint baseEdge )
{
if ( baseItem == nullptr )
return;

if ( const auto anchors = qskGetOrCreateAnchors( m_attachedItem ) )
{
const auto& ops = operators( edge );
Expand All @@ -206,74 +236,148 @@ void QskItemAnchors::addAnchor( Qt::AnchorPoint edge, QQuickItem* baseItem,

void QskItemAnchors::removeAnchor( Qt::AnchorPoint edge )
{
if ( const auto anchors = qskGetAnchors( m_attachedItem ) )
const auto anchors = qskGetAnchors( m_attachedItem );
if ( anchors == nullptr )
return;

if ( auto fill = anchors->fill() )
{
const auto& ops = operators( edge );
( anchors->*ops.resetLine ) ();
}
}
if ( !isCenterAnchorPoint( edge ) )
{
anchors->resetFill();

// setting the other borders as anchors
for ( auto anchorPoint : { Qt::AnchorLeft, Qt::AnchorRight,
Qt::AnchorTop, Qt::AnchorBottom } )
{
if ( edge != anchorPoint )
addAnchor( anchorPoint, fill, anchorPoint );
}
}

QQuickItem* QskItemAnchors::baseItem( Qt::AnchorPoint edge ) const
{
if ( const auto anchors = qskGetAnchors( m_attachedItem ) )
return;
}

if ( auto centerIn = anchors->centerIn() )
{
const auto& ops = operators( edge );
return ( ( anchors->*ops.line ) () ).item;
if ( isCenterAnchorPoint( edge ) )
{
anchors->resetCenterIn();

// setting the other direction as anchor
const auto otherEdge = ( edge == Qt::AnchorHorizontalCenter )
? Qt::AnchorVerticalCenter : Qt::AnchorHorizontalCenter;

addAnchor( otherEdge, centerIn, otherEdge );
}

return;
}

return nullptr;
const auto& ops = operators( edge );
( anchors->*ops.resetLine ) ();
}

Qt::AnchorPoint QskItemAnchors::basePosition( Qt::AnchorPoint edge ) const
void QskItemAnchors::clearAnchors()
{
if ( const auto anchors = qskGetAnchors( m_attachedItem ) )
{
/*
Anchoring to the baseline of the attachedItem might have been
done in QML code. As Qt::AnchorPoint does not have a corresponding
value for it and QSkinny does not support the baseline concept at all
we are lying and report Qt::AnchorTop instead. Hm ...
*/
anchors->resetFill();
anchors->resetCenterIn();

const auto& ops = operators( edge );
return toAnchorPoint( ( ( anchors->*ops.line ) () ).anchorLine );
for ( int i = 0; i < 6; i++ )
{
const auto& ops = operators( static_cast< Qt::AnchorPoint >( i ) );
( anchors->*ops.resetLine ) ();
}
}

return Qt::AnchorLeft; // something
}

void QskItemAnchors::setControlItem( QQuickItem* item, bool adjustSize )
void QskItemAnchors::setBorderAnchors( QQuickItem* baseItem, Qt::Orientations orientations )
{
if ( const auto anchors = qskGetOrCreateAnchors( m_attachedItem ) )
if ( baseItem == nullptr || m_attachedItem == nullptr )
return;

switch( orientations )
{
if ( adjustSize )
anchors->setFill( item );
else
anchors->setCenterIn( item );
case Qt::Horizontal:
{
addAnchor( Qt::AnchorLeft, baseItem, Qt::AnchorLeft );
addAnchor( Qt::AnchorRight, baseItem, Qt::AnchorRight );
break;
}

case Qt::Vertical:
{
addAnchor( Qt::AnchorTop, baseItem, Qt::AnchorTop );
addAnchor( Qt::AnchorBottom, baseItem, Qt::AnchorBottom );
break;
}

case Qt::Horizontal | Qt::Vertical:
{
auto anchors = qskGetOrCreateAnchors( m_attachedItem );
for ( int i = 0; i < 6; i++ )
{
const auto& ops = operators( static_cast< Qt::AnchorPoint >( i ) );
( anchors->*ops.resetLine ) ();
}

anchors->setFill( baseItem );
break;
}
}
}

void QskItemAnchors::removeControlItem( bool adjustSize )
void QskItemAnchors::setCenterAnchors( QQuickItem* baseItem, Qt::Orientations orientations )
{
if ( auto anchors = qskGetAnchors( m_attachedItem ) )
if ( baseItem == nullptr || m_attachedItem == nullptr )
return;

switch( orientations )
{
if ( adjustSize )
anchors->resetFill();
else
anchors->resetCenterIn();
case Qt::Horizontal:
{
addAnchor( Qt::AnchorHorizontalCenter, baseItem, Qt::AnchorHorizontalCenter );
break;
}

case Qt::Vertical:
{
addAnchor( Qt::AnchorVerticalCenter, baseItem, Qt::AnchorVerticalCenter );
break;
}

case Qt::Horizontal | Qt::Vertical:
{
auto anchors = qskGetOrCreateAnchors( m_attachedItem );

for ( int i = 0; i < 6; i++ )
{
const auto& ops = operators( static_cast< Qt::AnchorPoint >( i ) );
( anchors->*ops.resetLine ) ();
}

anchors->setCenterIn( baseItem );
break;
}
}
}

QQuickItem* QskItemAnchors::controlItem( bool adjustSize ) const
Qt::AnchorPoint QskItemAnchors::basePosition( Qt::AnchorPoint edge ) const
{
if ( const auto anchors = qskGetAnchors( m_attachedItem ) )
{
if ( adjustSize )
return anchors->fill();
else
return anchors->centerIn();
/*
Anchoring to the baseline of the attachedItem might have been
done in QML code. As Qt::AnchorPoint does not have a corresponding
value for it and QSkinny does not support the baseline concept at all
we are lying and report Qt::AnchorTop instead. Hm ...
*/

const auto& ops = operators( edge );
return toAnchorPoint( ( ( anchors->*ops.line ) () ).anchorLine );
}

return nullptr;
return Qt::AnchorLeft; // something
}
Loading

0 comments on commit 5d9bf64

Please sign in to comment.