File indexing completed on 2024-11-17 04:20:22
0001 /* 0002 * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com> 0003 * SPDX-FileCopyrightText: 2021-2022 Rohan Asokan <rohan.asokan@students.iiit.ac.in> 0004 * 0005 * SPDX-License-Identifier: GPL-3.0-or-later 0006 */ 0007 #include "inputmanager.h" 0008 #include "mathengine.h" 0009 #include "historymanager.h" 0010 #include <QDebug> 0011 0012 0013 InputManager::InputManager() 0014 { 0015 0016 KNumber::setDefaultFloatOutput(true); 0017 } 0018 0019 InputManager *InputManager::inst() 0020 { 0021 static InputManager singleton; 0022 return &singleton; 0023 } 0024 0025 const QString &InputManager::expression() const 0026 { 0027 return m_expression; 0028 } 0029 void InputManager::setExpression(const QString &expression) 0030 { 0031 m_expression = expression; 0032 } 0033 0034 const QString &InputManager::result() const 0035 { 0036 return m_result; 0037 } 0038 0039 const QString &InputManager::binaryResult() const 0040 { 0041 return m_binaryResult; 0042 } 0043 0044 const QString &InputManager::hexResult() const 0045 { 0046 return m_hexResult; 0047 } 0048 0049 void InputManager::append(const QString &subexpression) 0050 { 0051 // if expression was from result and input is numeric, clear expression 0052 if(m_moveFromResult && subexpression.size() == 1) 0053 { 0054 if(subexpression.at(0).isDigit() || subexpression.at(0) == QLatin1Char('.')) 0055 { 0056 m_expression.clear(); 0057 if (m_stack.size()) { 0058 m_stack.pop_back(); 0059 } 0060 } 0061 } 0062 m_moveFromResult = false; 0063 0064 // Call the corresponding parser based on the type of expression. 0065 MathEngine * engineInstance = MathEngine::inst(); 0066 if (m_isBinaryMode) { 0067 engineInstance->parseBinaryExpression(m_expression + subexpression); 0068 } else { 0069 engineInstance->parse(m_expression + subexpression); 0070 } 0071 0072 if(!MathEngine::inst()->error()) 0073 { 0074 m_stack.push_back(subexpression.size()); 0075 KNumber result = MathEngine::inst()->result(); 0076 m_result = result.toQString(); 0077 m_binaryResult = result.toBinaryString(0); 0078 m_hexResult = result.toHexString(0); 0079 m_expression += subexpression; 0080 Q_EMIT resultChanged(); 0081 Q_EMIT binaryResultChanged(); 0082 Q_EMIT hexResultChanged(); 0083 Q_EMIT expressionChanged(); 0084 } 0085 } 0086 0087 void InputManager::backspace() 0088 { 0089 if(!m_stack.empty()) 0090 { 0091 m_expression.chop(m_stack.back()); 0092 Q_EMIT expressionChanged(); 0093 0094 // Call the corresponding parser based on the type of expression. 0095 MathEngine * engineInstance = MathEngine::inst(); 0096 if (m_isBinaryMode) { 0097 engineInstance->parseBinaryExpression(m_expression); 0098 } else { 0099 engineInstance->parse(m_expression); 0100 } 0101 0102 if(!MathEngine::inst()->error()) 0103 { 0104 KNumber result = MathEngine::inst()->result(); 0105 m_result = result.toQString(); 0106 m_binaryResult = result.toBinaryString(0); 0107 m_hexResult = result.toHexString(0); 0108 Q_EMIT resultChanged(); 0109 Q_EMIT binaryResultChanged(); 0110 Q_EMIT hexResultChanged(); 0111 } 0112 } 0113 } 0114 0115 void InputManager::equal() 0116 { 0117 HistoryManager::inst()->addHistory(m_expression + QStringLiteral(" = ") + m_result); 0118 m_expression = m_result; 0119 m_result.clear(); 0120 m_binaryResult.clear(); 0121 m_hexResult.clear(); 0122 m_stack.clear(); 0123 m_stack.push_back(m_result.size()); 0124 0125 m_moveFromResult = true; 0126 Q_EMIT expressionChanged(); 0127 Q_EMIT resultChanged(); 0128 Q_EMIT binaryResultChanged(); 0129 Q_EMIT hexResultChanged(); 0130 } 0131 0132 void InputManager::clear() 0133 { 0134 m_expression.clear(); 0135 m_result.clear(); 0136 m_binaryResult.clear(); 0137 m_hexResult.clear(); 0138 m_stack.clear(); 0139 Q_EMIT expressionChanged(); 0140 Q_EMIT resultChanged(); 0141 Q_EMIT binaryResultChanged(); 0142 Q_EMIT hexResultChanged(); 0143 } 0144 0145 void InputManager::fromHistory(const QString &result) 0146 { 0147 m_expression = result; 0148 m_result.clear(); 0149 m_binaryResult.clear(); 0150 m_hexResult.clear(); 0151 m_stack.clear(); 0152 m_stack.push_back(result.size()); 0153 0154 m_moveFromResult = true; 0155 Q_EMIT expressionChanged(); 0156 Q_EMIT resultChanged(); 0157 Q_EMIT binaryResultChanged(); 0158 Q_EMIT hexResultChanged(); 0159 } 0160 0161 void InputManager::setBinaryMode(bool active) { 0162 m_isBinaryMode = active; 0163 clear(); 0164 } 0165 bool InputManager::binaryMode() 0166 { 0167 return m_isBinaryMode; 0168 }