summaryrefslogtreecommitdiffstats
path: root/src/datablocks/ingredient.cpp
blob: a17bedacbe41291d79c38cefec8cdf8a9daf2113 (plain)
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
/***************************************************************************
*   Copyright (C) 2003 by                                                 *
*   Unai Garro (ugarro@users.sourceforge.net)                             *
*   Cyril Bosselut (bosselut@b1project.com)                               *
*   Jason Kivlighn (jkivlighn@gmail.com)                                  *
*                                                                         *
*   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 "datablocks/ingredient.h"

#include <tqstringlist.h>

#include "mixednumber.h"

IngredientData::IngredientData() : amount( 0 ), amount_offset( 0 ), groupID( -1 )
{}

IngredientData::IngredientData( const TQString &_name, double _amount, const Unit &_units, int _unitID, int _ingredientID ) :
		ingredientID( _ingredientID ),
		name( _name ),
		amount( _amount ),
		amount_offset( 0 ),
		units( _units ),
		groupID( -1 )
{
units.id=_unitID;
}

//compare also using the group id because there may be the same ingredient in a list multiple times, but each in a different group
bool IngredientData::operator==( const IngredientData &ing ) const
{
	return ( ( ing.ingredientID == ingredientID ) && ( ing.groupID == groupID ) );
}

Ingredient::Ingredient() : IngredientData()
{}

Ingredient::Ingredient( const TQString &_name, double _amount, const Unit &_units, int _unitID, int _ingredientID ) :
	IngredientData(_name,_amount,_units,_unitID,_ingredientID)
{}

Ingredient::Ingredient( const IngredientData &d ) : IngredientData(d)
{}

void Ingredient::setAmount( const TQString &range, bool *ok )
{
	if ( range.isEmpty() ) {
		if ( ok ) *ok = true;
 		return;
	}

	TQStringList ranges = TQStringList::split('-',range);

	TQString amount_min = ranges[0];
	if ( amount_min.isEmpty() ) {
		if ( ok ) *ok = false;
		return;
	}

	TQString amount_max;
	switch ( ranges.count() ) {
		case 0:
		case 1: break;
		case 2: amount_max = ranges[1];
			break;
		default:
			if ( ok ) *ok = false;
			return;
	}

	MixedNumber nm_min = MixedNumber::fromString( amount_min, ok );
	if ( ok && *ok == false ) return;

	MixedNumber nm_max = MixedNumber::fromString( amount_max, ok );
	if ( ok && *ok == false ) return;

	amount = nm_min.toDouble();
	if ( nm_max > 0 )
		amount_offset = nm_max.toDouble() - amount;
}

//compare also using the group id because there may be the same ingredient in a list multiple times, but each in a different group
bool Ingredient::operator==( const Ingredient &ing ) const
{
	return ( ( ing.ingredientID == ingredientID ) && ( ing.groupID == groupID ) );
}

bool Ingredient::operator<( const Ingredient &ing ) const
{
	return ( TQString::localeAwareCompare( name, ing.name ) < 0 );
}