summaryrefslogtreecommitdiffstats
path: root/krecipes/src/widgets/propertylistview.cpp
blob: 29a3321ee535ab76d3c245b0a3a8ea2b8e47c151 (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
/***************************************************************************
*   Copyright (C) 2004 by                                                 *
*   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 "propertylistview.h"

#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdepopupmenu.h>
#include <kdebug.h>

#include "backends/recipedb.h"
#include "dialogs/createpropertydialog.h"

PropertyCheckListItem::PropertyCheckListItem( TQListView* klv, const IngredientProperty &property ) : TQCheckListItem( klv, TQString::null, TQCheckListItem::CheckBox ),
		m_property( property )
{
	//setOn( false ); // Set unchecked by default
}

PropertyCheckListItem::PropertyCheckListItem( TQListViewItem* it, const IngredientProperty &property ) : TQCheckListItem( it, TQString::null, TQCheckListItem::CheckBox ),
		m_property( property )
{
	//setOn( false ); // Set unchecked by default
}

TQString PropertyCheckListItem::text( int column ) const
{
	switch ( column ) {
	case 0:
		return m_property.name;
		break;
	case 1:
		return m_property.units;
		break;
	case 2:
		return TQString::number( m_property.id );
		break;

	}

	return TQString::null;
}


HidePropertyCheckListItem::HidePropertyCheckListItem( TQListView* klv, const IngredientProperty &property, bool enable ) : PropertyCheckListItem( klv, property )
{
	m_holdSettings = true;
	setOn( enable ); // Set checked by default
	m_holdSettings = false;
}

HidePropertyCheckListItem::HidePropertyCheckListItem( TQListViewItem* it, const IngredientProperty &property, bool enable ) : PropertyCheckListItem( it, property )
{
	m_holdSettings = true;
	setOn( enable ); // Set checked by default
	m_holdSettings = false;
}

void HidePropertyCheckListItem::stateChange( bool on )
{
	if ( !m_holdSettings ) {
		TDEConfig *config = TDEGlobal::config();
		config->setGroup("Formatting");
	
		config->sync();
		TQStringList hiddenList = config->readListEntry("HiddenProperties");
		if ( on )
			hiddenList.remove(m_property.name);
		else if ( !hiddenList.contains(m_property.name) )
			hiddenList.append(m_property.name);
	
		config->writeEntry("HiddenProperties",hiddenList);
	}
}

PropertyListView::PropertyListView( TQWidget *parent, RecipeDB *db ) : TDEListView( parent ),
		database( db )
{
	setAllColumnsShowFocus( true );
	setDefaultRenameAction( TQListView::Reject );

	connect( db, TQ_SIGNAL( propertyCreated( const IngredientProperty & ) ), TQ_SLOT( createProperty( const IngredientProperty & ) ) );	
	connect( db, TQ_SIGNAL( propertyRemoved( int ) ), TQ_SLOT( removeProperty( int ) ) );
}

void PropertyListView::reload()
{
	clear(); // Clear the view

	m_loading = true;

	IngredientPropertyList propertyList;
	database->loadProperties( &propertyList );

	//Populate this data into the TDEListView
	IngredientPropertyList::const_iterator prop_it;
	for ( prop_it = propertyList.begin(); prop_it != propertyList.end(); ++prop_it )
		createProperty( *prop_it );

	m_loading = false;
}



StdPropertyListView::StdPropertyListView( TQWidget *parent, RecipeDB *db, bool editable ) : PropertyListView( parent, db )
{
	addColumn( i18n( "Property" ) );
	addColumn( i18n( "Units" ) );

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

	setSorting( 0 );

	if ( editable ) {
		setRenameable( 0, true );

		TDEIconLoader *il = new TDEIconLoader;

		kpop = new TDEPopupMenu( this );
		kpop->insertItem( il->loadIcon( "document-new", TDEIcon::NoGroup, 16 ), i18n( "&Create" ), this, TQ_SLOT( createNew() ), CTRL + Key_C );
		kpop->insertItem( il->loadIcon( "edit-delete", TDEIcon::NoGroup, 16 ), i18n( "&Delete" ), this, TQ_SLOT( remove
			                  () ), Key_Delete );
		kpop->insertItem( il->loadIcon( "edit", TDEIcon::NoGroup, 16 ), i18n( "&Rename" ), this, TQ_SLOT( rename() ), CTRL + Key_R );
		kpop->polish();

		delete il;

		connect( this, TQ_SIGNAL( contextMenu( TDEListView *, TQListViewItem *, const TQPoint & ) ), TQ_SLOT( showPopup( TDEListView *, TQListViewItem *, const TQPoint & ) ) );
		connect( this, TQ_SIGNAL( doubleClicked( TQListViewItem* ) ), this, TQ_SLOT( modProperty( TQListViewItem* ) ) );
		connect( this, TQ_SIGNAL( itemRenamed( TQListViewItem* ) ), this, TQ_SLOT( saveProperty( TQListViewItem* ) ) );
	}
}

void StdPropertyListView::showPopup( TDEListView * /*l*/, TQListViewItem *i, const TQPoint &p )
{
	if ( i )
		kpop->exec( p );
}

