qskinny/src/layouts/QskDrawer.cpp

171 lines
4.0 KiB
C++
Raw Normal View History

2023-05-01 23:00:15 +00:00
#include "QskDrawer.h"
2023-04-25 21:28:42 +00:00
#include "QskAspect.h"
2023-04-26 21:35:28 +00:00
#include "QskControl.h"
2023-05-01 23:00:15 +00:00
2023-04-25 21:07:51 +00:00
#include <QskPopup.h>
#include <QskBox.h>
#include <QskAnimationHint.h>
#include <QskQuick.h>
2023-05-01 23:00:15 +00:00
2023-04-26 21:54:34 +00:00
QSK_SUBCONTROL( QskDrawer, Panel )
2023-05-01 23:00:15 +00:00
class QskDrawer::PrivateData {
public:
2023-04-26 21:35:28 +00:00
QskControl* content;
QskBox* contentBox;
2023-05-01 23:00:15 +00:00
Qt::Edge edge = Qt::LeftEdge;
2023-05-01 15:07:21 +00:00
Qt::Alignment alignment = Qt::AlignCenter;
2023-05-01 23:00:15 +00:00
};
QskDrawer::QskDrawer( QQuickItem* parentItem ) :
Inherited ( parentItem )
, m_data( new PrivateData { } )
{
setZ( 1 );
setPopupFlag( PopupFlag::CloseOnPressOutside, true );
2023-04-26 21:35:28 +00:00
m_data->contentBox = new QskBox(this);
2023-04-26 21:54:34 +00:00
m_data->contentBox->setSubcontrolProxy( QskBox::Panel, Panel );
2023-05-01 15:07:21 +00:00
setAnimationHint( Panel | QskAspect::Position, QskAnimationHint( 5000 ) );
2023-04-25 20:43:51 +00:00
2023-04-26 21:54:34 +00:00
setFaderAspect( Panel | QskAspect::Metric );
2023-05-01 15:07:21 +00:00
2023-04-25 21:28:42 +00:00
setSkinHint( Overlay | QskAspect::Style, false );
2023-05-01 23:00:15 +00:00
2023-04-25 19:40:24 +00:00
connect(this, &QskDrawer::closed, this, [this](){
2023-04-26 21:54:34 +00:00
startTransition( Panel | QskAspect::Metric,
animationHint( Panel | QskAspect::Position ),
2023-04-25 21:28:08 +00:00
0.0, 1.0 );
2023-04-25 19:40:24 +00:00
});
2023-05-01 23:00:15 +00:00
}
QskDrawer::~QskDrawer()
{
}
2023-05-01 15:07:21 +00:00
Qt::Edge QskDrawer::edge() const {
2023-05-01 23:00:15 +00:00
return m_data->edge;
}
2023-05-01 15:07:21 +00:00
Qt::Alignment QskDrawer::alignment() const {
return m_data->alignment;
}
2023-05-01 23:00:15 +00:00
void QskDrawer::setEdge( Qt::Edge edge ) {
if( m_data->edge == edge ) {
return;
}
2023-05-01 15:07:21 +00:00
update();
2023-05-01 23:00:15 +00:00
m_data->edge = edge;
edgeChanged( edge );
}
2023-05-01 15:07:21 +00:00
void QskDrawer::setAlignment( Qt::Alignment alignment ) {
if( m_data->alignment == alignment ) {
return;
}
update();
m_data->alignment = alignment;
alignmentChanged( alignment );
}
2023-04-26 21:35:28 +00:00
void QskDrawer::setContent( QskControl* content ) {
content->setParentItem( m_data->contentBox );
m_data->content = content;
2023-05-01 23:00:15 +00:00
}
void QskDrawer::updateLayout() {
2023-05-01 15:07:21 +00:00
const auto& contentSize = m_data->content->preferredSize();
const auto& parentSize = parentItem()->size();
2023-05-01 23:00:15 +00:00
switch( m_data->edge ) {
case Qt::Edge::LeftEdge:
{
2023-05-01 15:07:21 +00:00
qreal x = metric( faderAspect() ) * contentSize.width() * -1.0;
qreal y = 0;
if( alignment().testFlag( Qt::AlignVCenter ) ) {
y = ( parentSize.height() - contentSize.height() ) / 2.0;
} else if ( alignment().testFlag( Qt::AlignBottom ) ) {
y = ( parentSize.height() - contentSize.height() );
}
qskSetItemGeometry( m_data->contentBox,
2023-05-01 15:07:21 +00:00
x, y,
contentSize.width(), parentSize.height() );
break;
}
2023-05-01 23:00:15 +00:00
case Qt::Edge::RightEdge:
{
2023-05-01 15:07:21 +00:00
qreal x = ( metric( faderAspect() ) * contentSize.width() )
+ parentSize.width()
- contentSize.width();
qreal y = 0;
if( alignment().testFlag( Qt::AlignVCenter ) ) {
y = ( parentSize.height() - contentSize.height() ) / 2.0;
} else if ( alignment().testFlag( Qt::AlignBottom ) ) {
y = ( parentSize.height() - contentSize.height() );
}
qskSetItemGeometry( m_data->contentBox,
2023-05-01 15:07:21 +00:00
x, y,
contentSize.width(),
parentSize.height() );
break;
}
2023-05-01 23:00:15 +00:00
case Qt::Edge::TopEdge:
{
2023-05-01 15:07:21 +00:00
qreal x = 0;
qreal y = metric( faderAspect() ) * contentSize.height();
if( alignment().testFlag( Qt::AlignCenter) ) {
x = ( parentSize.width() - contentSize.width() ) / 2;
} else if( alignment().testFlag( Qt::AlignRight) ) {
x = ( parentSize.width() - contentSize.width() );
}
qskSetItemGeometry( m_data->contentBox,
2023-05-01 15:07:21 +00:00
x, -y,
parentSize.width(), contentSize.height());
break;
}
2023-05-01 23:00:15 +00:00
case Qt::Edge::BottomEdge:
{
2023-05-01 15:07:21 +00:00
qreal x = 0;
qreal y = metric( faderAspect() ) * contentSize.height() + parentSize.height() - contentSize.height();
if( alignment().testFlag( Qt::AlignCenter) ) {
x = ( parentSize.width() - contentSize.width() ) / 2;
} else if( alignment().testFlag( Qt::AlignRight) ) {
x = ( parentSize.width() - contentSize.width() );
}
qskSetItemGeometry( m_data->contentBox,
2023-05-01 15:07:21 +00:00
x, y,
parentSize.width(), contentSize.height());
break;
}
2023-05-01 23:00:15 +00:00
break;
2023-05-01 15:07:21 +00:00
}
2023-05-01 23:00:15 +00:00
Inherited::updateLayout();
}
void QskDrawer::aboutToShow()
{
2023-04-26 21:54:34 +00:00
startTransition( Panel | QskAspect::Metric,
animationHint( Panel | QskAspect::Position ),
2023-04-25 21:28:08 +00:00
1.0, 0.0 );
2023-05-01 23:00:15 +00:00
Inherited::aboutToShow();
}
#include "moc_QskDrawer.cpp"