summaryrefslogtreecommitdiff
path: root/noncore/apps/oxygen/kmolcalc.h
blob: c58913350d487648413542e1966191f9c9f2e3b5 (plain)
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
/* 
 * kmolcalc.h
 *
 * Copyright (C) 2000 Tomislav Gountchev <tomi@idiom.com>
 */


#ifndef KMOLCALC_H
#define KMOLCALC_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif 


#include "kmolelements.h"
#include <qdict.h>
#include <qlist.h>
#include <qstring.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qmessagebox.h>
#include <qtextstream.h>


/**
 * 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.
 */
class KMolCalc {

public:

  KMolCalc();
  ~KMolCalc();
  
  /** 
   * Parse a string S and construct the ElementList ELEMENTS, representing the composition
   * of S. Returns "OK" if successful, or an error message if parsing failed.
   * The elements is S must be valid element or group symbols, as stored in ELSTABLE.
   * See help files for correct formula syntax.
   */
  QString readFormula(const QString& s);
  
  /**
   * Calculate and return the molecular weight of the current chemical formula.
   */
  double getWeight();
  
  /**
   * Return the elemental composition of the current formula, as a string of tab-separated
   * element - percentage pairs, separated by newlines.
   */
  QString getEA();


  /**
   * Return the empirical formula of the current compound as a QString.
   */
  QString getEmpFormula();

  /**
   * Add a new element name - atomic weight record to the ELSTABLE hashtable. 
   * Assumes NAME has valid syntax.
   */
  void defineElement(const QString& name, double weight);

  /**
   * Add a new group definition to the ELSTABLE. Returns "OK" if OK, error message
   * if parsing FORMULA fails. Assumes the syntax of NAME is correct.
   */
  QString defineGroup(const QString& name, const QString& formula);

  /**
   * Remove a group or element definition from ELSTABLE.
   */
  void undefineGroup(const QString& name);

  /**
   * Read the element definitions file and construct ELSTABLE.
   */
  void readElstable();

  /**
   * The element dictionary.
   */
  QDict<SubUnit>* elstable;

  QString mwfile;

 private:
  double weight;

  QString readGroup(const QString& s, ElementList* els);
  void readMwfile(QFile& f);
  ElementList* elements;
};
    
#endif