18 #include <kstandarddirs.h>
66 if ( !list.contains( e ) )
79 elemCount =
search(_element);
103 m_elementList = list;
104 m_aliasList =
new QSet<QString>;
111 m_aliasList =
new QSet<QString>;
134 if ( _shortMoleculeString.isEmpty() )
138 m_aliasList -> clear();
139 QString _moleculeString;
147 kDebug() << _shortMoleculeString <<
"is going to be expanded";
148 _moleculeString = expandFormula(_shortMoleculeString);
149 kDebug() << _moleculeString <<
"is the expanded string";
153 start(_moleculeString);
154 parseSubmolecule(_resultMass, _resultMap);
178 MoleculeParser::parseSubmolecule(
double *_resultMass,
181 double subMass = 0.0;
186 while (parseTerm(&subMass, &subMap)) {
190 *_resultMass += subMass;
191 _resultMap->
add(subMap);
209 MoleculeParser::parseTerm(
double *_resultMass,
218 _resultMap->
add(m_elementVal, 1);
227 parseSubmolecule(_resultMass, _resultMap);
251 kDebug() <<
"Weight of term = " << *_resultMass;
268 kDebug() <<
"getNextToken(): Next character = "
283 m_elementVal = lookupElement(name);
303 MoleculeParser::lookupElement(
const QString& _name )
305 kDebug() <<
"looking up " << _name;
307 foreach(
Element* e, m_elementList ){
309 kDebug() <<
"Found element " << _name;
317 kDebug() <<
"no such element!: " << _name;
323 MoleculeParser::expandFormula(
const QString& _shortString)
326 QString::const_iterator i;
328 QString expandedTerm;
331 for(i = _shortString.constBegin(); i != _shortString.constEnd(); )
336 if((*i).category() == QChar::Letter_Uppercase) {
341 if(i != _shortString.end() && (*i).category() == QChar::Letter_Lowercase) {
347 if (lookupElement(temp)) {
352 else if (!((expandedTerm = expandTerm(temp)).isEmpty())) {
353 kDebug() <<
"expanded" << temp <<
"to" << expandedTerm;
354 _fullString +=
'('+expandedTerm+
')';
364 else if (*i ==
'(') {
368 else if (*i ==
')') {
374 else if (*i ==
'#') {
377 while (*i !=
'#' && i != _shortString.constEnd() )
383 if ( i == _shortString.constEnd()) {
388 else if (!temp.isEmpty())
391 if((expandedTerm = expandTerm(temp)).isEmpty())
395 _fullString += expandedTerm;
400 else if ((*i).category() == QChar::Number_DecimalDigit) {
407 kDebug() << *i <<
"invalid character!";
417 MoleculeParser::expandTerm (
const QString& _group)
420 QString shortForm, fullForm;
424 QString fileName = KStandardDirs::locate(
"data",
"libkdeedu/data/symbols2.csv");
425 QFile file(fileName);
428 if (!(!file.open(QIODevice::ReadOnly | QIODevice::Text)))
430 kDebug() << fileName <<
" opened";
431 QTextStream in(&file);
434 while (!in.atEnd()) {
435 QString line = in.readLine();
436 shortForm = line.section(
',', 0, 0);
437 shortForm.remove(QChar(
'\"'));
438 fullForm = line.section(
',', 1, 1);
439 fullForm.remove(QChar(
'\"'));
442 if (shortForm == _group)
444 *m_aliasList << (_group +
" : " + fullForm);
451 kDebug() << fileName <<
" could not be opened!";
456 fileName = KStandardDirs::locate(
"data",
"libkdeedu/data/symbols.csv");
457 QFile file2(fileName);
460 if (!(!file2.open(QIODevice::ReadOnly | QIODevice::Text)))
462 kDebug() << fileName <<
" opened";
463 QTextStream in(&file2);
466 while (!in.atEnd()) {
467 QString line = in.readLine();
468 shortForm = line.section(
',', 0, 0);
469 shortForm.remove(QChar(
'\"'));
470 fullForm = line.section(
',', 1, 1);
471 fullForm.remove(QChar(
'\"'));
473 if (shortForm == _group)
475 *m_aliasList << (_group +
" : " + fullForm);
482 kDebug() << fileName <<
" could not be opened!";
488 else if (_group ==
"Et")
void clear()
Clear the map of ElementCount pointers.
virtual ~MoleculeParser()
Destructor.
QList< Element * > elements()
Returns the elements in the molecule.
~ElementCountMap()
Destructor.
Element * element() const
ElementCountMap()
Constructor.
static const int INT_TOKEN
All characters are their own token value per default.
bool weight(const QString &_moleculeString, double *_resultMass, ElementCountMap *_resultMap)
Try to parse the molecule molecule and get the weight of it.
int nextChar() const
Peek at the next character;.
int getNextChar()
Make the next character the current one.
Element * m_element
The Element of the object.
int m_count
The number of occurrences.
the symbol of the element
QSet< QString > aliasList()
ElementCount * search(Element *_element)
QVariant dataAsVariant(ChemicalDataObject::BlueObelisk type) const
void start(const QString &_str)
Start a new parse.
void multiply(int _factor)
void add(ElementCountMap &_map)
void multiply(int _factor)
virtual int getNextToken()
Extends the standard tokenizer in Parser::getNextToken().
In this class all information about an element are stored.
int nextToken() const
Peek at the next token.
int m_nextToken
The next token to be used in the parser.
MoleculeParser(const QList< Element * > &list)
This class is used to count the elements in the molecule which is being calculated.
int intVal() const
Get the value stored for different types of tokens.
virtual int getNextToken()
Fetches the next token.
This is a general purpose parser originally written by Inge Wallin.