void StdPropertyListView::createNew()
{
	UnitList list;
	database->loadUnits( &list );
	CreatePropertyDialog* propertyDialog = new CreatePropertyDialog( this, &list );

	if ( propertyDialog->exec() == TQDialog::Accepted ) {
		TQString name = propertyDialog->newPropertyName();
		TQString units = propertyDialog->newUnitsName();
		if ( !( ( name.isEmpty() ) || ( units.isEmpty() ) ) )  // Make sure none of the fields are empty
		{
			//check bounds first
			if ( checkBounds( name ) )
				database->addProperty( name, units );
		}
	}
	delete propertyDialog;
}

void StdPropertyListView::remove
	()
{
	TQListViewItem * item = currentItem();

	if ( item ) {
		switch ( KMessageBox::warningContinueCancel( this, i18n( "Are you sure you want to delete this property?" ) ) ) {
		case KMessageBox::Continue:
			database->removeProperty( item->text( 2 ).toInt() );
			break;
		default:
			break;
		}
	}
}

void StdPropertyListView::rename()
{
	TQListViewItem * item = currentItem();

	if ( item )
		PropertyListView::rename( item, 0 );
}

void StdPropertyListView::removeProperty( int id )
{
	TQListViewItem * item = findItem( TQString::number( id ), 2 );

	Q_ASSERT( item );

	delete item;
}

void StdPropertyListView::createProperty( const IngredientProperty &property )
{
	( void ) new TQListViewItem( this, property.name, property.units, TQString::number( property.id ) );
}

void StdPropertyListView::modProperty( TQListViewItem* i )
{
	if ( i )
		PropertyListView::rename( i, 0 );
}

void StdPropertyListView::saveProperty( TQListViewItem* i )
{
	if ( !checkBounds( i->text( 0 ) ) ) {
		reload(); //reset the changed text
		return ;
	}
kdDebug() << "saveProp: " << i->text( 0 ) << endl;
	int existing_id = database->findExistingPropertyByName( i->text( 0 ) );
	int prop_id = i->text( 2 ).toInt();
	if ( existing_id != -1 && existing_id != prop_id )  //category already exists with this label... merge the two
	{
		switch ( KMessageBox::warningContinueCancel( this, i18n( "This property already exists.  Continuing will merge these two properties into one.  Are you sure?" ) ) )
		{
		case KMessageBox::Continue: {
				database->mergeProperties( existing_id, prop_id );
				break;
			}
		default:
			reload();
			break;
		}
	}
	else
		database->modProperty( prop_id, i->text( 0 ) );
}

bool StdPropertyListView::checkBounds( const TQString &name )
{
	if ( name.length() > database->maxPropertyNameLength() ) {
		KMessageBox::error( this, TQString( i18n( "Property name cannot be longer than %1 characters." ) ).arg( database->maxPropertyNameLength() ) );
		return false;
	}

	return true;
}



PropertyConstraintListView::PropertyConstraintListView( TQWidget *parent, RecipeDB *db ) : PropertyListView( parent, db )
{
	addColumn( i18n( "Enabled" ) );
	addColumn( i18n( "Property" ) );
	addColumn( i18n( "Min. Value" ) );
	addColumn( i18n( "Max. Value" ) );
	addColumn( "Id", 0 ); //hidden, only for internal purposes

	setRenameable( 0, true );
}

void PropertyConstraintListView::removeProperty( int id )
{
	TQListViewItem * item = findItem( TQString::number( id ), 4 );

	Q_ASSERT( item );

	delete item;
}

void PropertyConstraintListView::createProperty( const IngredientProperty &property )
{
	( void ) new ConstraintsListItem( this, property );
}


CheckPropertyListView::CheckPropertyListView( TQWidget *parent, RecipeDB *db, bool editable ) : StdPropertyListView( parent, db, editable )
{
}

void CheckPropertyListView::createProperty( const IngredientProperty &property )
{
	( void ) new HidePropertyCheckListItem( this, property, (m_loading)?false:true );
}

#include "propertylistview.moc"