File indexing completed on 2024-05-12 16:43:57
0001 /* 0002 SPDX-FileCopyrightText: 2016-2017 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "creditdebithelper.h" 0008 0009 // ---------------------------------------------------------------------------- 0010 // QT Includes 0011 0012 #include <QDebug> 0013 #include <QPointer> 0014 0015 // ---------------------------------------------------------------------------- 0016 // KDE Includes 0017 0018 // ---------------------------------------------------------------------------- 0019 // Project Includes 0020 0021 #include "amountedit.h" 0022 0023 class CreditDebitHelperPrivate 0024 { 0025 Q_DISABLE_COPY(CreditDebitHelperPrivate) 0026 Q_DECLARE_PUBLIC(CreditDebitHelper) 0027 0028 public: 0029 explicit CreditDebitHelperPrivate(CreditDebitHelper *qq) : 0030 q_ptr(qq) 0031 { 0032 } 0033 0034 void widgetChanged(AmountEdit* src, AmountEdit* dst) 0035 { 0036 // make sure the objects exist 0037 if(!src || !dst) { 0038 return; 0039 } 0040 0041 // in case both are filled with text, the src wins 0042 if(!src->text().isEmpty() && !dst->text().isEmpty()) { 0043 dst->clear(); 0044 } 0045 0046 // in case the source is negative, we negate the value 0047 // and load it into destination. 0048 if(src->value().isNegative()) { 0049 dst->setValue(-(src->value())); 0050 src->clear(); 0051 } 0052 Q_Q(CreditDebitHelper); 0053 emit q->valueChanged(); 0054 } 0055 0056 CreditDebitHelper *q_ptr; 0057 QPointer<AmountEdit> m_credit; 0058 QPointer<AmountEdit> m_debit; 0059 }; 0060 0061 CreditDebitHelper::CreditDebitHelper(QObject* parent, AmountEdit* credit, AmountEdit* debit) : 0062 QObject(parent), 0063 d_ptr(new CreditDebitHelperPrivate(this)) 0064 { 0065 Q_D(CreditDebitHelper); 0066 d->m_credit = credit; 0067 d->m_debit = debit; 0068 connect(d->m_credit.data(), &AmountEdit::valueChanged, this, &CreditDebitHelper::creditChanged); 0069 connect(d->m_debit.data(), &AmountEdit::valueChanged, this, &CreditDebitHelper::debitChanged); 0070 } 0071 0072 CreditDebitHelper::~CreditDebitHelper() 0073 { 0074 Q_D(CreditDebitHelper); 0075 delete d; 0076 } 0077 0078 void CreditDebitHelper::creditChanged() 0079 { 0080 Q_D(CreditDebitHelper); 0081 d->widgetChanged(d->m_credit, d->m_debit); 0082 } 0083 0084 void CreditDebitHelper::debitChanged() 0085 { 0086 Q_D(CreditDebitHelper); 0087 d->widgetChanged(d->m_debit, d->m_credit); 0088 } 0089 0090 bool CreditDebitHelper::haveValue() const 0091 { 0092 Q_D(const CreditDebitHelper); 0093 return (!d->m_credit->text().isEmpty()) || (!d->m_debit->text().isEmpty()); 0094 } 0095 0096 MyMoneyMoney CreditDebitHelper::value() const 0097 { 0098 Q_D(const CreditDebitHelper); 0099 MyMoneyMoney value; 0100 if(d->m_credit && d->m_debit) { 0101 if(!d->m_credit->text().isEmpty()) { 0102 value = -d->m_credit->value(); 0103 } else { 0104 value = d->m_debit->value(); 0105 } 0106 } else { 0107 qWarning() << "CreditDebitHelper::value() called with no objects attached. Zero returned."; 0108 } 0109 return value; 0110 } 0111 0112 void CreditDebitHelper::setValue(const MyMoneyMoney& value) 0113 { 0114 Q_D(CreditDebitHelper); 0115 if(d->m_credit && d->m_debit) { 0116 if(value.isNegative()) { 0117 d->m_credit->setValue(-value); 0118 d->m_debit->clear(); 0119 } else { 0120 d->m_debit->setValue(value); 0121 d->m_credit->clear(); 0122 } 0123 } else { 0124 qWarning() << "CreditDebitHelper::setValue() called with no objects attached. Skipped."; 0125 } 0126 }