File indexing completed on 2024-04-28 07:29:07
0001 /* 0002 KBruch - line edit used for entering prime factors 0003 SPDX-FileCopyrightText: 2011 Sebastian Stein <seb.kde@hpfsc.de> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "PrimeFactorsLineEdit.h" 0009 0010 #ifdef DEBUG 0011 #include <QDebug> 0012 #endif 0013 0014 /* constructor */ 0015 PrimeFactorsLineEdit::PrimeFactorsLineEdit(QWidget * parent) 0016 : QLineEdit(parent) 0017 { 0018 #ifdef DEBUG 0019 qDebug() << QStringLiteral("constructor PrimeFactorsLineEdit"); 0020 #endif 0021 m_usedFactors << QStringLiteral("2") << QStringLiteral("3") << QStringLiteral("5") << QStringLiteral("7") << QStringLiteral("11") << QStringLiteral("13") << QStringLiteral("17") << QStringLiteral("19"); 0022 // I need this so dead keys go trough keyPressEvent 0023 setAttribute(Qt::WA_InputMethodEnabled, false); 0024 connect(this, &PrimeFactorsLineEdit::textEdited, this, &PrimeFactorsLineEdit::textHasChanged); 0025 } 0026 0027 /* destructor */ 0028 PrimeFactorsLineEdit::~PrimeFactorsLineEdit() 0029 { 0030 #ifdef DEBUG 0031 qDebug() << QStringLiteral("destructor PrimeFactorsLineEdit"); 0032 #endif 0033 } 0034 0035 void PrimeFactorsLineEdit::textHasChanged(const QString &text) 0036 { 0037 QPalette palette; 0038 0039 if (!checkCorrectness(text)) { 0040 palette.setColor(QPalette::Base, Qt::red); 0041 setPalette(palette); 0042 Q_EMIT contentIsRight(false); 0043 } else { 0044 palette.setColor(QPalette::Base, Qt::white); 0045 setPalette(palette); 0046 Q_EMIT contentIsRight(true); 0047 } 0048 } 0049 0050 bool PrimeFactorsLineEdit::checkCorrectness(const QString& theText) 0051 { 0052 QString auxStr = theText; 0053 QString noSpaces = auxStr.remove(QStringLiteral(" ")); 0054 QStringList terms = noSpaces.split(QLatin1Char('x')); 0055 0056 return (areFactors(terms) || text().isEmpty()); 0057 } 0058 0059 bool PrimeFactorsLineEdit::areFactors(const QStringList& factors) 0060 { 0061 m_theFactors.clear(); 0062 for (const QString & str : factors) { 0063 bool found = false; 0064 for (const QString & aux : std::as_const(m_usedFactors)) { 0065 if (str.compare(str, aux) == 0) { 0066 found = true; 0067 m_theFactors.append(str); 0068 break; 0069 } 0070 } 0071 if (!found) { 0072 return false; 0073 } 0074 } 0075 0076 return true; 0077 } 0078 0079 QStringList PrimeFactorsLineEdit::getFactors() const 0080 { 0081 return m_theFactors; 0082 } 0083 0084 void PrimeFactorsLineEdit::keyPressEvent(QKeyEvent * event) 0085 { 0086 QString allowedChars = QStringLiteral("123579xX*"); 0087 QString allowedDigits = QStringLiteral("123579"); 0088 QString symbols = QStringLiteral("xX*"); 0089 bool backspaceKey = (event->key() == Qt::Key_Backspace); 0090 bool returnKey = (event->key() == Qt::Key_Return 0091 || event->key() == Qt::Key_Enter); 0092 0093 setCursorPosition(text().length()); 0094 0095 if (allowedChars.contains(event->text()) || backspaceKey || returnKey) { 0096 QString lastFactor; 0097 QString ch = QStringLiteral("#"); 0098 QString factor; 0099 if (!event->text().isEmpty()) { 0100 ch = event->text().at(0); 0101 } 0102 if (!text().isEmpty()) { 0103 lastFactor = text().section(QLatin1Char('x'), -1); 0104 factor = lastFactor + ch; 0105 } else { 0106 factor = ch; 0107 } 0108 0109 if (allowedChars.contains(ch) || backspaceKey || returnKey) { 0110 // turns '*' and 'X' into 'x' to avoid mixed symbols 0111 // and to make easier to split the text later 0112 if (!event->text().isEmpty() && (symbols.indexOf(ch) != -1) && (factor.compare(QLatin1String("x")) != 0) && !lastFactor.isEmpty() && lastFactor.compare(QLatin1String("1")) != 0) { 0113 QKeyEvent myKeyEvent(QKeyEvent::KeyPress, Qt::Key_X, Qt::NoModifier, QStringLiteral("x"), false, 0); 0114 QLineEdit::keyPressEvent(&myKeyEvent); 0115 } 0116 0117 if ((m_usedFactors.indexOf(factor) == -1) 0118 && (factor.compare(QStringLiteral("1")) != 0) && !backspaceKey && 0119 !returnKey) { 0120 return; 0121 } 0122 0123 QLineEdit::keyPressEvent(event); 0124 } 0125 } 0126 } 0127 0128 #include "moc_PrimeFactorsLineEdit.cpp"