Main | License | Installation | FAQ | Screenshots | Contact | Links | Sf.net | Freshmeat.net | KDE-Apps.org
Requirements | Design | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

ingredientViewItem.cpp

Go to the documentation of this file.
00001 /* Copyright (C) 2005 Jan Wedekind.
00002    This file is part of the recipe database application AnyMeal.
00003 
00004    AnyMeal is free software; you can redistribute it and/or modify it under
00005    the terms of the GNU GENERAL PUBLIC LICENSE as published by the Free
00006    Software Foundation; either version 3 of the License, or (at your option)
00007    any later version.
00008 
00009    AnyMeal is distributed in the hope that it will be useful, but WITHOUT ANY
00010    WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
00011    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
00012    details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
00016 #include <cassert>
00017 #ifndef NDEBUG
00018 #include <iostream>
00019 #endif
00020 #include <stdlib.h>
00021 #include "ingredientViewItem.hpp"
00022 #include "utils.hpp"
00023 
00024 using namespace std;
00025 
00026 IngredientViewItem::IngredientViewItem( QListViewItem *parent,
00027                                         const QString &_amount1,
00028                                         const QString &_amount2,
00029                                         const QString &_amount3,
00030                                         const QString &_unit,
00031                                         const QString &_name,
00032                                         const QString &_prep ):
00033   QListViewItem( parent, "", _unit, _name, _prep )
00034 {
00035   if ( !_amount1.isEmpty() )
00036     setAmount( atof( _amount1 ) );
00037   else
00038     setAmount( atoi( _amount2 ), atoi( _amount3 ) );
00039 }
00040 
00041 IngredientViewItem::IngredientViewItem( QListViewItem *parent,
00042                                         QListViewItem *after,
00043                                         const QString &_amount1,
00044                                         const QString &_amount2,
00045                                         const QString &_amount3,
00046                                         const QString &_unit,
00047                                         const QString &_name,
00048                                         const QString &_prep ):
00049   QListViewItem( parent, after, "", _unit, _name, _prep )
00050 {
00051   if ( !_amount1.isEmpty() )
00052     setAmount( atof( _amount1 ) );
00053   else
00054     setAmount( atoi( _amount2 ), atoi( _amount3 ) );
00055 }
00056 
00057 void IngredientViewItem::setAmount( double _amountDouble )
00058 {
00059   assert( _amountDouble >= 0.0 );
00060 
00061   amountDouble = _amountDouble;
00062   amountNominator = 0;
00063   amountDenominator = 0;
00064 
00065   if ( amountDouble != 0.0 )
00066     setText( 0, QString( "%1" ).arg( amountDouble ) );
00067   else
00068     setText( 0, "" );
00069 }
00070 
00071 void IngredientViewItem::setAmount( int _amountNominator,
00072                                     int _amountDenominator )
00073 {
00074   assert( _amountNominator >= 0 );
00075   assert( _amountDenominator >= 0 );
00076 
00077   amountDouble = 0.0;
00078   amountNominator = _amountNominator;
00079   amountDenominator = _amountDenominator;
00080 
00081   if ( amountDenominator == 0 ) amountDenominator = 1;
00082 
00083   if ( amountNominator > 0 ) {
00084 
00085     int greatestCommonDivisor = gcd( amountNominator, amountDenominator );
00086     amountNominator /= greatestCommonDivisor;
00087     amountDenominator /= greatestCommonDivisor;
00088 
00089     if ( amountDenominator > 1 ) {
00090       if ( amountNominator >= amountDenominator )
00091         setText( 0, QString( "%1 %2/%3" ).
00092                  arg( amountNominator / amountDenominator ).
00093                  arg( amountNominator % amountDenominator ).
00094                  arg( amountDenominator ) );
00095       else
00096         setText( 0, QString( "%1/%2" ).
00097                  arg( amountNominator ).
00098                  arg( amountDenominator ) );
00099     } else
00100       setText( 0, QString( "%1" ).arg( amountNominator ) );
00101   
00102   } else
00103     setText( 0, "" );
00104 }
00105 
00106 void IngredientViewItem::setFraction( bool _fraction )
00107 {
00108   if ( _fraction != isFraction() ) {
00109     if ( _fraction ) {
00110       setAmount( (int)( amountDouble * 1000 ), 1000 );
00111     } else {
00112       assert( amountDenominator > 0 );
00113       setAmount( (double)amountNominator / amountDenominator );
00114     };
00115   };
00116 }
00117 
00118 int IngredientViewItem::getAmountNumber(void) const
00119 {
00120   assert( amountDenominator > 0 );
00121   return amountNominator / amountDenominator;
00122 }
00123 
00124 int IngredientViewItem::getAmountNominator(void) const
00125 {
00126   assert( amountDenominator > 0 );
00127   return amountNominator % amountDenominator;
00128 }


anymeal 0.30 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Sep 14 16:01:26 2008 - GNU Free Documentation License