From c6cd1016df848aa00c708d1e4293a8570a379038 Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Sun, 20 Sep 2020 12:56:18 +0200 Subject: [PATCH] add shadow from outside ... because the class is not a QskControl --- examples/iot-dashboard/Box.cpp | 44 +++++++------- examples/iot-dashboard/Box.h | 4 +- examples/iot-dashboard/IndoorTemperature.cpp | 1 + examples/iot-dashboard/MainContent.cpp | 64 +++++++++++++++++++- examples/iot-dashboard/MainContent.h | 22 +++++++ examples/iot-dashboard/Usage.cpp | 1 + 6 files changed, 109 insertions(+), 27 deletions(-) diff --git a/examples/iot-dashboard/Box.cpp b/examples/iot-dashboard/Box.cpp index cdb40538..f88c4d2c 100644 --- a/examples/iot-dashboard/Box.cpp +++ b/examples/iot-dashboard/Box.cpp @@ -8,18 +8,18 @@ #include #include -ShadowBox::ShadowBox( QskControl* control, QQuickItem* parent ) +ShadowBox::ShadowBox( QQuickItem* parent ) : QskControl( parent ) - , m_control( control ) +// , m_control( control ) , m_rectangle( new ShadowedRectangle( this ) ) { - setAutoLayoutChildren( true ); +// setAutoLayoutChildren( true ); m_rectangle->shadow()->setColor( Qt::black ); m_rectangle->shadow()->setSize( 15 ); m_rectangle->setColor( Qt::white ); // ### opacity should only be for the shadow, not the background - m_rectangle->setOpacity( 0.1 ); - m_rectangle->setSize( m_control->preferredSize() ); +// m_rectangle->setOpacity( 0.1 ); +// m_rectangle->setSize( m_control->preferredSize() ); m_rectangle->corners()->setTopLeft( 6 ); m_rectangle->corners()->setTopRight( 6 ); m_rectangle->corners()->setBottomLeft( 6 ); @@ -28,10 +28,10 @@ ShadowBox::ShadowBox( QskControl* control, QQuickItem* parent ) void ShadowBox::updateLayout() { - const auto s = m_rectangle->shadow()->size(); - qDebug() << m_control->geometry() << geometry(); - m_control->setPosition( {15, 0} ); // ### why? - m_control->setZ( 1 ); +// const auto s = m_rectangle->shadow()->size(); +// qDebug() << m_control->geometry() << geometry(); +// m_control->setPosition( {15, 0} ); // ### why? +// m_control->setZ( 1 ); } Box::Box( const QString& title, QQuickItem* parent ) @@ -46,24 +46,24 @@ Box::Box( const QString& title, QQuickItem* parent ) setMarginsHint( Panel | QskAspect::Padding, 15 ); setPanel( true ); setBoxShapeHint( QskBox::Panel, 6 ); - setBoxBorderMetricsHint( QskBox::Panel, 2 ); +// setBoxBorderMetricsHint( QskBox::Panel, 2 ); QskBoxBorderColors borderColors( "#dddddd" ); borderColors.setAlpha( 100 ); setBoxBorderColorsHint( QskBox::Panel, borderColors ); - auto* r = new ShadowedRectangle( this ); - r->shadow()->setColor( Qt::black ); - r->shadow()->setSize( 15 ); - r->setPosition( {0, 0} ); - r->setOpacity( 0.1 ); - r->setSize( {100, 50} ); - r->corners()->setTopLeft( 6 ); - r->corners()->setTopRight( 6 ); - r->corners()->setBottomLeft( 6 ); - r->corners()->setBottomRight( 6 ); -// r->setRadius( 5 ); - auto* t = new QskTextLabel( "bla", r ); +// auto* r = new ShadowedRectangle( this ); +// r->shadow()->setColor( Qt::black ); +// r->shadow()->setSize( 15 ); +// r->setPosition( {0, 0} ); +// r->setOpacity( 0.1 ); +// r->setSize( {100, 50} ); +// r->corners()->setTopLeft( 6 ); +// r->corners()->setTopRight( 6 ); +// r->corners()->setBottomLeft( 6 ); +// r->corners()->setBottomRight( 6 ); +//// r->setRadius( 5 ); +// auto* t = new QskTextLabel( "bla", r ); m_label->setFontRole( DaytimeSkin::TitleFont ); diff --git a/examples/iot-dashboard/Box.h b/examples/iot-dashboard/Box.h index 7915d1ea..4eb9c9b2 100644 --- a/examples/iot-dashboard/Box.h +++ b/examples/iot-dashboard/Box.h @@ -11,13 +11,13 @@ class ShadowBox : public QskControl { Q_OBJECT public: - ShadowBox( QskControl* control, QQuickItem* parent ); + ShadowBox( QQuickItem* parent = nullptr ); protected: void updateLayout() override; private: - QskControl* m_control; +// QskControl* m_control; ShadowedRectangle* m_rectangle; }; diff --git a/examples/iot-dashboard/IndoorTemperature.cpp b/examples/iot-dashboard/IndoorTemperature.cpp index b7592083..580a7710 100644 --- a/examples/iot-dashboard/IndoorTemperature.cpp +++ b/examples/iot-dashboard/IndoorTemperature.cpp @@ -13,6 +13,7 @@ IndoorTemperature::IndoorTemperature( QQuickItem* parent ) : Box( "", parent ) { +// setBackgroundColor( Qt::transparent ); setMarginsHint( Panel | QskAspect::Padding, 10 ); auto* layout = new QskLinearBox( Qt::Horizontal, this ); diff --git a/examples/iot-dashboard/MainContent.cpp b/examples/iot-dashboard/MainContent.cpp index a93eb501..102d1830 100644 --- a/examples/iot-dashboard/MainContent.cpp +++ b/examples/iot-dashboard/MainContent.cpp @@ -16,24 +16,73 @@ #include #include +#include "src/shadowedrectangle.h" + +#include + +ShadowPositioner::ShadowPositioner( QQuickItem* parent ) : QskControl( parent ) +{ + setAutoLayoutChildren( true ); +} + +void ShadowPositioner::setGridBox( QskGridBox* gridBox ) +{ + m_gridBox = gridBox; + m_rectangles.reserve( m_gridBox->count() ); + + for( int i = 0; i < m_gridBox->count(); ++i ) + { + auto* r = new ShadowedRectangle( this ); + r->setZ( 5 ); + r->shadow()->setColor( Qt::black ); + r->shadow()->setSize( 15 ); + r->setColor( Qt::white ); // ### opacity should only be for the shadow, not the background + r->setOpacity( 0.1 ); + r->corners()->setTopLeft( 6 ); + r->corners()->setTopRight( 6 ); + r->corners()->setBottomLeft( 6 ); + r->corners()->setBottomRight( 6 ); + m_rectangles.append( r ); + } +} + +void ShadowPositioner::updateLayout() +{ + auto* mainContent = static_cast( parentItem() ); + + QTimer::singleShot( 0, this, [this, mainContent]() + { + for( int i = 0; i < m_rectangles.count(); ++i ) + { + auto* item = m_gridBox->itemAtIndex( i ); + m_rectangles[i]->setSize( item->size() ); + m_rectangles[i]->setPosition( mainContent->itemAtIndex( 1 )->position() + item->position() ); + } + } ); +} + MainContent::MainContent( QQuickItem* parent ) : QskLinearBox( Qt::Vertical, parent ) { + setAutoAddChildren( false ); setSizePolicy( QskSizePolicy::Expanding, QskSizePolicy::Expanding ); setDefaultAlignment( Qt::AlignTop ); setSpacing( 30 ); setBackgroundColor( "#FBFBFB" ); - new TopBar( this ); + auto* topBar = new TopBar( this ); + addItem( topBar ); auto* gridBox = new QskGridBox( this ); + gridBox->setSpacing( 15 ); gridBox->setMargins( {15, 0, 15, 20} ); gridBox->setPanel( true ); gridBox->setBoxShapeHint( QskBox::Panel, 6 ); gridBox->setBoxBorderMetricsHint( QskBox::Panel, 2 ); + addItem( gridBox ); auto* usage = new Usage( gridBox ); - auto* usageShadowBox = new ShadowBox( usage, this ); - gridBox->addItem( usageShadowBox, 0, 0, 2, 1 ); + + gridBox->addItem( usage, 0, 0, 2, 1 ); auto* indoorTemperature = new IndoorTemperature( gridBox ); gridBox->addItem( indoorTemperature, 0, 1 ); @@ -49,4 +98,13 @@ MainContent::MainContent( QQuickItem* parent ) : QskLinearBox( Qt::Vertical, par auto* lightIntensity = new LightIntensity( gridBox ); gridBox->addItem( lightIntensity, 2, 2 ); + + m_shadowPositioner = new ShadowPositioner( this ); + m_shadowPositioner->setGridBox( gridBox ); +} + +void MainContent::geometryChanged( const QRectF& newGeometry, const QRectF& oldGeometry ) +{ + QskLinearBox::geometryChanged( newGeometry, oldGeometry ); + m_shadowPositioner->polish(); } diff --git a/examples/iot-dashboard/MainContent.h b/examples/iot-dashboard/MainContent.h index 8ecc66b3..16e882e3 100644 --- a/examples/iot-dashboard/MainContent.h +++ b/examples/iot-dashboard/MainContent.h @@ -3,6 +3,24 @@ #include +class QskGridBox; +class ShadowBox; +class ShadowedRectangle; + +class ShadowPositioner : public QskControl +{ + Q_OBJECT + public: + ShadowPositioner( QQuickItem* parent ); + + void updateLayout() override; + void setGridBox( QskGridBox* gridBox ); + + private: + QskGridBox* m_gridBox; + QVector m_rectangles; +}; + class MainContent : public QskLinearBox { Q_OBJECT @@ -10,8 +28,12 @@ class MainContent : public QskLinearBox public: MainContent( QQuickItem* parent ); + protected: + void geometryChanged( const QRectF&, const QRectF& ) override; + private: QList< QskLinearBox* > m_columns; + ShadowPositioner* m_shadowPositioner; }; #endif // MAINCONTENT_H diff --git a/examples/iot-dashboard/Usage.cpp b/examples/iot-dashboard/Usage.cpp index 16dd042d..568e2ec6 100644 --- a/examples/iot-dashboard/Usage.cpp +++ b/examples/iot-dashboard/Usage.cpp @@ -6,6 +6,7 @@ Usage::Usage( QQuickItem* parent ) : Box( "Usage", parent ) { + setBackgroundColor( Qt::transparent ); auto* content = new QskLinearBox( Qt::Vertical, this ); auto* today = new QskLinearBox( Qt::Horizontal, content );