Commit 0eb1008f authored by Niklas Freund's avatar Niklas Freund
Browse files

Fixed several resizing issues

parent 6c3b4763
......@@ -690,6 +690,7 @@ void KCalculator::updateGeometry()
for (QObject *obj : leftPadList) {
if (auto const button = qobject_cast<KCalcButton *>(obj)) {
button->setMinimumWidth(em.width() * 4 + margin * 2);
button->setMinimumHeight(em.height() * 1.25 + margin * 2);
button->installEventFilter(this);
}
}
......@@ -699,6 +700,7 @@ void KCalculator::updateGeometry()
for (QObject *obj : rightPadList) {
if (auto const button = qobject_cast<KCalcButton *>(obj)) {
button->setMinimumWidth(em.width() * 3 + margin * 2);
button->setMinimumHeight(em.height() * 1.25 + margin * 2);
button->installEventFilter(this);
}
}
......@@ -708,6 +710,7 @@ void KCalculator::updateGeometry()
for (QObject *obj : numericPadList) {
if (auto const button = qobject_cast<KCalcButton *>(obj)) {
button->setMinimumWidth(em.width() * 3 + margin * 2);
button->setMinimumHeight(em.height() * 1.25 + margin * 2);
button->installEventFilter(this);
}
}
......@@ -1896,6 +1899,10 @@ void KCalculator::slotSetSimpleMode()
QSizePolicy policy = leftPad->sizePolicy();
policy.setHorizontalStretch(0);
leftPad->setSizePolicy(policy);
// update font size
QApplication::processEvents();
setFonts();
}
//------------------------------------------------------------------------------
......@@ -1939,6 +1946,10 @@ void KCalculator::slotSetScienceMode()
QSizePolicy policy = leftPad->sizePolicy();
policy.setHorizontalStretch(1);
leftPad->setSizePolicy(policy);
// update font size
QApplication::processEvents();
setFonts();
}
//------------------------------------------------------------------------------
......@@ -1982,6 +1993,10 @@ void KCalculator::slotSetStatisticMode()
QSizePolicy policy = leftPad->sizePolicy();
policy.setHorizontalStretch(1);
leftPad->setSizePolicy(policy);
// update font size
QApplication::processEvents();
setFonts();
}
//------------------------------------------------------------------------------
......@@ -2023,6 +2038,10 @@ void KCalculator::slotSetNumeralMode()
QSizePolicy policy = leftPad->sizePolicy();
policy.setHorizontalStretch(1);
leftPad->setSizePolicy(policy);
// update font size
QApplication::processEvents();
setFonts();
}
//------------------------------------------------------------------------------
......@@ -2474,8 +2493,6 @@ void KCalculator::setFonts()
button->setFont(buttonFont);
}
}
updateGeometry();
}
//------------------------------------------------------------------------------
......@@ -2487,6 +2504,7 @@ bool KCalculator::event(QEvent *e)
switch (e->type()) {
case QEvent::ApplicationFontChange:
setFonts();
updateGeometry();
break;
case QEvent::ApplicationPaletteChange:
setColors();
......@@ -2615,6 +2633,8 @@ void KCalculator::resizeEvent(QResizeEvent* event)
KXmlGuiWindow::resizeEvent(event);
// If the content size is now larger than the window size, resize window to fit
// (Workaround for bug where changing from simple to science mode in maximized state
// and then restoring results in the window being too small for content)
QSize contentSize = firstVerticalLayout->contentsRect().size();
QMargins contentMargins = KCalculator::contentsMargins();
QSize windowSize = KCalculator::frameSize();
......
......@@ -38,6 +38,8 @@ KCalcDisplay::KCalcDisplay(QWidget *parent)
, history_index_(0)
, selection_timer_(new QTimer(this))
{
baseFont_ = nullptr;
setFocusPolicy(Qt::StrongFocus);
setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
......@@ -600,16 +602,31 @@ void KCalcDisplay::setText(const QString &string)
//------------------------------------------------------------------------------
// Name: setFont
// Desc: Set the font and recalculate the font size to better fit
// Desc: Set the base font and recalculate the font size to better fit
//------------------------------------------------------------------------------
void KCalcDisplay::setFont(const QFont &font)
{
// Make a copy of the font
QFont* newFont = new 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 KCalcDisplay::updateFont()
{
// Make a working copy of the font
QFont* newFont = new QFont(baseFont());
// Calculate ideal font size
// constant arbitrarily chosen, adjust/increase if scaling issues arise
newFont->setPointSizeF(qMax(double(font.pointSize()), contentsRect().height() / 3.6));
newFont->setPointSizeF(qMax(double(baseFont().pointSize()), contentsRect().height() / 3.6));
// Apply font
QFrame::setFont(*newFont);
......@@ -618,6 +635,15 @@ void KCalcDisplay::setFont(const QFont &font)
delete newFont;
}
//------------------------------------------------------------------------------
// Name: baseFont
// Desc:
//------------------------------------------------------------------------------
const QFont& KCalcDisplay::baseFont() const
{
return *baseFont_;
}
//------------------------------------------------------------------------------
// Name: formatDecimalNumber
// Desc: Convert decimal number to locale-dependent format.
......@@ -1065,8 +1091,8 @@ void KCalcDisplay::resizeEvent(QResizeEvent* event)
{
QFrame::resizeEvent(event);
// Set font again (forcing size recalculation)
setFont(KCalcSettings::displayFont());
// Update font size
updateFont();
}
//------------------------------------------------------------------------------
......@@ -1075,11 +1101,14 @@ void KCalcDisplay::resizeEvent(QResizeEvent* event)
//------------------------------------------------------------------------------
QSize KCalcDisplay::sizeHint() const
{
// font metrics of base font
const QFontMetrics fmBase(baseFont());
// basic size
QSize sz = fontMetrics().size(Qt::TextSingleLine, text_);
QSize sz = fmBase.size(Qt::TextSingleLine, text_);
// expanded by 3/4 font height to make room for the status texts
QFont fnt(font());
QFont fnt(baseFont());
fnt.setPointSize(qMax(((fnt.pointSize() * 3) / 4), 7));
const QFontMetrics fm(fnt);
......
......@@ -66,6 +66,8 @@ public:
void setPrecision(int precision);
void setText(const QString &string);
void setFont(const QFont &font);
void updateFont();
const QFont &baseFont() const;
QString formatDecimalNumber(QString string);
QString groupDigits(const QString &displayString, int numDigits);
QString text() const;
......@@ -119,6 +121,8 @@ private:
int fixed_precision_; // "-1" = no fixed_precision
KNumber display_amount_;
QFont* baseFont_;
QVector<KNumber> history_list_;
int history_index_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment