File indexing completed on 2024-04-21 05:50:08

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 the history window
0080 //------------------------------------------------------------------------------
0081 void KCalcHistory::addFuncToHistory(const QString &func)
0082 {
0083     QString textToHistory = QStringLiteral("&nbsp;") + func + QStringLiteral("&nbsp;");
0084     addToHistory(textToHistory, false);
0085 }
0086 
0087 //------------------------------------------------------------------------------
0088 // Name: clearHistory
0089 // Desc: Clears the content of the history window
0090 //------------------------------------------------------------------------------
0091 void KCalcHistory::clearHistory()
0092 {
0093     clear();
0094     add_new_line_ = false;
0095 }
0096 
0097 //------------------------------------------------------------------------------
0098 // Name: changeSettings
0099 // Desc:
0100 //------------------------------------------------------------------------------
0101 void KCalcHistory::changeSettings()
0102 {
0103     QPalette pal = palette();
0104 
0105     pal.setColor(QPalette::Text, KCalcSettings::foreColor());
0106     pal.setColor(QPalette::Base, KCalcSettings::backColor());
0107 
0108     setPalette(pal);
0109 
0110     setFont(KCalcSettings::historyFont());
0111 }
0112 
0113 //------------------------------------------------------------------------------
0114 // Name: setFont
0115 // Desc: Set the base font and recalculate the font size to better fit
0116 //------------------------------------------------------------------------------
0117 void KCalcHistory::setFont(const QFont &font)
0118 {
0119     // Overwrite current baseFont
0120     baseFont_ = font;
0121     updateFont();
0122 }
0123 
0124 //------------------------------------------------------------------------------
0125 // Name: updateFont
0126 // Desc: Update font using baseFont to better fit
0127 //------------------------------------------------------------------------------
0128 void KCalcHistory::updateFont(double zoomFactor)
0129 {
0130     // Make a working copy of the font
0131     QFont* newFont = new QFont(baseFont());
0132 
0133     // Calculate actual font size by keeping the ratio, keeping previous zoomFactor, using historyFont as minimum size
0134     double ratio = (minimumSize().width() - contentsMargins().left() - contentsMargins().right()) / baseFont().pointSizeF();
0135     idealPointSizeF_ = contentsRect().width() / ratio;
0136     newFont->setPointSizeF(qMax(double(baseFont().pointSizeF()), idealPointSizeF_) * zoomFactor);
0137 
0138     // Apply font
0139     QTextEdit::setFont(*newFont);
0140     
0141     // Free the memory
0142     delete newFont;
0143 }
0144 
0145 //------------------------------------------------------------------------------
0146 // Name: baseFont
0147 // Desc: Returns current baseFont
0148 //------------------------------------------------------------------------------
0149 const QFont& KCalcHistory::baseFont() const
0150 {
0151     return baseFont_;
0152 }
0153 
0154 //------------------------------------------------------------------------------
0155 // Name: resizeEvent
0156 // Desc: resize history and adjust font size
0157 //------------------------------------------------------------------------------
0158 void KCalcHistory::resizeEvent(QResizeEvent* event)
0159 {
0160     QTextEdit::resizeEvent(event);
0161 
0162     // Determine current zoom
0163     double zoomFactor = currentFont().pointSizeF() / idealPointSizeF_;
0164 
0165     // Update font size
0166     updateFont(zoomFactor);
0167 
0168     updateGeometry();
0169 }
0170 
0171 #include "moc_kcalchistory.cpp"