File indexing completed on 2021-12-21 14:36:20

0001 /*
0002     SPDX-FileCopyrightText: 2014 Christoph Feck <christoph@maxiom.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kcalc_statusbar.h"
0008 
0009 #include <QLabel>
0010 
0011 #include <KLocalizedString>
0012 
0013 KCalcStatusBar::KCalcStatusBar(QWidget *parent)
0014     : QStatusBar(parent)
0015     , shift_indicator_(
0016           addIndicator(QList<QString>() << i18nc("Normal button functions are active", "NORM") << i18nc("Second button functions are active", "SHIFT")))
0017     , base_indicator_(addIndicator(QList<QString>() << QStringLiteral("DEC") << QStringLiteral("BIN") << QStringLiteral("OCT") << QStringLiteral("HEX")))
0018     , angle_mode_indicator_(addIndicator(QList<QString>() << QStringLiteral("DEG") << QStringLiteral("RAD") << QStringLiteral("GRA")))
0019     , memory_indicator_(addIndicator(QList<QString>() << QString() << i18nc("Memory indicator in status bar", "M")))
0020 {
0021     setSizeGripEnabled(false);
0022 }
0023 
0024 QLabel *KCalcStatusBar::addIndicator(const QList<QString> &indicatorTexts)
0025 {
0026     auto l = new QLabel(indicatorTexts.at(0), this);
0027 
0028     // find widest indicator text
0029     QFontMetrics fm(l->font());
0030     int maxWidth = 0;
0031     for (const QString &text : indicatorTexts) {
0032         maxWidth = qMax(maxWidth, fm.boundingRect(text).width());
0033     }
0034     // add some padding
0035     maxWidth += fm.height();
0036 
0037     l->setFixedSize(maxWidth, fm.height());
0038     l->setAlignment(Qt::AlignCenter);
0039 
0040     addPermanentWidget(l);
0041     return l;
0042 }
0043 
0044 void KCalcStatusBar::setBaseIndicatorVisible(bool visible)
0045 {
0046     base_indicator_->setVisible(visible);
0047 }
0048 
0049 void KCalcStatusBar::setAngleModeIndicatorVisible(bool visible)
0050 {
0051     angle_mode_indicator_->setVisible(visible);
0052 }
0053 
0054 void KCalcStatusBar::setShiftIndicator(bool shift)
0055 {
0056     shift_indicator_->setText(shift ? i18nc("Second button functions are active", "SHIFT") : i18nc("Normal button functions are active", "NORM"));
0057 }
0058 
0059 void KCalcStatusBar::setBase(int base)
0060 {
0061     QString text;
0062 
0063     switch (base) {
0064     case 2:
0065         text = QStringLiteral("BIN");
0066         break;
0067     case 8:
0068         text = QStringLiteral("OCT");
0069         break;
0070     case 10:
0071     default:
0072         text = QStringLiteral("DEC");
0073         break;
0074     case 16:
0075         text = QStringLiteral("HEX");
0076         break;
0077     }
0078     base_indicator_->setText(text);
0079 }
0080 
0081 void KCalcStatusBar::setAngleMode(AngleMode mode)
0082 {
0083     QString text;
0084 
0085     switch (mode) {
0086     case DegMode:
0087         text = QStringLiteral("DEG");
0088         break;
0089     case RadMode:
0090         text = QStringLiteral("RAD");
0091         break;
0092     case GradMode:
0093         text = QStringLiteral("GRA");
0094         break;
0095     }
0096     angle_mode_indicator_->setText(text);
0097 }
0098 
0099 void KCalcStatusBar::setMemoryIndicator(bool memory)
0100 {
0101     memory_indicator_->setText(memory ? i18nc("Memory indicator in status bar", "M") : QString());
0102 }