void MXPImporter::loadIngredients( QTextStream &stream, Recipe &recipe ) { //============ingredients=================// stream.skipWhiteSpace(); ( void ) stream.readLine(); QString current = stream.readLine(); if ( !current.contains( "NONE" ) && !current.isEmpty() ) { while ( !current.isEmpty() && !stream.atEnd() ) { Ingredient new_ingredient; //amount QString amount_str = current.mid( 0, 9 ).simplified(); if ( !amount_str.isEmpty() ) // case of amount_str.isEmpty() correctly handled by class default { MixedNumber amount; QValidator::State state; state = MixedNumber::fromString( amount_str, amount, false ); if ( state != QValidator::Acceptable ) { addWarningMsg( i18n( "While loading recipe \"%1\" Invalid amount \"%2\" in the line \"%3\"" , recipe.title, amount_str , current.trimmed() ) ); current = stream.readLine(); continue; } new_ingredient.amount = amount.toDouble(); } //units QString units( current.mid( 9, 13 ) ); new_ingredient.units = Unit( units.simplified(), new_ingredient.amount ); //name int dash_index = current.indexOf( "--" ); int length; if ( dash_index == -1 || dash_index == 24 ) //ignore a dash in the first position (index 24) length = current.length(); else length = dash_index - 22; QString ingredient_name( current.mid( 22, length ) ); new_ingredient.name = ingredient_name.trimmed(); //prep method if ( dash_index != -1 && dash_index != 24 ) //ignore a dash in the first position (index 24) new_ingredient.prepMethodList.append( Element(current.mid( dash_index + 2, current.length() ).trimmed()) ); recipe.ingList.append( new_ingredient ); //kDebug()<<"Found ingredient: amount="<<new_ingredient.amount // <<", unit:"<<new_ingredient.units // <<", name:"<<new_ingredient.name // <<", prep_method:"<<prep_method; current = stream.readLine(); } } //else // kDebug()<<"No ingredients found."; }
void RecipeMLImporter::readRecipemlQty( const QDomElement &qty, Ingredient &ing ) { QDomNodeList qtyChilds = qty.childNodes(); for ( int i = 0; i < qtyChilds.count(); i++ ) { QDomElement qtyChild = qtyChilds.item( i ).toElement(); QString tagName = qtyChild.tagName(); if ( tagName == "range" ) readRecipemlRange( qtyChild, ing.amount, ing.amount_offset ); else if ( tagName.isEmpty() ) { MixedNumber number; MixedNumber::fromString( qty.text(), number ); ing.amount = number.toDouble(); } else kDebug() << "Unknown tag within <qty>: " << tagName ; } }
void AmountUnitInput::setAmount( const MixedNumber &amount ) { amountInput->disconnect( this ); if ( amount.toDouble() < 0 ) amountInput->clear(); else amountInput->setValue( amount, 0 ); connect( amountInput, SIGNAL(valueChanged(const MixedNumber &)), SLOT(emitValueChanged()) ); }
void RecipeMLImporter::readRecipemlRange( const QDomElement& range, double &amount, double &amount_offset ) { QDomNodeList rangeChilds = range.childNodes(); double q1 = 1, q2 = 0; // default quantity assumed by RecipeML DTD MixedNumber number; for ( int j = 0; j < rangeChilds.count(); j++ ) { QDomElement rangeChild = rangeChilds.item( j ).toElement(); QString subTagName = rangeChild.tagName(); if ( subTagName == "q1" ) { MixedNumber::fromString( rangeChild.text(), number ); q1 = number.toDouble(); } else if ( subTagName == "q2" ) { MixedNumber::fromString( rangeChild.text(), number ); q2 = number.toDouble(); } else kDebug() << "Unknown tag within <range>: " << subTagName ; } amount = q1; amount_offset = q2-q1; }
void IngredientMatcherDialog::itemRenamed( Q3ListViewItem* item, const QPoint &, int col ) { if ( col == 1 ) { QPointer<KDialog> amountEditDialog = new KDialog(this); amountEditDialog->setCaption(i18nc("@title:window", "Enter amount")); amountEditDialog->setButtons(KDialog::Cancel | KDialog::Ok); amountEditDialog->setDefaultButton(KDialog::Ok); amountEditDialog->setModal( false ); Q3GroupBox *box = new Q3GroupBox( 1, Qt::Horizontal, i18nc("@title:group", "Amount"), amountEditDialog ); AmountUnitInput *amountEdit = new AmountUnitInput( box, database ); // Set the values from the item if ( !item->text(1).isEmpty() ) { MixedNumber number; MixedNumber::fromString( item->text(2), number ); amountEdit->setAmount( number ); Unit u; u.setId(item->text(3).toInt()); amountEdit->setUnit( u ); } else { amountEdit->setAmount( MixedNumber(-1) ); Unit u; u.setId(-1); amountEdit->setUnit( u ); } amountEditDialog->setMainWidget(box); amountEditDialog->adjustSize(); amountEditDialog->resize( 400, amountEditDialog->size().height() ); amountEditDialog->setFixedHeight( amountEditDialog->size().height() ); if ( amountEditDialog->exec() == QDialog::Accepted ) { MixedNumber amount = amountEdit->amount(); Unit unit = amountEdit->unit(); if ( amount.toDouble() <= 1e-5 ) { amount = MixedNumber(-1); unit.setId(-1); item->setText(1,QString()); } else { item->setText(1,amount.toString()+' '+unit.determineName(amount.toDouble(), /*useAbbrev=*/false)); } item->setText(2,amount.toString()); item->setText(3,QString::number(unit.id())); IngredientList::iterator ing_it = m_item_ing_map[item]; (*ing_it).amount = amount.toDouble(); (*ing_it).units = unit; } delete amountEditDialog; } }
void MX2Importer::readRecipe( const QDomNodeList& l, Recipe *recipe ) { for ( int i = 0; i < l.count(); i++ ) { QDomElement el = l.item( i ).toElement(); QString tagName = el.tagName(); if ( tagName == "Serv" ) { recipe->yield.setAmount(el.attribute( "qty" ).toInt()); recipe->yield.setType(i18n("servings")); } else if ( tagName == "PrpT" ) recipe->prepTime = QTime::fromString( el.attribute( "elapsed" ), "h:mm" ); else if ( tagName == "CatS" ) { QDomNodeList categories = el.childNodes(); for ( int j = 0; j < categories.count(); j++ ) { QDomElement c = categories.item( j ).toElement(); if ( c.tagName() == "CatT" ) { if ( c.text().length() > 0 ) { Element cat( c.text().trimmed() ); recipe->categoryList.append( cat ); } } } } else if ( tagName == "IngR" ) { double quantity1=0, quantity2=0, offset=0; QStringList qtyStrList = el.attribute( "qty" ).split( ' ' ); if ( !qtyStrList.isEmpty() ) { QValidator::State state; MixedNumber number; state = MixedNumber::fromString( qtyStrList.first(), number, false ); if ( state != QValidator::Acceptable ) quantity1 = 0; else quantity1 = number.toDouble(); state = MixedNumber::fromString( qtyStrList.last(), number, false ); if ( state != QValidator::Acceptable ) quantity2 = 0; else quantity2 = number.toDouble(); } offset = quantity2 - quantity1; Ingredient new_ing( el.attribute( "name" ), quantity1, Unit( el.attribute( "unit" ), quantity1 ) ); new_ing.amount_offset = offset; if ( el.hasChildNodes() ) { QDomNodeList iChilds = el.childNodes(); for ( int j = 0; j < iChilds.count(); j++ ) { QDomElement iChild = iChilds.item( j ).toElement(); if ( iChild.tagName() == "IPrp" ) { QString prepMethodStr = iChild.text().trimmed(); if ( !prepMethodStr.isEmpty() ) new_ing.prepMethodList.append( Element( prepMethodStr ) ); } else if ( iChild.tagName() == "INtI" ) { ; // TODO: What does it mean?... ingredient nutrient info? } } } recipe->ingList.append( new_ing ); } else if ( tagName == "DirS" ) { QStringList directions; QDomNodeList dirs = el.childNodes(); for ( int j = 0; j < dirs.count(); j++ ) { QDomElement dir = dirs.item( j ).toElement(); if ( dir.tagName() == "DirT" ) directions.append( dir.text().trimmed() ); } QString directionsText; // TODO This is copied from RecipeML, maybe a QStringList // for directions in Recipe instead? if ( directions.count() > 1 ) { for ( int i = 1; i <= directions.count(); i++ ) { if ( i != 1 ) { directionsText += "\n\n"; } QString sWith = QString( "%1. " ).arg( i ); QString text = directions[ i - 1 ]; if ( !text.trimmed().startsWith( sWith ) ) directionsText += sWith; directionsText += text; } } else directionsText = directions[ 0 ]; recipe->instructions = directionsText; } else if ( tagName == "SrvI" ) { // Don't know what to do with it, for now add it to directions // btw lets hope this is read after the directions recipe->instructions += "\n\n" + el.text().trimmed(); } else if ( tagName == "Note" ) { // Don't know what to do with it, for now add it to directions // btw lets hope this is read after the directions recipe->instructions += "\n\n" + el.text().trimmed(); } else if ( tagName == "Nutr" ) { //example: <Nutr>Per Serving (excluding unknown items): 51 Calories; 6g Fat (99.5% calories from fat); trace Protein; trace Carbohydrate; 0g Dietary Fiber; 16mg Cholesterol; 137mg Sodium. Exchanges: 1 Fat.</Nutr> // Don't know what to do with it, for now add it to directions // btw lets hope this is read after the directions recipe->instructions += "\n\n" + el.text().trimmed(); } /* tags to check for (example follows: <Srce>SARA'S SECRETS with Sara Moulton - (Show # SS-1B43) - from the TV FOOD NETWORK</Srce> <AltS label="Formatted for MC7" source="07-11-2003 by Joe Comiskey - Mad's Recipe Emporium"/> */ // TODO Have i missed some tag? } }