1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Assistant of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "bookmarkitem.h"
30
31#include <QtCore/QCoreApplication>
32#include <QtCore/QDebug>
33
34QT_BEGIN_NAMESPACE
35
36BookmarkItem::BookmarkItem(const DataVector &data, BookmarkItem *parent)
37 : m_data(data)
38 , m_parent(parent)
39{
40}
41
42BookmarkItem::~BookmarkItem()
43{
44 qDeleteAll(c: m_children);
45}
46
47BookmarkItem*
48BookmarkItem::parent() const
49{
50 return m_parent;
51}
52
53void
54BookmarkItem::setParent(BookmarkItem *parent)
55{
56 m_parent = parent;
57}
58
59void
60BookmarkItem::addChild(BookmarkItem *child)
61{
62 child->setParent(this);
63 m_children.append(t: child);
64}
65
66BookmarkItem*
67BookmarkItem::child(int number) const
68{
69 if (number >= 0 && number < m_children.count())
70 return m_children[number];
71 return nullptr;
72}
73
74int BookmarkItem::childCount() const
75{
76 return m_children.count();
77}
78
79int BookmarkItem::childNumber() const
80{
81 if (m_parent)
82 return m_parent->m_children.indexOf(t: const_cast<BookmarkItem*>(this));
83 return 0;
84}
85
86QVariant
87BookmarkItem::data(int column) const
88{
89 if (column == 0)
90 return m_data[0];
91
92 if (column == 1 || column == UserRoleUrl)
93 return m_data[1];
94
95 if (column == UserRoleFolder)
96 return m_data[1].toString() == QLatin1String("Folder");
97
98 if (column == UserRoleExpanded)
99 return m_data[2];
100
101 return QVariant();
102}
103
104void
105BookmarkItem::setData(const DataVector &data)
106{
107 m_data = data;
108}
109
110bool
111BookmarkItem::setData(int column, const QVariant &newValue)
112{
113 int index = -1;
114 if (column == 0 || column == 1)
115 index = column;
116
117 if (column == UserRoleUrl || column == UserRoleFolder)
118 index = 1;
119
120 if (column == UserRoleExpanded)
121 index = 2;
122
123 if (index < 0)
124 return false;
125
126 m_data[index] = newValue;
127 return true;
128}
129
130bool
131BookmarkItem::insertChildren(bool isFolder, int position, int count)
132{
133 if (position < 0 || position > m_children.size())
134 return false;
135
136 for (int row = 0; row < count; ++row) {
137 m_children.insert(i: position, t: new BookmarkItem(DataVector()
138 << (isFolder
139 ? QCoreApplication::translate(context: "BookmarkItem", key: "New Folder")
140 : QCoreApplication::translate(context: "BookmarkItem", key: "Untitled"))
141 << (isFolder ? "Folder" : "about:blank") << false, this));
142 }
143
144 return true;
145}
146
147bool
148BookmarkItem::removeChildren(int position, int count)
149{
150 if (position < 0 || position > m_children.size())
151 return false;
152
153 for (int row = 0; row < count; ++row)
154 delete m_children.takeAt(i: position);
155
156 return true;
157}
158
159void
160BookmarkItem::dumpTree(int indent) const
161{
162 const QString tree(indent, ' ');
163 qDebug() << tree + (data(column: UserRoleFolder).toBool() ? "Folder" : "Bookmark")
164 << "Label:" << data(column: 0).toString() << "parent:" << m_parent << "this:"
165 << this;
166
167 for (BookmarkItem *item : m_children)
168 item->dumpTree(indent: indent + 4);
169}
170
171QT_END_NAMESPACE
172

source code of qttools/src/assistant/assistant/bookmarkitem.cpp