2019-08-30 11:13:32 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
* This file may be used under the terms of the QSkinny License, Version 1.0
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "AnchorBox.h"
|
|
|
|
|
|
|
|
#include "Solver.h"
|
|
|
|
#include "Constraint.h"
|
|
|
|
#include "Variable.h"
|
|
|
|
#include "Expression.h"
|
|
|
|
|
|
|
|
#include <QskEvent.h>
|
|
|
|
#include <QskQuick.h>
|
2020-04-05 17:25:59 +00:00
|
|
|
#include <QskLayoutHint.h>
|
2019-08-30 11:13:32 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
static inline Qt::Orientation qskOrientation( int edge )
|
|
|
|
{
|
|
|
|
return ( edge <= Qt::AnchorRight ) ? Qt::Horizontal : Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
static inline Qt::AnchorPoint qskAnchorPoint(
|
|
|
|
Qt::Corner corner, Qt::Orientation orientation )
|
|
|
|
{
|
|
|
|
if ( orientation == Qt::Horizontal )
|
|
|
|
return ( corner & 0x1 ) ? Qt::AnchorRight : Qt::AnchorLeft;
|
|
|
|
else
|
|
|
|
return ( corner >= 0x2 ) ? Qt::AnchorBottom : Qt::AnchorTop;
|
|
|
|
}
|
|
|
|
|
2019-08-30 11:13:32 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class Geometry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Expression expressionAt( int anchorPoint )
|
|
|
|
{
|
|
|
|
switch( anchorPoint )
|
|
|
|
{
|
|
|
|
case Qt::AnchorLeft:
|
2020-04-04 16:47:44 +00:00
|
|
|
return Term( m_left );
|
2019-08-30 11:13:32 +00:00
|
|
|
|
|
|
|
case Qt::AnchorHorizontalCenter:
|
|
|
|
return centerH();
|
|
|
|
|
|
|
|
case Qt::AnchorRight:
|
2020-04-04 16:47:44 +00:00
|
|
|
return Term( m_right );
|
2019-08-30 11:13:32 +00:00
|
|
|
|
|
|
|
case Qt::AnchorTop:
|
2020-04-04 16:47:44 +00:00
|
|
|
return Term( m_top );
|
2019-08-30 11:13:32 +00:00
|
|
|
|
|
|
|
case Qt::AnchorVerticalCenter:
|
|
|
|
return centerV();
|
|
|
|
|
|
|
|
case Qt::AnchorBottom:
|
2020-04-04 16:47:44 +00:00
|
|
|
return Term( m_bottom );
|
2019-08-30 11:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Expression();
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
inline Expression length( Qt::Orientation orientation )
|
2019-08-30 11:13:32 +00:00
|
|
|
{
|
|
|
|
return ( orientation == Qt::Horizontal ) ? width() : height();
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
inline QRectF rect() const
|
2019-08-30 11:13:32 +00:00
|
|
|
{
|
2020-04-04 16:47:44 +00:00
|
|
|
return QRectF( m_left.value(), m_top.value(),
|
|
|
|
m_right.value() - m_left.value(), m_bottom.value() - m_top.value() );
|
2019-08-30 11:13:32 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
inline QSizeF size() const
|
|
|
|
{
|
|
|
|
return QSizeF( m_right.value() - m_left.value(),
|
|
|
|
m_bottom.value() - m_top.value() );
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
inline Expression width() const { return m_right - m_left; }
|
|
|
|
inline Expression height() const { return m_bottom - m_top; }
|
2019-08-30 11:13:32 +00:00
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
inline Expression centerH() const { return m_left + 0.5 * width(); }
|
|
|
|
inline Expression centerV() const { return m_top + 0.5 * height(); }
|
2019-08-30 11:13:32 +00:00
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
inline const Variable& left() const { return m_left; }
|
|
|
|
inline const Variable& right() const { return m_right; }
|
|
|
|
inline const Variable& top() const { return m_top; }
|
|
|
|
inline const Variable& bottom() const { return m_bottom; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Variable m_left, m_right, m_top, m_bottom;
|
2019-08-30 11:13:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Anchor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QQuickItem* item1 = nullptr;
|
|
|
|
Qt::AnchorPoint edge1;
|
|
|
|
|
|
|
|
QQuickItem* item2 = nullptr;
|
|
|
|
Qt::AnchorPoint edge2;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class AnchorBox::PrivateData
|
|
|
|
{
|
|
|
|
public:
|
2020-04-04 16:47:44 +00:00
|
|
|
void setItemGeometries( const AnchorBox*, const QRectF& );
|
|
|
|
|
|
|
|
void setupAnchorConstraints( Solver& );
|
2020-04-05 17:25:59 +00:00
|
|
|
void setupSizeConstraints( const AnchorBox*, bool, Solver& );
|
2019-08-30 11:13:32 +00:00
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
public:
|
2019-08-30 11:13:32 +00:00
|
|
|
QMap< QQuickItem*, Geometry > geometries;
|
|
|
|
QVector< Anchor > anchors;
|
|
|
|
};
|
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
void AnchorBox::PrivateData::setupAnchorConstraints( Solver& solver )
|
2019-08-30 11:13:32 +00:00
|
|
|
{
|
|
|
|
for ( const auto& anchor : anchors )
|
|
|
|
{
|
|
|
|
auto& r1 = geometries[ anchor.item1 ];
|
|
|
|
auto& r2 = geometries[ anchor.item2 ];
|
|
|
|
|
|
|
|
solver.addConstraint( r1.expressionAt( anchor.edge1 )
|
|
|
|
== r2.expressionAt( anchor.edge2 ) );
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
{
|
2020-04-04 16:47:44 +00:00
|
|
|
const auto o = qskOrientation( anchor.edge1 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
A constraint with medium strength to make anchored item
|
|
|
|
being stretched according to their stretch factors s1, s2.
|
|
|
|
( For the moment we don't support having specific factors. )
|
|
|
|
*/
|
2019-08-30 11:13:32 +00:00
|
|
|
const auto s1 = 1.0;
|
|
|
|
const auto s2 = 1.0;
|
|
|
|
|
|
|
|
Constraint c( r1.length( o ) * s1 == r2.length( o ) * s2, Strength::medium );
|
|
|
|
solver.addConstraint( c );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-04-04 16:47:44 +00:00
|
|
|
}
|
2019-08-30 11:13:32 +00:00
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
void AnchorBox::PrivateData::setupSizeConstraints(
|
|
|
|
const AnchorBox* box, bool preferred, Solver& solver )
|
2020-04-04 16:47:44 +00:00
|
|
|
{
|
2019-08-30 11:13:32 +00:00
|
|
|
for ( auto it = geometries.begin(); it != geometries.end(); ++it )
|
|
|
|
{
|
|
|
|
const auto item = it.key();
|
2020-04-05 17:25:59 +00:00
|
|
|
if ( item == box )
|
|
|
|
continue;
|
|
|
|
|
2019-08-30 11:13:32 +00:00
|
|
|
auto& r = it.value();
|
|
|
|
|
|
|
|
{
|
2020-04-04 16:47:44 +00:00
|
|
|
// minimum size
|
2019-09-10 15:01:47 +00:00
|
|
|
const auto minSize = qskSizeConstraint( item, Qt::MinimumSize );
|
|
|
|
|
|
|
|
if ( minSize.width() >= 0.0 )
|
2020-04-04 16:47:44 +00:00
|
|
|
solver.addConstraint( r.right() >= r.left() + minSize.width() );
|
2019-09-10 15:01:47 +00:00
|
|
|
|
|
|
|
if ( minSize.height() >= 0.0 )
|
2020-04-04 16:47:44 +00:00
|
|
|
solver.addConstraint( r.bottom() >= r.top() + minSize.height() );
|
2019-08-30 11:13:32 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
if ( preferred )
|
2019-08-30 11:13:32 +00:00
|
|
|
{
|
2020-04-04 16:47:44 +00:00
|
|
|
// preferred size
|
2019-09-10 15:01:47 +00:00
|
|
|
const auto prefSize = qskSizeConstraint( item, Qt::PreferredSize );
|
2019-08-30 11:13:32 +00:00
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
Constraint c1( r.right() == r.left() + prefSize.width(), Strength::strong );
|
2019-08-30 11:13:32 +00:00
|
|
|
solver.addConstraint( c1 );
|
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
Constraint c2( r.bottom() == r.top() + prefSize.height(), Strength::strong );
|
2019-08-30 11:13:32 +00:00
|
|
|
solver.addConstraint( c2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-04-04 16:47:44 +00:00
|
|
|
// maximum size
|
2019-09-10 15:01:47 +00:00
|
|
|
const auto maxSize = qskSizeConstraint( item, Qt::MaximumSize );
|
|
|
|
if ( maxSize.width() >= 0.0 )
|
2020-04-04 16:47:44 +00:00
|
|
|
solver.addConstraint( r.right() <= r.left() + maxSize.width() );
|
2019-08-30 11:13:32 +00:00
|
|
|
|
2019-09-10 15:01:47 +00:00
|
|
|
if ( maxSize.height() >= 0.0 )
|
2020-04-04 16:47:44 +00:00
|
|
|
solver.addConstraint( r.bottom() <= r.top() + maxSize.height() );
|
2019-08-30 11:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:47:44 +00:00
|
|
|
void AnchorBox::PrivateData::setItemGeometries(
|
|
|
|
const AnchorBox* box, const QRectF& rect )
|
|
|
|
{
|
2020-04-05 17:25:59 +00:00
|
|
|
// Unefficient as we are always starting from scratch TODO ...
|
2020-04-04 16:47:44 +00:00
|
|
|
Solver solver;
|
2020-04-05 17:25:59 +00:00
|
|
|
setupAnchorConstraints( solver );
|
|
|
|
setupSizeConstraints( box, true, solver );
|
2020-04-04 16:47:44 +00:00
|
|
|
|
|
|
|
const auto& r0 = geometries[ const_cast< AnchorBox* >( box ) ];
|
|
|
|
|
|
|
|
solver.addConstraint( r0.left() == rect.left() );
|
|
|
|
solver.addConstraint( r0.right() == rect.right() );
|
|
|
|
solver.addConstraint( r0.top() == rect.top() );
|
|
|
|
solver.addConstraint( r0.bottom() == rect.bottom() );
|
|
|
|
|
|
|
|
solver.updateVariables();
|
|
|
|
|
|
|
|
for ( auto it = geometries.begin(); it != geometries.end(); ++it )
|
|
|
|
qskSetItemGeometry( it.key(), it.value().rect() );
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
qDebug() << "=== Rect:" << rect;
|
|
|
|
for ( auto it = geometries.begin(); it != geometries.end(); ++it )
|
|
|
|
qDebug() << it.key()->objectName() << it.value().rect();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-30 11:13:32 +00:00
|
|
|
AnchorBox::AnchorBox( QQuickItem* parent )
|
|
|
|
: QskControl( parent )
|
|
|
|
, m_data( new PrivateData )
|
|
|
|
{
|
|
|
|
(void)m_data->geometries[ this ];
|
|
|
|
}
|
|
|
|
|
|
|
|
AnchorBox::~AnchorBox()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
void AnchorBox::addAnchors( QQuickItem* item, Qt::Orientations orientations )
|
|
|
|
{
|
|
|
|
addAnchors( item, this, orientations );
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorBox::addAnchors( QQuickItem* item1,
|
|
|
|
QQuickItem* item2, Qt::Orientations orientations )
|
|
|
|
{
|
|
|
|
if ( orientations & Qt::Horizontal )
|
|
|
|
{
|
|
|
|
addAnchor( item1, Qt::AnchorLeft, item2, Qt::AnchorLeft );
|
|
|
|
addAnchor( item1, Qt::AnchorRight, item2, Qt::AnchorRight );
|
|
|
|
}
|
|
|
|
if ( orientations & Qt::Vertical )
|
|
|
|
{
|
|
|
|
addAnchor( item1, Qt::AnchorTop, item2, Qt::AnchorTop );
|
|
|
|
addAnchor( item1, Qt::AnchorBottom, item2, Qt::AnchorBottom );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorBox::addAnchors( QQuickItem* item, Qt::Corner corner )
|
|
|
|
{
|
|
|
|
addAnchors( item, corner, this, corner );
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorBox::addAnchors( QQuickItem* item1,
|
|
|
|
Qt::Corner corner1, QQuickItem* item2, Qt::Corner corner2 )
|
|
|
|
{
|
|
|
|
addAnchor( item1, qskAnchorPoint( corner1, Qt::Horizontal ),
|
|
|
|
item2, qskAnchorPoint( corner2, Qt::Horizontal ) );
|
|
|
|
|
|
|
|
addAnchor( item1, qskAnchorPoint( corner1, Qt::Vertical ),
|
|
|
|
item2, qskAnchorPoint( corner2, Qt::Vertical ) );
|
|
|
|
}
|
|
|
|
|
2019-08-30 11:13:32 +00:00
|
|
|
void AnchorBox::addAnchor( QQuickItem* item,
|
|
|
|
Qt::AnchorPoint edge1, Qt::AnchorPoint edge2 )
|
|
|
|
{
|
|
|
|
addAnchor( item, edge1, this, edge2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorBox::addAnchor( QQuickItem* item1, Qt::AnchorPoint edge1,
|
|
|
|
QQuickItem* item2, Qt::AnchorPoint edge2 )
|
|
|
|
{
|
|
|
|
if ( item1 == item2 || item1 == nullptr || item2 == nullptr )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( item1 != this )
|
|
|
|
{
|
|
|
|
if ( item1->parent() == nullptr )
|
|
|
|
item1->setParent( this );
|
|
|
|
|
|
|
|
if ( item1->parentItem() != this )
|
|
|
|
item1->setParentItem( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( item2 != this )
|
|
|
|
{
|
|
|
|
if ( item2->parent() == nullptr )
|
|
|
|
item2->setParent( this );
|
|
|
|
|
|
|
|
if ( item2->parentItem() != this )
|
|
|
|
item2->setParentItem( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)m_data->geometries[ item1 ];
|
|
|
|
(void)m_data->geometries[ item2 ];
|
|
|
|
|
|
|
|
Anchor anchor;
|
|
|
|
anchor.item1 = item1;
|
|
|
|
anchor.edge1 = edge1;
|
|
|
|
anchor.item2 = item2;
|
|
|
|
anchor.edge2 = edge2;
|
|
|
|
|
|
|
|
m_data->anchors += anchor;
|
|
|
|
}
|
|
|
|
|
2020-04-05 17:25:59 +00:00
|
|
|
QSizeF AnchorBox::layoutSizeHint( Qt::SizeHint which, const QSizeF& constraint ) const
|
2019-08-30 11:13:32 +00:00
|
|
|
{
|
2020-04-05 17:25:59 +00:00
|
|
|
if ( constraint.width() >= 0.0 || constraint.height() >= 0.0 )
|
|
|
|
{
|
|
|
|
// TODO ...
|
|
|
|
return QSizeF();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& r0 = m_data->geometries[ const_cast< AnchorBox* >( this ) ];
|
|
|
|
|
|
|
|
Solver solver;
|
|
|
|
m_data->setupAnchorConstraints( solver );
|
|
|
|
m_data->setupSizeConstraints( this, which == Qt::PreferredSize, solver );
|
|
|
|
|
|
|
|
if ( which != Qt::PreferredSize )
|
|
|
|
{
|
|
|
|
const qreal b = ( which == Qt::MinimumSize ) ? 0.0 : QskLayoutHint::unlimited;
|
|
|
|
|
|
|
|
// why do we need strong here ?
|
|
|
|
solver.addConstraint( Constraint( r0.width() == b, Strength::strong ) );
|
|
|
|
solver.addConstraint( Constraint( r0.height() == b, Strength::strong ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
solver.updateVariables();
|
|
|
|
|
|
|
|
return r0.size();
|
2019-08-30 11:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorBox::geometryChangeEvent( QskGeometryChangeEvent* event )
|
|
|
|
{
|
|
|
|
Inherited::geometryChangeEvent( event );
|
|
|
|
|
|
|
|
if ( event->isResized() )
|
|
|
|
polish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnchorBox::updateLayout()
|
|
|
|
{
|
|
|
|
if ( !maybeUnresized() )
|
2020-04-04 16:47:44 +00:00
|
|
|
m_data->setItemGeometries( this, layoutRect() );
|
2019-08-30 11:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_AnchorBox.cpp"
|