Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / keditbookmarks / treeitem.cpp
blob343b38b3fb32e07c0f50b85d7357e09cfd18a984
1 /* This file is part of the KDE project
2 Copyright (C) 2005 Daniel Teske <teske@squorn.de>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) version 3.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>
18 #include "treeitem_p.h"
19 #include <kdebug.h>
20 #include <QtCore/QVector>
22 TreeItem::TreeItem(const KBookmark& bk, TreeItem * parent)
23 : mparent(parent), mbk(bk)
25 init = false;
28 TreeItem::~TreeItem()
30 qDeleteAll(children);
31 children.clear();
34 TreeItem * TreeItem::child(int row)
36 if(!init)
37 initChildren();
38 if(row < 0 || row >= children.count())
39 return parent();
40 return children.at(row);
43 int TreeItem::childCount()
45 if(!init)
46 initChildren();
47 return children.count();
50 TreeItem * TreeItem::parent() const
52 return mparent;
55 void TreeItem::insertChildren(int first, int last)
57 // Find child number last
58 KBookmarkGroup parent = bookmark().toGroup();
59 KBookmark child = parent.first();
60 for(int j=0; j < last; ++j)
61 child = parent.next(child);
63 //insert children
64 int i = last;
67 children.insert(i, new TreeItem(child, this));
68 child = parent.previous(child);
69 --i;
70 } while(i >= first);
74 void TreeItem::deleteChildren(int first, int last)
76 QList<TreeItem *>::iterator firstIt, lastIt, it;
77 firstIt = children.begin() + first;
78 lastIt = children.begin() + last + 1;
79 for( it = firstIt; it != lastIt; ++it)
81 delete *it;
83 children.erase(firstIt, lastIt);
86 KBookmark TreeItem::bookmark() const
88 return mbk;
91 void TreeItem::initChildren()
93 init = true;
94 if(mbk.isGroup())
96 KBookmarkGroup parent = mbk.toGroup();
97 for(KBookmark child = parent.first(); child.hasParent(); child = parent.next(child) )
99 TreeItem * item = new TreeItem(child, this);
100 children.append(item);
105 TreeItem * TreeItem::treeItemForBookmark(const KBookmark& bk)
107 if(bk.address() == mbk.address())
108 return this;
109 QString commonParent = KBookmark::commonParent(bk.address(), mbk.address());
110 if(commonParent == mbk.address()) //mbk is a parent of bk
112 QList<TreeItem *>::const_iterator it, end;
113 end = children.constEnd();
114 for( it = children.constBegin(); it != end; ++it)
116 KBookmark child = (*it)->bookmark();
117 if( KBookmark::commonParent(child.address(), bk.address()) == child.address())
118 return (*it)->treeItemForBookmark(bk);
120 return 0;
122 else
124 if(parent() == 0)
125 return 0;
126 return parent()->treeItemForBookmark(bk);
130 // kate: space-indent on; indent-width 4; replace-tabs on;