18 #include <kstandarddirs.h>
79 elemCount =
search(_element);
103 m_elementList = list;
134 if ( _shortMoleculeString.
isEmpty() )
138 m_aliasList -> clear();
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)
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()) {
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)
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";
434 while (!in.atEnd()) {
436 shortForm = line.
section(
',', 0, 0);
438 fullForm = line.
section(
',', 1, 1);
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";
466 while (!in.atEnd()) {
468 shortForm = line.
section(
',', 0, 0);
470 fullForm = line.
section(
',', 1, 1);
473 if (shortForm == _group)
475 *m_aliasList << (_group +
" : " + fullForm);
482 kDebug() << fileName <<
" could not be opened!";
488 else if (_group ==
"Et")
const_iterator constBegin() const
QString & append(QChar ch)
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.
QString & remove(int position, int n)
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.
const_iterator constEnd() const
void append(const T &value)
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.
bool contains(const T &value) const
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)
QString section(QChar sep, int start, int end, QFlags< QString::SectionFlag > flags) const
double toDouble(bool *ok) const
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.