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

0001 /*
0002     SPDX-FileCopyrightText: 2013-2014 Christian Dávid <christian-david@web.de>
0003     SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kmymoneytextedit.h"
0008 #include "kmymoneytextedithighlighter.h"
0009 
0010 #include <QKeyEvent>
0011 
0012 class KMyMoneyTextEditPrivate
0013 {
0014     Q_DISABLE_COPY(KMyMoneyTextEditPrivate)
0015     Q_DECLARE_PUBLIC(KMyMoneyTextEdit)
0016 
0017 public:
0018     explicit KMyMoneyTextEditPrivate(KMyMoneyTextEdit *qq) :
0019         q_ptr(qq),
0020         m_maxLength(-1),
0021         m_maxLineLength(-1),
0022         m_maxLines(-1),
0023         m_allowedChars(QString(QString())),
0024         m_highligther(0)
0025     {
0026     }
0027 
0028     bool isEventAllowed(QKeyEvent* e) const
0029     {
0030         Q_Q(const KMyMoneyTextEdit);
0031         const QString text = e->text();
0032         if (!text.isEmpty()) {
0033             if (text.at(0).isPrint()) {
0034                 if (!m_allowedChars.contains(text))
0035                     return false;
0036 
0037                 // Do not check max lengths etc if something is replaced
0038                 if (q->textCursor().hasSelection())
0039                     return true;
0040 
0041                 const QString plainText = q->toPlainText();
0042 
0043                 if (m_maxLength != -1 && plainText.length() >= m_maxLength)
0044                     return false;
0045                 if (m_maxLineLength != -1 && q->textCursor().block().length() - 1 >= m_maxLineLength)
0046                     return false;
0047             } else if (m_maxLines != -1 && text.at(0) == '\r' && q->toPlainText().count('\n') + 1 >= m_maxLines) {
0048                 // Does this work on non-linux OSes as well?
0049                 return false;
0050             }
0051         }
0052         return true;
0053     }
0054 
0055     KMyMoneyTextEdit *q_ptr;
0056     int m_maxLength;
0057     int m_maxLineLength;
0058     int m_maxLines;
0059     QString m_allowedChars;
0060     KMyMoneyTextEditHighlighter* m_highligther;
0061 };
0062 
0063 
0064 void KMyMoneyTextEdit::setReadOnly(bool readOnly)
0065 {
0066     KTextEdit::setReadOnly(readOnly);
0067 }
0068 
0069 /* KMyMoneyTextEdit */
0070 
0071 KMyMoneyTextEdit::KMyMoneyTextEdit(QWidget* parent) :
0072     KTextEdit(parent),
0073     d_ptr(new KMyMoneyTextEditPrivate(this))
0074 {
0075     Q_D(KMyMoneyTextEdit);
0076     setWordWrapMode(QTextOption::ManualWrap);
0077     d->m_highligther = new KMyMoneyTextEditHighlighter(this);
0078 }
0079 
0080 KMyMoneyTextEdit::~KMyMoneyTextEdit()
0081 {
0082     Q_D(KMyMoneyTextEdit);
0083     delete d;
0084 }
0085 
0086 bool KMyMoneyTextEdit::isValid() const
0087 {
0088     Q_D(const KMyMoneyTextEdit);
0089     const QString text = toPlainText();
0090 
0091     if (d->m_maxLength != -1 && text.length() >= d->m_maxLength)
0092         return false;
0093 
0094     const QStringList lines = text.split('\n');
0095 
0096     if (d->m_maxLines != -1 && lines.count() >= d->m_maxLines) {
0097         return false;
0098     }
0099 
0100     if (d->m_maxLineLength != -1) {
0101         foreach (QString line, lines) {
0102             if (line.length() > d->m_maxLineLength)
0103                 return false;
0104         }
0105     }
0106 
0107     const int length = text.length();
0108     for (auto i = 0; i < length; ++i) {
0109         if (!d->m_allowedChars.contains(text.at(i)))
0110             return false;
0111     }
0112     return true;
0113 }
0114 
0115 void KMyMoneyTextEdit::keyReleaseEvent(QKeyEvent* e)
0116 {
0117     Q_D(KMyMoneyTextEdit);
0118     if (d->isEventAllowed(e))
0119         KTextEdit::keyReleaseEvent(e);
0120 }
0121 
0122 void KMyMoneyTextEdit::keyPressEvent(QKeyEvent* e)
0123 {
0124     Q_D(KMyMoneyTextEdit);
0125     if (d->isEventAllowed(e))
0126         KTextEdit::keyPressEvent(e);
0127 }
0128 
0129 int KMyMoneyTextEdit::maxLength() const
0130 {
0131     Q_D(const KMyMoneyTextEdit);
0132     return d->m_maxLength;
0133 }
0134 
0135 void KMyMoneyTextEdit::setMaxLength(const int& maxLength)
0136 {
0137     Q_D(KMyMoneyTextEdit);
0138     d->m_maxLength = maxLength;
0139     d->m_highligther->setMaxLength(d->m_maxLength);
0140 }
0141 
0142 int KMyMoneyTextEdit::maxLineLength() const
0143 {
0144     Q_D(const KMyMoneyTextEdit);
0145     return d->m_maxLineLength;
0146 }
0147 
0148 void KMyMoneyTextEdit::setMaxLineLength(const int& maxLineLength)
0149 {
0150     Q_D(KMyMoneyTextEdit);
0151     d->m_maxLineLength = maxLineLength;
0152     d->m_highligther->setMaxLineLength(maxLineLength);
0153 }
0154 
0155 int KMyMoneyTextEdit::maxLines() const
0156 {
0157     Q_D(const KMyMoneyTextEdit);
0158     return d->m_maxLines;
0159 }
0160 
0161 void KMyMoneyTextEdit::setMaxLines(const int& maxLines)
0162 {
0163     Q_D(KMyMoneyTextEdit);
0164     d->m_maxLines = maxLines;
0165     d->m_highligther->setMaxLines(maxLines);
0166 }
0167 
0168 QString KMyMoneyTextEdit::allowedChars() const
0169 {
0170     Q_D(const KMyMoneyTextEdit);
0171     return d->m_allowedChars;
0172 }
0173 
0174 void KMyMoneyTextEdit::setAllowedChars(const QString& allowedChars)
0175 {
0176     Q_D(KMyMoneyTextEdit);
0177     d->m_allowedChars = allowedChars;
0178     d->m_highligther->setAllowedChars(allowedChars);
0179 }