• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kcalc

  • sources
  • kde-4.14
  • kdeutils
  • kcalc
kcalc_const_menu.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2001 - 2013 Evan Teran
3  evan.teran@gmail.com
4 
5 Copyright (C) 2003 - 2005 Klaus Niederkrueger
6  kniederk@math.uni-koeln.de
7 
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "kcalc_const_menu.h"
23 
24 #include <QDomDocument>
25 #include <QFile>
26 #include <kdebug.h>
27 #include <klocale.h>
28 #include <kstandarddirs.h>
29 
30 namespace {
31  QList<science_constant> Constants;
32 
33  ConstantCategory stringToCategory(const QString &s) {
34  if (s == QLatin1String("mathematics")) {
35  return Mathematics;
36  }
37 
38  if (s == QLatin1String("electromagnetism")) {
39  return Electromagnetic;
40  }
41 
42  if (s == QLatin1String("nuclear")) {
43  return Nuclear;
44  }
45 
46  if (s == QLatin1String("thermodynamics")) {
47  return Thermodynamics;
48  }
49 
50  if (s == QLatin1String("gravitation")) {
51  return Gravitation;
52  }
53 
54  kDebug() << "Invalid Category For Constant: " << s;
55  return Mathematics;
56  }
57 
58 }
59 
60 
61 void KCalcConstMenu::init_consts() {
62  QDomDocument doc(QLatin1String("list_of_constants"));
63  QFile file(KGlobal::dirs()->findResource("appdata", QLatin1String("scienceconstants.xml")));
64 
65  if (!file.open(QIODevice::ReadOnly)) {
66  kDebug() << "Did not find file \"scienceconstants.xml\". No constants will be available.";
67  return;
68  }
69  if (!doc.setContent(&file)) {
70  file.close();
71  kDebug() << "The file \"scienceconstants.xml\" does not seem to be a valid description file. No constants will be available.";
72  return;
73  }
74  file.close();
75 
76  // print out the element names of all elements that are direct children
77  // of the outermost element.
78  QDomElement docElem = doc.documentElement();
79 
80  int i = 0;
81  QDomNode n = docElem.firstChild();
82  while (!n.isNull()) {
83  QDomElement e = n.toElement(); // try to convert the node to an element.
84  if (!e.isNull() && e.tagName() == QLatin1String("constant")) {
85  science_constant tmp_const;
86 
87  tmp_const.name = I18N_NOOP(e.attributeNode(QLatin1String("name")).value());
88  tmp_const.label = e.attributeNode(QLatin1String("symbol")).value();
89  tmp_const.value = e.attributeNode(QLatin1String("value")).value();
90 
91  QString tmp_str_category = e.attributeNode(QLatin1String("category")).value();
92 
93  tmp_const.category = stringToCategory(tmp_str_category);
94  tmp_const.whatsthis = e.firstChildElement(QLatin1String("description")).text();
95 
96  Constants.append(tmp_const);
97  }
98  n = n.nextSibling();
99  i++;
100  }
101 }
102 
103 void KCalcConstMenu::init_all()
104 {
105  QMenu *math_menu = addMenu(i18n("Mathematics"));
106  QMenu *em_menu = addMenu(i18n("Electromagnetism"));
107  QMenu *nuclear_menu = addMenu(i18n("Atomic && Nuclear"));
108  QMenu *thermo_menu = addMenu(i18n("Thermodynamics"));
109  QMenu *gravitation_menu = addMenu(i18n("Gravitation"));
110 
111  connect(this, SIGNAL(triggered(QAction*)), SLOT(slotPassSignalThrough(QAction*)));
112 
113 
114  for (int i = 0; i < Constants.size(); i++) {
115  QAction *tmp_action = new QAction(i18n(Constants.at(i).name.toAscii().data()), this);
116  tmp_action->setData(QVariant(i));
117  if (Constants.at(i).category & Mathematics)
118  math_menu->addAction(tmp_action);
119  if (Constants.at(i).category & Electromagnetic)
120  em_menu->addAction(tmp_action);
121  if (Constants.at(i).category & Nuclear)
122  nuclear_menu->addAction(tmp_action);
123  if (Constants.at(i).category & Thermodynamics)
124  thermo_menu->addAction(tmp_action);
125  if (Constants.at(i).category & Gravitation)
126  gravitation_menu->addAction(tmp_action);
127  }
128 }
129 
130 void KCalcConstMenu::slotPassSignalThrough(QAction *chosen_const)
131 {
132  bool tmp_bool;
133  int chosen_const_idx = (chosen_const->data()).toInt(& tmp_bool);
134  emit triggeredConstant(Constants.at(chosen_const_idx));
135 }
136 
137 KCalcConstMenu::KCalcConstMenu(const QString &title, QWidget * parent)
138  : QMenu(title, parent)
139 {
140  init_all();
141 }
142 
143 KCalcConstMenu::KCalcConstMenu(QWidget * parent)
144  : QMenu(parent)
145 {
146  init_all();
147 }
148 
149 
150 #include "kcalc_const_menu.moc"
KCalcConstMenu::triggeredConstant
void triggeredConstant(const science_constant &)
KCalcConstMenu::KCalcConstMenu
KCalcConstMenu(QWidget *parent=0)
Definition: kcalc_const_menu.cpp:143
QWidget
Mathematics
Definition: kcalc_const_menu.h:29
QAction::data
QVariant data() const
Gravitation
Definition: kcalc_const_menu.h:33
QList::at
const T & at(int i) const
QMenu::addAction
void addAction(QAction *action)
QMenu::triggered
void triggered(QAction *action)
science_constant::category
ConstantCategory category
Definition: kcalc_const_menu.h:41
QDomDocument::documentElement
QDomElement documentElement() const
QDomNode
kcalc_const_menu.h
QFile
QList::size
int size() const
QDomNode::nextSibling
QDomNode nextSibling() const
KCalcConstMenu::init_consts
static void init_consts()
Definition: kcalc_const_menu.cpp:61
QDomNode::toElement
QDomElement toElement() const
Electromagnetic
Definition: kcalc_const_menu.h:30
science_constant
Definition: kcalc_const_menu.h:36
QList::append
void append(const T &value)
QDomElement::text
QString text() const
science_constant::label
QString label
Definition: kcalc_const_menu.h:37
Constants
Definition: kcalc.h:78
science_constant::name
QString name
Definition: kcalc_const_menu.h:38
Nuclear
Definition: kcalc_const_menu.h:31
Thermodynamics
Definition: kcalc_const_menu.h:32
QString
QList
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QAction::setData
void setData(const QVariant &userData)
QMenu
QDomDocument
science_constant::whatsthis
QString whatsthis
Definition: kcalc_const_menu.h:39
QFile::close
virtual void close()
QDomAttr::value
QString value() const
QDomNode::isNull
bool isNull() const
KCalcConstMenu::slotPassSignalThrough
void slotPassSignalThrough(QAction *chosen_const)
Definition: kcalc_const_menu.cpp:130
QDomNode::firstChild
QDomNode firstChild() const
QLatin1String
ConstantCategory
ConstantCategory
Definition: kcalc_const_menu.h:28
QMenu::addMenu
QAction * addMenu(QMenu *menu)
QDomNode::firstChildElement
QDomElement firstChildElement(const QString &tagName) const
QAction
science_constant::value
QString value
Definition: kcalc_const_menu.h:40
QDomElement::tagName
QString tagName() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QDomElement
QDomElement::attributeNode
QDomAttr attributeNode(const QString &name)
QDomDocument::setContent
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kcalc

Skip menu "kcalc"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal