summaryrefslogtreecommitdiffstats
path: root/krecipes/src/dialogs/ingredientsdialog.cpp
blob: 86dce629cd1e353ffc15ee1481a41a0031f1b966 (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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/***************************************************************************
*   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 "ingredientsdialog.h"
#include "backends/recipedb.h"
#include "createelementdialog.h"
#include "datablocks/ingredientpropertylist.h"
#include "datablocks/mixednumber.h"
#include "datablocks/weight.h"
#include "unitsdialog.h"
#include "usdadatadialog.h"
#include "selectpropertydialog.h"
#include "selectunitdialog.h"
#include "dependanciesdialog.h"
#include "widgets/ingredientlistview.h"
#include "widgets/weightinput.h"
#include "dialogs/ingredientgroupsdialog.h"
#include "dialogs/createingredientweightdialog.h"

#include <tdeapplication.h>
#include <kcursor.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdeglobal.h>
#include <tdeconfig.h>

#include <ntqheader.h>
#include <ntqmessagebox.h>
#include <ntqtabwidget.h>

class WeightListItem : public TQListViewItem
{
public:
	WeightListItem( TQListView *listview, TQListViewItem *item, const Weight &w ) : TQListViewItem(listview,item), m_weight(w){}

	void setWeight( const Weight &w ) { m_weight = w; }
	Weight weight() const { return m_weight; }

	void setAmountUnit( double amount, const Unit &unit, const Element &prepMethod )
	{
		m_weight.perAmount = amount;
		m_weight.perAmountUnitID = unit.id;
		m_weight.perAmountUnit = (m_weight.perAmount>1)?unit.plural:unit.name;
		m_weight.prepMethodID = prepMethod.id;
		m_weight.prepMethod = prepMethod.name;
	}

	void setWeightUnit( double weight, const Unit &unit )
	{
		m_weight.weight = weight;
		m_weight.weightUnitID = unit.id;
		m_weight.weightUnit = (m_weight.weight>1)?unit.plural:unit.name;
	}

	virtual TQString text( int c ) const
	{
		if ( c == 0 )
			return TQString::number(m_weight.weight)+" "+m_weight.weightUnit;
		else if ( c == 1 )
			return TQString::number(m_weight.perAmount)+" "
			  +m_weight.perAmountUnit
			  +((m_weight.prepMethodID!=-1)?", "+m_weight.prepMethod:TQString::null);
		else
			return TQString::null;
	}

private:
	Weight m_weight;
};

IngredientsDialog::IngredientsDialog( TQWidget* parent, RecipeDB *db ) : TQWidget( parent )
{

	// Store pointer to database
	database = db;

	// Initialize internal variables
	propertiesList = new IngredientPropertyList;
	perUnitListBack = new ElementList;

	// Design dialog

	TQHBoxLayout* page_layout = new TQHBoxLayout( this, KDialog::marginHint(), KDialog::spacingHint() );

	TQTabWidget *tabWidget = new TQTabWidget( this );

	TQWidget *ingredientTab = new TQWidget( tabWidget );

	layout = new TQGridLayout( ingredientTab, 1, 1, 0, 0 );
	TQSpacerItem* spacer_left = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_left, 1, 0 );
	TQSpacerItem* spacer_top = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_top, 0, 1 );

	ingredientListView = new KreListView ( ingredientTab, i18n( "Ingredient list" ), true, 0 );
	StdIngredientListView *list_view = new StdIngredientListView( ingredientListView, database, true );
	ingredientListView->setListView( list_view );
	layout->addMultiCellWidget ( ingredientListView, 1, 5, 1, 1 );
	ingredientListView->setSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding ) );

	TQSpacerItem* spacer_rightIngredients = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_rightIngredients, 1, 2 );


	addIngredientButton = new TQPushButton( ingredientTab );
	addIngredientButton->setText( "+" );
	layout->addWidget( addIngredientButton, 1, 3 );
	addIngredientButton->setMinimumSize( TQSize( 30, 30 ) );
	addIngredientButton->setMaximumSize( TQSize( 30, 30 ) );
	addIngredientButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	addIngredientButton->setFlat( true );

	removeIngredientButton = new TQPushButton( ingredientTab );
	removeIngredientButton->setText( "-" );
	layout->addWidget( removeIngredientButton, 3, 3 );
	removeIngredientButton->setMinimumSize( TQSize( 30, 30 ) );
	removeIngredientButton->setMaximumSize( TQSize( 30, 30 ) );
	removeIngredientButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	removeIngredientButton->setFlat( true );

	TQSpacerItem* spacer_Ing_Buttons = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	layout->addItem( spacer_Ing_Buttons, 2, 3 );


	TQSpacerItem* spacer_Ing_Units = new TQSpacerItem( 30, 5, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	layout->addItem( spacer_Ing_Units, 1, 4 );


	TDEConfig *config = TDEGlobal::config();
	config->setGroup( "Advanced" );
	bool show_id = config->readBoolEntry( "ShowID", false );

	TQScrollView *scrollView1 = new TQScrollView( ingredientTab, "scrollView1" );
	scrollView1->enableClipper(true);
	TQWidget *rightWidget = new TQWidget(scrollView1);
	TQGridLayout *rightLayout = new TQGridLayout( rightWidget, 1, 1, 0, 0 );

	unitsListView = new KreListView ( rightWidget, i18n( "Unit list" ) );
	unitsListView->listView() ->addColumn( i18n( "Units" ) );
	unitsListView->listView() ->addColumn( i18n( "Id" ), show_id ? -1 : 0 );
	unitsListView->listView() ->setSorting( 0 );
	unitsListView->listView() ->setAllColumnsShowFocus( true );
	rightLayout->addMultiCellWidget ( unitsListView, 1, 4, 0, 0 );
	unitsListView->listView() ->setMinimumWidth( 150 );
	unitsListView->setSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding ) );

	TQSpacerItem* spacer_rightUnits = new TQSpacerItem( 5, 5, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	rightLayout->addItem( spacer_rightUnits, 1, 1 );

	addUnitButton = new TQPushButton( rightWidget );
	addUnitButton->setText( "+" );
	rightLayout->addWidget( addUnitButton, 1, 2 );
	addUnitButton->resize( TQSize( 30, 30 ) );
	addUnitButton->setMinimumSize( TQSize( 30, 30 ) );
	addUnitButton->setMaximumSize( TQSize( 30, 30 ) );
	addUnitButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	addUnitButton->setFlat( true );

	removeUnitButton = new TQPushButton( rightWidget );
	removeUnitButton->setText( "-" );
	rightLayout->addWidget( removeUnitButton, 3, 2 );
	removeUnitButton->resize( TQSize( 30, 30 ) );
	removeUnitButton->setMinimumSize( TQSize( 30, 30 ) );
	removeUnitButton->setMaximumSize( TQSize( 30, 30 ) );
	removeUnitButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	removeUnitButton->setFlat( true );
	TQSpacerItem* spacer_Units_Properties = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
 	rightLayout->addItem( spacer_Units_Properties, 2, 2 );


	propertiesListView = new KreListView ( rightWidget, i18n( "Ingredient Properties" ) );
	rightLayout->addMultiCellWidget ( propertiesListView, 6, 9, 0, 0 );

	propertiesListView->listView() ->addColumn( i18n( "Property" ) );
	propertiesListView->listView() ->addColumn( i18n( "Amount" ) );
	propertiesListView->listView() ->addColumn( i18n( "Units" ) );
	propertiesListView->listView() ->addColumn( i18n( "Id" ), show_id ? -1 : 0 );
	propertiesListView->listView() ->setAllColumnsShowFocus( true );
	propertiesListView->listView() ->setSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding ) );
	propertiesListView->listView() ->setSorting( -1 ); // Disable sorting. For the moment, the order is important to identify the per_units ID corresponding to this row. So the user shouldn't change this order.

	addPropertyButton = new TQPushButton( rightWidget );
	addPropertyButton->setText( "+" );
	rightLayout->addWidget( addPropertyButton, 6, 2 );
	addPropertyButton->resize( TQSize( 30, 30 ) );
	addPropertyButton->setMinimumSize( TQSize( 30, 30 ) );
	addPropertyButton->setMaximumSize( TQSize( 30, 30 ) );
	addPropertyButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	addPropertyButton->setFlat( true );

	removePropertyButton = new TQPushButton( rightWidget );
	removePropertyButton->setText( "-" );
	rightLayout->addWidget( removePropertyButton, 8, 2 );
	removePropertyButton->resize( TQSize( 30, 30 ) );
	removePropertyButton->setMinimumSize( TQSize( 30, 30 ) );
	removePropertyButton->setMaximumSize( TQSize( 30, 30 ) );
	removePropertyButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	removePropertyButton->setFlat( true );

	TQSpacerItem* spacer_Prop_Buttons = new TQSpacerItem( 9, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	rightLayout->addItem( spacer_Prop_Buttons, 7, 2 );

	weightsListView = new KreListView ( rightWidget, i18n( "Ingredient Weights" ) );
	weightsListView->listView() ->addColumn( i18n( "Weight" ) );
	weightsListView->listView() ->addColumn( i18n( "Per Amount" ) );
	weightsListView->listView() ->setAllColumnsShowFocus( true );
	rightLayout->addMultiCellWidget ( weightsListView, 10, 14, 0, 0 );
	weightsListView->listView() ->setMinimumWidth( 150 );
	weightsListView->setSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding ) );

	//TQSpacerItem* spacer_rightWeights = new TQSpacerItem( 5, 5, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
	//layout->addItem( spacer_rightWeights, 1, 6 );

	addWeightButton = new TQPushButton( rightWidget );
	addWeightButton->setText( "+" );
	rightLayout->addWidget( addWeightButton, 10, 2 );
	addWeightButton->resize( TQSize( 30, 30 ) );
	addWeightButton->setMinimumSize( TQSize( 30, 30 ) );
	addWeightButton->setMaximumSize( TQSize( 30, 30 ) );
	addWeightButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	addWeightButton->setFlat( true );

	removeWeightButton = new TQPushButton( rightWidget );
	removeWeightButton->setText( "-" );
	rightLayout->addWidget( removeWeightButton, 12, 2 );
	removeWeightButton->resize( TQSize( 30, 30 ) );
	removeWeightButton->setMinimumSize( TQSize( 30, 30 ) );
	removeWeightButton->setMaximumSize( TQSize( 30, 30 ) );
	removeWeightButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
	removeWeightButton->setFlat( true );

	TQSpacerItem* spacer_Weight_Properties = new TQSpacerItem( 10, 10, TQSizePolicy::Minimum, TQSizePolicy::Fixed );
	rightLayout->addItem( spacer_Weight_Properties, 11, 2 );

	TQSpacerItem* spacerBottom = new TQSpacerItem( 10, 10, TQSizePolicy::Fixed, TQSizePolicy::Expanding );
	rightLayout->addItem( spacerBottom, 13, 2 );

	scrollView1->setResizePolicy( TQScrollView::AutoOneFit );
	layout->addMultiCellWidget(scrollView1,1,4,5,5);

	TQPushButton *loadUsdaButton = new TQPushButton( ingredientTab );
	loadUsdaButton->setText( i18n( "Load USDA data" ) );
	loadUsdaButton->setFlat( true );
	layout->addWidget(loadUsdaButton,5,5);

	scrollView1->addChild(rightWidget);
	scrollView1->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Ignored );


	inputBox = new KDoubleNumInput( propertiesListView->listView() ->viewport() );
	propertiesListView->listView() ->addChild( inputBox );
	inputBox->hide();

	tabWidget->insertTab( ingredientTab, i18n( "Ingredients" ) );

	groupsDialog = new IngredientGroupsDialog(database,tabWidget,"groupsDialog");
	tabWidget->insertTab( groupsDialog, i18n( "Headers" ) );

	page_layout->addWidget( tabWidget );

	// Initialize
	unitList = new UnitList;

	// Signals & Slots
	connect( ingredientListView->listView(), TQ_SIGNAL( selectionChanged() ), this, TQ_SLOT( updateLists() ) );
	connect( addIngredientButton, TQ_SIGNAL( clicked() ), list_view, TQ_SLOT( createNew() ) );
	connect( addUnitButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( addUnitToIngredient() ) );
	connect( removeUnitButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( removeUnitFromIngredient() ) );
	connect( addWeightButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( addWeight() ) );
	connect( removeWeightButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( removeWeight() ) );
	connect( removeIngredientButton, TQ_SIGNAL( clicked() ), list_view, TQ_SLOT( remove
		         () ) );
	connect( addPropertyButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( addPropertyToIngredient() ) );
	connect( removePropertyButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( removePropertyFromIngredient() ) );
	connect( propertiesListView->listView(), TQ_SIGNAL( executed( TQListViewItem* ) ), this, TQ_SLOT( insertPropertyEditBox( TQListViewItem* ) ) );
	connect( propertiesListView->listView(), TQ_SIGNAL( selectionChanged() ), inputBox, TQ_SLOT( hide() ) );
	connect( inputBox, TQ_SIGNAL( valueChanged( double ) ), this, TQ_SLOT( setPropertyAmount( double ) ) );

	connect( weightsListView->listView(), TQ_SIGNAL( doubleClicked( TQListViewItem*, const TQPoint &, int ) ), TQ_SLOT( itemRenamed( TQListViewItem*, const TQPoint &, int ) ) );

	connect( loadUsdaButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( openUSDADialog() ) );
}


IngredientsDialog::~IngredientsDialog()
{
	delete unitList;
	delete perUnitListBack;
	delete propertiesList;
}

void IngredientsDialog::reloadIngredientList( ReloadFlags flag )
{
	( ( StdIngredientListView* ) ingredientListView->listView() ) ->reload(flag);

	// Reload Unit List
	updateLists();

}

void IngredientsDialog::reloadUnitList()
{

	int ingredientID = -1;
	// Find selected ingredient
	TQListViewItem *it;
	it = ingredientListView->listView() ->selectedItem();

	if ( it ) {  // Check if an ingredient is selected first
		ingredientID = it->text( 1 ).toInt();
	}


	unitList->clear();
	unitsListView->listView() ->clear();

	if ( ingredientID >= 0 ) {
		database->loadPossibleUnits( ingredientID, unitList );

		//Populate this data into the TDEListView

		for ( UnitList::const_iterator unit_it = unitList->begin(); unit_it != unitList->end(); ++unit_it ) {
			if ( !( *unit_it ).name.isEmpty() ) {
				( void ) new TQListViewItem( unitsListView->listView(), ( *unit_it ).name, TQString::number( ( *unit_it ).id ) );
			}
		}

		// Select the first unit
		unitsListView->listView() ->setSelected( unitsListView->listView() ->firstChild(), true );

	}
}

void IngredientsDialog::addWeight()
{
	TQListViewItem *it = ingredientListView->listView()->selectedItem();
	if ( it ) {
		CreateIngredientWeightDialog weightDialog( this, database );
		if ( weightDialog.exec() == TQDialog::Accepted ) {
			Weight w = weightDialog.weight();
			w.ingredientID = it->text( 1 ).toInt();
			database->addIngredientWeight( w );

			TQListViewItem * lastElement = weightsListView->listView()->lastItem();
	
			WeightListItem *weight_it = new WeightListItem( weightsListView->listView(), lastElement, w );
			weight_it->setAmountUnit( w.perAmount, database->unitName(w.perAmountUnitID),
			  Element(w.prepMethod,w.prepMethodID)
			);
			weight_it->setWeightUnit( w.weight, database->unitName(w.weightUnitID) );
		}
	}
}

void IngredientsDialog::removeWeight()
{
	TQListViewItem *it = weightsListView->listView() ->selectedItem();
	if ( it ) {
			switch ( KMessageBox::warningContinueCancel(this, i18n("Recipes may require this information for nutrient analysis. Are you sure you want to delete this entry?"), TQString::null, KStdGuiItem::cont(), "DeleteIngredientWeight") ) {
		case KMessageBox::Continue:
			database->removeIngredientWeight( ((WeightListItem*)it)->weight().id );
			delete it;
			break;
		default: break;
		}
	}
}

void IngredientsDialog::itemRenamed( TQListViewItem* item, const TQPoint &, int col )
{
	WeightListItem *weight_it = (WeightListItem*)item;
	Weight w = weight_it->weight();

	if ( col == 0 ) {
		KDialogBase amountEditDialog(this,"WeightAmountEdit",
		  false, i18n("Enter amount"), KDialogBase::Cancel | KDialogBase::Ok, KDialogBase::Ok);

		TQGroupBox *box = new TQGroupBox( 1, Horizontal, i18n("Amount"), &amountEditDialog );
		AmountUnitInput *amountEdit = new AmountUnitInput( box, database, Unit::Mass, MixedNumber::DecimalFormat );

		WeightListItem *it = (WeightListItem*)item;
		Weight w = it->weight();

		amountEdit->setAmount( w.weight );
		amountEdit->setUnit( Unit(w.weightUnit,w.weightUnit,w.weightUnitID) );

		amountEditDialog.setMainWidget(box);

		if ( amountEditDialog.exec() == TQDialog::Accepted ) {
			MixedNumber amount = amountEdit->amount();
			Unit unit = amountEdit->unit();

			it->setWeightUnit( amount.toDouble(), unit );
			database->addIngredientWeight( it->weight() );
		}
	}
	else if ( col == 1 ) {
		KDialogBase amountEditDialog(this,"PerAmountEdit",
		  false, i18n("Enter amount"), KDialogBase::Cancel | KDialogBase::Ok, KDialogBase::Ok);

		TQGroupBox *box = new TQGroupBox( 1, Horizontal, i18n("Amount"), &amountEditDialog );
		WeightInput *amountEdit = new WeightInput( box, database, Unit::All, MixedNumber::DecimalFormat );

		WeightListItem *it = (WeightListItem*)item;
		Weight w = it->weight();

		amountEdit->setAmount( w.perAmount );
		amountEdit->setUnit( Unit(w.perAmountUnit,w.perAmountUnit,w.perAmountUnitID) );
		amountEdit->setPrepMethod( Element(w.prepMethod,w.prepMethodID) );

		amountEditDialog.setMainWidget(box);

		if ( amountEditDialog.exec() == TQDialog::Accepted ) {
			MixedNumber amount = amountEdit->amount();
			Unit unit = amountEdit->unit();

			it->setAmountUnit( amount.toDouble(), unit, amountEdit->prepMethod() );
			database->addIngredientWeight( it->weight() );
		}
	}
}

void IngredientsDialog::addUnitToIngredient( void )
{

	// Find selected ingredient item
	TQListViewItem * it;
	int ingredientID = -1;
	if ( ( it = ingredientListView->listView() ->selectedItem() ) ) {
		ingredientID = it->text( 1 ).toInt();
	}
	if ( ingredientID >= 0 )  // an ingredient was selected previously
	{
		UnitList allUnits;
		database->loadUnits( &allUnits );

		SelectUnitDialog unitsDialog( this, allUnits, SelectUnitDialog::HideEmptyUnit );

		if ( unitsDialog.exec() == TQDialog::Accepted )
		{
			int unitID = unitsDialog.unitID();

			if ( !( database->ingredientContainsUnit( ingredientID, unitID ) ) )
				database->addUnitToIngredient( ingredientID, unitID ); // Add chosen unit to ingredient in database
			else {
				TQMessageBox::information( this, i18n( "Unit Exists" ), i18n( "The ingredient contains already the unit that you have chosen." ) );
			}
			reloadUnitList(); // Reload the list from database
		}
	}
}

void IngredientsDialog::removeUnitFromIngredient( void )
{

	// Find selected ingredient/unit item combination
	TQListViewItem * it;
	int ingredientID = -1, unitID = -1;
	if ( ( it = ingredientListView->listView() ->selectedItem() ) )
		ingredientID = it->text( 1 ).toInt();
	if ( ( it = unitsListView->listView() ->selectedItem() ) )
		unitID = it->text( 1 ).toInt();

	if ( ( ingredientID >= 0 ) && ( unitID >= 0 ) )  // an ingredient/unit combination was selected previously
	{
		ElementList dependingRecipes, dependingPropertiesInfo;

		database->findIngredientUnitDependancies( ingredientID, unitID, &dependingRecipes, &dependingPropertiesInfo );

		TQValueList<ListInfo> lists;
		if ( !dependingRecipes.isEmpty() ) {
			ListInfo info;
			info.list = dependingRecipes;
			info.name = i18n("Recipes");
			lists << info;
		}
		if ( !dependingPropertiesInfo.isEmpty() ) {
			ListInfo info;
			info.list = dependingPropertiesInfo;
			info.name = i18n("Properties");
			lists << info;
		}

		if ( lists.isEmpty() )
			database->removeUnitFromIngredient( ingredientID, unitID );
		else
		{ // must warn!
			DependanciesDialog warnDialog( this, lists );
			if ( !dependingRecipes.isEmpty() )
				warnDialog.setCustomWarning( i18n("You are about to permanantly delete recipes from your database.") );
			if ( warnDialog.exec() == TQDialog::Accepted )
				database->removeUnitFromIngredient( ingredientID, unitID );
		}
		reloadUnitList(); // Reload the list from database
		reloadPropertyList(); // Properties could have been removed if a unit is removed, so we need to reload.
	}
}

void IngredientsDialog:: reloadPropertyList( void )
{
	propertiesList->clear();
	propertiesListView->listView() ->clear();
	perUnitListBack->clear();

	inputBox->hide();


	//If none is selected, select first item
	TQListViewItem *it;
	it = ingredientListView->listView() ->selectedItem();

	//Populate this data into the TDEListView
	if ( it ) { // make sure that the ingredient list is not empty

		database->loadProperties( propertiesList, it->text( 1 ).toInt() ); // load the list for this ingredient
		for ( IngredientPropertyList::const_iterator prop_it = propertiesList->begin(); prop_it != propertiesList->end(); ++prop_it ) {
			TQListViewItem * lastElement = propertiesListView->listView() ->lastItem();
			//Insert property after the last one (it's important to keep the order in the case of the properties to be able to identify the per_units ID later on).
			( void ) new TQListViewItem( propertiesListView->listView(), lastElement, (*prop_it).name, TQString::number( (*prop_it).amount ), (*prop_it).units + TQString( "/" ) + (*prop_it).perUnit.name, TQString::number( (*prop_it).id ) );
			// Store the perUnits with the ID for using later
			Element perUnitEl;
			perUnitEl.id = (*prop_it).perUnit.id;
			perUnitEl.name = (*prop_it).perUnit.name;
			perUnitListBack->append( perUnitEl );

		}
	}
}

void IngredientsDialog::reloadWeightList( void )
{
	weightsListView->listView() ->clear();

	//If none is selected, select first item
	TQListViewItem *it = ingredientListView->listView() ->selectedItem();

	//Populate this data into the TDEListView
	if ( it ) { // make sure that the ingredient list is not empty
		WeightList list = database->ingredientWeightUnits( it->text( 1 ).toInt() ); // load the list for this ingredient
		for ( WeightList::const_iterator weight_it = list.begin(); weight_it != list.end(); ++weight_it ) {
			TQListViewItem * lastElement = weightsListView->listView() ->lastItem();

			Weight w = *weight_it;
			WeightListItem *weight_it1 = new WeightListItem( weightsListView->listView(), lastElement, w );
			weight_it1->setAmountUnit( w.perAmount,
			  database->unitName(w.perAmountUnitID),
			  Element((w.prepMethodID==-1)?TQString::null:database->prepMethodName(w.prepMethodID),w.prepMethodID)
			);
			weight_it1->setWeightUnit( w.weight, database->unitName(w.weightUnitID) );
		}
	}
}

void IngredientsDialog:: updateLists( void )
{
	reloadUnitList();
	reloadPropertyList();
	reloadWeightList();
}

void IngredientsDialog::addPropertyToIngredient( void )
{

	// Find selected ingredient item
	TQListViewItem * it;
	int ingredientID = -1;
	if ( ( it = ingredientListView->listView() ->selectedItem() ) ) {
		ingredientID = it->text( 1 ).toInt();
	}
	if ( ingredientID >= 0 )  // an ingredient was selected previously
	{
		IngredientPropertyList allProperties;
		database->loadProperties( &allProperties );
		UnitList unitList;
		database->loadPossibleUnits( ingredientID, &unitList );
		SelectPropertyDialog propertyDialog( this, &allProperties, &unitList, SelectPropertyDialog::HideEmptyUnit );

		if ( propertyDialog.exec() == TQDialog::Accepted )
		{

			int propertyID = propertyDialog.propertyID();
			int perUnitsID = propertyDialog.perUnitsID();
			if ( !( database->ingredientContainsProperty( ingredientID, propertyID, perUnitsID ) ) ) {
				if ( ( propertyID >= 0 ) && ( perUnitsID >= 0 ) )  // check if the property is not -1 ... (not selected)
					database->addPropertyToIngredient( ingredientID, propertyID, 0, perUnitsID ); // Add result chosen property to ingredient in database, with amount 0 by default
			}
			else {
				TQMessageBox::information( this, i18n( "Property Exists" ), i18n( "The property you tried to add already exists in the ingredient with the same per units." ) );
			}
			reloadPropertyList(); // Reload the list from database
		}
	}
}

void IngredientsDialog::removePropertyFromIngredient( void )
{

	// Find selected ingredient/property item combination
	TQListViewItem * it;
	int ingredientID = -1, propertyID = -1;
	int perUnitsID = -1;
	if ( ( it = ingredientListView->listView() ->selectedItem() ) )
		ingredientID = it->text( 1 ).toInt();
	if ( ( it = propertiesListView->listView() ->selectedItem() ) )
		propertyID = it->text( 3 ).toInt();
	if ( propertyID >= 0 )
		perUnitsID = perUnitListBack->getElement( findPropertyNo( it ) ).id ;

	if ( ( ingredientID >= 0 ) && ( propertyID >= 0 ) && ( perUnitsID >= 0 ) )  // an ingredient/property combination was selected previously
	{
		ElementList results;
		database->removePropertyFromIngredient( ingredientID, propertyID, perUnitsID );

		reloadPropertyList(); // Reload the list from database

	}
}

void IngredientsDialog::insertPropertyEditBox( TQListViewItem* it )
{
	TQRect r = propertiesListView->listView() ->header() ->sectionRect( 1 );

	r.moveBy( 0, propertiesListView->listView() ->itemRect( it ).y() ); //Move down to the item, note that its height is same as header's right now.

	r.setHeight( it->height() ); // Set the item's height

	inputBox->setGeometry( r );

	inputBox->setValue( it->text( 1 ).toDouble() );
	inputBox->show();
}

void IngredientsDialog::setPropertyAmount( double amount )
{
	TQListViewItem *ing_it = ingredientListView->listView() ->selectedItem(); // Find selected ingredient
	TQListViewItem *prop_it = propertiesListView->listView() ->selectedItem();

	if ( ing_it && prop_it ) // Appart from property, Check if an ingredient is selected first, just in case
	{
		prop_it->setText( 1, TQString::number( amount ) );
		int propertyID = prop_it->text( 3 ).toInt();
		int ingredientID = ing_it->text( 1 ).toInt();
		int per_units = perUnitListBack->getElement( findPropertyNo( prop_it ) ).id ;
		database->changePropertyAmountToIngredient( ingredientID, propertyID, amount, per_units );
	}
}

int IngredientsDialog::findPropertyNo( TQListViewItem * /*it*/ )
{
	bool found = false;
	int i = 0;
	TQListViewItem* item = propertiesListView->listView() ->firstChild();
	while ( i < propertiesListView->listView() ->childCount() && !found ) {
		if ( item == propertiesListView->listView() ->currentItem() )
			found = true;
		else {
			item = item->nextSibling();
			++i;
		}
	}
	if ( found ) {
		return ( i );
	}
	else {
		return ( -1 );
	}
}

void IngredientsDialog::reload( ReloadFlags flag )
{
	reloadIngredientList( flag );
	groupsDialog->reload( flag );
}

void IngredientsDialog::openUSDADialog( void )
{
	TQListViewItem * ing_it = ingredientListView->listView() ->selectedItem(); // Find selected ingredient
	if ( ing_it ) {
		TDEApplication::setOverrideCursor( KCursor::waitCursor() );
		USDADataDialog usda_dialog( Element( ing_it->text( 0 ), ing_it->text( 1 ).toInt() ), database, this );
		TDEApplication::restoreOverrideCursor();

		if ( usda_dialog.exec() == TQDialog::Accepted ) {
			reloadPropertyList(); //update property list upon success
			reloadWeightList();
		}
	}
	else
		TQMessageBox::information( this, TQString::null, i18n( "No ingredient selected." ) );
}

#include "ingredientsdialog.moc"