diff options
author | Michele Calgaro <[email protected]> | 2024-10-13 11:56:14 +0900 |
---|---|---|
committer | Michele Calgaro <[email protected]> | 2024-10-29 21:58:42 +0900 |
commit | 2879ff70be9271550477982a1a6371714db38562 (patch) | |
tree | c2054149dba923ab080fe7093432c7663a990111 /src/datablocks/categorytree.cpp | |
parent | 3eb38d2556f676d1027746f20bf12a1dd74451ef (diff) | |
download | krecipes-2879ff70be9271550477982a1a6371714db38562.tar.gz krecipes-2879ff70be9271550477982a1a6371714db38562.zip |
Rearrange folders structure to remove unnecessary 'krecipes' second level subfolder
Signed-off-by: Michele Calgaro <[email protected]>
(cherry picked from commit 0c8ed6c9a4000af8f48581a81c4b5c2f5b9fd502)
Diffstat (limited to 'src/datablocks/categorytree.cpp')
-rw-r--r-- | src/datablocks/categorytree.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/datablocks/categorytree.cpp b/src/datablocks/categorytree.cpp new file mode 100644 index 0000000..bb61de6 --- /dev/null +++ b/src/datablocks/categorytree.cpp @@ -0,0 +1,133 @@ +/*************************************************************************** +* Copyright (C) 2004 by 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 "categorytree.h" + +#include <kdebug.h> + +#include "element.h" + +CategoryTree::CategoryTree( CategoryTree *parent ) : + m_parent( 0 ), m_child( 0 ), m_sibling( 0 ), m_last(0), m_count(0) +{ + if ( parent ) + parent->insertItem( this ); +} + +CategoryTree::~CategoryTree() +{ + if ( m_parent ) + m_parent->takeItem( this ); + + CategoryTree * i = m_child; + m_child = 0; + while ( i ) { + i->m_parent = 0; + CategoryTree * n = i->m_sibling; + delete i; + i = n; + } +} + +CategoryTree *CategoryTree::add + ( const Element &cat ) +{ + CategoryTree * new_child = new CategoryTree( this ); + new_child->category = cat; + + m_count++; + + return new_child; +} + +void CategoryTree::insertItem( CategoryTree *newChild ) +{ + newChild->m_parent = this; + if ( m_child && m_child->m_last ) + m_child->m_last->m_sibling = newChild; + else + m_child = newChild; + + m_child->m_last = newChild; + m_count++; +} + +void CategoryTree::takeItem( CategoryTree *tree ) +{ + kdError()<< "Both these methods seem to be broken... don't use this function!" << endl; + + CategoryTree *lastItem = m_child->m_last; +#if 0 + CategoryTree ** nextChild = &m_child; + while( nextChild && *nextChild && tree != *nextChild ) + nextChild = &((*nextChild)->m_sibling); + + if ( nextChild && tree == *nextChild ) { + *nextChild = (*nextChild)->m_sibling; + } + tree->m_parent = 0; + tree->m_sibling = 0; +#else + for ( CategoryTree *it = firstChild(); it; it = it->nextSibling() ) { + if ( it->nextSibling() == tree ) { + it->m_sibling = tree->nextSibling(); + break; + } + } + tree->m_parent = 0; + tree->m_sibling = 0; +#endif + if ( tree != m_last ) + m_child->m_last = lastItem; + else //FIXME: unstable behavior if this is the case + kdDebug()<<"CategoryTree::takeItem: warning - unstable behavior expected"<<endl; + m_count--; +} + +void CategoryTree::clear() +{ + CategoryTree *c = m_child; + CategoryTree *n; + while( c ) { + n = c->m_sibling; + delete c; + c = n; + } +} + +bool CategoryTree::contains( int id ) const +{ + bool result = false; + + for ( CategoryTree * child_it = firstChild(); child_it; child_it = child_it->nextSibling() ) { + if ( child_it->category.id == id ) + return true; + + result = child_it->contains( id ); + } + + return result; +} + +CategoryTree* CategoryTree::find( int id ) const +{ + CategoryTree *result = 0; + + for ( CategoryTree * child_it = firstChild(); child_it; child_it = child_it->nextSibling() ) { + if ( child_it->category.id == id ) + return child_it; + + result = child_it->find( id ); + } + + return result; +} + + |