qskinny/playground/parrots/main.cpp

209 lines
4.9 KiB
C++
Raw Normal View History

/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#include <SkinnyNamespace.h>
#include <QskFunctions.h>
#include <QskLinearBox.h>
#include <QskPushButton.h>
#include <QskQuick.h>
#include <QskWindow.h>
#include <QskGraphicLabel.h>
#include <QskGraphic.h>
#include <QskEvent.h>
2023-12-15 18:16:28 +00:00
#include <QskRgbValue.h>
#include <QGuiApplication>
#include <QDebug>
#include <SkinnyShortcut.h>
#include <cmath>
2023-12-08 14:47:32 +00:00
#include "Overlay.h"
2023-12-04 11:48:11 +00:00
2023-12-15 18:16:28 +00:00
class Image : public QskGraphicLabel
{
public:
Image( QQuickItem* parent = nullptr )
: QskGraphicLabel( parent )
{
const QImage image( ":/images/parrots.jpg" );
setGraphic( QskGraphic::fromImage( image ) );
}
};
class ForegroundItem : public QskLinearBox
{
public:
ForegroundItem( QQuickItem* parent = nullptr )
: QskLinearBox( Qt::Vertical, parent )
{
setMargins( 20 );
#if 1
2023-12-15 18:16:28 +00:00
auto label = new Image( this );
label->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
label->setLayoutAlignmentHint( Qt::AlignCenter );
label->setObjectName( "miniParrots" );
#endif
auto button = new QskPushButton( "Button", this );
button->setLayoutAlignmentHint( Qt::AlignHCenter | Qt::AlignBottom );
button->setObjectName( "button" );
setObjectName( "foreground" );
}
};
2023-12-08 14:47:32 +00:00
class OverlayBox : public Overlay
{
2023-12-08 14:47:32 +00:00
using Inherited = Overlay;
public:
2023-12-08 14:47:32 +00:00
OverlayBox( QQuickItem* parent = nullptr )
2023-12-07 10:24:47 +00:00
: Inherited( parent )
{
setObjectName( "overlay" );
}
protected:
2023-12-04 11:48:11 +00:00
void geometryChange( const QRectF& newRect, const QRectF& oldRect ) override
{
Inherited::geometryChange( newRect, oldRect );
const auto rect = qskItemRect( this );
const auto children = childItems();
for ( auto child : children )
{
if ( qskIsAdjustableByLayout( child ) )
{
const auto r = qskConstrainedItemRect( child, rect );
qskSetItemGeometry( child, r );
}
}
}
};
class BackgroundItem : public QskControl
{
2023-12-07 10:24:47 +00:00
using Inherited = QskControl;
public:
BackgroundItem( QQuickItem* parent = nullptr )
: QskControl( parent )
{
2023-12-15 18:16:28 +00:00
setObjectName( "background" );
2023-12-15 18:16:28 +00:00
#if 1
m_label = new Image( this );
m_label->setFillMode( QskGraphicLabel::Stretch );
m_label->setObjectName( "parrots" );
#endif
startTimer( 20 );
}
2023-12-07 10:24:47 +00:00
protected:
void timerEvent( QTimerEvent* ) override
2023-12-07 10:24:47 +00:00
{
updateLabel();
}
void geometryChange( const QRectF& newGeometry,
const QRectF& oldGeometry ) override
{
Inherited::geometryChange( newGeometry, oldGeometry );
updateLabel();
}
private:
void updateLabel()
2023-12-04 11:48:11 +00:00
{
static int counter = 0;
2023-12-04 11:48:11 +00:00
const auto angle = counter++ / 50.0 * M_PI * 2.0;
const auto x = std::cos( angle );
const auto y = std::sin( angle );
const qreal margin = 20;
auto labelRect = rect();
labelRect.adjust( margin, margin, -margin, -margin );
labelRect.translate( margin * x, margin * y );
2023-12-15 18:16:28 +00:00
if ( m_label )
m_label->setGeometry( labelRect );
}
private:
2023-12-15 18:16:28 +00:00
QskGraphicLabel* m_label = nullptr;
};
class MainView : public QskControl
{
public:
MainView( QQuickItem* parent = nullptr )
: QskControl( parent )
{
setPolishOnResize( true );
m_background = new BackgroundItem( this );
#if 0
{
auto box = new QskBox( m_background );
2023-12-15 18:16:28 +00:00
box->setGeometry( 20, 20, 600, 400 );
box->setFillGradient( QskRgb::SaddleBrown );
box->setObjectName( "redBox" );
}
#endif
2023-12-08 14:47:32 +00:00
m_overlay = new OverlayBox( m_background );
(void )new ForegroundItem( m_overlay );
2023-12-14 12:01:23 +00:00
#if 0
{
auto box = new QskBox( m_background );
box->setGeometry( 50, 50, 400, 200 );
2023-12-15 18:16:28 +00:00
box->setFillGradient( QskRgb::PaleGreen );
box->setObjectName( "blueBox" );
}
#endif
setObjectName( "mainView" );
}
protected:
void updateLayout()
{
if ( m_background )
m_background->setGeometry( rect() );
QRectF blurredRect( QPointF(), 0.7 * size() );
blurredRect.moveCenter( rect().center() );
if ( m_overlay )
qskSetItemGeometry( m_overlay, blurredRect );
}
private:
BackgroundItem* m_background = nullptr;
Overlay* m_overlay = nullptr;
};
int main( int argc, char** argv )
{
QGuiApplication app( argc, argv );
SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts );
QskWindow window;
window.setColor( Qt::darkGray );
window.addItem( new MainView( window.contentItem() ) );
window.resize( 800, 600 );
window.show();
return app.exec();
}