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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
|
/*
SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com>
SPDX-FileCopyrightText: 1996-2000 Bernd Johannes Wuebben <wuebben@kde.org>
SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kcalc_display.h"
#include <QApplication>
#include <QClipboard>
#include <QMouseEvent>
#include <QPainter>
#include <QStyle>
#include <QStyleOption>
#include <QTimer>
#include <KColorScheme>
#include <KNotification>
#include "kcalc_core.h"
#include "kcalc_settings.h"
#define ERROR_TEXT_FONTPOINT_REDUCTION 6
const QString KCalcDisplay::m_MathErrorText = QStringLiteral("Math error");
const QString KCalcDisplay::m_SyntaxErrorText = QStringLiteral("Syntax error");
const QString KCalcDisplay::m_MalformedExpressionText = QStringLiteral("Malformed expression");
KCalcDisplay::KCalcDisplay(QWidget *parent)
: QFrame(parent)
, m_usingTempSettings(false)
, beep_(false)
, groupdigits_(true)
, twoscomplement_(true)
, button_(0)
, lit_(false)
, num_base_(NB_DECIMAL)
, precision_(9)
, fixed_precision_(-1)
, display_amount_(0)
, selection_timer_(new QTimer(this))
{
setAccessibleDescription(i18nc("@label accessibility description of the calculation result display", "Result Display"));
setFocusPolicy(Qt::StrongFocus);
setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
setBackgroundRole(QPalette::Base);
setForegroundRole(QPalette::Text);
setFrameStyle(QFrame::NoFrame); // set in kalc.ui
setContentsMargins(6, 0, 6, 6);
KNumber::setDefaultFloatOutput(true);
KNumber::setDefaultFractionalInput(true);
connect(this, &KCalcDisplay::clicked, this, &KCalcDisplay::slotDisplaySelected);
connect(selection_timer_, &QTimer::timeout, this, &KCalcDisplay::slotSelectionTimedOut);
connect(this, &KCalcDisplay::changedText, this, &KCalcDisplay::restoreSettings);
connect(this, &KCalcDisplay::changedAmount, this, &KCalcDisplay::restoreSettings);
sendEvent(EventReset);
}
KCalcDisplay::~KCalcDisplay() = default;
void KCalcDisplay::changeSettings()
{
basePalette_ = palette();
baseFont_ = KCalcSettings::displayFont();
basePalette_.setColor(QPalette::Text, KCalcSettings::foreColor());
basePalette_.setColor(QPalette::Base, KCalcSettings::backColor());
setPrecision(KCalcSettings::precision());
if (!KCalcSettings::fixed()) {
setFixedPrecision(-1);
} else {
setFixedPrecision(KCalcSettings::fixedPrecision());
}
setBeep(KCalcSettings::beep());
setGroupDigits(KCalcSettings::groupDigits());
setTwosComplement(KCalcSettings::twosComplement());
setBinaryGrouping(KCalcSettings::binaryGrouping());
setOctalGrouping(KCalcSettings::octalGrouping());
setHexadecimalGrouping(KCalcSettings::hexadecimalGrouping());
updateFont();
if (!m_usingTempSettings) {
setPalette(basePalette_);
updateDisplay();
}
}
void KCalcDisplay::updateFromCore(const CalcEngine &core)
{
bool tmp_error;
const KNumber &output = core.lastOutput(tmp_error);
setAmount(output);
}
bool KCalcDisplay::sendEvent(Event event)
{
switch (event) {
case EventClear:
case EventReset:
display_amount_ = KNumber::Zero;
text_.clear();
updateDisplay();
return true;
case EventError:
updateDisplay();
return true;
default:
return false;
}
}
void KCalcDisplay::setTempSettings()
{
if (m_usingTempSettings) {
return;
}
QFont newFont = font();
newFont.setWeight(QFont::Weight::ExtraLight);
newFont.setPointSize(newFont.pointSize() - ERROR_TEXT_FONTPOINT_REDUCTION);
QFrame::setFont(newFont);
QPalette tmpPalette = parentWidget()->palette();
KColorScheme colorScheme(QPalette::Active, KColorScheme::View);
colorScheme.adjustForeground(tmpPalette, KColorScheme::NegativeText);
setPalette(tmpPalette);
m_usingTempSettings = true;
}
void KCalcDisplay::showErrorMessage(ErrorMessage errorMessage)
{
setTempSettings();
switch (errorMessage) {
case MathError:
setUnformattedText(m_MathErrorText);
break;
case SyntaxError:
setUnformattedText(m_SyntaxErrorText);
break;
case MalformedExpression:
setUnformattedText(m_MalformedExpressionText);
break;
default:
Q_UNREACHABLE();
}
if (beep_) {
KNotification::beep();
}
}
void KCalcDisplay::slotCut()
{
slotCopy();
sendEvent(EventReset);
}
void KCalcDisplay::slotCopy()
{
QString txt = text_;
switch (num_base_) {
case NB_HEX:
txt.prepend(QLatin1String("0x"));
txt.remove(QLatin1Char(' '));
break;
case NB_BINARY:
txt.prepend(QLatin1String("0b"));
txt.remove(QLatin1Char(' '));
break;
case NB_OCTAL:
txt.prepend(QLatin1String("0o"));
txt.remove(QLatin1Char(' '));
break;
case NB_DECIMAL:
txt.remove(QLocale().groupSeparator());
break;
}
QApplication::clipboard()->setText(txt, QClipboard::Clipboard);
QApplication::clipboard()->setText(txt, QClipboard::Selection);
}
void KCalcDisplay::slotDisplaySelected()
{
if (button_ == Qt::LeftButton) {
if (lit_) {
slotCopy();
selection_timer_->start(100);
} else {
selection_timer_->stop();
}
invertColors();
}
}
void KCalcDisplay::slotSelectionTimedOut()
{
lit_ = false;
invertColors();
selection_timer_->stop();
}
void KCalcDisplay::invertColors()
{
QPalette tmp_palette = palette();
tmp_palette.setColor(QPalette::Base, palette().color(QPalette::Text));
tmp_palette.setColor(QPalette::Text, palette().color(QPalette::Base));
setPalette(tmp_palette);
}
void KCalcDisplay::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
lit_ = !lit_;
button_ = Qt::LeftButton;
} else {
button_ = Qt::MiddleButton;
}
Q_EMIT clicked();
}
void KCalcDisplay::setPrecision(int precision)
{
precision_ = precision;
}
void KCalcDisplay::setFixedPrecision(int precision)
{
if (fixed_precision_ > precision_) {
fixed_precision_ = -1;
} else {
fixed_precision_ = precision;
}
}
void KCalcDisplay::setBeep(bool flag)
{
beep_ = flag;
}
void KCalcDisplay::setGroupDigits(bool flag)
{
groupdigits_ = flag;
}
void KCalcDisplay::setTwosComplement(bool flag)
{
twoscomplement_ = flag;
}
void KCalcDisplay::setBinaryGrouping(int digits)
{
binaryGrouping_ = digits;
}
void KCalcDisplay::setOctalGrouping(int digits)
{
octalGrouping_ = digits;
}
void KCalcDisplay::setHexadecimalGrouping(int digits)
{
hexadecimalGrouping_ = digits;
}
const KNumber &KCalcDisplay::getAmount() const
{
return display_amount_;
}
QString KCalcDisplay::getAmountQString(bool addPreffix /*= true*/) const
{
QString amountQString = text_;
switch (num_base_) {
case NB_HEX:
if (addPreffix) {
amountQString.prepend(QLatin1String("0x"));
}
amountQString.remove(QLatin1Char(' '));
break;
case NB_BINARY:
if (addPreffix) {
amountQString.prepend(QLatin1String("0b"));
}
amountQString.remove(QLatin1Char(' '));
break;
case NB_OCTAL:
if (addPreffix) {
amountQString.prepend(QLatin1String("0o"));
}
amountQString.remove(QLatin1Char(' '));
break;
case NB_DECIMAL:
amountQString.remove(QLocale().groupSeparator());
break;
}
return amountQString;
}
bool KCalcDisplay::setAmount(const KNumber &new_amount)
{
QString display_str;
if ((num_base_ != NB_DECIMAL) && (new_amount.type() != KNumber::TypeError)) {
display_amount_ = new_amount.integerPart();
if (twoscomplement_) {
// treat number as 64-bit unsigned
const quint64 tmp_workaround = display_amount_.toUint64();
display_str = QString::number(tmp_workaround, num_base_).toUpper();
} else {
// QString::number treats non-decimal as unsigned
qint64 tmp_workaround = display_amount_.toInt64();
const bool neg = tmp_workaround < 0;
if (neg) {
tmp_workaround = qAbs(tmp_workaround);
}
display_str = QString::number(tmp_workaround, num_base_).toUpper();
if (neg) {
display_str.prepend(QLocale().negativeSign());
}
}
} else {
// num_base_ == NB_DECIMAL || new_amount.type() == KNumber::TypeError
display_amount_ = new_amount;
display_str = display_amount_.toQString(KCalcSettings::precision(), fixed_precision_);
}
// TODO: to avoid code duplication, don't format complex number for now,
// we need to mode this functionality to the KNumber library
if (display_amount_.type() != KNumber::TypeComplex) {
display_str = formatNumber(display_str);
}
setText(display_str);
Q_EMIT changedAmount(display_amount_);
return true;
}
void KCalcDisplay::setUnformattedText(const QString &string)
{
text_ = string;
update();
setAccessibleName(text_);
}
void KCalcDisplay::setText(const QString &string)
{
// note that "C" locale is being used internally
text_ = string;
// text_ = formatNumber(text_);
update();
setAccessibleName(text_); // "Labels should be represented by only QAccessibleInterface and return their text as name"
Q_EMIT changedText(text_);
}
void KCalcDisplay::setFont(const QFont &font)
{
// Overwrite current baseFont
baseFont_ = font;
updateFont();
}
void KCalcDisplay::updateFont()
{
// Make a working copy of the font
QFont newFont(baseFont());
// Calculate ideal font size
// constants arbitrarily chosen, adjust/increase if scaling issues arise
newFont.setPointSizeF(qMax(double(baseFont().pointSize()), qMin(contentsRect().height() / 4.5, contentsRect().width() / 24.6)));
QFrame::setFont(newFont);
if (m_usingTempSettings) {
setTempSettings();
}
}
void KCalcDisplay::restoreSettings()
{
if (m_usingTempSettings) {
m_usingTempSettings = false;
updateFont();
setPalette(basePalette_);
}
}
const QFont &KCalcDisplay::baseFont() const
{
return baseFont_;
}
QString KCalcDisplay::formatNumber(QString string)
{
QString formattedNumber = string;
const bool special = (string.contains(QLatin1String("nan")) || string.contains(QLatin1String("inf")));
// The decimal mode needs special treatment for two reasons, because: a) it uses KGlobal::locale() to get a localized
// format and b) it has possible numbers after the decimal place. Neither applies to Binary, Hexadecimal or Octal.
if ((groupdigits_ || num_base_ == NB_DECIMAL) && !special) {
switch (num_base_) {
case NB_DECIMAL:
formattedNumber = formatDecimalNumber(string);
break;
case NB_BINARY:
formattedNumber = groupDigits(string, binaryGrouping_);
break;
case NB_OCTAL:
formattedNumber = groupDigits(string, octalGrouping_);
break;
case NB_HEX:
formattedNumber = groupDigits(string, hexadecimalGrouping_);
break;
}
}
return formattedNumber;
}
QString KCalcDisplay::formatDecimalNumber(QString string)
{
QLocale locale;
string.replace(QLatin1Char('.'), locale.decimalPoint());
if (groupdigits_ && !(locale.numberOptions() & QLocale::OmitGroupSeparator)) {
// find position after last digit
int pos = string.indexOf(locale.decimalPoint());
if (pos < 0) {
// do not group digits after the exponent part
const int expPos = string.indexOf(QLatin1Char('e'));
if (expPos > 0) {
pos = expPos;
} else {
pos = string.length();
}
}
// find first digit to not group leading spaces or signs
int firstDigitPos = 0;
for (int i = 0, total = string.length(); i < total; ++i) {
if (string.at(i).isDigit()) {
firstDigitPos = i;
break;
}
}
const auto groupSeparator = locale.groupSeparator();
const int groupSize = 3;
string.reserve(string.length() + (pos - 1) / groupSize);
while ((pos -= groupSize) > firstDigitPos) {
string.insert(pos, groupSeparator);
}
}
string.replace(QLatin1Char('-'), locale.negativeSign());
string.replace(QLatin1Char('+'), locale.positiveSign());
// Digits in unicode is encoded in contiguous range and with the digit zero as the first.
// To convert digits to other locales,
// just add the digit value and the leading zero's code point.
// ref: Unicode15 chapter 4.6 Numeric Value https://www.unicode.org/versions/Unicode15.0.0/ch04.pdf
// QLocale switched return type of many functions from QChar to QString,
// because UTF-16 may need surrogate pairs to represent these fields.
// We only need digits, thus we only need the first QChar with Qt>=6.
auto zero = locale.zeroDigit().at(0).unicode();
for (auto &i : string) {
if (i.isDigit()) {
i = QChar(zero + i.digitValue());
}
}
return string;
}
QString KCalcDisplay::groupDigits(const QString &displayString, int numDigits)
{
QString tmpDisplayString;
const int stringLength = displayString.length();
for (int i = stringLength; i > 0; i--) {
if (i % numDigits == 0 && i != stringLength) {
tmpDisplayString = tmpDisplayString + QLatin1Char(' ');
}
tmpDisplayString = tmpDisplayString + displayString[stringLength - i];
}
return tmpDisplayString;
}
QString KCalcDisplay::text() const
{
return text_;
}
int KCalcDisplay::setBase(NumBase base)
{
switch (base) {
case NB_HEX:
num_base_ = NB_HEX;
break;
case NB_DECIMAL:
num_base_ = NB_DECIMAL;
break;
case NB_OCTAL:
num_base_ = NB_OCTAL;
break;
case NB_BINARY:
num_base_ = NB_BINARY;
break;
default:
Q_ASSERT(0);
}
setAmount(display_amount_);
return num_base_;
}
void KCalcDisplay::setStatusText(int i, const QString &text)
{
if (i < NUM_STATUS_TEXT) {
str_status_[i] = text;
}
update();
}
void KCalcDisplay::updateDisplay()
{
QString txt = text_;
txt.remove(QLatin1Char(' '));
if (num_base_ == NB_DECIMAL) {
txt.remove(QLocale().groupSeparator());
}
setText(formatNumber(txt));
}
void KCalcDisplay::initStyleOption(QStyleOptionFrame *option) const
{
if (!option) {
return;
}
option->initFrom(this);
option->state &= ~QStyle::State_HasFocus; // don't draw focus highlight
if (frameShadow() == QFrame::Sunken) {
option->state |= QStyle::State_Sunken;
} else if (frameShadow() == QFrame::Raised) {
option->state |= QStyle::State_Raised;
}
option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, this);
option->midLineWidth = 0;
}
void KCalcDisplay::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QStyleOptionFrame option;
initStyleOption(&option);
style()->drawPrimitive(QStyle::PE_PanelLineEdit, &option, &painter, this);
// draw display text
const int margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, nullptr);
QRect cr = contentsRect();
cr.adjust(margin * 2, 0, -margin * 2, 0); // provide a margin
const int align = QStyle::visualAlignment(layoutDirection(), Qt::AlignRight | Qt::AlignVCenter);
painter.drawText(cr, align | Qt::TextSingleLine, text_);
// draw the status texts using half of the normal
// font size but not smaller than 7pt
QFont fnt(font());
fnt.setPointSizeF(qMax((fnt.pointSize() / 2.0), 7.0));
painter.setFont(fnt);
QFontMetrics fm(fnt);
const uint w = fm.boundingRect(QStringLiteral("________")).width();
const uint h = fm.height();
for (int n = 0; n < NUM_STATUS_TEXT; ++n) {
painter.drawText(5 + n * w, h, str_status_[n]);
}
}
void KCalcDisplay::resizeEvent(QResizeEvent *event)
{
QFrame::resizeEvent(event);
// Update font size
updateFont();
}
QSize KCalcDisplay::sizeHint() const
{
// font metrics of base font
const QFontMetrics fmBase(baseFont());
// basic size
QSize sz = fmBase.size(0, QStringLiteral("M"));
// expanded by 3/4 font height to make room for the status texts
QFont fnt(baseFont());
fnt.setPointSize(qMax(((fnt.pointSize() * 3) / 4), 7));
const QFontMetrics fm(fnt);
sz.setHeight(sz.height() + fm.height());
QStyleOptionFrame option;
initStyleOption(&option);
return (style()->sizeFromContents(QStyle::CT_LineEdit, &option, sz, this));
}
#include "moc_kcalc_display.cpp"
|