qskinny/examples/iotdashboard/kirigami/shadowedrectangle.cpp

369 lines
7.7 KiB
C++
Raw Normal View History

Add IOT dashboard example (#116) * Add IOT dashboard example * Add images * more content * add pie chart * Add skin factories etc. * more work on the pie chart * Try to use quick shapes * Revert "Try to use quick shapes" This reverts commit df6b5b22a339173d2a70ed85744b598811c26b30. Doesn't work that easily unfortunately. * implement design * Add fonts; for now as a resource We should use fontconfig of course later * improve menu bar * implement top bar * use QNanoPainter for circular graphs * Revert "use QNanoPainter for circular graphs" This reverts commit ba0263cb1c19462cc41063ec7087c95e176c8293. Try with QQuickPaintedItem instead for now. * use painted items for circular bar graphs (for now) * use different colors * use some gradients all of this is very hackish still * add to top bar * fix fonts and time display * implement usage * implement indoor temperature * implement Humidity * implement My Devices * fix opacity issue with devices * make icons quadratic with some quick fixes as usual * Add diagram * try to smooth out curves * Add diagram caption * use tiny font * make caption smaller * add wekdays * add grid lines * fix my devices * add light intensity * add box around each section * rename Card to Box * Put indoor temperature inside a box * put Humidity in a box * put the rest in a box * some small stuff * add kirigami code * something works somehow * maybe we don't need our own class still some work to do, but the main thing works * add shadow from outside ... because the class is not a QskControl * fine-tune the layout * cross compilation: Make sure examples find libraries at link time * fix compilation for embedded target * add night time skin * add new button class to better style it * more hints for the night time skin * change hints for dimmer * change hints for progress bars * Use animator for light dimmer * use animator for progress bars * Add Kirigami code It was on oversight that this was forgotten earlier. We could of course strip this down a lot to the part that we are actually using (i.e. the shadowed rectangle). * fix build with new QSkinny version * fix paddings, something in the API changed * fix stretch factors * fix build with new version * clang tidy fixes * fix unused parameter warnings should clean this up properly * beautify example * use astyle * style menu bar properly * fix warning * more size hints * refactor skins * more skin hints * graphic label skin hints * menu item states instead of own API * main grid box styling * top bar styling * fix build * style round progress bars * style time * style indoor temperature and humidity * simplify temperature and humidity * style some more * style My Devices section * style My Devices some more * fix styles when switching between them * style diagram * style more elements inside diagram * more diagram style * fix skin changes * style light intensity * Fix Humidity * fix light intensity layout and other stuff * style light intensity * style button value label * style round button * style button boxes some more * style menu bar top label * style menu bar icons * remove ShadowBox, it is not used * style shadow boxes * remove QskShadowedRectangle We are not using it * style usage spacer * fine tune * Refactor diagram before replacing it * Add Diagram drawn with OpenGL * use new Diagram class * Support more than one data point in a diagram * change data points and colors a bit * position caption box * adapt the spline to show nice curves * remove boost::math dependency We just hardcode the values here so we can get rid of the dependency. * Remove kirigami code that we don't need We only need the shadow * move kirigami code * rename header guards * add license headers * rename some classes
2021-04-26 06:22:35 +02:00
/*
* SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
*
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "shadowedrectangle.h"
#include <QQuickWindow>
#include "scenegraph/shadowedrectanglenode.h"
#include "scenegraph/paintedrectangleitem.h"
BorderGroup::BorderGroup( QObject* parent )
: QObject( parent )
{
}
qreal BorderGroup::width() const
{
return m_width;
}
void BorderGroup::setWidth( qreal newWidth )
{
if( newWidth == m_width )
{
return;
}
m_width = newWidth;
Q_EMIT changed();
}
QColor BorderGroup::color() const
{
return m_color;
}
void BorderGroup::setColor( const QColor& newColor )
{
if( newColor == m_color )
{
return;
}
m_color = newColor;
Q_EMIT changed();
}
ShadowGroup::ShadowGroup( QObject* parent )
: QObject( parent )
{
}
qreal ShadowGroup::size() const
{
return m_size;
}
void ShadowGroup::setSize( qreal newSize )
{
if( newSize == m_size )
{
return;
}
m_size = newSize;
Q_EMIT changed();
}
qreal ShadowGroup::xOffset() const
{
return m_xOffset;
}
void ShadowGroup::setXOffset( qreal newXOffset )
{
if( newXOffset == m_xOffset )
{
return;
}
m_xOffset = newXOffset;
Q_EMIT changed();
}
qreal ShadowGroup::yOffset() const
{
return m_yOffset;
}
void ShadowGroup::setYOffset( qreal newYOffset )
{
if( newYOffset == m_yOffset )
{
return;
}
m_yOffset = newYOffset;
Q_EMIT changed();
}
QColor ShadowGroup::color() const
{
return m_color;
}
void ShadowGroup::setColor( const QColor& newColor )
{
if( newColor == m_color )
{
return;
}
m_color = newColor;
Q_EMIT changed();
}
CornersGroup::CornersGroup( QObject* parent )
: QObject( parent )
{
}
qreal CornersGroup::topLeft() const
{
return m_topLeft;
}
void CornersGroup::setTopLeft( qreal newTopLeft )
{
if( newTopLeft == m_topLeft )
{
return;
}
m_topLeft = newTopLeft;
Q_EMIT changed();
}
qreal CornersGroup::topRight() const
{
return m_topRight;
}
void CornersGroup::setTopRight( qreal newTopRight )
{
if( newTopRight == m_topRight )
{
return;
}
m_topRight = newTopRight;
Q_EMIT changed();
}
qreal CornersGroup::bottomLeft() const
{
return m_bottomLeft;
}
void CornersGroup::setBottomLeft( qreal newBottomLeft )
{
if( newBottomLeft == m_bottomLeft )
{
return;
}
m_bottomLeft = newBottomLeft;
Q_EMIT changed();
}
qreal CornersGroup::bottomRight() const
{
return m_bottomRight;
}
void CornersGroup::setBottomRight( qreal newBottomRight )
{
if( newBottomRight == m_bottomRight )
{
return;
}
m_bottomRight = newBottomRight;
Q_EMIT changed();
}
QVector4D CornersGroup::toVector4D( float all ) const
{
return QVector4D
{
m_bottomRight < 0.0 ? all : m_bottomRight,
m_topRight < 0.0 ? all : m_topRight,
m_bottomLeft < 0.0 ? all : m_bottomLeft,
m_topLeft < 0.0 ? all : m_topLeft
};
}
ShadowedRectangle::ShadowedRectangle( QQuickItem* parentItem )
: QQuickItem( parentItem )
, m_border( new BorderGroup )
, m_shadow( new ShadowGroup )
, m_corners( new CornersGroup )
{
setFlag( QQuickItem::ItemHasContents, true );
connect( m_border.get(), &BorderGroup::changed, this, &ShadowedRectangle::update );
connect( m_shadow.get(), &ShadowGroup::changed, this, &ShadowedRectangle::update );
connect( m_corners.get(), &CornersGroup::changed, this, &ShadowedRectangle::update );
}
ShadowedRectangle::~ShadowedRectangle()
{
}
BorderGroup* ShadowedRectangle::border() const
{
return m_border.get();
}
ShadowGroup* ShadowedRectangle::shadow() const
{
return m_shadow.get();
}
CornersGroup* ShadowedRectangle::corners() const
{
return m_corners.get();
}
qreal ShadowedRectangle::radius() const
{
return m_radius;
}
void ShadowedRectangle::setRadius( qreal newRadius )
{
if( newRadius == m_radius )
{
return;
}
m_radius = newRadius;
if( !isSoftwareRendering() )
{
update();
}
Q_EMIT radiusChanged();
}
QColor ShadowedRectangle::color() const
{
return m_color;
}
void ShadowedRectangle::setColor( const QColor& newColor )
{
if( newColor == m_color )
{
return;
}
m_color = newColor;
if( !isSoftwareRendering() )
{
update();
}
Q_EMIT colorChanged();
}
void ShadowedRectangle::componentComplete()
{
QQuickItem::componentComplete();
checkSoftwareItem();
}
bool ShadowedRectangle::isSoftwareRendering() const
{
2021-04-26 08:16:06 +02:00
return false;
Add IOT dashboard example (#116) * Add IOT dashboard example * Add images * more content * add pie chart * Add skin factories etc. * more work on the pie chart * Try to use quick shapes * Revert "Try to use quick shapes" This reverts commit df6b5b22a339173d2a70ed85744b598811c26b30. Doesn't work that easily unfortunately. * implement design * Add fonts; for now as a resource We should use fontconfig of course later * improve menu bar * implement top bar * use QNanoPainter for circular graphs * Revert "use QNanoPainter for circular graphs" This reverts commit ba0263cb1c19462cc41063ec7087c95e176c8293. Try with QQuickPaintedItem instead for now. * use painted items for circular bar graphs (for now) * use different colors * use some gradients all of this is very hackish still * add to top bar * fix fonts and time display * implement usage * implement indoor temperature * implement Humidity * implement My Devices * fix opacity issue with devices * make icons quadratic with some quick fixes as usual * Add diagram * try to smooth out curves * Add diagram caption * use tiny font * make caption smaller * add wekdays * add grid lines * fix my devices * add light intensity * add box around each section * rename Card to Box * Put indoor temperature inside a box * put Humidity in a box * put the rest in a box * some small stuff * add kirigami code * something works somehow * maybe we don't need our own class still some work to do, but the main thing works * add shadow from outside ... because the class is not a QskControl * fine-tune the layout * cross compilation: Make sure examples find libraries at link time * fix compilation for embedded target * add night time skin * add new button class to better style it * more hints for the night time skin * change hints for dimmer * change hints for progress bars * Use animator for light dimmer * use animator for progress bars * Add Kirigami code It was on oversight that this was forgotten earlier. We could of course strip this down a lot to the part that we are actually using (i.e. the shadowed rectangle). * fix build with new QSkinny version * fix paddings, something in the API changed * fix stretch factors * fix build with new version * clang tidy fixes * fix unused parameter warnings should clean this up properly * beautify example * use astyle * style menu bar properly * fix warning * more size hints * refactor skins * more skin hints * graphic label skin hints * menu item states instead of own API * main grid box styling * top bar styling * fix build * style round progress bars * style time * style indoor temperature and humidity * simplify temperature and humidity * style some more * style My Devices section * style My Devices some more * fix styles when switching between them * style diagram * style more elements inside diagram * more diagram style * fix skin changes * style light intensity * Fix Humidity * fix light intensity layout and other stuff * style light intensity * style button value label * style round button * style button boxes some more * style menu bar top label * style menu bar icons * remove ShadowBox, it is not used * style shadow boxes * remove QskShadowedRectangle We are not using it * style usage spacer * fine tune * Refactor diagram before replacing it * Add Diagram drawn with OpenGL * use new Diagram class * Support more than one data point in a diagram * change data points and colors a bit * position caption box * adapt the spline to show nice curves * remove boost::math dependency We just hardcode the values here so we can get rid of the dependency. * Remove kirigami code that we don't need We only need the shadow * move kirigami code * rename header guards * add license headers * rename some classes
2021-04-26 06:22:35 +02:00
}
PaintedRectangleItem* ShadowedRectangle::softwareItem() const
{
return m_softwareItem;
}
void ShadowedRectangle::itemChange( QQuickItem::ItemChange change, const QQuickItem::ItemChangeData& value )
{
if( change == QQuickItem::ItemSceneChange && value.window )
{
checkSoftwareItem();
//TODO: only conditionally emit?
Q_EMIT softwareRenderingChanged();
}
}
QSGNode* ShadowedRectangle::updatePaintNode( QSGNode* node, QQuickItem::UpdatePaintNodeData* data )
{
Q_UNUSED( data );
auto shadowNode = static_cast<ShadowedRectangleNode*>( node );
if( !shadowNode )
{
shadowNode = new ShadowedRectangleNode{};
// Cache lowPower state so we only execute the full check once.
static bool lowPower = QByteArrayList{"1", "true"}.contains( qgetenv( "KIRIGAMI_LOWPOWER_HARDWARE" ).toLower() );
if( lowPower )
{
shadowNode->setShaderType( ShadowedRectangleMaterial::ShaderType::LowPower );
}
}
shadowNode->setBorderEnabled( m_border->isEnabled() );
shadowNode->setRect( boundingRect() );
shadowNode->setSize( m_shadow->size() );
shadowNode->setRadius( m_corners->toVector4D( m_radius ) );
shadowNode->setOffset( QVector2D{float( m_shadow->xOffset() ), float( m_shadow->yOffset() )} );
shadowNode->setColor( m_color );
shadowNode->setShadowColor( m_shadow->color() );
shadowNode->setBorderWidth( m_border->width() );
shadowNode->setBorderColor( m_border->color() );
shadowNode->updateGeometry();
return shadowNode;
}
void ShadowedRectangle::checkSoftwareItem()
{
if( !m_softwareItem && isSoftwareRendering() )
{
m_softwareItem = new PaintedRectangleItem{this};
// The software item is added as a "normal" child item, this means it
// will be part of the normal item sort order. Since there is no way to
// control the ordering of children, just make sure to have a very low Z
// value for the child, to force it to be the lowest item.
m_softwareItem->setZ( -99.0 );
auto updateItem = [this]()
{
auto borderWidth = m_border->width();
auto rect = boundingRect().adjusted( -borderWidth / 2, -borderWidth / 2, borderWidth / 2, borderWidth / 2 );
m_softwareItem->setX( -borderWidth / 2 );
m_softwareItem->setY( -borderWidth / 2 );
m_softwareItem->setSize( rect.size() );
m_softwareItem->setColor( m_color );
m_softwareItem->setRadius( m_radius );
m_softwareItem->setBorderWidth( borderWidth );
m_softwareItem->setBorderColor( m_border->color() );
};
updateItem();
connect( this, &ShadowedRectangle::widthChanged, m_softwareItem, updateItem );
connect( this, &ShadowedRectangle::heightChanged, m_softwareItem, updateItem );
connect( this, &ShadowedRectangle::colorChanged, m_softwareItem, updateItem );
connect( this, &ShadowedRectangle::radiusChanged, m_softwareItem, updateItem );
connect( m_border.get(), &BorderGroup::changed, m_softwareItem, updateItem );
setFlag( QQuickItem::ItemHasContents, false );
}
}