summaryrefslogtreecommitdiff
authorcniehaus <cniehaus>2002-09-15 16:18:47 (UTC)
committer cniehaus <cniehaus>2002-09-15 16:18:47 (UTC)
commit7a8dc6b0a16db160a1e50fde8f298eb9e3cb0099 (patch) (side-by-side diff)
tree16e49af9eabdcf6621654a1f80488c65951fe4b5
parenta013fbe2ba39e5ebd249766f223dbb67fe18027c (diff)
downloadopie-7a8dc6b0a16db160a1e50fde8f298eb9e3cb0099.zip
opie-7a8dc6b0a16db160a1e50fde8f298eb9e3cb0099.tar.gz
opie-7a8dc6b0a16db160a1e50fde8f298eb9e3cb0099.tar.bz2
wow... I just don't get my own code... Help needed
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/checkbook/calcdlg.ui44
-rw-r--r--noncore/apps/checkbook/calcdlgui.cpp69
-rw-r--r--noncore/apps/checkbook/calcdlgui.h29
-rw-r--r--noncore/apps/checkbook/kmolcalc.cpp4
-rw-r--r--noncore/apps/checkbook/oxygen.cpp5
-rw-r--r--noncore/apps/checkbook/oxygen.pro4
6 files changed, 84 insertions, 71 deletions
diff --git a/noncore/apps/checkbook/calcdlg.ui b/noncore/apps/checkbook/calcdlg.ui
index 7637ca8..e0b19f9 100644
--- a/noncore/apps/checkbook/calcdlg.ui
+++ b/noncore/apps/checkbook/calcdlg.ui
@@ -1,243 +1,243 @@
<!DOCTYPE UI><UI>
<class>CalcDlg</class>
<widget>
<class>QWidget</class>
<property stdset="1">
<name>name</name>
<cstring>CalcDlg</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>0</x>
<y>0</y>
- <width>222</width>
+ <width>218</width>
<height>274</height>
</rect>
</property>
<property stdset="1">
<name>caption</name>
<string>Network Time</string>
</property>
<property>
<name>layoutMargin</name>
</property>
<property>
<name>layoutSpacing</name>
</property>
<widget>
<class>QTabWidget</class>
<property stdset="1">
<name>name</name>
<cstring>TabWidgetMain</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>10</x>
<y>0</y>
<width>200</width>
<height>260</height>
</rect>
</property>
<property>
<name>layoutMargin</name>
</property>
<property>
<name>layoutSpacing</name>
</property>
<widget>
<class>QWidget</class>
<property stdset="1">
<name>name</name>
<cstring>tabMain</cstring>
</property>
<attribute>
<name>title</name>
<string>Molweight</string>
</attribute>
<widget>
<class>QFrame</class>
<property stdset="1">
<name>name</name>
<cstring>FrameSystemTime</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>2</x>
<y>2</y>
<width>180</width>
<height>220</height>
</rect>
</property>
<property stdset="1">
<name>frameShape</name>
<enum>StyledPanel</enum>
</property>
<property stdset="1">
<name>frameShadow</name>
<enum>Raised</enum>
</property>
<widget>
<class>QLabel</class>
<property stdset="1">
<name>name</name>
<cstring>TextLabel1_4</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>1</x>
<y>1</y>
<width>298</width>
<height>16</height>
</rect>
</property>
<property stdset="1">
<name>text</name>
<string>Formula:</string>
</property>
</widget>
<widget>
- <class>QLabel</class>
- <property stdset="1">
- <name>name</name>
- <cstring>TextLabel2_4</cstring>
- </property>
- <property stdset="1">
- <name>geometry</name>
- <rect>
- <x>1</x>
- <y>86</y>
- <width>298</width>
- <height>16</height>
- </rect>
- </property>
- <property stdset="1">
- <name>text</name>
- <string>Result:</string>
- </property>
- </widget>
- <widget>
<class>QLayoutWidget</class>
<property stdset="1">
<name>name</name>
<cstring>Layout7</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>0</x>
<y>50</y>
<width>175</width>
<height>30</height>
</rect>
</property>
<hbox>
<property stdset="1">
<name>margin</name>
<number>0</number>
</property>
<property stdset="1">
<name>spacing</name>
<number>6</number>
</property>
<widget>
<class>QPushButton</class>
<property stdset="1">
<name>name</name>
<cstring>calculate</cstring>
</property>
<property stdset="1">
<name>text</name>
<string>Calculate</string>
</property>
</widget>
<widget>
<class>QPushButton</class>
<property stdset="1">
<name>name</name>
<cstring>clear_fields</cstring>
</property>
<property stdset="1">
<name>text</name>
<string>Clear</string>
</property>
</widget>
</hbox>
</widget>
<widget>
<class>QLineEdit</class>
<property stdset="1">
<name>name</name>
<cstring>formula</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>0</x>
<y>20</y>
<width>170</width>
<height>22</height>
</rect>
</property>
</widget>
<widget>
<class>QMultiLineEdit</class>
<property stdset="1">
<name>name</name>
<cstring>anal_display</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>0</x>
<y>130</y>
<width>170</width>
<height>80</height>
</rect>
</property>
</widget>
<widget>
<class>QLineEdit</class>
<property stdset="1">
<name>name</name>
- <cstring>LineEdit2</cstring>
+ <cstring>result</cstring>
</property>
<property stdset="1">
<name>geometry</name>
<rect>
<x>0</x>
<y>100</y>
<width>170</width>
<height>22</height>
</rect>
</property>
</widget>
+ <widget>
+ <class>QLabel</class>
+ <property stdset="1">
+ <name>name</name>
+ <cstring>TextLabel2_4</cstring>
+ </property>
+ <property stdset="1">
+ <name>geometry</name>
+ <rect>
+ <x>1</x>
+ <y>86</y>
+ <width>298</width>
+ <height>16</height>
+ </rect>
+ </property>
+ <property stdset="1">
+ <name>text</name>
+ <string>Result:</string>
+ </property>
+ </widget>
</widget>
</widget>
<widget>
<class>QWidget</class>
<property stdset="1">
<name>name</name>
<cstring>tabNtp</cstring>
</property>
<attribute>
<name>title</name>
<string>Nernst</string>
</attribute>
<grid>
<property stdset="1">
<name>margin</name>
<number>1</number>
</property>
<property stdset="1">
<name>spacing</name>
<number>2</number>
</property>
</grid>
</widget>
</widget>
</widget>
</UI>
diff --git a/noncore/apps/checkbook/calcdlgui.cpp b/noncore/apps/checkbook/calcdlgui.cpp
index a86374b..e9b40d3 100644
--- a/noncore/apps/checkbook/calcdlgui.cpp
+++ b/noncore/apps/checkbook/calcdlgui.cpp
@@ -1,52 +1,55 @@
/***************************************************************************
* *
* 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. *
* *
- * ***********************************************************************/
+ **************************************************************************/
#include "oxygen.h"
-#include <qmenubar.h>
-#include <qstatusbar.h>
-#include <qpopupmenu.h>
-#include <qlabel.h>
-#include <qapplication.h>
+#include "calcdlgui.h"
+#include "kmolcalc.h"
+#include <qlineedit.h>
+#include <qmultilineedit.h>
#include <qpushbutton.h>
-#include <qfile.h>
-#include <qdir.h>
-#include <qvbox.h>
-#include "calcdlg.h"
-Oxygen::Oxygen()
- : QMainWindow()
+calcDlgUI::calcDlgUI() : CalcDlg()
{
- this->setCaption( "Oxygen" );
- vbox = new QVBox( this );
- QPushButton *setButton = new QPushButton( "Settings", vbox );
- connect ( setButton, SIGNAL( clicked() ), this, SLOT( slotSettings() ) );
- QPushButton *calcButton = new QPushButton( "Calculations", vbox );
- connect ( calcButton, SIGNAL( clicked() ), this, SLOT( slotCalculations() ) );
- QPushButton *pseButton = new QPushButton( "PSE", vbox );
- connect ( pseButton, SIGNAL( clicked() ), this, SLOT( slotPSE() ) );
-
- setCentralWidget( vbox );
+ kmolcalc = new KMolCalc;
+ connect( calculate, SIGNAL( clicked() ), this, SLOT( calc() ) );
+ connect( clear_fields, SIGNAL( clicked() ), this, SLOT( clear() ) );
}
-
-void Oxygen::close()
+void calcDlgUI::calc()
{
- QApplication::exit();
+ QString compound( formula->text() );
+ if ( compound.isEmpty() ) {
+ clear();
+ return;
+ }
+ QString errors( kmolcalc->readFormula( compound ) );
+ QString mw, ea;
+ double weight = kmolcalc->getWeight();
+ if ( errors == "OK" ) {
+ mw.setNum( weight );
+ ea = kmolcalc->getEmpFormula() + " :\n" + kmolcalc->getEA();
+ } else {
+ mw = "???";
+ ea = tr( "ERROR: \n" ) + errors + "\n";
+ }
+ result->setText( mw );
+ anal_display->setText( ea );
}
-//SLOTS
-
-void Oxygen::slotCalculations(){
- CalcDlgUI *calcDlgUI = new calcDlgUI();
- calcDlgUI->show();
-};
+/**
+ * * Clear all text entry / result fields.
+ * */
+void calcDlgUI::clear()
+{
+ formula->clear();
+ result->clear();
+ anal_display->clear();
+}
-void Oxygen::slotSettings(){ };
-void Oxygen::slotPSE(){ };
diff --git a/noncore/apps/checkbook/calcdlgui.h b/noncore/apps/checkbook/calcdlgui.h
index 1e923ee..33a1ec1 100644
--- a/noncore/apps/checkbook/calcdlgui.h
+++ b/noncore/apps/checkbook/calcdlgui.h
@@ -1,22 +1,27 @@
-#include <qpe/resource.h>
-#include <qmainwindow.h>
-#include <qtoolbar.h>
+/***************************************************************************
+ * *
+ * 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. *
+ * *
+ **************************************************************************/
#include <qstring.h>
-#include <qpopupmenu.h>
+#include "calcdlg.h"
class QVBox;
+class KMolCalc;
-class Oxygen : public QMainWindow
+class calcDlgUI : public CalcDlg
{
Q_OBJECT
public:
- Oxygen();
- QVBox *vbox;
- private slots:
- void slotCalculations();
- void slotSettings();
- void slotPSE();
- void close();
+ calcDlgUI();
+ KMolCalc *kmolcalc;
+
+ public slots:
+ void calc();
+ void clear();
};
diff --git a/noncore/apps/checkbook/kmolcalc.cpp b/noncore/apps/checkbook/kmolcalc.cpp
index e5ab736..50e1375 100644
--- a/noncore/apps/checkbook/kmolcalc.cpp
+++ b/noncore/apps/checkbook/kmolcalc.cpp
@@ -1,230 +1,232 @@
/*
* kmolcalc.cpp
*
* Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com>
+ * Copyright (C) 2002 Carsten Niehaus <cniehaus@handhelds.org>
*/
/**
* KMOLCALC is the calculation engine. It knows about a hashtable of user defined atomic
* weights and group definitions ELSTABLE, and the currently processed formula, stored
* as a list of elements and their coefficients, ELEMENTS.
*/
#include "kmolcalc.h"
#include <qdict.h>
#include <qdir.h>
+#include <qfile.h>
#include <iostream.h>
/**
* Construct a new calculator object.
*/
KMolCalc::KMolCalc() {
elements = new ElementList;
elstable = NULL;
readElstable();
}
KMolCalc::~KMolCalc() {
delete elements;
}
void KMolCalc::readElstable() {
weight = -1; // not calculated yet
if (elstable) delete elstable;
elstable = new QDict<SubUnit> (197, TRUE);
elstable->setAutoDelete(TRUE);
QStringList files;// = // KGlobal::dirs()->findAllResources("appdata", "kmolweights");
-// mwfile = locateLocal("data", "kmol")+"/kmolweights";
+ mwfile = "/home/opie/opie/noncore/apps/oxigen/kmolweights";
QFile f(mwfile);
QString* latest_f = &mwfile;
for (uint i=0; i<files.count(); i++) {
if (QFileInfo(QFile(files[i])).lastModified() > QFileInfo(QFile(*latest_f)).lastModified()) {
latest_f = &files[i];
}
}
QFile lf(*latest_f);
if (f.exists()) readMwfile(f);
if (!f.exists()) {
readMwfile(lf);
writeElstable();
} else if (QFileInfo(f).lastModified() < QFileInfo(lf).lastModified()) {
// announce
QMessageBox::information
(0, "Warning:", "Found new global Mw file.\nLocal definitions will be updated.", QMessageBox::Ok);
readMwfile(lf);
writeElstable();
}
}
/**
* Parse a string S and construct the ElementList this->ELEMENTS, representing the
* composition of S. Returns 0 if successful, or an error code (currently -1) if
* parsing failed.
* The elements is S must be valid element or group symbols, as stored in this->ELSTABLE.
* See help files for correct formula syntax.
*/
QString KMolCalc::readFormula(const QString& s) {
weight = -1;
if (elements) delete elements;
elements = new ElementList;
return KMolCalc::readGroup(s, elements);
}
// read a formula group recursively. Called by readFormula.
QString KMolCalc::readGroup(const QString& s, ElementList* els) {
if (s.isEmpty()) return QString ("Enter a formula."); //ERROR
int sl = s.length();
int i = 0;
QString errors ("OK");
bool ok = TRUE;
while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++;
double prefix = (i == 0 ? 1 : s.left(i).toDouble(&ok));
if (! ok || i == sl || prefix == 0) return QString ("Bad formula."); // ERROR
ElementList* elstemp = new ElementList;
while (i < sl) {
int j = i;
if (s[i] == '(') {
ElementList* inner = new ElementList;
int level = 1; // count levels of nested ( ).
while (1) {
if (i++ == sl) {
delete inner;
delete elstemp;
return QString ("Bad formula."); //ERROR
}
if (s[i] == '(') level++;
if (s[i] == ')') level--;
if (level == 0) break;
}
errors = KMolCalc::readGroup(s.mid(j+1, i-j-1), inner);
j = ++i;
while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++;
double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok));
if (! ok || suffix == 0) {
delete inner;
delete elstemp;
return QString ("Bad formula."); // ERROR
}
inner->addTo(*elstemp, suffix);
delete inner;
inner = NULL;
} else if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) {
while (++i < sl && ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '*' ||
s[i] == '\''));
QString elname = s.mid(j, i-j);
j = i;
while (i < sl && ((s[i] <= '9' && s[i] >= '0') || s[i] == '.')) i++;
double suffix = (i == j ? 1 : s.mid(j, i-j).toDouble(&ok));
if (! ok || suffix == 0) {
delete elstemp;
return QString ("Bad formula."); // ERROR
}
SubUnit* group = elstable->find(elname);
if (group == 0) {
delete elstemp;
return QString ("Undefined symbol: ") + elname; //ERROR
}
group->addTo(*elstemp, suffix);
} else if (s[i] == '+') {
if (elstemp->isEmpty()) {
delete elstemp;
return QString ("Bad formula."); //ERROR
}
elstemp->addTo(*els, prefix);
delete elstemp;
errors = KMolCalc::readGroup(s.mid(i+1, sl-i-1), els);
return errors;
} else {
delete elstemp;
return QString ("Bad formula."); //ERROR
}
}
elstemp->addTo(*els, prefix);
delete elstemp;
return errors;
}
/**
* Calculate and return the molecular weight of the current chemical formula.
*/
double KMolCalc::getWeight() {
if (weight == -1) weight = elements->getWeight(elstable);
return weight;
}
/**
* Return the elemental composition of the current formula, as a string of tab-separated
* element - percentage pairs, separated by newlines.
*/
QString KMolCalc::getEA() {
if (weight == -1) weight = elements->getWeight(elstable);
if (weight == -1) return QString("ERROR: Couldn't get Mw..."); // ERROR
return elements->getEA(elstable, weight);
}
/**
* Return the empirical formula of the current compound as a QString.
*/
QString KMolCalc::getEmpFormula() {
return elements->getEmpFormula();
}
// Read the element definition file.
void KMolCalc::readMwfile(QFile& f) {
if (! f.open(IO_ReadOnly)) return; //ERROR
QTextStream fs (&f);
QString line;
while (! fs.eof()) {
line = fs.readLine();
SubUnit* s = SubUnit::makeSubUnit(line);
elstable->replace(s->getName(), s);
}
f.close();
}
/**
* Save the element definitions file.
*/
void KMolCalc::writeElstable() {
QFile f(mwfile);
if (! f.open(IO_WriteOnly)) return; //ERROR
QTextStream fs (&f);
QString line;
QDictIterator<SubUnit> it(*elstable);
while (it.current()) {
it.current()->writeOut(line);
fs << line << endl;
++it;
}
f.close();
}
/**
* Remove a group or element definition from ELSTABLE.
*/
void KMolCalc::undefineGroup (const QString& name) {
elstable->remove (name);
}
/**
* Add a new element name - atomic weight record to the ELSTABLE hashtable. Assumes
* NAME has valid syntax.
*/
void KMolCalc::defineElement (const QString& name, double weight) {
Element* el = new Element(name, weight);
elstable->replace(name, el);
}
/**
* Add a new group definition to the ELSTABLE. Returns 0 if OK, -1 if parsing FORMULA
* fails. Assumes the syntax of grpname is correct.
*/
QString KMolCalc::defineGroup (const QString& grpname, const QString& formula) {
ElementList* els = new ElementList(grpname);
QString error = readGroup(formula, els);
if (error != "OK") return error;
if (els->contains(grpname)) return QString("Can't define a group recursively!\n");
diff --git a/noncore/apps/checkbook/oxygen.cpp b/noncore/apps/checkbook/oxygen.cpp
index a86374b..81897e9 100644
--- a/noncore/apps/checkbook/oxygen.cpp
+++ b/noncore/apps/checkbook/oxygen.cpp
@@ -1,52 +1,53 @@
/***************************************************************************
* *
* 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. *
* *
* ***********************************************************************/
#include "oxygen.h"
#include <qmenubar.h>
#include <qstatusbar.h>
#include <qpopupmenu.h>
#include <qlabel.h>
#include <qapplication.h>
#include <qpushbutton.h>
#include <qfile.h>
#include <qdir.h>
#include <qvbox.h>
#include "calcdlg.h"
+#include "calcdlgui.h"
Oxygen::Oxygen()
: QMainWindow()
{
this->setCaption( "Oxygen" );
vbox = new QVBox( this );
QPushButton *setButton = new QPushButton( "Settings", vbox );
connect ( setButton, SIGNAL( clicked() ), this, SLOT( slotSettings() ) );
QPushButton *calcButton = new QPushButton( "Calculations", vbox );
connect ( calcButton, SIGNAL( clicked() ), this, SLOT( slotCalculations() ) );
QPushButton *pseButton = new QPushButton( "PSE", vbox );
connect ( pseButton, SIGNAL( clicked() ), this, SLOT( slotPSE() ) );
setCentralWidget( vbox );
}
void Oxygen::close()
{
QApplication::exit();
}
//SLOTS
void Oxygen::slotCalculations(){
- CalcDlgUI *calcDlgUI = new calcDlgUI();
- calcDlgUI->show();
+ calcDlgUI *CalcDlgUI = new calcDlgUI();
+ CalcDlgUI->show();
};
void Oxygen::slotSettings(){ };
void Oxygen::slotPSE(){ };
diff --git a/noncore/apps/checkbook/oxygen.pro b/noncore/apps/checkbook/oxygen.pro
index f8e0197..c5448e7 100644
--- a/noncore/apps/checkbook/oxygen.pro
+++ b/noncore/apps/checkbook/oxygen.pro
@@ -1,18 +1,20 @@
TEMPLATE = app
CONFIG = qt warn_on release
HEADERS = oxygen.h \
kmolcalc.h \
- kmolelements.h
+ kmolelements.h \
+ calcdlgui.h
SOURCES = main.cpp \
oxygen.cpp \
kmolcalc.cpp \
+ calcdlgui.cpp \
kmolelements.cpp
INCLUDEPATH += $(OPIEDIR)/include
DEPENDPATH += $(OPIEDIR)/include
LIBS += -lqpe
INTERFACES = calcdlg.ui
TARGET = oxygen
DESTDIR = $(OPIEDIR)/bin
TRANSLATIONS = ../../../i18n/de/oxygen.ts