[Kalzium] branches/KDE/3.5/kdeedu/kalzium/src

Carsten Niehaus cniehaus at gmx.de
Fri Dec 30 10:54:38 CET 2005


SVN commit 492531 by cniehaus:

This fixes bug #117774 The calculator has two issues. First, it didn't
tell the user when the string was invalid (eg H2Ojdfkjd). Second,
it didn't stop calculating in some cases (H2g produces a different
error than H2Og, only the first stopped the calculation). With this commit
I am introducing a new string "Invalid input" which is displayed 
when the parser notices an error.
CCMAIL:kalzium at kde.org
BUG:117774


 M  +47 -43    molcalcwidget.cpp  
 M  +1 -0      molcalcwidget.h  
 M  +13 -6     moleculeparser.cpp  
 M  +3 -2      moleculeparser.h  


--- branches/KDE/3.5/kdeedu/kalzium/src/molcalcwidget.cpp #492530:492531
@@ -68,41 +68,53 @@
 
 void MolcalcWidget::updateUI()
 {
-	QString str;
+	if ( m_validInput ){
 
-	// The complexString stores the whole molecule like this:
-	// 1 Seaborgium. Cumulative Mass: 263.119 u (39.2564 %)
-	QString complexString;
-	
-	// Create the list of elements making up the molecule
-	ElementCountMap::Iterator  it    = m_elementMap.begin();
-	ElementCountMap::Iterator  itEnd = m_elementMap.end();
-	for ( ; it != itEnd; ++it ) {
-		// Update the resultLabel
-		str += i18n( "For example: \"1 Carbon\" or \"3 Oxygen\"", "%1 %2\n" )
-		  .arg( (*it)->count() )
-		  .arg( (*it)->element()->elname() );
+		QString str;
 
-		complexString
-		  += i18n( "For example: 1 Seaborgium. Cumulative Mass: 263.119 u (39.25%)",
-				   "%1 %2. Cumulative Mass: %3 u (%4%)\n" )
-		  .arg( (*it)->count() )
-		  .arg( (*it)->element()->elname() )
-		  .arg( (*it)->count() * (*it)->element()->mass() )
-		  .arg( KalziumUtils::strippedValue( (( (*it)->count() * (*it)->element()->mass() )
-											  / m_mass ) * 100 ) );
+		// The complexString stores the whole molecule like this:
+		// 1 Seaborgium. Cumulative Mass: 263.119 u (39.2564 %)
+		QString complexString;
+
+		// Create the list of elements making up the molecule
+		ElementCountMap::Iterator  it    = m_elementMap.begin();
+		ElementCountMap::Iterator  itEnd = m_elementMap.end();
+		for ( ; it != itEnd; ++it ) {
+			// Update the resultLabel
+			str += i18n( "For example: \"1 Carbon\" or \"3 Oxygen\"", "%1 %2\n" )
+				.arg( (*it)->count() )
+				.arg( (*it)->element()->elname() );
+
+			complexString
+				+= i18n( "For example: 1 Seaborgium. Cumulative Mass: 263.119 u (39.25%)",
+						"%1 %2. Cumulative Mass: %3 u (%4%)\n" )
+				.arg( (*it)->count() )
+				.arg( (*it)->element()->elname() )
+				.arg( (*it)->count() * (*it)->element()->mass() )
+				.arg( KalziumUtils::strippedValue( (( (*it)->count() * (*it)->element()->mass() )
+								/ m_mass ) * 100 ) );
+		}
+		resultLabel->setText( str );
+
+		// The composition
+		resultComposition->setText( compositionString(m_elementMap) );
+
+		// The mass
+		resultMass->setText( i18n( "Molecular mass: %1 u" ).arg( m_mass ) );
+
+		QToolTip::add( resultMass,        complexString );
+		QToolTip::add( resultComposition, complexString );
+		QToolTip::add( resultLabel,       complexString );
 	}
-	resultLabel->setText( str );
-	
-	// The composition
-	resultComposition->setText( compositionString(m_elementMap) );
-	
-	// The mass
-	resultMass->setText( i18n( "Molecular mass: %1 u" ).arg( m_mass ) );
-	
-	QToolTip::add( resultMass,        complexString );
-	QToolTip::add( resultComposition, complexString );
-	QToolTip::add( resultLabel,       complexString );
+	else{//the input was invalid, so tell this the user
+		resultComposition->setText( i18n( "Invalid input" ) );
+		resultLabel->setText( QString() );
+		resultMass->setText( QString() );
+		
+		QToolTip::add( resultMass,        i18n( "Invalid input" ) );
+		QToolTip::add( resultComposition, i18n( "Invalid input" ) );
+		QToolTip::add( resultLabel,       i18n( "Invalid input" ) );
+	}
 }
 
 
@@ -132,17 +144,9 @@
 
 	// Parse the molecule, and at the same time calculate the total
 	// mass, and the composition of it.
-	bool parseOk = m_parser.weight(molecule, &m_mass, &m_elementMap);
-
-	if (parseOk) {
-		kdDebug() << "Mass of " << molecule << " = " << m_mass << endl;
-
-		updateUI();
-	}
-	else {
-		// FIXME: Do something better here.
-		kdDebug() << "Parse error" << endl;
-	}
+	m_validInput = m_parser.weight(molecule, &m_mass, &m_elementMap);
+	
+	updateUI();
 }
 
 
--- branches/KDE/3.5/kdeedu/kalzium/src/molcalcwidget.h #492530:492531
@@ -72,6 +72,7 @@
 		MoleculeParser   m_parser;
 		
 		double           m_mass;
+		bool 			 m_validInput;
 		ElementCountMap  m_elementMap;
 };
 
--- branches/KDE/3.5/kdeedu/kalzium/src/moleculeparser.cpp #492530:492531
@@ -124,8 +124,9 @@
 					   double          *_resultMass,
 					   ElementCountMap *_resultMap)
 {
-    // Clear the result variables.
+    // Clear the result variables and set m_error to false
     _resultMap->clear();
+	m_error = false;
     *_resultMass = 0.0;
 
 	// Initialize the parsing process, and parse te molecule.
@@ -135,6 +136,9 @@
     if (nextToken() != -1)
 		return false;
 
+	if ( m_error )//there was an error in the input...
+		return false;
+
     return true;
 }
 
@@ -286,12 +290,15 @@
     const EList::ConstIterator  end = elementList.constEnd();
 
     for (; it != end; ++it) {
-	if ( (*it)->symbol() == _name ) {
-	    kdDebug() << "Found element " << _name << endl;
-	    return *it;
-	}
+		if ( (*it)->symbol() == _name ) {
+			kdDebug() << "Found element " << _name << endl;
+			return *it;
+		}
     }
 
-    kdDebug() << k_funcinfo << "no such element: " << _name << endl;
+	//if there is an error make m_error true.
+	m_error = true;
+
+    kdDebug() << k_funcinfo << "no such element, parsing error!: " << _name << endl;
     return NULL;
 }
--- branches/KDE/3.5/kdeedu/kalzium/src/moleculeparser.h #492530:492531
@@ -103,8 +103,6 @@
     bool  weight(QString         _moleculeString,
 				 double          *_resultMass,
 				 ElementCountMap *_resultMap);
-    //QMap<Element*, int>   elementMap();
-    //QValueList<Element*>  elementList();
 
  private:
     // Helper functions
@@ -117,6 +115,9 @@
 
     QMap<Element*, int> m_elementMap;
 
+	//if this booloean is "true" the parser found an error
+	bool m_error;
+
 protected:
 
     /**


More information about the Kalzium mailing list