File indexing completed on 2024-05-12 16:44:03

0001 /*
0002     SPDX-FileCopyrightText: 2001 Felix Rodriguez <frodriguez@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2002 Michael Edwardes <mte@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2006-2011 Thomas Baumgart <tbaumgart@kde.org>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kmymoneylineedit.h"
0009 
0010 // ----------------------------------------------------------------------------
0011 // QT Includes
0012 
0013 #include <QKeyEvent>
0014 #include <QFocusEvent>
0015 #include <QTimer>
0016 #include <QLocale>
0017 
0018 // ----------------------------------------------------------------------------
0019 // KDE Includes
0020 
0021 // ----------------------------------------------------------------------------
0022 // Project Includes
0023 
0024 class KMyMoneyLineEdit::Private
0025 {
0026 public:
0027     /**
0028       * This member keeps the initial value. It is used during
0029       * resetText() to set the widgets text back to this initial value
0030       * and as comparison during focusOutEvent() to emit the lineChanged
0031       * signal if the current text is different.
0032       */
0033     QString m_text;
0034 
0035     /**
0036       * This member keeps the status if overriding the numeric keypad comma key
0037       * is requested or not.
0038       */
0039     bool m_forceMonetaryDecimalSymbol;
0040     bool  skipSelectAll;
0041 };
0042 
0043 KMyMoneyLineEdit::KMyMoneyLineEdit(QWidget *w, bool forceMonetaryDecimalSymbol, Qt::Alignment alignment) :
0044     KLineEdit(w),
0045     d(new Private)
0046 {
0047     d->m_forceMonetaryDecimalSymbol = forceMonetaryDecimalSymbol;
0048     setAlignment(alignment);
0049     skipSelectAll(false);
0050 }
0051 
0052 KMyMoneyLineEdit::~KMyMoneyLineEdit()
0053 {
0054     delete d;
0055 }
0056 
0057 void KMyMoneyLineEdit::skipSelectAll(bool skipIt)
0058 {
0059     d->skipSelectAll = skipIt;
0060 }
0061 
0062 void KMyMoneyLineEdit::resetText()
0063 {
0064     setText(d->m_text);
0065 }
0066 
0067 void KMyMoneyLineEdit::loadText(const QString& text)
0068 {
0069     d->m_text = text;
0070     setText(text);
0071 }
0072 
0073 void KMyMoneyLineEdit::focusOutEvent(QFocusEvent *ev)
0074 {
0075     // if the current text is not in the list of
0076     // possible completions, we have a new payee
0077     // and signal that to the outside world.
0078     if (text() != d->m_text) {
0079         emit lineChanged(text());
0080     }
0081     KLineEdit::focusOutEvent(ev);
0082 
0083     // force update of hint
0084     if (text().isEmpty())
0085         repaint();
0086 }
0087 
0088 void KMyMoneyLineEdit::focusInEvent(QFocusEvent *ev)
0089 {
0090     KLineEdit::focusInEvent(ev);
0091     // select the text so it can be edited by the user - only if the widget
0092     // is not focused after a popup is closed (which could be the completer
0093     // of the KMyMoneyCombo).
0094     //
0095     // Delay that selection until the application is idle to prevent a
0096     // recursive loop which otherwise entered when the focus is set to this
0097     // widget using the mouse. (bko #259369)
0098     if (ev->reason() != Qt::PopupFocusReason && ev->reason() != Qt::ActiveWindowFocusReason) {
0099         if (!d->skipSelectAll)
0100             QTimer::singleShot(0, this, SLOT(selectAll()));
0101         d->skipSelectAll = false;
0102     }
0103 }
0104 
0105 void KMyMoneyLineEdit::keyReleaseEvent(QKeyEvent* k)
0106 {
0107     if (d->m_forceMonetaryDecimalSymbol) {
0108         if (k->modifiers() & Qt::KeypadModifier) {
0109             if (k->key() == Qt::Key_Comma
0110                     || k->key() == Qt::Key_Period) {
0111                 if (QLocale().decimalPoint() == QLatin1Char(',')) {
0112                     QKeyEvent newk(k->type(), Qt::Key_Comma, k->modifiers(), ",", k->isAutoRepeat(), k->count());
0113                     KLineEdit::keyReleaseEvent(&newk);
0114                     k->accept();
0115                     return;
0116                 }
0117 
0118                 if (QLocale().decimalPoint() == QLatin1Char('.')) {
0119                     QKeyEvent newk(k->type(), Qt::Key_Comma, k->modifiers(), ".", k->isAutoRepeat(), k->count());
0120                     KLineEdit::keyReleaseEvent(&newk);
0121                     k->accept();
0122                     return;
0123                 }
0124             }
0125         }
0126     }
0127     KLineEdit::keyReleaseEvent(k);
0128 }
0129 
0130 void KMyMoneyLineEdit::keyPressEvent(QKeyEvent* k)
0131 {
0132     if (d->m_forceMonetaryDecimalSymbol) {
0133         if (k->modifiers() & Qt::KeypadModifier) {
0134             if (k->key() == Qt::Key_Comma
0135                     || k->key() == Qt::Key_Period) {
0136                 if (QLocale().decimalPoint() == QLatin1Char(',')) {
0137                     QKeyEvent newk(k->type(), Qt::Key_Comma, k->modifiers(), ",", k->isAutoRepeat(), k->count());
0138                     KLineEdit::keyPressEvent(&newk);
0139                     k->accept();
0140                     return;
0141                 }
0142 
0143                 if (QLocale().decimalPoint() == QLatin1Char('.')) {
0144                     QKeyEvent newk(k->type(), Qt::Key_Period, k->modifiers(), ".", k->isAutoRepeat(), k->count());
0145                     KLineEdit::keyPressEvent(&newk);
0146                     k->accept();
0147                     return;
0148                 }
0149             }
0150         }
0151     }
0152     KLineEdit::keyPressEvent(k);
0153 }