File indexing completed on 2024-04-28 05:49:44
0001 /* 0002 SPDX-FileCopyrightText: 2023 Gabriel Barrantes <gabriel.barrantes.dev@outlook.com> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "kcalc_input_display.h" 0007 0008 #include <QLineEdit> 0009 // #include <kcalc_parser.h> 0010 0011 //------------------------------------------------------------------------------ 0012 // Name: KCalcInputDisplay 0013 // Desc: constructor 0014 //------------------------------------------------------------------------------ 0015 KCalcInputDisplay::KCalcInputDisplay(QWidget *parent) 0016 : QLineEdit(parent) 0017 { 0018 overwrite_ = false; 0019 hard_overwrite_ = false; 0020 } 0021 0022 //------------------------------------------------------------------------------ 0023 // Name: ~KCalcInputDisplay 0024 // Desc: 0025 //------------------------------------------------------------------------------ 0026 KCalcInputDisplay::~KCalcInputDisplay() = default; 0027 0028 //------------------------------------------------------------------------------ 0029 // Name: insertToken 0030 // Desc: 0031 //------------------------------------------------------------------------------ 0032 void KCalcInputDisplay::insertToken(const QString &token) 0033 { 0034 if (overwrite_ || hard_overwrite_) { 0035 this->clear(); 0036 overwrite_ = false; 0037 hard_overwrite_ = false; 0038 } 0039 0040 this->insert(token); 0041 } 0042 0043 //------------------------------------------------------------------------------ 0044 // Name: slotSetOverwrite 0045 // Desc: when set, inserting to the display will clear it, and then insert. 0046 // (It can be reset refocusing the display) 0047 //------------------------------------------------------------------------------ 0048 void KCalcInputDisplay::slotSetOverwrite() 0049 { 0050 overwrite_ = true; 0051 this->clearFocus(); 0052 // this->focusNextChild(); /* workaround to defocus the QWidget, 0053 // although it works, it can lead to 0054 // issues if the GUI changes */ 0055 } 0056 0057 //------------------------------------------------------------------------------ 0058 // Name: slotSetHardOverwrite 0059 // Desc: when set, focusing or inserting to the display will clear it, 0060 // and then insert (if requested), 0061 //------------------------------------------------------------------------------ 0062 void KCalcInputDisplay::slotSetHardOverwrite() 0063 { 0064 hard_overwrite_ = true; 0065 overwrite_ = true; 0066 this->focusNextChild(); /* workaround to defocus the QWidget, 0067 although it works, it can lead to 0068 issues if the GUI changes */ 0069 } 0070 0071 //------------------------------------------------------------------------------ 0072 // Name: slotClearOverwrite 0073 // Desc: resets Overwrite/hardOverwrite flags, 0074 // deletes finishing '=' when required 0075 //------------------------------------------------------------------------------ 0076 void KCalcInputDisplay::slotClearOverwrite() 0077 { 0078 overwrite_ = false; 0079 int tmp_cursorPosition = this->cursorPosition(); 0080 if (this->text().endsWith(QLatin1String("="))) { 0081 this->end(false); 0082 this->backspace(); 0083 } 0084 this->setCursorPosition(tmp_cursorPosition); 0085 0086 if (hard_overwrite_) { 0087 hard_overwrite_ = false; 0088 this->clear(); 0089 } 0090 } 0091 0092 //------------------------------------------------------------------------------ 0093 // Name: focusInEvent 0094 // Desc: focusIn, if hardOverwrite, display is cleared, 0095 // then clears overwrite and hardOverwrite flags. 0096 //------------------------------------------------------------------------------ 0097 void KCalcInputDisplay::focusInEvent(QFocusEvent *e) 0098 { 0099 QLineEdit::focusInEvent(e); 0100 slotClearOverwrite(); 0101 } 0102 0103 #include "moc_kcalc_input_display.cpp"