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

0001 /*
0002     SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com>
0003     SPDX-FileCopyrightText: 2003-2005 Klaus Niederkrueger <kniederk@math.uni-koeln.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kcalc_const_button.h"
0009 #include "kcalc_const_menu.h"
0010 #include "kcalc_settings.h"
0011 
0012 #include <QInputDialog>
0013 
0014 #include <KLocalizedString>
0015 
0016 //------------------------------------------------------------------------------
0017 // Name: KCalcConstButton
0018 // Desc: constructor
0019 //------------------------------------------------------------------------------
0020 KCalcConstButton::KCalcConstButton(QWidget *parent)
0021     : KCalcButton(parent)
0022 {
0023     setTextColor(KCalcSettings::constantsFontsColor());
0024     addMode(ModeShift, i18nc("Write display data into memory", "Store"), i18n("Write display data into memory"));
0025     initPopupMenu();
0026     connect(this, &QAbstractButton::clicked, this, &KCalcConstButton::slotClicked);
0027 }
0028 
0029 //------------------------------------------------------------------------------
0030 // Name: KCalcConstButton
0031 // Desc: constructor
0032 //------------------------------------------------------------------------------
0033 KCalcConstButton::KCalcConstButton(const QString &label, QWidget *parent, const QString &tooltip)
0034     : KCalcButton(label, parent, tooltip)
0035 {
0036     setTextColor(KCalcSettings::constantsFontsColor());
0037     addMode(ModeShift, i18nc("Write display data into memory", "Store"), i18n("Write display data into memory"));
0038     initPopupMenu();
0039 }
0040 
0041 //------------------------------------------------------------------------------
0042 // Name: constant
0043 // Desc: get the value of the const as a QString
0044 //------------------------------------------------------------------------------
0045 QString KCalcConstButton::constant() const
0046 {
0047     return KCalcSettings::valueConstant(button_num_);
0048 }
0049 
0050 //------------------------------------------------------------------------------
0051 // Name: setButtonNumber
0052 // Desc: remembers the "index" of the const button
0053 //------------------------------------------------------------------------------
0054 void KCalcConstButton::setButtonNumber(int num)
0055 {
0056     button_num_ = num;
0057 }
0058 
0059 //------------------------------------------------------------------------------
0060 // Name: setLabelAndTooltip
0061 // Desc: sets both the label and the tooltip for the const button
0062 //------------------------------------------------------------------------------
0063 void KCalcConstButton::setLabelAndTooltip()
0064 {
0065     QString new_label = QLatin1String("C") + QString::number(button_num_ + 1);
0066     QString new_tooltip;
0067 
0068     new_label = (KCalcSettings::nameConstant(button_num_).isNull() ? new_label : KCalcSettings::nameConstant(button_num_));
0069 
0070     new_tooltip = new_label + QLatin1Char('=') + KCalcSettings::valueConstant(button_num_);
0071 
0072     addMode(ModeNormal, new_label, new_tooltip);
0073 }
0074 
0075 //------------------------------------------------------------------------------
0076 // Name: initPopupMenu
0077 // Desc: initializes the const button popup
0078 //------------------------------------------------------------------------------
0079 void KCalcConstButton::initPopupMenu()
0080 {
0081     auto const a = new QAction(this);
0082     a->setText(i18n("Set Name"));
0083     connect(a, &QAction::triggered, this, &KCalcConstButton::slotConfigureButton);
0084     addAction(a);
0085 
0086     auto const tmp_menu = new KCalcConstMenu(this);
0087     tmp_menu->menuAction()->setText(i18n("Choose From List"));
0088     addAction(tmp_menu->menuAction());
0089     setContextMenuPolicy(Qt::ActionsContextMenu);
0090 
0091     connect(tmp_menu, &KCalcConstMenu::triggeredConstant, this, &KCalcConstButton::slotChooseScientificConst);
0092 }
0093 
0094 //------------------------------------------------------------------------------
0095 // Name: slotConfigureButton
0096 // Desc: lets the user set the name for a constant
0097 //------------------------------------------------------------------------------
0098 void KCalcConstButton::slotConfigureButton()
0099 {
0100     bool yes_no;
0101     const QString input =
0102         QInputDialog::getText(this, i18n("New Name for Constant"), i18n("New name:"), QLineEdit::Normal, text(), &yes_no); // "nameUserConstants-Dialog"
0103     if (yes_no && !input.isEmpty()) {
0104         KCalcSettings::setNameConstant(button_num_, input);
0105         setLabelAndTooltip();
0106     }
0107 }
0108 
0109 //------------------------------------------------------------------------------
0110 // Name: slotChooseScientificConst
0111 // Desc: set the buttons's scientific constant
0112 //------------------------------------------------------------------------------
0113 void KCalcConstButton::slotChooseScientificConst(const science_constant &const_chosen)
0114 {
0115     KCalcSettings::setValueConstant(button_num_, const_chosen.value);
0116     KCalcSettings::setNameConstant(button_num_, const_chosen.label);
0117     setLabelAndTooltip();
0118 }
0119 
0120 //------------------------------------------------------------------------------
0121 // Name: slotClicked
0122 // Desc: constant button was clicked
0123 //------------------------------------------------------------------------------
0124 void KCalcConstButton::slotClicked()
0125 {
0126     Q_EMIT constButtonClicked(button_num_);
0127 }