summaryrefslogtreecommitdiffstats
path: root/krecipes/src/recipefilter.cpp
blob: 9958e67496082b0204eee2c36258a0556b73a695 (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
/***************************************************************************
*   Copyright (C) 2004 by                                                 *
*   Jason Kivlighn (jkivlighn@gmail.com)                                  *
*   Unai Garro (ugarro@users.sourceforge.net)                             *
*                                                                         *
*   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 "recipefilter.h"

#include <kdebug.h>

#include "widgets/recipelistview.h"

RecipeFilter::RecipeFilter( KListView *klv ) : TQObject( klv ),
		listview( klv ),
		currentCategory( 0 )
{}

void RecipeFilter::filter( const TQString &s )
{
	//do this to only iterate over children of 'currentCategory'
	TQListViewItem * pEndItem = NULL;
	if ( currentCategory ) {
		TQListViewItem * pStartItem = currentCategory;
		do {
			if ( pStartItem->nextSibling() )
				pEndItem = pStartItem->nextSibling();
			else
				pStartItem = pStartItem->parent();
		}
		while ( pStartItem && !pEndItem );
	}

	//Re-show everything
	TQListViewItemIterator list_it;
	if ( currentCategory )
		list_it = TQListViewItemIterator( currentCategory );
	else
		list_it = TQListViewItemIterator( listview );
	while ( list_it.current() != pEndItem ) {
		list_it.current() ->setVisible( true );
		list_it++;
	}

	// Only filter if the filter text isn't empty
	if ( !s.isEmpty() ) {
		TQListViewItemIterator list_it( listview );
		while ( TQListViewItem * it = list_it.current() ) {
			if ( it->rtti() == 1000 )  // Its a recipe
			{
				RecipeListItem * recipe_it = ( RecipeListItem* ) it;

				if ( recipe_it->title().contains( s, false ) )
				{
					if ( currentCategory ) {
						if ( isParentOf( currentCategory, recipe_it ) )
							recipe_it->setVisible( true );
						else
							recipe_it->setVisible( false );
					}
					else
						recipe_it->setVisible( true );
				}
				else
					recipe_it->setVisible( false );
			}

			++list_it;
		}
		hideIfEmpty();
	}
}

void RecipeFilter::filterCategory( int categoryID )
{
	kdDebug() << "I got category :" << categoryID << "\n";

	if ( categoryID == -1 )
		currentCategory = 0;
	else {
		TQListViewItemIterator list_it( listview );
		while ( TQListViewItem * it = list_it.current() ) {
			if ( it->rtti() == 1001 ) {
				CategoryListItem * cat_it = ( CategoryListItem* ) it;
				if ( cat_it->categoryId() == categoryID ) {
					currentCategory = cat_it;
					break;
				}
			}

			++list_it;
		}
	}

	TQListViewItemIterator list_it( listview );
	while ( TQListViewItem * it = list_it.current() ) {
		if ( categoryID == -1 )
			it->setVisible( true ); // We're not filtering categories
		else if ( it == currentCategory || isParentOf( it, currentCategory ) || isParentOf( currentCategory, it ) )
			it->setVisible( true );
		else
			it->setVisible( false );

		++list_it;
	}

	if ( currentCategory )
		currentCategory->setOpen( true );
}

bool RecipeFilter::hideIfEmpty( TQListViewItem *parent )
{
	TQListViewItem * it;
	if ( parent == 0 )
		it = listview->firstChild();
	else
		it = parent->firstChild();

	bool parent_should_show = false;
	for ( ; it; it = it->nextSibling() ) {
		if ( (it->rtti() == 1000 && it->isVisible()) || (it->rtti() == NEXTLISTITEM_RTTI || it->rtti() == PREVLISTITEM_RTTI) ) {
			parent_should_show = true;
		}
		else {
			bool result = hideIfEmpty( it );
			if ( parent_should_show == false )
				parent_should_show = result;
		}
	}

	if ( parent && parent->rtti() != 1000 ) {
		if ( parent_should_show )
			parent->setOpen( true );
		parent->setVisible( parent_should_show );
	}
	return parent_should_show;
}

bool RecipeFilter::isParentOf( TQListViewItem *parent, TQListViewItem *to_check )
{
	for ( TQListViewItem * it = to_check->parent(); it; it = it->parent() ) {
		if ( it == parent )
			return true;
	}

	return false;
}

#include "recipefilter.moc"