File indexing completed on 2022-01-30 18:32:35
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 0020 // Initialize idealPointSizeF_ 0021 idealPointSizeF_ = currentFont().pointSizeF(); 0022 } 0023 0024 //------------------------------------------------------------------------------ 0025 // Name: KCalcHistory 0026 // Desc: destructor 0027 //------------------------------------------------------------------------------ 0028 KCalcHistory::~KCalcHistory() = default; 0029 0030 //------------------------------------------------------------------------------ 0031 // Name: addToHistory 0032 // Desc: Adds the latest calculations to history window 0033 //------------------------------------------------------------------------------ 0034 void KCalcHistory::addToHistory(const QString &str, bool set_new_line_) 0035 { 0036 // add_new_line_ is false on launch and after clearHistory() 0037 // so the first line doesn't get an unnecessary new line 0038 if (add_new_line_) { 0039 insertHtml(QStringLiteral("<br>")); 0040 moveCursor(QTextCursor::Start); 0041 add_new_line_ = false; 0042 } 0043 0044 insertHtml(str); 0045 0046 if (set_new_line_) { 0047 moveCursor(QTextCursor::Start); 0048 add_new_line_ = true; 0049 } 0050 0051 setAlignment(Qt::AlignRight); 0052 ensureCursorVisible(); 0053 } 0054 0055 //------------------------------------------------------------------------------ 0056 // Name: addResultToHistory 0057 // Desc: Used mostly for functions that are not in CalcEngine::Operation 0058 // adds "=" and the result with newline endings 0059 //------------------------------------------------------------------------------ 0060 void KCalcHistory::addResultToHistory(const QString &display_content) 0061 { 0062 addToHistory(QStringLiteral(" = ") + display_content, true); 0063 } 0064 0065 //------------------------------------------------------------------------------ 0066 // Name: addFuncToHistory 0067 // Desc: Adds the current function symbol, taken via CalcEngine::Operation 0068 // to the history window 0069 //------------------------------------------------------------------------------ 0070 void KCalcHistory::addFuncToHistory(const CalcEngine::Operation FUNC) 0071 { 0072 QString textToHistroy = QStringLiteral(" "); 0073 0074 if (FUNC == CalcEngine::FUNC_PERCENT) { 0075 textToHistroy += QStringLiteral("%"); 0076 } else if (FUNC == CalcEngine::FUNC_OR) { 0077 textToHistroy += QStringLiteral("OR"); 0078 } else if (FUNC == CalcEngine::FUNC_XOR) { 0079 textToHistroy += QStringLiteral("XOR"); 0080 } else if (FUNC == CalcEngine::FUNC_AND) { 0081 textToHistroy += QStringLiteral("AND"); 0082 } else if (FUNC == CalcEngine::FUNC_LSH) { 0083 textToHistroy += QStringLiteral("Lsh"); 0084 } else if (FUNC == CalcEngine::FUNC_RSH) { 0085 textToHistroy += QStringLiteral("Rsh"); 0086 } else if (FUNC == CalcEngine::FUNC_ADD) { 0087 textToHistroy += QStringLiteral("+"); 0088 } else if (FUNC == CalcEngine::FUNC_SUBTRACT) { 0089 textToHistroy += QStringLiteral("-"); 0090 } else if (FUNC == CalcEngine::FUNC_MULTIPLY) { 0091 textToHistroy += QStringLiteral("×"); 0092 } else if (FUNC == CalcEngine::FUNC_DIVIDE) { 0093 textToHistroy += QStringLiteral("÷"); 0094 } else if (FUNC == CalcEngine::FUNC_MOD) { 0095 textToHistroy += QStringLiteral("Mod"); 0096 } else if (FUNC == CalcEngine::FUNC_INTDIV) { 0097 textToHistroy += QStringLiteral("IntDiv"); 0098 } else if (FUNC == CalcEngine::FUNC_BINOM) { 0099 textToHistroy += QStringLiteral("Binom"); 0100 } 0101 0102 textToHistroy += QStringLiteral(" "); 0103 addToHistory(textToHistroy, false); 0104 } 0105 0106 //------------------------------------------------------------------------------ 0107 // Name: addFuncToHistory 0108 // Desc: Adds the current function symbol the history window 0109 //------------------------------------------------------------------------------ 0110 void KCalcHistory::addFuncToHistory(const QString &func) 0111 { 0112 QString textToHistroy = QStringLiteral(" ") + func + QStringLiteral(" "); 0113 addToHistory(textToHistroy, false); 0114 } 0115 0116 //------------------------------------------------------------------------------ 0117 // Name: clearHistory 0118 // Desc: Clears the content of the history window 0119 //------------------------------------------------------------------------------ 0120 void KCalcHistory::clearHistory() 0121 { 0122 clear(); 0123 add_new_line_ = false; 0124 } 0125 0126 //------------------------------------------------------------------------------ 0127 // Name: changeSettings 0128 // Desc: 0129 //------------------------------------------------------------------------------ 0130 void KCalcHistory::changeSettings() 0131 { 0132 QPalette pal = palette(); 0133 0134 pal.setColor(QPalette::Text, KCalcSettings::foreColor()); 0135 pal.setColor(QPalette::Base, KCalcSettings::backColor()); 0136 0137 setPalette(pal); 0138 0139 setFont(KCalcSettings::historyFont()); 0140 } 0141 0142 //------------------------------------------------------------------------------ 0143 // Name: setFont 0144 // Desc: Set the base font and recalculate the font size to better fit 0145 //------------------------------------------------------------------------------ 0146 void KCalcHistory::setFont(const QFont &font) 0147 { 0148 // Overwrite current baseFont 0149 baseFont_ = font; 0150 updateFont(); 0151 } 0152 0153 //------------------------------------------------------------------------------ 0154 // Name: updateFont 0155 // Desc: Update font using baseFont to better fit 0156 //------------------------------------------------------------------------------ 0157 void KCalcHistory::updateFont(double zoomFactor) 0158 { 0159 // Make a working copy of the font 0160 QFont* newFont = new QFont(baseFont()); 0161 0162 // Calculate actual font size by keeping the ratio, keeping previous zoomFactor, using historyFont as minimum size 0163 double ratio = (minimumSize().width() - contentsMargins().left() - contentsMargins().right()) / baseFont().pointSizeF(); 0164 idealPointSizeF_ = contentsRect().width() / ratio; 0165 newFont->setPointSizeF(qMax(double(baseFont().pointSizeF()), idealPointSizeF_) * zoomFactor); 0166 0167 // Apply font 0168 QTextEdit::setFont(*newFont); 0169 0170 // Free the memory 0171 delete newFont; 0172 } 0173 0174 //------------------------------------------------------------------------------ 0175 // Name: baseFont 0176 // Desc: Returns current baseFont 0177 //------------------------------------------------------------------------------ 0178 const QFont& KCalcHistory::baseFont() const 0179 { 0180 return baseFont_; 0181 } 0182 0183 //------------------------------------------------------------------------------ 0184 // Name: resizeEvent 0185 // Desc: resize history and adjust font size 0186 //------------------------------------------------------------------------------ 0187 void KCalcHistory::resizeEvent(QResizeEvent* event) 0188 { 0189 QTextEdit::resizeEvent(event); 0190 0191 // Determine current zoom 0192 double zoomFactor = currentFont().pointSizeF() / idealPointSizeF_; 0193 0194 // Update font size 0195 updateFont(zoomFactor); 0196 0197 updateGeometry(); 0198 }