-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
kcalc_const_button.cpp
129 lines (110 loc) · 4.9 KB
/
kcalc_const_button.cpp
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
/*
SPDX-FileCopyrightText: 2001-2013 Evan Teran <[email protected]>
SPDX-FileCopyrightText: 2003-2005 Klaus Niederkrueger <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kcalc_const_button.h"
#include "kcalc_const_menu.h"
#include "kcalc_settings.h"
#include <QInputDialog>
#include <KLocalizedString>
//------------------------------------------------------------------------------
// Name: KCalcConstButton
// Desc: constructor
//------------------------------------------------------------------------------
KCalcConstButton::KCalcConstButton(QWidget *parent)
: KCalcButton(parent)
{
setTextColor(KCalcSettings::constantsFontsColor());
addMode(ModeShift, i18nc("Write display data into memory", "Store"), i18n("Write display data into memory"));
initPopupMenu();
connect(this, &QAbstractButton::clicked, this, &KCalcConstButton::slotClicked);
}
//------------------------------------------------------------------------------
// Name: KCalcConstButton
// Desc: constructor
//------------------------------------------------------------------------------
KCalcConstButton::KCalcConstButton(const QString &label, QWidget *parent, const QString &tooltip)
: KCalcButton(label, parent, tooltip)
{
setTextColor(KCalcSettings::constantsFontsColor());
addMode(ModeShift, i18nc("Write display data into memory", "Store"), i18n("Write display data into memory"));
initPopupMenu();
}
//------------------------------------------------------------------------------
// Name: constant
// Desc: get the value of the const as a QString
//------------------------------------------------------------------------------
QString KCalcConstButton::constant() const
{
return KCalcSettings::valueConstant(button_num_);
}
//------------------------------------------------------------------------------
// Name: setButtonNumber
// Desc: remembers the "index" of the const button
//------------------------------------------------------------------------------
void KCalcConstButton::setButtonNumber(int num)
{
button_num_ = num;
}
//------------------------------------------------------------------------------
// Name: setLabelAndTooltip
// Desc: sets both the label and the tooltip for the const button
//------------------------------------------------------------------------------
void KCalcConstButton::setLabelAndTooltip()
{
QString new_label = QLatin1String("C") + QString::number(button_num_ + 1);
QString new_tooltip;
new_label = (KCalcSettings::nameConstant(button_num_).isNull() ? new_label : KCalcSettings::nameConstant(button_num_));
new_tooltip = new_label + QLatin1Char('=') + KCalcSettings::valueConstant(button_num_);
addMode(ModeNormal, new_label, new_tooltip);
}
//------------------------------------------------------------------------------
// Name: initPopupMenu
// Desc: initializes the const button popup
//------------------------------------------------------------------------------
void KCalcConstButton::initPopupMenu()
{
auto const a = new QAction(this);
a->setText(i18n("Set Name"));
connect(a, &QAction::triggered, this, &KCalcConstButton::slotConfigureButton);
addAction(a);
auto const tmp_menu = new KCalcConstMenu(this);
tmp_menu->menuAction()->setText(i18n("Choose From List"));
addAction(tmp_menu->menuAction());
setContextMenuPolicy(Qt::ActionsContextMenu);
connect(tmp_menu, &KCalcConstMenu::triggeredConstant, this, &KCalcConstButton::slotChooseScientificConst);
}
//------------------------------------------------------------------------------
// Name: slotConfigureButton
// Desc: lets the user set the name for a constant
//------------------------------------------------------------------------------
void KCalcConstButton::slotConfigureButton()
{
bool yes_no;
const QString input =
QInputDialog::getText(this, i18n("New Name for Constant"), i18n("New name:"), QLineEdit::Normal, text(), &yes_no); // "nameUserConstants-Dialog"
if (yes_no && !input.isEmpty()) {
KCalcSettings::setNameConstant(button_num_, input);
setLabelAndTooltip();
}
}
//------------------------------------------------------------------------------
// Name: slotChooseScientificConst
// Desc: set the button's scientific constant
//------------------------------------------------------------------------------
void KCalcConstButton::slotChooseScientificConst(const science_constant &const_chosen)
{
KCalcSettings::setValueConstant(button_num_, const_chosen.value);
KCalcSettings::setNameConstant(button_num_, const_chosen.label);
setLabelAndTooltip();
}
//------------------------------------------------------------------------------
// Name: slotClicked
// Desc: constant button was clicked
//------------------------------------------------------------------------------
void KCalcConstButton::slotClicked()
{
Q_EMIT constButtonClicked(button_num_);
}
#include "moc_kcalc_const_button.cpp"