Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Utilities
KCalc
Commits
e40a90ac
Commit
e40a90ac
authored
Sep 17, 2021
by
Niklas Freund
Browse files
Added KCalcHistory font scaling support
parent
1c068ee3
Changes
4
Hide whitespace changes
Inline
Side-by-side
kcalcdisplay.cpp
View file @
e40a90ac
...
...
@@ -38,8 +38,6 @@ KCalcDisplay::KCalcDisplay(QWidget *parent)
,
history_index_
(
0
)
,
selection_timer_
(
new
QTimer
(
this
))
{
baseFont_
=
nullptr
;
setFocusPolicy
(
Qt
::
StrongFocus
);
setSizePolicy
(
QSizePolicy
(
QSizePolicy
::
Expanding
,
QSizePolicy
::
Fixed
));
...
...
kcalcdisplay.h
View file @
e40a90ac
...
...
@@ -122,7 +122,7 @@ private:
KNumber
display_amount_
;
QFont
*
baseFont_
;
QFont
*
baseFont_
=
nullptr
;
QVector
<
KNumber
>
history_list_
;
int
history_index_
;
...
...
kcalchistory.cpp
View file @
e40a90ac
...
...
@@ -32,19 +32,19 @@ KCalcHistory::~KCalcHistory()
//------------------------------------------------------------------------------
void
KCalcHistory
::
addToHistory
(
const
QString
&
str
,
bool
set_new_line_
)
{
// add_new_line is false on launch and after clearHistory()
// 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
)
{
if
(
add_new_line
_
)
{
insertHtml
(
QStringLiteral
(
"<br>"
));
moveCursor
(
QTextCursor
::
Start
);
add_new_line
=
false
;
add_new_line
_
=
false
;
}
insertHtml
(
str
);
if
(
set_new_line_
)
{
moveCursor
(
QTextCursor
::
Start
);
add_new_line
=
true
;
add_new_line
_
=
true
;
}
setAlignment
(
Qt
::
AlignRight
);
...
...
@@ -119,9 +119,13 @@ void KCalcHistory::addFuncToHistory(const QString &func)
void
KCalcHistory
::
clearHistory
()
{
clear
();
add_new_line
=
false
;
add_new_line
_
=
false
;
}
//------------------------------------------------------------------------------
// Name: changeSettings
// Desc:
//------------------------------------------------------------------------------
void
KCalcHistory
::
changeSettings
()
{
QPalette
pal
=
palette
();
...
...
@@ -133,3 +137,59 @@ void KCalcHistory::changeSettings()
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
if
(
baseFont_
)
{
delete
baseFont_
;
}
baseFont_
=
new
QFont
(
font
);
updateFont
();
}
//------------------------------------------------------------------------------
// Name: updateFont
// Desc: Update font using baseFont to better fit
//------------------------------------------------------------------------------
void
KCalcHistory
::
updateFont
()
{
// Make a working copy of the font
QFont
*
newFont
=
new
QFont
(
baseFont
());
// Calculate ideal font size based on width, using historyFont as minimum size
// constant arbitrarily chosen: 252 is min content width. 252/x = 10pt. Thus, x = 252/10 = 25.2
newFont
->
setPointSizeF
(
qMax
(
double
(
baseFont
().
pointSizeF
()),
contentsRect
().
width
()
/
25.2
));
// Apply font
QTextEdit
::
setFont
(
*
newFont
);
// Free the memory
delete
newFont
;
}
//------------------------------------------------------------------------------
// Name: baseFont
// Desc:
//------------------------------------------------------------------------------
const
QFont
&
KCalcHistory
::
baseFont
()
const
{
return
*
baseFont_
;
}
//------------------------------------------------------------------------------
// Name: resizeEvent
// Desc: resize history and adjust font size
//------------------------------------------------------------------------------
void
KCalcHistory
::
resizeEvent
(
QResizeEvent
*
event
)
{
QTextEdit
::
resizeEvent
(
event
);
// Update font size
updateFont
();
}
kcalchistory.h
View file @
e40a90ac
...
...
@@ -29,12 +29,19 @@ public:
void
addFuncToHistory
(
const
QString
&
);
void
changeSettings
();
void
setFont
(
const
QFont
&
font
);
const
QFont
&
baseFont
()
const
;
public
Q_SLOTS
:
void
clearHistory
();
protected:
void
resizeEvent
(
QResizeEvent
*
event
)
override
;
private:
bool
add_new_line
=
false
;
bool
add_new_line_
=
false
;
QFont
*
baseFont_
=
nullptr
;
void
updateFont
();
};
#endif
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment