From kde-commits Tue Sep 30 22:05:35 2014 From: Christoph Feck Date: Tue, 30 Sep 2014 22:05:35 +0000 To: kde-commits Subject: [kcalc/frameworks] /: KF5 port: KStatusBar -> KCalcStatusBar Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=141211474612585 Git commit 9878605355966cec70ec98e20de1fe3ac2423ffd by Christoph Feck. Committed on 24/09/2014 at 18:50. Pushed by cfeck into branch 'frameworks'. KF5 port: KStatusBar -> KCalcStatusBar REVIEW: 120356 M +1 -0 CMakeLists.txt M +15 -44 kcalc.cpp M +2 -3 kcalc.h A +121 -0 kcalc_statusbar.cpp [License: GPL (v2+)] A +60 -0 kcalc_statusbar.h [License: GPL (v2+)] http://commits.kde.org/kcalc/9878605355966cec70ec98e20de1fe3ac2423ffd diff --git a/CMakeLists.txt b/CMakeLists.txt index 471b144..719a815 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,7 @@ set(kcalc_KDEINIT_SRCS ${libknumber_la_SRCS} kcalc_const_menu.cpp = kcalc_core.cpp = kcalcdisplay.cpp = + kcalc_statusbar.cpp stats.cpp ) = ki18n_wrap_ui(kcalc_KDEINIT_SRCS diff --git a/kcalc.cpp b/kcalc.cpp index 85b6bc6..873d899 100644 --- a/kcalc.cpp +++ b/kcalc.cpp @@ -49,7 +49,6 @@ along with this program. If not, see . #include #include #include -#include #include #include #include @@ -57,6 +56,7 @@ along with this program. If not, see . #include "kcalc_bitset.h" #include "kcalc_const_menu.h" #include "kcalc_settings.h" +#include "kcalc_statusbar.h" #include "kcalcdisplay.h" #include "version.h" = @@ -92,7 +92,7 @@ KCalculator::KCalculator(QWidget *parent) : // setup interface (order is critical) setupUi(central); setupMainActions(); - setupStatusbar(); + setStatusBar(new KCalcStatusBar(this)); createGUI(); setupKeys(); = @@ -242,31 +242,11 @@ KCalcConstMenu *KCalculator::createConstantsMenu() { = //------------------------------------------------------------------------= ------ // Name: statusBar -// Desc: temporary KF5 porting helper +// Desc: returns a pointer to the status bar //------------------------------------------------------------------------= ------ -KStatusBar *KCalculator::statusBar() { - return static_cast(KXmlGuiWindow::statusBar()); -} - -//------------------------------------------------------------------------= ------ -// Name: setupStatusbar -// Desc: sets up the status bar with default text -//------------------------------------------------------------------------= ------ -void KCalculator::setupStatusbar() { - setStatusBar(new KStatusBar(this)); - - // Status bar contents - statusBar()->insertPermanentFixedItem(QLatin1String(" NORM "), ShiftField= ); - statusBar()->setItemAlignment(ShiftField, Qt::AlignCenter); - - statusBar()->insertPermanentFixedItem(QLatin1String(" HEX "), BaseField); - statusBar()->setItemAlignment(BaseField, Qt::AlignCenter); - - statusBar()->insertPermanentFixedItem(QLatin1String(" DEG "), AngleField); - statusBar()->setItemAlignment(AngleField, Qt::AlignCenter); +KCalcStatusBar *KCalculator::statusBar() { = - statusBar()->insertPermanentFixedItem(QLatin1String(" \xa0\xa0 "), MemFie= ld); // nbsp - statusBar()->setItemAlignment(MemField, Qt::AlignCenter); + return static_cast(KXmlGuiWindow::statusBar()); } = //------------------------------------------------------------------------= ------ @@ -766,29 +746,25 @@ void KCalculator::slotBaseSelected(int base) { int current_base; = // set display & statusbar (if item exist in statusbar) + statusBar()->setBase(base); switch (base) { case BinMode: current_base =3D calc_display->setBase(NumBase(2)); - statusBar()->changeItem(QLatin1String("BIN"), BaseField); calc_display->setStatusText(BaseField, QLatin1String("Bin")); break; case OctMode: current_base =3D calc_display->setBase(NumBase(8)); - statusBar()->changeItem(QLatin1String("OCT"), BaseField); calc_display->setStatusText(BaseField, QLatin1String("Oct")); break; case DecMode: current_base =3D calc_display->setBase(NumBase(10)); - statusBar()->changeItem(QLatin1String("DEC"), BaseField); calc_display->setStatusText(BaseField, QLatin1String("Dec")); break; case HexMode: current_base =3D calc_display->setBase(NumBase(16)); - statusBar()->changeItem(QLatin1String("HEX"), BaseField); calc_display->setStatusText(BaseField, QLatin1String("Hex")); break; default: - statusBar()->changeItem(QLatin1String("Error"), BaseField); calc_display->setStatusText(BaseField, QLatin1String("Error")); return; } @@ -872,17 +848,15 @@ void KCalculator::slotAngleSelected(int mode) { = angle_mode_ =3D mode; = + statusBar()->setAngleMode(KCalcStatusBar::AngleMode(mode)); switch (mode) { case DegMode: - statusBar()->changeItem(QLatin1String("DEG"), AngleField); calc_display->setStatusText(AngleField, QLatin1String("Deg")); break; case RadMode: - statusBar()->changeItem(QLatin1String("RAD"), AngleField); calc_display->setStatusText(AngleField, QLatin1String("Rad")); break; case GradMode: - statusBar()->changeItem(QLatin1String("GRA"), AngleField); calc_display->setStatusText(AngleField, QLatin1String("Gra")); break; default: // we shouldn't ever end up here @@ -910,11 +884,10 @@ void KCalculator::slotShifttoggled(bool flag) { = emit switchMode(ModeShift, flag); = + statusBar()->setShiftIndicator(shift_mode_); if (shift_mode_) { - statusBar()->changeItem(i18nc("Second button functions are active", "SHI= FT"), ShiftField); calc_display->setStatusText(ShiftField, i18n("Shift")); } else { - statusBar()->changeItem(i18nc("Normal button functions are active", "NOR= M"), ShiftField); calc_display->setStatusText(ShiftField, QString()); } } @@ -954,7 +927,7 @@ void KCalculator::slotMemStoreclicked() { = memory_num_ =3D calc_display->getAmount(); calc_display->setStatusText(MemField, QLatin1String("M")); - statusBar()->changeItem(QLatin1String("M"), MemField); + statusBar()->setMemoryIndicator(true); pbMemRecall->setEnabled(true); } = @@ -1042,7 +1015,7 @@ void KCalculator::slotMemPlusMinusclicked() { } = pbShift->setChecked(false); - statusBar()->changeItem(i18n("M"), MemField); + statusBar()->setMemoryIndicator(true); calc_display->setStatusText(MemField, i18n("M")); pbMemRecall->setEnabled(true); } @@ -1264,7 +1237,7 @@ void KCalculator::slotPowerclicked() { void KCalculator::slotMemClearclicked() { = memory_num_ =3D KNumber::Zero; - statusBar()->changeItem(QLatin1String(" \xa0\xa0 "), MemField); // nbsp + statusBar()->setMemoryIndicator(false); calc_display->setStatusText(MemField, QString()); pbMemRecall->setDisabled(true); } @@ -1929,7 +1902,7 @@ void KCalculator::showScienceButtons(bool toggled) { } = setAngle(); - statusBar()->setItemFixed(AngleField, -1); + statusBar()->setAngleModeIndicatorVisible(true); } else { foreach(QAbstractButton* btn, scientific_buttons_) { btn->hide(); @@ -1939,8 +1912,7 @@ void KCalculator::showScienceButtons(bool toggled) { btn->hide(); } = - statusBar()->changeItem(QString(), AngleField); - statusBar()->setItemFixed(AngleField, 0); + statusBar()->setAngleModeIndicatorVisible(false); calc_display->setStatusText(AngleField, QString()); } } @@ -1961,7 +1933,7 @@ void KCalculator::showLogicButtons(bool toggled) { } = setBase(); - statusBar()->setItemFixed(BaseField, -1); + statusBar()->setBaseIndicatorVisible(true); = foreach(QAbstractButton *btn, base_choose_group_->buttons()) { btn->show(); @@ -1986,8 +1958,7 @@ void KCalculator::showLogicButtons(bool toggled) { btn->hide(); } = - statusBar()->changeItem(QString(), BaseField); - statusBar()->setItemFixed(BaseField, 0); + statusBar()->setBaseIndicatorVisible(false); calc_display->setStatusText(BaseField, QString()); for (int i =3D 10; i < 16; ++i) { (num_button_group_->button(i))->hide(); diff --git a/kcalc.h b/kcalc.h index 0f04fa8..1e6dc28 100644 --- a/kcalc.h +++ b/kcalc.h @@ -26,7 +26,7 @@ class Constants; class QButtonGroup; class KToggleAction; class KCalcConstMenu; -class KStatusBar; +class KCalcStatusBar; = /* Kcalc basically consist of a class for the GUI (here), a class for @@ -118,7 +118,6 @@ private: virtual bool eventFilter(QObject *o, QEvent *e); void updateGeometry(); void setupMainActions(); - void setupStatusbar(); void setupKeys(); void setupNumberKeys(); void setupRightKeypad(); @@ -135,7 +134,7 @@ private: void setBase(); = void updateDisplay(UpdateFlags flags); - KStatusBar *statusBar(); + KCalcStatusBar *statusBar(); = // button sets void showMemButtons(bool toggled); diff --git a/kcalc_statusbar.cpp b/kcalc_statusbar.cpp new file mode 100644 index 0000000..7bfea33 --- /dev/null +++ b/kcalc_statusbar.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2014 Christoph Feck + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1= 301, USA. + */ + +#include "kcalc_statusbar.h" + +#include + +#include + +#include "kcalc_statusbar.moc" + +KCalcStatusBar::KCalcStatusBar(QWidget *parent) + : QStatusBar(parent) +{ + setSizeGripEnabled(false); + + shift_indicator_ =3D addIndicator(QList() << i18nc("Normal bu= tton functions are active", "NORM") << i18nc("Second button functions are a= ctive", "SHIFT")); + base_indicator_ =3D addIndicator(QList() << "DEC" << "BIN" <<= "OCT" << "HEX"); + angle_mode_indicator_ =3D addIndicator(QList() << "DEG" << "R= AD" << "GRA"); + memory_indicator_ =3D addIndicator(QList() << QString() << i1= 8nc("Memory indicator in status bar", "M")); +} + +KCalcStatusBar::~KCalcStatusBar() +{ +} + +QLabel *KCalcStatusBar::addIndicator(QList indicatorTexts) +{ + QLabel *l =3D new QLabel(indicatorTexts.at(0), this); + + // find widest indicator text + QFontMetrics fm(l->font()); + int maxWidth =3D 0; + foreach (const QString &text, indicatorTexts) { + maxWidth =3D qMax(maxWidth, fm.boundingRect(text).width()); + } + // add some padding + maxWidth +=3D fm.height(); + + l->setFixedSize(maxWidth, fm.height()); + l->setAlignment(Qt::AlignCenter); + + addPermanentWidget(l); + return l; +} + +void KCalcStatusBar::setBaseIndicatorVisible(bool visible) +{ + base_indicator_->setVisible(visible); +} + +void KCalcStatusBar::setAngleModeIndicatorVisible(bool visible) +{ + angle_mode_indicator_->setVisible(visible); +} + +void KCalcStatusBar::setShiftIndicator(bool shift) +{ + shift_indicator_->setText(shift ? i18nc("Second button functions are a= ctive", "SHIFT") : i18nc("Normal button functions are active", "NORM")); +} + +void KCalcStatusBar::setBase(int base) +{ + QString text; + + switch (base) { + case 2: + text =3D "BIN"; + break; + case 8: + text =3D "OCT"; + break; + case 10: + default: + text =3D "DEC"; + break; + case 16: + text =3D "HEX"; + break; + } + base_indicator_->setText(text); +} + +void KCalcStatusBar::setAngleMode(AngleMode mode) +{ + QString text; + + switch (mode) { + case DegMode: + text =3D "DEG"; + break; + case RadMode: + text =3D "RAD"; + break; + case GradMode: + text =3D "GRA"; + break; + } + angle_mode_indicator_->setText(text); +} + +void KCalcStatusBar::setMemoryIndicator(bool memory) +{ + memory_indicator_->setText(memory ? i18nc("Memory indicator in status = bar", "M") : QString()); +} + diff --git a/kcalc_statusbar.h b/kcalc_statusbar.h new file mode 100644 index 0000000..63c3425 --- /dev/null +++ b/kcalc_statusbar.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2014 Christoph Feck + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1= 301, USA. + */ + +#ifndef KCALC_STATUSBAR_H_ +#define KCALC_STATUSBAR_H_ + +#include + +class QLabel; + +class KCalcStatusBar : public QStatusBar +{ + Q_OBJECT + +public: + KCalcStatusBar(QWidget *parent =3D 0); + ~KCalcStatusBar(); + +public: + enum AngleMode { + DegMode, + RadMode, + GradMode + }; + +public: + void setBaseIndicatorVisible(bool visible); + void setAngleModeIndicatorVisible(bool visible); + + void setShiftIndicator(bool shift); + void setBase(int base); + void setAngleMode(AngleMode mode); + void setMemoryIndicator(bool memory); + +private: + QLabel *addIndicator(QList indicatorTexts); + +private: + QLabel *shift_indicator_; + QLabel *base_indicator_; + QLabel *angle_mode_indicator_; + QLabel *memory_indicator_; +}; + +#endif