File indexing completed on 2022-11-29 15:26:23

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005 Daniel Teske <teske@squorn.de>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public License as
0006    published by the Free Software Foundation; either version 2 of
0007    the License, or (at your option) version 3.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012    GNU General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program.  If not, see <http://www.gnu.org/licenses/>
0016 */
0017 
0018 #include "treeitem_p.h"
0019 
0020 TreeItem::TreeItem(const KBookmark &bk, TreeItem *parent)
0021     : mParent(parent)
0022     , mBookmark(bk)
0023     , mInitDone(false)
0024 {
0025 }
0026 
0027 TreeItem::~TreeItem()
0028 {
0029     qDeleteAll(children);
0030     children.clear();
0031 }
0032 
0033 TreeItem *TreeItem::child(int row)
0034 {
0035     if (!mInitDone)
0036         initChildren();
0037     if (row < 0 || row >= children.count())
0038         return parent();
0039     return children.at(row);
0040 }
0041 
0042 int TreeItem::childCount()
0043 {
0044     if (!mInitDone)
0045         initChildren();
0046     return children.count();
0047 }
0048 
0049 TreeItem *TreeItem::parent() const
0050 {
0051     return mParent;
0052 }
0053 
0054 void TreeItem::insertChildren(int first, int last)
0055 {
0056     // Find child number last
0057     KBookmarkGroup parent = bookmark().toGroup();
0058     KBookmark child = parent.first();
0059     for (int j = 0; j < last; ++j)
0060         child = parent.next(child);
0061 
0062     // insert children
0063     int i = last;
0064     do {
0065         children.insert(i, new TreeItem(child, this));
0066         child = parent.previous(child);
0067         --i;
0068     } while (i >= first);
0069 }
0070 
0071 void TreeItem::deleteChildren(int first, int last)
0072 {
0073     if (!mInitDone) {
0074         return;
0075     }
0076     Q_ASSERT(first <= last);
0077     Q_ASSERT(last < children.count());
0078     QList<TreeItem *>::iterator firstIt, lastIt, it;
0079     firstIt = children.begin() + first;
0080     lastIt = children.begin() + last + 1;
0081     for (it = firstIt; it != lastIt; ++it) {
0082         delete *it;
0083     }
0084     children.erase(firstIt, lastIt);
0085 }
0086 
0087 KBookmark TreeItem::bookmark() const
0088 {
0089     return mBookmark;
0090 }
0091 
0092 void TreeItem::initChildren()
0093 {
0094     mInitDone = true;
0095     if (mBookmark.isGroup()) {
0096         KBookmarkGroup parent = mBookmark.toGroup();
0097         for (KBookmark child = parent.first(); child.hasParent(); child = parent.next(child)) {
0098             TreeItem *item = new TreeItem(child, this);
0099             children.append(item);
0100         }
0101     }
0102 }
0103 
0104 TreeItem *TreeItem::treeItemForBookmark(const KBookmark &bk)
0105 {
0106     if (bk.address() == mBookmark.address())
0107         return this;
0108     if (!mInitDone) {
0109         initChildren();
0110     }
0111     QString commonParent = KBookmark::commonParent(bk.address(), mBookmark.address());
0112     if (commonParent == mBookmark.address()) // mBookmark is a parent of bk
0113     {
0114         QList<TreeItem *>::const_iterator it, end;
0115         end = children.constEnd();
0116         for (it = children.constBegin(); it != end; ++it) {
0117             KBookmark child = (*it)->bookmark();
0118             if (KBookmark::commonParent(child.address(), bk.address()) == child.address())
0119                 return (*it)->treeItemForBookmark(bk);
0120         }
0121         return nullptr;
0122     } else {
0123         if (parent() == nullptr)
0124             return nullptr;
0125         return parent()->treeItemForBookmark(bk);
0126     }
0127 }
0128 
0129 // kate: space-indent on; indent-width 4; replace-tabs on;