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
|
/*
SPDX-FileCopyrightText: 2021 Antonio Prcela <antonio.prcela@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kcalc_history.h"
#include "kcalc_settings.h"
//------------------------------------------------------------------------------
// Name: KCalcHistory
// Desc: constructor
//------------------------------------------------------------------------------
KCalcHistory::KCalcHistory(QWidget *parent)
: QTextEdit(parent)
{
setReadOnly(true);
setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
setProperty("_breeze_borders_sides", QVariant::fromValue(QFlags{Qt::LeftEdge}));
// Initialize idealPointSizeF_
idealPointSizeF_ = currentFont().pointSizeF();
}
//------------------------------------------------------------------------------
// Name: KCalcHistory
// Desc: destructor
//------------------------------------------------------------------------------
KCalcHistory::~KCalcHistory() = default;
//------------------------------------------------------------------------------
// Name: addToHistory
// Desc: Adds the latest calculations to history window
//------------------------------------------------------------------------------
void KCalcHistory::addToHistory(const QString &str, bool set_new_line_)
{
// QTextEdit's cursor location might be changed by mouse clicks.
// We have to move the cursor to correct position.
// Ensures the cursor goes back to topmost line's end during a calculation.
// 1 + 1 + _
if (!add_new_line_ && !set_new_line_) {
moveCursor(QTextCursor::Start);
moveCursor(QTextCursor::EndOfLine);
}
// add_new_line_ is false on launch and after clearHistory()
// so the first line doesn't get an unnecessary new line
if (add_new_line_) {
moveCursor(QTextCursor::Start);
insertHtml(QStringLiteral("<br>"));
moveCursor(QTextCursor::Start);
add_new_line_ = false;
}
insertHtml(str);
if (set_new_line_) {
moveCursor(QTextCursor::Start);
add_new_line_ = true;
}
setAlignment(Qt::AlignRight);
ensureCursorVisible();
}
//------------------------------------------------------------------------------
// Name: addResultToHistory
// Desc: Used mostly for functions that are not in CalcEngine::Operation
// adds "=" and the result with newline endings
//------------------------------------------------------------------------------
void KCalcHistory::addResultToHistory(const QString &display_content)
{
addToHistory(/*QStringLiteral(" = ") +*/ display_content, true);
}
//------------------------------------------------------------------------------
// Name: addFuncToHistory
// Desc: Adds the current function symbol the history window
//------------------------------------------------------------------------------
void KCalcHistory::addFuncToHistory(const QString &func)
{
QString textToHistory = QStringLiteral(" ") + func + QStringLiteral(" ");
addToHistory(textToHistory, false);
}
//------------------------------------------------------------------------------
// Name: clearHistory
// Desc: Clears the content of the history window
//------------------------------------------------------------------------------
void KCalcHistory::clearHistory()
{
clear();
add_new_line_ = false;
}
//------------------------------------------------------------------------------
// Name: changeSettings
// Desc:
//------------------------------------------------------------------------------
void KCalcHistory::changeSettings()
{
QPalette pal = palette();
pal.setColor(QPalette::Text, KCalcSettings::foreColor());
pal.setColor(QPalette::Base, KCalcSettings::backColor());
setPalette(pal);
setFont(KCalcSettings::historyFont());
}
//------------------------------------------------------------------------------
// Name: setFont
// Desc: Set the base font and recalculate the font size to better fit
//------------------------------------------------------------------------------
void KCalcHistory::setFont(const QFont &font)
{
// Overwrite current baseFont
baseFont_ = font;
updateFont();
}
//------------------------------------------------------------------------------
// Name: updateFont
// Desc: Update font using baseFont to better fit
//------------------------------------------------------------------------------
void KCalcHistory::updateFont(double zoomFactor)
{
// Make a working copy of the font
QFont newFont(baseFont());
// Calculate actual font size by keeping the ratio, keeping previous zoomFactor, using historyFont as minimum size
double ratio = (minimumSize().width() - contentsMargins().left() - contentsMargins().right()) / baseFont().pointSizeF();
idealPointSizeF_ = contentsRect().width() / ratio;
newFont.setPointSizeF(qMax(double(baseFont().pointSizeF()), idealPointSizeF_) * zoomFactor);
// Apply font
QTextEdit::setFont(newFont);
}
//------------------------------------------------------------------------------
// Name: baseFont
// Desc: Returns current baseFont
//------------------------------------------------------------------------------
const QFont &KCalcHistory::baseFont() const
{
return baseFont_;
}
//------------------------------------------------------------------------------
// Name: resizeEvent
// Desc: resize history and adjust font size
//------------------------------------------------------------------------------
void KCalcHistory::resizeEvent(QResizeEvent *event)
{
QTextEdit::resizeEvent(event);
// Determine current zoom
double zoomFactor = currentFont().pointSizeF() / idealPointSizeF_;
// Update font size
updateFont(zoomFactor);
updateGeometry();
}
#include "moc_kcalc_history.cpp"
|