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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
Copyright (C) 2001 - 2013 Evan Teran
evan.teran@gmail.com
Copyright (C) 2003 - 2005 Klaus Niederkrueger
kniederk@math.uni-koeln.de
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, see <http://www.gnu.org/licenses/>.
*/
#include "kcalc_const_menu.h"
#include <QDebug>
#include <QDomDocument>
#include <QFile>
#include <QStandardPaths>
#include <KLocalizedString>
namespace {
QList<science_constant> Constants;
ConstantCategory stringToCategory(const QString &s) {
if (s == QLatin1String("mathematics")) {
return Mathematics;
}
if (s == QLatin1String("electromagnetism")) {
return Electromagnetic;
}
if (s == QLatin1String("nuclear")) {
return Nuclear;
}
if (s == QLatin1String("thermodynamics")) {
return Thermodynamics;
}
if (s == QLatin1String("gravitation")) {
return Gravitation;
}
qDebug() << "Invalid Category For Constant: " << s;
return Mathematics;
}
}
void KCalcConstMenu::init_consts() {
QDomDocument doc(QStringLiteral("list_of_constants"));
QFile file(QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("scienceconstants.xml")));
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Did not find file \"scienceconstants.xml\". No constants will be available.";
return;
}
if (!doc.setContent(&file)) {
file.close();
qDebug() << "The file \"scienceconstants.xml\" does not seem to be a valid description file. No constants will be available.";
return;
}
file.close();
// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = doc.documentElement();
int i = 0;
QDomNode n = docElem.firstChild();
while (!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if (!e.isNull() && e.tagName() == QLatin1String("constant")) {
science_constant tmp_const;
tmp_const.name = I18N_NOOP(e.attributeNode(QLatin1String("name")).value());
tmp_const.label = e.attributeNode(QStringLiteral("symbol")).value();
tmp_const.value = e.attributeNode(QStringLiteral("value")).value();
QString tmp_str_category = e.attributeNode(QStringLiteral("category")).value();
tmp_const.category = stringToCategory(tmp_str_category);
tmp_const.whatsthis = e.firstChildElement(QStringLiteral("description")).text();
Constants.append(tmp_const);
}
n = n.nextSibling();
i++;
}
}
void KCalcConstMenu::init_all()
{
QMenu *math_menu = addMenu(i18n("Mathematics"));
QMenu *em_menu = addMenu(i18n("Electromagnetism"));
QMenu *nuclear_menu = addMenu(i18n("Atomic && Nuclear"));
QMenu *thermo_menu = addMenu(i18n("Thermodynamics"));
QMenu *gravitation_menu = addMenu(i18n("Gravitation"));
connect(this, &KCalcConstMenu::triggered, this, &KCalcConstMenu::slotPassSignalThrough);
for (int i = 0; i < Constants.size(); i++) {
QAction *tmp_action = new QAction(i18n(Constants.at(i).name.toLatin1().data()), this);
tmp_action->setData(QVariant(i));
if (Constants.at(i).category & Mathematics)
math_menu->addAction(tmp_action);
if (Constants.at(i).category & Electromagnetic)
em_menu->addAction(tmp_action);
if (Constants.at(i).category & Nuclear)
nuclear_menu->addAction(tmp_action);
if (Constants.at(i).category & Thermodynamics)
thermo_menu->addAction(tmp_action);
if (Constants.at(i).category & Gravitation)
gravitation_menu->addAction(tmp_action);
}
}
void KCalcConstMenu::slotPassSignalThrough(QAction *chosen_const)
{
bool tmp_bool;
int chosen_const_idx = (chosen_const->data()).toInt(& tmp_bool);
emit triggeredConstant(Constants.at(chosen_const_idx));
}
KCalcConstMenu::KCalcConstMenu(const QString &title, QWidget * parent)
: QMenu(title, parent)
{
init_all();
}
KCalcConstMenu::KCalcConstMenu(QWidget * parent)
: QMenu(parent)
{
init_all();
}
|