File indexing completed on 2024-05-26 05:10:36

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This file is a delegate for budget.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgbudgetdelegate.h"
0012 
0013 #include <kcolorscheme.h>
0014 
0015 #include <qpainter.h>
0016 #include <qsortfilterproxymodel.h>
0017 
0018 #include "skgobjectmodelbase.h"
0019 #include "skgprogressbar.h"
0020 #include "skgtraces.h"
0021 
0022 SKGBudgetDelegate::SKGBudgetDelegate(QObject* iParent, SKGDocument* iDoc) : QStyledItemDelegate(iParent), m_document(iDoc)
0023 {}
0024 
0025 SKGBudgetDelegate::~SKGBudgetDelegate()
0026 {
0027     m_document = nullptr;
0028 }
0029 
0030 void SKGBudgetDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0031 {
0032     bool done = false;
0033     if (index.isValid()) {
0034         const auto* m = qobject_cast<const SKGObjectModelBase*> (index.model());
0035         const auto* p = qobject_cast<const QSortFilterProxyModel*> (index.model());
0036         if (p != nullptr) {
0037             m = qobject_cast<SKGObjectModelBase*>(p->sourceModel());
0038         }
0039         if (m != nullptr) {
0040             QString att = m->getAttribute(index.column());
0041 
0042             // Compute percent of time
0043             QModelIndex idxs = index;
0044             if (p != nullptr) {
0045                 idxs = p->mapToSource(index);
0046             }
0047             SKGObjectBase obj = m->getObject(idxs);
0048 
0049             int year = SKGServices::stringToDouble(obj.getAttribute(QStringLiteral("i_year")));
0050             int month = SKGServices::stringToDouble(obj.getAttribute(QStringLiteral("i_month")));
0051             QDate today = QDate::currentDate();
0052             double pourcent = 1;
0053             bool actif = true;
0054             if (year == today.year()) {
0055                 if (month == 0) {
0056                     QDate d1(year, 1, 1);
0057                     pourcent = static_cast<double>(d1.daysTo(today)) / static_cast<double>(today.daysInYear());
0058                 } else if (month == today.month()) {
0059                     QDate d1(year, month, 1);
0060                     pourcent = static_cast<double>(d1.daysTo(today)) / static_cast<double>(today.daysInMonth());
0061                 } else if (month > today.month()) {
0062                     pourcent = 0;
0063                     actif = false;
0064                 }
0065             } else if (year > today.year())     {
0066                 pourcent = 0;
0067                 actif = false;
0068             }
0069 
0070             if ((att == QStringLiteral("f_budgeted") || att == QStringLiteral("f_budgeted_modified")) && actif) {
0071                 double budgeted = SKGServices::stringToDouble(obj.getAttribute(att));
0072                 double amount = SKGServices::stringToDouble(obj.getAttribute(QStringLiteral("f_CURRENTAMOUNT")));
0073 
0074                 KColorScheme scheme(QPalette::Normal);
0075                 QColor negativeC = scheme.foreground(KColorScheme::NegativeText).color().toHsv();
0076                 QColor positiveC = scheme.foreground(KColorScheme::PositiveText).color().toHsv();
0077                 QColor backC = scheme.foreground(KColorScheme::LinkText).color().toHsv();
0078 
0079                 double coef = 0.3;
0080                 negativeC.setHsv(negativeC.hue(), negativeC.saturation()*coef, negativeC.value());
0081                 positiveC.setHsv(positiveC.hue(), positiveC.saturation()*coef, positiveC.value());
0082                 backC.setHsv(backC.hue(), backC.saturation()*coef, backC.value());
0083 
0084                 QBrush negative(negativeC);
0085                 QBrush positive(positiveC);
0086                 QBrush back(backC);
0087 
0088                 painter->save();
0089                 painter->setRenderHint(QPainter::Antialiasing);
0090 
0091                 QStyleOptionViewItem opt = option;
0092                 QStyledItemDelegate::initStyleOption(&opt, index);
0093                 QRect rect = opt.rect.adjusted(1, 1, -1, -1);
0094 
0095                 // handle selection
0096                 if ((option.state & QStyle::State_Selected) != 0u) {
0097                     KStatefulBrush sb(KColorScheme::View, KColorScheme::NormalBackground);
0098 
0099                     QBrush selectionBrush(sb.brush(QPalette::Active));
0100                     painter->setBrush(selectionBrush);
0101                     painter->drawRect(rect);
0102                 }
0103 
0104                 rect = opt.rect.adjusted(1, 1, -1, -1);
0105 
0106                 painter->setPen(Qt::NoPen);
0107 
0108                 if (budgeted > 0) {
0109                     if (amount < 0) {
0110                         amount = 0;
0111                     }
0112                     // Income
0113                     if (amount < budgeted) {
0114                         // Draw red zone
0115                         painter->setBrush(negative);
0116                         painter->drawRect(rect);
0117 
0118                         // Draw blue zone
0119                         painter->setBrush(back);
0120                         QRect r2(rect.left(), rect.top(), rect.width() * (budgeted == 0 ? 1 : amount / budgeted), rect.height());
0121                         painter->drawRect(r2);
0122                     } else {
0123                         // Draw green zone
0124                         painter->setBrush(positive);
0125                         painter->drawRect(rect);
0126 
0127                         // Draw blue zone
0128                         painter->setBrush(back);
0129                         QRect r2(rect.left(), rect.top(), rect.width() * (amount == 0 ? 0 : budgeted / amount), rect.height());
0130                         painter->drawRect(r2);
0131                     }
0132                 } else {
0133                     if (amount > 0) {
0134                         amount = 0;
0135                     }
0136                     // Expenditure
0137                     if (amount < budgeted) {
0138                         // Draw red zone
0139                         painter->setBrush(negative);
0140                         painter->drawRect(rect);
0141 
0142                         // Draw blue zone
0143                         painter->setBrush(back);
0144                         QRect r2(rect.left(), rect.top(), rect.width() * (amount == 0 ? 0 : budgeted / amount), rect.height());
0145                         painter->drawRect(r2);
0146                     } else {
0147                         // Draw green zone
0148                         painter->setBrush(positive);
0149                         painter->drawRect(rect);
0150 
0151                         // Draw blue zone
0152                         painter->setBrush(back);
0153                         QRect r2(rect.left(), rect.top(), rect.width() * (budgeted == 0 ? 1 : amount / budgeted), rect.height());
0154                         painter->drawRect(r2);
0155                     }
0156                 }
0157 
0158                 // Draw time progress
0159                 painter->setPen(Qt::black);
0160                 QLine r2(rect.left() + rect.width()*pourcent, rect.top() + 1, rect.left() + rect.width()*pourcent, rect.top() + rect.height() - 1);
0161                 painter->drawLine(r2);
0162 
0163                 // Draw text
0164                 painter->setPen(m->data(idxs, Qt::TextColorRole).value<QColor>());
0165 
0166                 QTextOption to;
0167                 to.setAlignment(static_cast<Qt::AlignmentFlag>(m->data(idxs, Qt::TextAlignmentRole).toInt()));
0168                 painter->drawText(rect, m->data(idxs).toString(), to);
0169 
0170                 painter->restore();
0171                 done = true;
0172             }
0173         }
0174     }
0175     if (!done) {
0176         QStyledItemDelegate::paint(painter, option, index);
0177     }
0178 }
0179 
0180