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_menu.h"
0009 
0010 #include <QDebug>
0011 #include <QDomDocument>
0012 #include <QFile>
0013 #include <QStandardPaths>
0014 
0015 #include <KLocalizedString>
0016 
0017 namespace
0018 {
0019 QList<science_constant> scienceConstantList;
0020 
0021 ConstantCategory stringToCategory(const QString &s)
0022 {
0023     if (s == QLatin1String("mathematics")) {
0024         return Mathematics;
0025     }
0026 
0027     if (s == QLatin1String("electromagnetism")) {
0028         return Electromagnetic;
0029     }
0030 
0031     if (s == QLatin1String("nuclear")) {
0032         return Nuclear;
0033     }
0034 
0035     if (s == QLatin1String("thermodynamics")) {
0036         return Thermodynamics;
0037     }
0038 
0039     if (s == QLatin1String("gravitation")) {
0040         return Gravitation;
0041     }
0042 
0043     qDebug() << "Invalid Category For Constant: " << s;
0044     return Mathematics;
0045 }
0046 
0047 }
0048 
0049 void KCalcConstMenu::init_consts()
0050 {
0051     QDomDocument doc(QStringLiteral("list_of_constants"));
0052     QFile file(QStringLiteral(":/kcalc/scienceconstants.xml"));
0053 
0054     if (!file.open(QIODevice::ReadOnly)) {
0055         qDebug() << "Did not find file \"scienceconstants.xml\". No constants will be available.";
0056         return;
0057     }
0058     if (!doc.setContent(&file)) {
0059         file.close();
0060         qDebug() << "The file \"scienceconstants.xml\" does not seem to be a valid description file. No constants will be available.";
0061         return;
0062     }
0063     file.close();
0064 
0065     // print out the element names of all elements that are direct children
0066     // of the outermost element.
0067     QDomElement docElem = doc.documentElement();
0068 
0069     int i = 0;
0070     QDomNode n = docElem.firstChild();
0071     while (!n.isNull()) {
0072         QDomElement e = n.toElement(); // try to convert the node to an element.
0073         if (!e.isNull() && e.tagName() == QLatin1String("constant")) {
0074             science_constant tmp_const;
0075 
0076             tmp_const.name = I18N_NOOP(e.attributeNode(QStringLiteral("name")).value());
0077             tmp_const.label = e.attributeNode(QStringLiteral("symbol")).value();
0078             tmp_const.value = e.attributeNode(QStringLiteral("value")).value();
0079 
0080             QString tmp_str_category = e.attributeNode(QStringLiteral("category")).value();
0081 
0082             tmp_const.category = stringToCategory(tmp_str_category);
0083             tmp_const.whatsthis = e.firstChildElement(QStringLiteral("description")).text();
0084 
0085             scienceConstantList.append(tmp_const);
0086         }
0087         n = n.nextSibling();
0088         i++;
0089     }
0090 }
0091 
0092 void KCalcConstMenu::init_all()
0093 {
0094     QMenu *math_menu = addMenu(i18n("Mathematics"));
0095     QMenu *em_menu = addMenu(i18n("Electromagnetism"));
0096     QMenu *nuclear_menu = addMenu(i18n("Atomic && Nuclear"));
0097     QMenu *thermo_menu = addMenu(i18n("Thermodynamics"));
0098     QMenu *gravitation_menu = addMenu(i18n("Gravitation"));
0099 
0100     connect(this, &KCalcConstMenu::triggered, this, &KCalcConstMenu::slotPassSignalThrough);
0101 
0102     for (int i = 0, total = scienceConstantList.size(); i < total; ++i) {
0103         const auto scienceConstantListItem = scienceConstantList.at(i);
0104         auto tmp_action = new QAction(i18n(scienceConstantListItem.name.toLatin1().data()), this);
0105         tmp_action->setData(QVariant(i));
0106         if (scienceConstantListItem.category & Mathematics)
0107             math_menu->addAction(tmp_action);
0108         if (scienceConstantListItem.category & Electromagnetic)
0109             em_menu->addAction(tmp_action);
0110         if (scienceConstantListItem.category & Nuclear)
0111             nuclear_menu->addAction(tmp_action);
0112         if (scienceConstantListItem.category & Thermodynamics)
0113             thermo_menu->addAction(tmp_action);
0114         if (scienceConstantListItem.category & Gravitation)
0115             gravitation_menu->addAction(tmp_action);
0116     }
0117 }
0118 
0119 void KCalcConstMenu::slotPassSignalThrough(QAction *chosen_const)
0120 {
0121     bool tmp_bool;
0122     int chosen_const_idx = (chosen_const->data()).toInt(&tmp_bool);
0123     Q_EMIT triggeredConstant(scienceConstantList.at(chosen_const_idx));
0124 }
0125 
0126 KCalcConstMenu::KCalcConstMenu(const QString &title, QWidget *parent)
0127     : QMenu(title, parent)
0128 {
0129     init_all();
0130 }
0131 
0132 KCalcConstMenu::KCalcConstMenu(QWidget *parent)
0133     : QMenu(parent)
0134 {
0135     init_all();
0136 }