kcalc
kcalc_const_menu.cpp
Go to the documentation of this file.00001 /* 00002 kCalculator, a simple scientific calculator for KDE 00003 00004 Copyright (C) 2003 Klaus Niederkrueger <kniederk@math.uni-koeln.de> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 00020 */ 00021 00022 #include <QDomDocument> 00023 #include <QFile> 00024 #include <klocale.h> 00025 #include <kstandarddirs.h> 00026 00027 #include "kcalc_const_menu.h" 00028 00029 QList<struct science_constant> KCalcConstMenu::Constants; 00030 00031 00032 void KCalcConstMenu::init_consts(void) 00033 { 00034 QDomDocument doc("list_of_constants"); 00035 QFile file(KGlobal::dirs()->findResource("appdata", "scienceconstants.xml")); 00036 00037 if (!file.open(QIODevice::ReadOnly)) { 00038 qDebug("Didn't find file \"scienceconstants.xml\"." 00039 "No constants will be available."); 00040 return; 00041 } 00042 if (!doc.setContent(&file)) { 00043 file.close(); 00044 qDebug("The file \"scienceconstants.xml\" doesn't seem" 00045 "to be a valid description file." 00046 "No constants will be available."); 00047 return; 00048 } 00049 file.close(); 00050 00051 // print out the element names of all elements that are direct children 00052 // of the outermost element. 00053 QDomElement docElem = doc.documentElement(); 00054 00055 int i = 0; 00056 QDomNode n = docElem.firstChild(); 00057 while(!n.isNull()) { 00058 QDomElement e = n.toElement(); // try to convert the node to an element. 00059 if(!e.isNull() && e.tagName() == "constant") { 00060 struct science_constant tmp_const; 00061 00062 tmp_const.name = I18N_NOOP(e.attributeNode("name").value()); 00063 tmp_const.label = e.attributeNode("symbol").value(); 00064 tmp_const.value = e.attributeNode("value").value(); 00065 00066 QString tmp_str_category = e.attributeNode("category").value(); 00067 00068 if (tmp_str_category == "mathematics") 00069 tmp_const.category = Mathematics; 00070 if (tmp_str_category == "electromagnetism") 00071 tmp_const.category = Electromagnetic; 00072 if (tmp_str_category == "nuclear") 00073 tmp_const.category = Nuclear; 00074 if (tmp_str_category == "thermodynamics") 00075 tmp_const.category = Thermodynamics; 00076 if (tmp_str_category == "gravitation") 00077 tmp_const.category = Gravitation; 00078 00079 tmp_const.whatsthis = e.firstChildElement("description").text(); 00080 00081 Constants.append(tmp_const); 00082 } 00083 n = n.nextSibling(); 00084 i++; 00085 } 00086 } 00087 00088 void KCalcConstMenu::_init_all(void) 00089 { 00090 QMenu *math_menu = addMenu(i18n("Mathematics")); 00091 QMenu *em_menu = addMenu(i18n("Electromagnetism")); 00092 QMenu *nuclear_menu = addMenu(i18n("Atomic && Nuclear")); 00093 QMenu *thermo_menu = addMenu(i18n("Thermodynamics")); 00094 QMenu *gravitation_menu = addMenu(i18n("Gravitation")); 00095 00096 connect(this, SIGNAL(triggered(QAction *)), SLOT(slotPassSignalThrough(QAction *))); 00097 00098 00099 for (int i = 0; i<Constants.size(); i++) { 00100 QAction *tmp_action = new QAction(i18n(Constants.at(i).name.toAscii().data()), this); 00101 tmp_action->setData(QVariant(i)); 00102 if(Constants.at(i).category & Mathematics) 00103 math_menu->addAction(tmp_action); 00104 if(Constants.at(i).category & Electromagnetic) 00105 em_menu->addAction(tmp_action); 00106 if(Constants.at(i).category & Nuclear) 00107 nuclear_menu->addAction(tmp_action); 00108 if(Constants.at(i).category & Thermodynamics) 00109 thermo_menu->addAction(tmp_action); 00110 if(Constants.at(i).category & Gravitation) 00111 gravitation_menu->addAction(tmp_action); 00112 } 00113 } 00114 00115 void KCalcConstMenu::slotPassSignalThrough(QAction *chosen_const) 00116 { 00117 bool tmp_bool; 00118 int chosen_const_idx = (chosen_const->data()).toInt(& tmp_bool); 00119 emit triggeredConstant(Constants.at(chosen_const_idx)); 00120 } 00121 00122 KCalcConstMenu::KCalcConstMenu(QString const & title, QWidget * parent) 00123 : QMenu(title, parent) 00124 { 00125 _init_all(); 00126 } 00127 00128 KCalcConstMenu::KCalcConstMenu(QWidget * parent) 00129 : QMenu(parent) 00130 { 00131 _init_all(); 00132 } 00133 00134 00135 #include "kcalc_const_menu.moc"
KDE 4.0 API Reference