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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/***************************************************************************
* Copyright (C) 2003 by Unai Garro *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "propertycalculator.h"
#include <math.h> // For fabs()
#include <kdebug.h>
#include "backends/recipedb.h"
#include "datablocks/elementlist.h"
#include "datablocks/ingredientpropertylist.h"
#include "datablocks/recipe.h"
bool autoConvert( RecipeDB* database, const Ingredient &from, const Ingredient &to, Ingredient &result )
{
RecipeDB::ConversionStatus status = database->convertIngredientUnits( from, to.units, result );
bool converted = status == RecipeDB::Success || status == RecipeDB::MismatchedPrepMethodUsingApprox;
if ( converted ) // There is a ratio
{
double ratio = result.amount / from.amount;
if ( ratio > 1 ) // Convert to unit 1, since unit1 is bigger
{
result.units = from.units;
result.amount = from.amount + to.amount / ratio;
}
else { //Convert to unit2, since unit2 is bigger (just add, units are now correct)
result.amount += to.amount;
}
return true;
}
else
return false;
}
/*
** Version with database I/O. DB must be provided
*/
void calculateProperties( Recipe& recipe, RecipeDB* database )
{
recipe.properties.clear();
// Note that recipePropertyList is not attached to any ingredient. It's just the total of the recipe
IngredientPropertyList ingredientPropertyList; // property list for each ingredient
int ingredientNo = 1;
for ( IngredientList::const_iterator ing_it = recipe.ingList.begin(); ing_it != recipe.ingList.end(); ++ing_it ) {
database->loadProperties( &ingredientPropertyList, ( *ing_it ).ingredientID );
ingredientPropertyList.divide( recipe.yield.amount ); // calculates properties per yield unit
addPropertyToList( database, &recipe.properties, ingredientPropertyList, *ing_it, ingredientNo );
ingredientNo++;
}
}
void addPropertyToList( RecipeDB *database, IngredientPropertyList *recipePropertyList, IngredientPropertyList &ingPropertyList, const Ingredient &ing, int ingredientNo )
{
QMap<int,double> ratioCache; //unit->ratio
IngredientPropertyList::const_iterator prop_it;
for ( prop_it = ingPropertyList.begin(); prop_it != ingPropertyList.end(); ++prop_it ) {
// Find if property was listed before
int pos = recipePropertyList->findIndex( *prop_it );
if ( pos >= 0 ) //Exists. Add to it
{
IngredientPropertyList::iterator rec_property_it = recipePropertyList->at( pos );
Ingredient result;
bool converted;
QMap<int,double>::const_iterator cache_it = ratioCache.find((*prop_it).perUnit.id);
if ( cache_it == ratioCache.end() ) {
RecipeDB::ConversionStatus status = database->convertIngredientUnits( ing, (*prop_it).perUnit, result );
converted = status == RecipeDB::Success || status == RecipeDB::MismatchedPrepMethodUsingApprox;
if ( converted )
ratioCache.insert((*prop_it).perUnit.id,result.amount / ing.amount);
else
ratioCache.insert((*prop_it).perUnit.id,-1);
}
else {
result.units = (*prop_it).perUnit;
result.amount = ing.amount * (*cache_it);
converted = result.amount > 0;
}
if ( converted ) // Could convert units to perUnit
(*rec_property_it).amount += ( (*prop_it).amount ) * result.amount;
}
else // Append new property
{
IngredientProperty property;
property.id = (*prop_it).id;
property.name = (*prop_it).name;
property.perUnit.id = -1; // It's not per unit, it's total sum of the recipe
property.perUnit.name = QString::null; // "
property.units = (*prop_it).units;
Ingredient result;
bool converted;
QMap<int,double>::const_iterator cache_it = ratioCache.find((*prop_it).perUnit.id);
if ( cache_it == ratioCache.end() ) {
RecipeDB::ConversionStatus status = database->convertIngredientUnits( ing, (*prop_it).perUnit, result );
converted = status == RecipeDB::Success || status == RecipeDB::MismatchedPrepMethodUsingApprox;
if ( converted )
ratioCache.insert((*prop_it).perUnit.id,result.amount / ing.amount);
else
ratioCache.insert((*prop_it).perUnit.id,-1);
}
else {
result.units = (*prop_it).perUnit;
result.amount = ing.amount * (*cache_it);
converted = result.amount > 0;
}
if ( converted ) // Could convert units to perUnit
{
property.amount = ( (*prop_it).amount ) * result.amount;
recipePropertyList->append( property );
}
}
}
}
|