qskinny/examples/iotdashboard/kirigami/scenegraph/shadowedrectanglenode.cpp

210 lines
6.2 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 "shadowedrectanglenode.h"
#include "shadowedrectanglematerial.h"
#include "shadowedborderrectanglematerial.h"
QColor premultiply(const QColor &color)
{
return QColor::fromRgbF(
color.redF() * color.alphaF(),
color.greenF() * color.alphaF(),
color.blueF() * color.alphaF(),
color.alphaF()
);
}
ShadowedRectangleNode::ShadowedRectangleNode()
{
m_geometry = new QSGGeometry{QSGGeometry::defaultAttributes_TexturedPoint2D(), 4};
setGeometry(m_geometry);
setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial);
}
void ShadowedRectangleNode::setBorderEnabled(bool enabled)
{
// We can achieve more performant shaders by splitting the two into separate
// shaders. This requires separating the materials as well. So when
// borderWidth is increased to something where the border should be visible,
// switch to the with-border material. Otherwise use the no-border version.
if (enabled) {
if (!m_material || m_material->type() == borderlessMaterialType()) {
auto newMaterial = createBorderMaterial();
newMaterial->shaderType = m_shaderType;
setMaterial(newMaterial);
m_material = newMaterial;
m_rect = QRectF{};
markDirty(QSGNode::DirtyMaterial);
}
} else {
if (!m_material || m_material->type() == borderMaterialType()) {
auto newMaterial = createBorderlessMaterial();
newMaterial->shaderType = m_shaderType;
setMaterial(newMaterial);
m_material = newMaterial;
m_rect = QRectF{};
markDirty(QSGNode::DirtyMaterial);
}
}
}
void ShadowedRectangleNode::setRect(const QRectF& rect)
{
if (rect == m_rect) {
return;
}
m_rect = rect;
QVector2D newAspect{1.0, 1.0};
if (m_rect.width() >= m_rect.height()) {
newAspect.setX(m_rect.width() / m_rect.height());
} else {
newAspect.setY(m_rect.height() / m_rect.width());
}
if (m_material->aspect != newAspect) {
m_material->aspect = newAspect;
markDirty(QSGNode::DirtyMaterial);
m_aspect = newAspect;
}
}
void ShadowedRectangleNode::setSize(qreal size)
{
auto minDimension = std::min(m_rect.width(), m_rect.height());
float uniformSize = (size / minDimension) * 2.0;
if (!qFuzzyCompare(m_material->size, uniformSize)) {
m_material->size = uniformSize;
markDirty(QSGNode::DirtyMaterial);
m_size = size;
}
}
void ShadowedRectangleNode::setRadius(const QVector4D &radius)
{
float minDimension = std::min(m_rect.width(), m_rect.height());
auto uniformRadius = QVector4D{
std::min(radius.x() * 2.0f / minDimension, 1.0f),
std::min(radius.y() * 2.0f / minDimension, 1.0f),
std::min(radius.z() * 2.0f / minDimension, 1.0f),
std::min(radius.w() * 2.0f / minDimension, 1.0f)
};
if (m_material->radius != uniformRadius) {
m_material->radius = uniformRadius;
markDirty(QSGNode::DirtyMaterial);
m_radius = radius;
}
}
void ShadowedRectangleNode::setColor(const QColor &color)
{
auto premultiplied = premultiply(color);
if (m_material->color != premultiplied) {
m_material->color = premultiplied;
markDirty(QSGNode::DirtyMaterial);
}
}
void ShadowedRectangleNode::setShadowColor(const QColor& color)
{
auto premultiplied = premultiply(color);
if (m_material->shadowColor != premultiplied) {
m_material->shadowColor = premultiplied;
markDirty(QSGNode::DirtyMaterial);
}
}
void ShadowedRectangleNode::setOffset(const QVector2D& offset)
{
auto minDimension = std::min(m_rect.width(), m_rect.height());
auto uniformOffset = offset / minDimension;
if (m_material->offset != uniformOffset) {
m_material->offset = uniformOffset;
markDirty(QSGNode::DirtyMaterial);
m_offset = offset;
}
}
void ShadowedRectangleNode::setBorderWidth(qreal width)
{
if (m_material->type() != borderMaterialType()) {
return;
}
auto minDimension = std::min(m_rect.width(), m_rect.height());
float uniformBorderWidth = width / minDimension;
auto borderMaterial = static_cast<ShadowedBorderRectangleMaterial*>(m_material);
if (!qFuzzyCompare(borderMaterial->borderWidth, uniformBorderWidth)) {
borderMaterial->borderWidth = uniformBorderWidth;
markDirty(QSGNode::DirtyMaterial);
m_borderWidth = width;
}
}
void ShadowedRectangleNode::setBorderColor(const QColor& color)
{
if (m_material->type() != borderMaterialType()) {
return;
}
auto borderMaterial = static_cast<ShadowedBorderRectangleMaterial*>(m_material);
auto premultiplied = premultiply(color);
if (borderMaterial->borderColor != premultiplied) {
borderMaterial->borderColor = premultiplied;
markDirty(QSGNode::DirtyMaterial);
}
}
void ShadowedRectangleNode::setShaderType(ShadowedRectangleMaterial::ShaderType type)
{
m_shaderType = type;
}
void ShadowedRectangleNode::updateGeometry()
{
auto rect = m_rect;
if (m_shaderType == ShadowedRectangleMaterial::ShaderType::Standard) {
rect = rect.adjusted(-m_size * m_aspect.x(), -m_size * m_aspect.y(),
m_size * m_aspect.x(), m_size * m_aspect.y());
auto offsetLength = m_offset.length();
rect = rect.adjusted(-offsetLength * m_aspect.x(), -offsetLength * m_aspect.y(),
offsetLength * m_aspect.x(), offsetLength * m_aspect.y());
}
QSGGeometry::updateTexturedRectGeometry(m_geometry, rect, QRectF{0.0, 0.0, 1.0, 1.0});
markDirty(QSGNode::DirtyGeometry);
}
ShadowedRectangleMaterial *ShadowedRectangleNode::createBorderlessMaterial()
{
return new ShadowedRectangleMaterial{};
}
ShadowedBorderRectangleMaterial *ShadowedRectangleNode::createBorderMaterial()
{
return new ShadowedBorderRectangleMaterial{};
}
QSGMaterialType *ShadowedRectangleNode::borderlessMaterialType()
{
return &ShadowedRectangleMaterial::staticType;
}
QSGMaterialType *ShadowedRectangleNode::borderMaterialType()
{
return &ShadowedBorderRectangleMaterial::staticType;
}