1 | /* |
2 | SPDX-FileCopyrightText: 2001-2013 Evan Teran <evan.teran@gmail.com> |
3 | SPDX-FileCopyrightText: 2003-2005 Klaus Niederkrueger <kniederk@math.uni-koeln.de> |
4 | |
5 | SPDX-License-Identifier: GPL-2.0-or-later |
6 | */ |
7 | |
8 | #include "kcalc_const_button.h" |
9 | #include "kcalc_const_menu.h" |
10 | #include "kcalc_settings.h" |
11 | |
12 | #include <QInputDialog> |
13 | |
14 | #include <KLocalizedString> |
15 | |
16 | //------------------------------------------------------------------------------ |
17 | // Name: KCalcConstButton |
18 | // Desc: constructor |
19 | //------------------------------------------------------------------------------ |
20 | KCalcConstButton::KCalcConstButton(QWidget *parent) |
21 | : KCalcButton(parent) |
22 | { |
23 | setTextColor(KCalcSettings::constantsFontsColor()); |
24 | addMode(ModeShift, i18nc("Write display data into memory" , "Store" ), i18n("Write display data into memory" )); |
25 | initPopupMenu(); |
26 | connect(this, &QAbstractButton::clicked, this, &KCalcConstButton::slotClicked); |
27 | } |
28 | |
29 | //------------------------------------------------------------------------------ |
30 | // Name: KCalcConstButton |
31 | // Desc: constructor |
32 | //------------------------------------------------------------------------------ |
33 | KCalcConstButton::KCalcConstButton(const QString &label, QWidget *parent, const QString &tooltip) |
34 | : KCalcButton(label, parent, tooltip) |
35 | { |
36 | setTextColor(KCalcSettings::constantsFontsColor()); |
37 | addMode(ModeShift, i18nc("Write display data into memory" , "Store" ), i18n("Write display data into memory" )); |
38 | initPopupMenu(); |
39 | } |
40 | |
41 | //------------------------------------------------------------------------------ |
42 | // Name: constant |
43 | // Desc: get the value of the const as a QString |
44 | //------------------------------------------------------------------------------ |
45 | QString KCalcConstButton::constant() const |
46 | { |
47 | return KCalcSettings::valueConstant(button_num_); |
48 | } |
49 | |
50 | //------------------------------------------------------------------------------ |
51 | // Name: setButtonNumber |
52 | // Desc: remembers the "index" of the const button |
53 | //------------------------------------------------------------------------------ |
54 | void KCalcConstButton::setButtonNumber(int num) |
55 | { |
56 | button_num_ = num; |
57 | } |
58 | |
59 | //------------------------------------------------------------------------------ |
60 | // Name: setLabelAndTooltip |
61 | // Desc: sets both the label and the tooltip for the const button |
62 | //------------------------------------------------------------------------------ |
63 | void KCalcConstButton::setLabelAndTooltip() |
64 | { |
65 | QString new_label = QLatin1String("C" ) + QString::number(button_num_ + 1); |
66 | QString new_tooltip; |
67 | |
68 | new_label = (KCalcSettings::nameConstant(button_num_).isNull() ? new_label : KCalcSettings::nameConstant(button_num_)); |
69 | |
70 | new_tooltip = new_label + QLatin1Char('=') + KCalcSettings::valueConstant(button_num_); |
71 | |
72 | addMode(ModeNormal, new_label, new_tooltip); |
73 | } |
74 | |
75 | //------------------------------------------------------------------------------ |
76 | // Name: initPopupMenu |
77 | // Desc: initializes the const button popup |
78 | //------------------------------------------------------------------------------ |
79 | void KCalcConstButton::() |
80 | { |
81 | auto const a = new QAction(this); |
82 | a->setText(i18n("Set Name" )); |
83 | connect(a, &QAction::triggered, this, &KCalcConstButton::slotConfigureButton); |
84 | addAction(a); |
85 | |
86 | auto const = new KCalcConstMenu(this); |
87 | tmp_menu->menuAction()->setText(i18n("Choose From List" )); |
88 | addAction(tmp_menu->menuAction()); |
89 | setContextMenuPolicy(Qt::ActionsContextMenu); |
90 | |
91 | connect(tmp_menu, &KCalcConstMenu::triggeredConstant, this, &KCalcConstButton::slotChooseScientificConst); |
92 | } |
93 | |
94 | //------------------------------------------------------------------------------ |
95 | // Name: slotConfigureButton |
96 | // Desc: lets the user set the name for a constant |
97 | //------------------------------------------------------------------------------ |
98 | void KCalcConstButton::slotConfigureButton() |
99 | { |
100 | bool yes_no; |
101 | const QString input = |
102 | QInputDialog::getText(this, i18n("New Name for Constant" ), i18n("New name:" ), QLineEdit::Normal, text(), &yes_no); // "nameUserConstants-Dialog" |
103 | if (yes_no && !input.isEmpty()) { |
104 | KCalcSettings::setNameConstant(button_num_, input); |
105 | setLabelAndTooltip(); |
106 | } |
107 | } |
108 | |
109 | //------------------------------------------------------------------------------ |
110 | // Name: slotChooseScientificConst |
111 | // Desc: set the button's scientific constant |
112 | //------------------------------------------------------------------------------ |
113 | void KCalcConstButton::slotChooseScientificConst(const science_constant &const_chosen) |
114 | { |
115 | KCalcSettings::setValueConstant(button_num_, const_chosen.value); |
116 | KCalcSettings::setNameConstant(button_num_, const_chosen.label); |
117 | setLabelAndTooltip(); |
118 | } |
119 | |
120 | //------------------------------------------------------------------------------ |
121 | // Name: slotClicked |
122 | // Desc: constant button was clicked |
123 | //------------------------------------------------------------------------------ |
124 | void KCalcConstButton::slotClicked() |
125 | { |
126 | Q_EMIT constButtonClicked(button_num_); |
127 | } |
128 | |
129 | #include "moc_kcalc_const_button.cpp" |
130 | |