File: kcalc_input_display.cpp

package info (click to toggle)
kcalc 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,740 kB
  • sloc: cpp: 13,605; xml: 699; python: 49; makefile: 6; sh: 4
file content (221 lines) | stat: -rw-r--r-- 7,001 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
    SPDX-FileCopyrightText: 2023 Gabriel Barrantes <gabriel.barrantes.dev@outlook.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "kcalc_input_display.h"

#include <QKeyEvent>
#include <QLineEdit>

//------------------------------------------------------------------------------
// Name: KCalcInputDisplay
// Desc: constructor
//------------------------------------------------------------------------------
KCalcInputDisplay::KCalcInputDisplay(QWidget *parent)
    : QLineEdit(parent)
{
    overwrite_ = false;
    hard_overwrite_ = false;
    has_result_ = false;
}

//------------------------------------------------------------------------------
// Name: ~KCalcInputDisplay
// Desc:
//------------------------------------------------------------------------------
KCalcInputDisplay::~KCalcInputDisplay() = default;

//------------------------------------------------------------------------------
// Name: reset
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::reset(bool clearRedoUndo /*=false*/)
{
    this->clear();
    overwrite_ = false;
    hard_overwrite_ = false;
    has_result_ = false;
    if (clearRedoUndo) {
        this->setText(QStringLiteral(""));
    }
}

//------------------------------------------------------------------------------
// Name: insertToken
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::insertToken(const QString &token)
{
    if (overwrite_ || hard_overwrite_) {
        this->clear();
        overwrite_ = false;
        hard_overwrite_ = false;
    }

    this->insert(token);
    has_result_ = false;
}

//------------------------------------------------------------------------------
// Name: insertTokenNumeric
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::insertTokenNumeric(const QString &token)
{
    if (overwrite_ || hard_overwrite_ || has_result_) {
        this->clear();
        overwrite_ = false;
        hard_overwrite_ = false;
        has_result_ = false;
    }

    this->insert(token);
}

//------------------------------------------------------------------------------
// Name: insertTokenFunction
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::insertTokenFunction(const QString &token)
{
    if (overwrite_ || hard_overwrite_) {
        overwrite_ = false;
        hard_overwrite_ = false;
        this->clear();
        this->insert(token);
        this->insert(QStringLiteral("("));
    } else if (has_result_) {
        this->home(false);
        this->insert(token);
        this->insert(QStringLiteral("("));
        this->end(false);
        this->insert(QStringLiteral(")"));
    } else {
        this->insert(token);
        this->insert(QStringLiteral("("));
    }
}

//------------------------------------------------------------------------------
// Name: setHasResult
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::setHasResult()
{
    has_result_ = true;
    this->clearFocus();
}

//------------------------------------------------------------------------------
// Name: slotSetOverwrite
// Desc: when set, inserting to the display will clear it, and then insert.
//       (It can be reset refocusing the display)
//------------------------------------------------------------------------------
void KCalcInputDisplay::slotSetOverwrite()
{
    overwrite_ = true;
    this->clearFocus();
    // this->focusNextChild(); /* workaround to defocus the QWidget,
    // although it works, it can lead to
    // issues if the GUI changes */
}

//------------------------------------------------------------------------------
// Name: slotSetHardOverwrite
// Desc: when set, focusing or inserting to the display will clear it,
//       and then insert (if requested),
//------------------------------------------------------------------------------
void KCalcInputDisplay::slotSetHardOverwrite()
{
    hard_overwrite_ = true;
    overwrite_ = true;
    this->focusNextChild(); /* workaround to defocus the QWidget,
                               although it works, it can lead to
                               issues if the GUI changes */
}

//------------------------------------------------------------------------------
// Name: clearHasResult
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::clearHasResult()
{
    has_result_ = false;
}

//------------------------------------------------------------------------------
// Name: slotClearOverwrite
// Desc: resets Overwrite/hardOverwrite flags,
//       deletes finishing '=' when required
//------------------------------------------------------------------------------
void KCalcInputDisplay::slotClearOverwrite()
{
    overwrite_ = false;
    int tmp_cursorPosition = this->cursorPosition();
    if (this->text().endsWith(QLatin1String("="))) {
        this->end(false);
        this->backspace();
    }
    this->setCursorPosition(tmp_cursorPosition);

    if (hard_overwrite_) {
        hard_overwrite_ = false;
        this->clear();
    }
}

//------------------------------------------------------------------------------
// Name: keyPressEvent
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::keyPressEvent(QKeyEvent *event)
{
    if (event->matches(QKeySequence::Undo)) {
        undoCalculation();
    } else if (event->matches(QKeySequence::Redo)) {
        redoCalculation();
    } else {
        QLineEdit::keyPressEvent(event);
    }
}

//------------------------------------------------------------------------------
// Name: undoCalculation
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::undoCalculation()
{
    QLineEdit::undo();
    if (this->isUndoAvailable() && text().isEmpty()) {
        QLineEdit::undo();
        QLineEdit::deselect();
    }
}

//------------------------------------------------------------------------------
// Name: redoCalculation
// Desc:
//------------------------------------------------------------------------------
void KCalcInputDisplay::redoCalculation()
{
    QLineEdit::redo();
    if (this->isRedoAvailable() && text().isEmpty()) {
        QLineEdit::redo();
        QLineEdit::deselect();
    }
}

//------------------------------------------------------------------------------
// Name: focusInEvent
// Desc: focusIn, if hardOverwrite, display is cleared,
//       then clears overwrite and hardOverwrite flags.
//------------------------------------------------------------------------------
void KCalcInputDisplay::focusInEvent(QFocusEvent *e)
{
    QLineEdit::focusInEvent(e);
    slotClearOverwrite();
    clearHasResult();
}

#include "moc_kcalc_input_display.cpp"