File indexing completed on 2023-05-30 09:13:19

0001 /*
0002  *  Copyright 2014  Sebastian Gottfried <sebastiangottfried@web.de>
0003  *
0004  *  This library is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU Lesser General Public
0006  *  License as published by the Free Software Foundation; either
0007  *  version 2.1 of the License, or (at your option) version 3, or any
0008  *  later version accepted by the membership of KDE e.V. (or its
0009  *  successor approved by the membership of KDE e.V.), which shall
0010  *  act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  *  This library is distributed in the hope that it will be useful,
0013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  *  Lesser General Public License for more details.
0016  *
0017  *  You should have received a copy of the GNU Lesser General Public
0018  */
0019 
0020 #include "chartforegroundpainter.h"
0021 
0022 #include <QPainter>
0023 
0024 #include "chartcore.h"
0025 
0026 ChartForegroundPainter::ChartForegroundPainter(QQuickItem *parent) :
0027     QQuickPaintedItem(parent),
0028     m_chartCore(nullptr)
0029 {
0030     setFlag(QQuickItem::ItemHasContents, true);
0031 }
0032 
0033 ChartCore* ChartForegroundPainter::chartCore() const
0034 {
0035     return m_chartCore;
0036 }
0037 
0038 void ChartForegroundPainter::setChartCore(ChartCore* chartCore)
0039 {
0040     if (chartCore != m_chartCore)
0041     {
0042         if (m_chartCore)
0043         {
0044             m_chartCore->disconnect(this);
0045         }
0046 
0047         m_chartCore = chartCore;
0048 
0049         if (m_chartCore)
0050         {
0051             connect(m_chartCore, &ChartCore::updated, this, &ChartForegroundPainter::triggerUpdate);
0052         }
0053 
0054         triggerUpdate();
0055         emit chartCoreChanged();
0056     }
0057 }
0058 
0059 QColor ChartForegroundPainter::backgroundColor() const
0060 {
0061     return m_backgroundColor;
0062 }
0063 
0064 void ChartForegroundPainter::setBackgroundColor(const QColor& backgroundColor)
0065 {
0066     if (backgroundColor != m_backgroundColor)
0067     {
0068         m_backgroundColor = backgroundColor;
0069         triggerUpdate();
0070         emit backgroundColorChanged();
0071     }
0072 }
0073 
0074 void ChartForegroundPainter::paint(QPainter* painter)
0075 {
0076     if (!m_chartCore)
0077         return;
0078 
0079     const qreal overlapWidth = m_chartCore->pitch() / 2;
0080 
0081     QColor startColor = m_backgroundColor;
0082     QColor stopColor = startColor;
0083     stopColor.setAlpha(0);
0084 
0085     QLinearGradient leftFadeOutGradiend(QPointF(0, 0), QPointF(overlapWidth, 0));
0086     leftFadeOutGradiend.setColorAt(0, startColor);
0087     leftFadeOutGradiend.setColorAt(1, stopColor);
0088     painter->fillRect(QRectF(QPointF(0, 0), QPointF(overlapWidth, height())), QBrush(leftFadeOutGradiend));
0089 
0090     QLinearGradient rightFadeOutGradiend(QPointF(width() - overlapWidth , 0), QPointF(width(), 0));
0091     rightFadeOutGradiend.setColorAt(0, stopColor);
0092     rightFadeOutGradiend.setColorAt(1, startColor);
0093     painter->fillRect(QRectF(QPointF(width() - overlapWidth , 0), QPointF(width(), height())), QBrush(rightFadeOutGradiend));
0094 
0095 }
0096 
0097 void ChartForegroundPainter::triggerUpdate()
0098 {
0099     if (!m_chartCore)
0100         return;
0101 
0102     update();
0103 }