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_menu.h" |
9 | |
10 | #include <QDebug> |
11 | #include <QDomDocument> |
12 | #include <QFile> |
13 | #include <QStandardPaths> |
14 | |
15 | #include <KLocalizedString> |
16 | |
17 | namespace |
18 | { |
19 | QList<science_constant> scienceConstantList; |
20 | |
21 | ConstantCategory stringToCategory(const QString &s) |
22 | { |
23 | if (s == QLatin1String("mathematics" )) { |
24 | return Mathematics; |
25 | } |
26 | |
27 | if (s == QLatin1String("electromagnetism" )) { |
28 | return Electromagnetic; |
29 | } |
30 | |
31 | if (s == QLatin1String("nuclear" )) { |
32 | return Nuclear; |
33 | } |
34 | |
35 | if (s == QLatin1String("thermodynamics" )) { |
36 | return Thermodynamics; |
37 | } |
38 | |
39 | if (s == QLatin1String("gravitation" )) { |
40 | return Gravitation; |
41 | } |
42 | |
43 | qDebug() << "Invalid Category For Constant: " << s; |
44 | return Mathematics; |
45 | } |
46 | |
47 | } |
48 | |
49 | void KCalcConstMenu::() |
50 | { |
51 | QDomDocument doc(QStringLiteral("list_of_constants" )); |
52 | QFile file(QStringLiteral(":/kcalc/scienceconstants.xml" )); |
53 | |
54 | if (!file.open(QIODevice::ReadOnly)) { |
55 | qDebug() << "Did not find file \"scienceconstants.xml\". No constants will be available." ; |
56 | return; |
57 | } |
58 | if (!doc.setContent(&file)) { |
59 | file.close(); |
60 | qDebug() << "The file \"scienceconstants.xml\" does not seem to be a valid description file. No constants will be available." ; |
61 | return; |
62 | } |
63 | file.close(); |
64 | |
65 | // print out the element names of all elements that are direct children |
66 | // of the outermost element. |
67 | QDomElement docElem = doc.documentElement(); |
68 | |
69 | int i = 0; |
70 | QDomNode n = docElem.firstChild(); |
71 | while (!n.isNull()) { |
72 | QDomElement e = n.toElement(); // try to convert the node to an element. |
73 | if (!e.isNull() && e.tagName() == QLatin1String("constant" )) { |
74 | science_constant tmp_const; |
75 | |
76 | tmp_const.name = e.attributeNode(QStringLiteral("name" )).value(); |
77 | tmp_const.label = e.attributeNode(QStringLiteral("symbol" )).value(); |
78 | tmp_const.value = e.attributeNode(QStringLiteral("value" )).value(); |
79 | |
80 | QString tmp_str_category = e.attributeNode(QStringLiteral("category" )).value(); |
81 | |
82 | tmp_const.category = stringToCategory(tmp_str_category); |
83 | tmp_const.whatsthis = e.firstChildElement(QStringLiteral("description" )).text(); |
84 | |
85 | scienceConstantList.append(tmp_const); |
86 | } |
87 | n = n.nextSibling(); |
88 | i++; |
89 | } |
90 | } |
91 | |
92 | void KCalcConstMenu::() |
93 | { |
94 | QMenu * = addMenu(i18n("Mathematics" )); |
95 | QMenu * = addMenu(i18n("Electromagnetism" )); |
96 | QMenu * = addMenu(i18n("Atomic && Nuclear" )); |
97 | QMenu * = addMenu(i18n("Thermodynamics" )); |
98 | QMenu * = addMenu(i18n("Gravitation" )); |
99 | |
100 | connect(this, &KCalcConstMenu::triggered, this, &KCalcConstMenu::slotPassSignalThrough); |
101 | |
102 | for (int i = 0, total = scienceConstantList.size(); i < total; ++i) { |
103 | const auto scienceConstantListItem = scienceConstantList.at(i); |
104 | auto tmp_action = new QAction(i18n(scienceConstantListItem.name.toLatin1().data()), this); |
105 | tmp_action->setData(QVariant(i)); |
106 | if (scienceConstantListItem.category & Mathematics) |
107 | math_menu->addAction(tmp_action); |
108 | if (scienceConstantListItem.category & Electromagnetic) |
109 | em_menu->addAction(tmp_action); |
110 | if (scienceConstantListItem.category & Nuclear) |
111 | nuclear_menu->addAction(tmp_action); |
112 | if (scienceConstantListItem.category & Thermodynamics) |
113 | thermo_menu->addAction(tmp_action); |
114 | if (scienceConstantListItem.category & Gravitation) |
115 | gravitation_menu->addAction(tmp_action); |
116 | } |
117 | } |
118 | |
119 | void KCalcConstMenu::(QAction *chosen_const) |
120 | { |
121 | bool tmp_bool; |
122 | int chosen_const_idx = (chosen_const->data()).toInt(&tmp_bool); |
123 | Q_EMIT triggeredConstant(scienceConstantList.at(chosen_const_idx)); |
124 | } |
125 | |
126 | KCalcConstMenu::(const QString &title, QWidget *parent) |
127 | : QMenu(title, parent) |
128 | { |
129 | init_all(); |
130 | } |
131 | |
132 | KCalcConstMenu::(QWidget *parent) |
133 | : QMenu(parent) |
134 | { |
135 | init_all(); |
136 | } |
137 | |
138 | #include "moc_kcalc_const_menu.cpp" |
139 | |