File indexing completed on 2024-05-05 10:13:12

0001 /*
0002     SPDX-FileCopyrightText: 2021 Antonio Prcela <antonio.prcela@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kcalchistory.h"
0008 #include "kcalc_settings.h"
0009 
0010 //------------------------------------------------------------------------------
0011 // Name: KCalcHistory
0012 // Desc: constructor
0013 //------------------------------------------------------------------------------
0014 KCalcHistory::KCalcHistory(QWidget *parent)
0015     : QTextEdit(parent)
0016 {
0017     setReadOnly(true);
0018     setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
0019     setProperty("_breeze_borders_sides", QVariant::fromValue(QFlags{Qt::LeftEdge}));
0020 
0021     // Initialize idealPointSizeF_
0022     idealPointSizeF_ = currentFont().pointSizeF();
0023 }
0024 
0025 //------------------------------------------------------------------------------
0026 // Name: KCalcHistory
0027 // Desc: destructor
0028 //------------------------------------------------------------------------------
0029 KCalcHistory::~KCalcHistory() = default;
0030 
0031 //------------------------------------------------------------------------------
0032 // Name: addToHistory
0033 // Desc: Adds the latest calculations to  history window
0034 //------------------------------------------------------------------------------
0035 void KCalcHistory::addToHistory(const QString &str, bool set_new_line_)
0036 {
0037     // QTextEdit's cursor location might be changed by mouse clicks.
0038     // We have to move the cursor to correct position.
0039 
0040     //  Ensures the cursor goes back to topmost line's end during a calculation.
0041     //  1 + 1 + _
0042     if (!add_new_line_ && !set_new_line_) {
0043         moveCursor(QTextCursor::Start);
0044         moveCursor(QTextCursor::EndOfLine);
0045     }
0046 
0047     // add_new_line_ is false on launch and after clearHistory()
0048     // so the first line doesn't get an unnecessary new line
0049     if (add_new_line_) {
0050         moveCursor(QTextCursor::Start);
0051         insertHtml(QStringLiteral("<br>"));
0052         moveCursor(QTextCursor::Start);
0053         add_new_line_ = false;
0054     }
0055 
0056     insertHtml(str);
0057 
0058     if (set_new_line_) {
0059         moveCursor(QTextCursor::Start);
0060         add_new_line_ = true;
0061     }
0062 
0063     setAlignment(Qt::AlignRight);
0064     ensureCursorVisible();
0065 }
0066 
0067 //------------------------------------------------------------------------------
0068 // Name: addResultToHistory
0069 // Desc: Used mostly for functions that are not in CalcEngine::Operation
0070 //       adds "=" and the result with newline endings
0071 //------------------------------------------------------------------------------
0072 void KCalcHistory::addResultToHistory(const QString &display_content)
0073 {
0074     addToHistory(QStringLiteral("&nbsp;=&nbsp;") + display_content, true);
0075 }
0076 
0077 //------------------------------------------------------------------------------
0078 // Name: addFuncToHistory
0079 // Desc: Adds the current function symbol, taken via CalcEngine::Operation
0080 //       to the history window
0081 //------------------------------------------------------------------------------
0082 void KCalcHistory::addFuncToHistory(const CalcEngine::Operation FUNC)
0083 {
0084     QString textToHistory = QStringLiteral("&nbsp;");
0085 
0086     if (FUNC == CalcEngine::FUNC_PERCENT) {
0087         textToHistory += QStringLiteral("%");
0088     } else if (FUNC == CalcEngine::FUNC_OR) {
0089         textToHistory += QStringLiteral("OR");
0090     } else if (FUNC == CalcEngine::FUNC_XOR) {
0091         textToHistory += QStringLiteral("XOR");
0092     } else if (FUNC == CalcEngine::FUNC_AND) {
0093         textToHistory += QStringLiteral("AND");
0094     } else if (FUNC == CalcEngine::FUNC_LSH) {
0095         textToHistory += QStringLiteral("Lsh");
0096     } else if (FUNC == CalcEngine::FUNC_RSH) {
0097         textToHistory += QStringLiteral("Rsh");
0098     } else if (FUNC == CalcEngine::FUNC_ADD) {
0099         textToHistory += QStringLiteral("+");
0100     } else if (FUNC == CalcEngine::FUNC_SUBTRACT) {
0101         textToHistory += QStringLiteral("-");
0102     } else if (FUNC == CalcEngine::FUNC_MULTIPLY) {
0103         textToHistory += QStringLiteral("×");
0104     } else if (FUNC == CalcEngine::FUNC_DIVIDE) {
0105         textToHistory += QStringLiteral("÷");
0106     } else if (FUNC == CalcEngine::FUNC_MOD) {
0107         textToHistory += QStringLiteral("Mod");
0108     } else if (FUNC == CalcEngine::FUNC_INTDIV) {
0109         textToHistory += QStringLiteral("IntDiv");
0110     } else if (FUNC == CalcEngine::FUNC_BINOM) {
0111         textToHistory += QStringLiteral("Binom");
0112     }
0113 
0114     textToHistory += QStringLiteral("&nbsp;");
0115     addToHistory(textToHistory, false);
0116 }
0117 
0118 //------------------------------------------------------------------------------
0119 // Name: addFuncToHistory
0120 // Desc: Adds the current function symbol the history window
0121 //------------------------------------------------------------------------------
0122 void KCalcHistory::addFuncToHistory(const QString &func)
0123 {
0124     QString textToHistory = QStringLiteral("&nbsp;") + func + QStringLiteral("&nbsp;");
0125     addToHistory(textToHistory, false);
0126 }
0127 
0128 //------------------------------------------------------------------------------
0129 // Name: clearHistory
0130 // Desc: Clears the content of the history window
0131 //------------------------------------------------------------------------------
0132 void KCalcHistory::clearHistory()
0133 {
0134     clear();
0135     add_new_line_ = false;
0136 }
0137 
0138 //------------------------------------------------------------------------------
0139 // Name: changeSettings
0140 // Desc:
0141 //------------------------------------------------------------------------------
0142 void KCalcHistory::changeSettings()
0143 {
0144     QPalette pal = palette();
0145 
0146     pal.setColor(QPalette::Text, KCalcSettings::foreColor());
0147     pal.setColor(QPalette::Base, KCalcSettings::backColor());
0148 
0149     setPalette(pal);
0150 
0151     setFont(KCalcSettings::historyFont());
0152 }
0153 
0154 //------------------------------------------------------------------------------
0155 // Name: setFont
0156 // Desc: Set the base font and recalculate the font size to better fit
0157 //------------------------------------------------------------------------------
0158 void KCalcHistory::setFont(const QFont &font)
0159 {
0160     // Overwrite current baseFont
0161     baseFont_ = font;
0162     updateFont();
0163 }
0164 
0165 //------------------------------------------------------------------------------
0166 // Name: updateFont
0167 // Desc: Update font using baseFont to better fit
0168 //------------------------------------------------------------------------------
0169 void KCalcHistory::updateFont(double zoomFactor)
0170 {
0171     // Make a working copy of the font
0172     QFont* newFont = new QFont(baseFont());
0173 
0174     // Calculate actual font size by keeping the ratio, keeping previous zoomFactor, using historyFont as minimum size
0175     double ratio = (minimumSize().width() - contentsMargins().left() - contentsMargins().right()) / baseFont().pointSizeF();
0176     idealPointSizeF_ = contentsRect().width() / ratio;
0177     newFont->setPointSizeF(qMax(double(baseFont().pointSizeF()), idealPointSizeF_) * zoomFactor);
0178 
0179     // Apply font
0180     QTextEdit::setFont(*newFont);
0181     
0182     // Free the memory
0183     delete newFont;
0184 }
0185 
0186 //------------------------------------------------------------------------------
0187 // Name: baseFont
0188 // Desc: Returns current baseFont
0189 //------------------------------------------------------------------------------
0190 const QFont& KCalcHistory::baseFont() const
0191 {
0192     return baseFont_;
0193 }
0194 
0195 //------------------------------------------------------------------------------
0196 // Name: resizeEvent
0197 // Desc: resize history and adjust font size
0198 //------------------------------------------------------------------------------
0199 void KCalcHistory::resizeEvent(QResizeEvent* event)
0200 {
0201     QTextEdit::resizeEvent(event);
0202 
0203     // Determine current zoom
0204     double zoomFactor = currentFont().pointSizeF() / idealPointSizeF_;
0205 
0206     // Update font size
0207     updateFont(zoomFactor);
0208 
0209     updateGeometry();
0210 }
0211 
0212 #include "moc_kcalchistory.cpp"