1/*
2 SPDX-FileCopyrightText: 2021 Antonio Prcela <antonio.prcela@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "kcalchistory.h"
8#include "kcalc_settings.h"
9
10//------------------------------------------------------------------------------
11// Name: KCalcHistory
12// Desc: constructor
13//------------------------------------------------------------------------------
14KCalcHistory::KCalcHistory(QWidget *parent)
15 : QTextEdit(parent)
16{
17 setReadOnly(true);
18 setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
19
20 // Initialize idealPointSizeF_
21 idealPointSizeF_ = currentFont().pointSizeF();
22}
23
24//------------------------------------------------------------------------------
25// Name: KCalcHistory
26// Desc: destructor
27//------------------------------------------------------------------------------
28KCalcHistory::~KCalcHistory() = default;
29
30//------------------------------------------------------------------------------
31// Name: addToHistory
32// Desc: Adds the latest calculations to history window
33//------------------------------------------------------------------------------
34void KCalcHistory::addToHistory(const QString &str, bool set_new_line_)
35{
36 // QTextEdit's cursor location might be changed by mouse clicks.
37 // We have to move the cursor to correct position.
38
39 // Ensures the cursor goes back to topmost line's end during a calculation.
40 // 1 + 1 + _
41 if (!add_new_line_ && !set_new_line_) {
42 moveCursor(QTextCursor::Start);
43 moveCursor(QTextCursor::EndOfLine);
44 }
45
46 // add_new_line_ is false on launch and after clearHistory()
47 // so the first line doesn't get an unnecessary new line
48 if (add_new_line_) {
49 moveCursor(QTextCursor::Start);
50 insertHtml(QStringLiteral("<br>"));
51 moveCursor(QTextCursor::Start);
52 add_new_line_ = false;
53 }
54
55 insertHtml(str);
56
57 if (set_new_line_) {
58 moveCursor(QTextCursor::Start);
59 add_new_line_ = true;
60 }
61
62 setAlignment(Qt::AlignRight);
63 ensureCursorVisible();
64}
65
66//------------------------------------------------------------------------------
67// Name: addResultToHistory
68// Desc: Used mostly for functions that are not in CalcEngine::Operation
69// adds "=" and the result with newline endings
70//------------------------------------------------------------------------------
71void KCalcHistory::addResultToHistory(const QString &display_content)
72{
73 addToHistory(QStringLiteral("&nbsp;=&nbsp;") + display_content, true);
74}
75
76//------------------------------------------------------------------------------
77// Name: addFuncToHistory
78// Desc: Adds the current function symbol, taken via CalcEngine::Operation
79// to the history window
80//------------------------------------------------------------------------------
81void KCalcHistory::addFuncToHistory(const CalcEngine::Operation FUNC)
82{
83 QString textToHistory = QStringLiteral("&nbsp;");
84
85 if (FUNC == CalcEngine::FUNC_PERCENT) {
86 textToHistory += QStringLiteral("%");
87 } else if (FUNC == CalcEngine::FUNC_OR) {
88 textToHistory += QStringLiteral("OR");
89 } else if (FUNC == CalcEngine::FUNC_XOR) {
90 textToHistory += QStringLiteral("XOR");
91 } else if (FUNC == CalcEngine::FUNC_AND) {
92 textToHistory += QStringLiteral("AND");
93 } else if (FUNC == CalcEngine::FUNC_LSH) {
94 textToHistory += QStringLiteral("Lsh");
95 } else if (FUNC == CalcEngine::FUNC_RSH) {
96 textToHistory += QStringLiteral("Rsh");
97 } else if (FUNC == CalcEngine::FUNC_ADD) {
98 textToHistory += QStringLiteral("+");
99 } else if (FUNC == CalcEngine::FUNC_SUBTRACT) {
100 textToHistory += QStringLiteral("-");
101 } else if (FUNC == CalcEngine::FUNC_MULTIPLY) {
102 textToHistory += QStringLiteral("×");
103 } else if (FUNC == CalcEngine::FUNC_DIVIDE) {
104 textToHistory += QStringLiteral("÷");
105 } else if (FUNC == CalcEngine::FUNC_MOD) {
106 textToHistory += QStringLiteral("Mod");
107 } else if (FUNC == CalcEngine::FUNC_INTDIV) {
108 textToHistory += QStringLiteral("IntDiv");
109 } else if (FUNC == CalcEngine::FUNC_BINOM) {
110 textToHistory += QStringLiteral("Binom");
111 }
112
113 textToHistory += QStringLiteral("&nbsp;");
114 addToHistory(textToHistory, false);
115}
116
117//------------------------------------------------------------------------------
118// Name: addFuncToHistory
119// Desc: Adds the current function symbol the history window
120//------------------------------------------------------------------------------
121void KCalcHistory::addFuncToHistory(const QString &func)
122{
123 QString textToHistory = QStringLiteral("&nbsp;") + func + QStringLiteral("&nbsp;");
124 addToHistory(textToHistory, false);
125}
126
127//------------------------------------------------------------------------------
128// Name: clearHistory
129// Desc: Clears the content of the history window
130//------------------------------------------------------------------------------
131void KCalcHistory::clearHistory()
132{
133 clear();
134 add_new_line_ = false;
135}
136
137//------------------------------------------------------------------------------
138// Name: changeSettings
139// Desc:
140//------------------------------------------------------------------------------
141void KCalcHistory::changeSettings()
142{
143 QPalette pal = palette();
144
145 pal.setColor(QPalette::Text, KCalcSettings::foreColor());
146 pal.setColor(QPalette::Base, KCalcSettings::backColor());
147
148 setPalette(pal);
149
150 setFont(KCalcSettings::historyFont());
151}
152
153//------------------------------------------------------------------------------
154// Name: setFont
155// Desc: Set the base font and recalculate the font size to better fit
156//------------------------------------------------------------------------------
157void KCalcHistory::setFont(const QFont &font)
158{
159 // Overwrite current baseFont
160 baseFont_ = font;
161 updateFont();
162}
163
164//------------------------------------------------------------------------------
165// Name: updateFont
166// Desc: Update font using baseFont to better fit
167//------------------------------------------------------------------------------
168void KCalcHistory::updateFont(double zoomFactor)
169{
170 // Make a working copy of the font
171 QFont* newFont = new QFont(baseFont());
172
173 // Calculate actual font size by keeping the ratio, keeping previous zoomFactor, using historyFont as minimum size
174 double ratio = (minimumSize().width() - contentsMargins().left() - contentsMargins().right()) / baseFont().pointSizeF();
175 idealPointSizeF_ = contentsRect().width() / ratio;
176 newFont->setPointSizeF(qMax(double(baseFont().pointSizeF()), idealPointSizeF_) * zoomFactor);
177
178 // Apply font
179 QTextEdit::setFont(*newFont);
180
181 // Free the memory
182 delete newFont;
183}
184
185//------------------------------------------------------------------------------
186// Name: baseFont
187// Desc: Returns current baseFont
188//------------------------------------------------------------------------------
189const QFont& KCalcHistory::baseFont() const
190{
191 return baseFont_;
192}
193
194//------------------------------------------------------------------------------
195// Name: resizeEvent
196// Desc: resize history and adjust font size
197//------------------------------------------------------------------------------
198void KCalcHistory::resizeEvent(QResizeEvent* event)
199{
200 QTextEdit::resizeEvent(event);
201
202 // Determine current zoom
203 double zoomFactor = currentFont().pointSizeF() / idealPointSizeF_;
204
205 // Update font size
206 updateFont(zoomFactor);
207
208 updateGeometry();
209}
210
211#include "moc_kcalchistory.cpp"
212

source code of kcalc/kcalchistory.cpp