summaryrefslogtreecommitdiffstats
path: root/src/dialogs/selectpropertydialog.cpp
blob: 1ce1887243b20ac8b68f0d1cb330021c8224e3fb (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
/***************************************************************************
*   Copyright (C) 2003 by                                                 *
*   Unai Garro ([email protected])                             *
*   Cyril Bosselut ([email protected])                               *
*   Jason Kivlighn ([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 "selectpropertydialog.h"

#include <tdeconfig.h>
#include <tdeglobal.h>
#include <tdelocale.h>

#include "datablocks/ingredientpropertylist.h"

SelectPropertyDialog::SelectPropertyDialog( TQWidget* parent, IngredientPropertyList *propertyList, UnitList *unitList, OptionFlag showEmpty )
		: KDialogBase( parent, "SelectPropertyDialog", true, i18n( "Choose Property" ),
		    KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok ), m_showEmpty(showEmpty)
{

	// Initialize internal variables
	unitListBack = new UnitList;

	// Initialize Widgets
	TQVBox *page = makeVBoxMainWidget();

	box = new TQGroupBox( page );
	box->setTitle( i18n( "Choose Property" ) );
	box->setColumnLayout( 0, TQt::Vertical );
	box->layout() ->setSpacing( 6 );
	box->layout() ->setMargin( 11 );
	TQVBoxLayout *boxLayout = new TQVBoxLayout( box->layout() );
	boxLayout->setAlignment( TQt::AlignTop );

	propertyChooseView = new TDEListView( box, "propertyChooseView" );

	TDEConfig *config = TDEGlobal::config();
	config->setGroup( "Advanced" );
	bool show_id = config->readBoolEntry( "ShowID", false );
	propertyChooseView->addColumn( i18n( "Id" ), show_id ? -1 : 0 );

	propertyChooseView->addColumn( i18n( "Property" ) );
	propertyChooseView->setAllColumnsShowFocus( true );
	boxLayout->addWidget( propertyChooseView );

	TQHBoxLayout *layout2 = new TQHBoxLayout;

	perUnitsLabel = new TQLabel( box );
	perUnitsLabel->setGeometry( TQRect( 5, 285, 100, 30 ) );
	perUnitsLabel->setText( i18n( "Per units:" ) );
	layout2->addWidget( perUnitsLabel );

	perUnitsBox = new KComboBox( FALSE, box );
	layout2->addWidget( perUnitsBox );
	boxLayout->addLayout( layout2 );

	resize( TQSize( 200, 380 ).expandedTo( minimumSizeHint() ) );
	clearWState( WState_Polished );

	// Load data
	loadProperties( propertyList );
	loadUnits( unitList );
}


SelectPropertyDialog::~SelectPropertyDialog()
{}


int SelectPropertyDialog::propertyID( void )
{

	TQListViewItem * it;
	if ( ( it = propertyChooseView->selectedItem() ) ) {
		return ( it->text( 0 ).toInt() );
	}
	else
		return ( -1 );
}

int SelectPropertyDialog::perUnitsID()
{

	int comboCount = perUnitsBox->count();
	if ( comboCount > 0 ) { // If not, the list may be empty (no list defined) and crashes while reading as seen before. So check just in case.
		int comboID = perUnitsBox->currentItem();
		return ( *unitListBack->at( comboID ) ).id;
	}
	else
		return ( -1 );
}

void SelectPropertyDialog::loadProperties( IngredientPropertyList *propertyList )
{
	for ( IngredientPropertyList::const_iterator prop_it = propertyList->begin(); prop_it != propertyList->end(); ++prop_it ) {
		( void ) new TQListViewItem( propertyChooseView, TQString::number( (*prop_it).id ), (*prop_it).name );
	}
}
void SelectPropertyDialog::loadUnits( UnitList *unitList )
{
	for ( UnitList::const_iterator unit_it = unitList->begin(); unit_it != unitList->end(); ++unit_it ) {
		TQString unitName = ( *unit_it ).name;
		if ( unitName.isEmpty() ) {
			if ( m_showEmpty == ShowEmptyUnit )
				unitName = " "+i18n("-No unit-");
			else
				continue;
		}

		// Insert in the combobox
		perUnitsBox->insertItem( unitName );

		// Store with index for using later
		Unit newUnit( *unit_it );
		newUnit.name = unitName;
		unitListBack->append( newUnit );
	}
}