1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "bookmarkitem.h"
5
6#include <QtCore/QCoreApplication>
7#include <QtCore/QDebug>
8
9QT_BEGIN_NAMESPACE
10
11BookmarkItem::BookmarkItem(const DataVector &data, BookmarkItem *parent)
12 : m_data(data)
13 , m_parent(parent)
14{
15}
16
17BookmarkItem::~BookmarkItem()
18{
19 qDeleteAll(c: m_children);
20}
21
22BookmarkItem*
23BookmarkItem::parent() const
24{
25 return m_parent;
26}
27
28void
29BookmarkItem::setParent(BookmarkItem *parent)
30{
31 m_parent = parent;
32}
33
34void
35BookmarkItem::addChild(BookmarkItem *child)
36{
37 child->setParent(this);
38 m_children.append(t: child);
39}
40
41BookmarkItem*
42BookmarkItem::child(int number) const
43{
44 if (number >= 0 && number < m_children.size())
45 return m_children[number];
46 return nullptr;
47}
48
49int BookmarkItem::childCount() const
50{
51 return m_children.size();
52}
53
54int BookmarkItem::childNumber() const
55{
56 if (m_parent)
57 return m_parent->m_children.indexOf(t: const_cast<BookmarkItem*>(this));
58 return 0;
59}
60
61QVariant
62BookmarkItem::data(int column) const
63{
64 if (column == 0)
65 return m_data[0];
66
67 if (column == 1 || column == UserRoleUrl)
68 return m_data[1];
69
70 if (column == UserRoleFolder)
71 return m_data[1].toString() == QLatin1String("Folder");
72
73 if (column == UserRoleExpanded)
74 return m_data[2];
75
76 return QVariant();
77}
78
79void
80BookmarkItem::setData(const DataVector &data)
81{
82 m_data = data;
83}
84
85bool
86BookmarkItem::setData(int column, const QVariant &newValue)
87{
88 int index = -1;
89 if (column == 0 || column == 1)
90 index = column;
91
92 if (column == UserRoleUrl || column == UserRoleFolder)
93 index = 1;
94
95 if (column == UserRoleExpanded)
96 index = 2;
97
98 if (index < 0)
99 return false;
100
101 m_data[index] = newValue;
102 return true;
103}
104
105bool
106BookmarkItem::insertChildren(bool isFolder, int position, int count)
107{
108 if (position < 0 || position > m_children.size())
109 return false;
110
111 for (int row = 0; row < count; ++row) {
112 m_children.insert(i: position, t: new BookmarkItem(DataVector()
113 << (isFolder
114 ? QCoreApplication::translate(context: "BookmarkItem", key: "New Folder")
115 : QCoreApplication::translate(context: "BookmarkItem", key: "Untitled"))
116 << (isFolder ? "Folder" : "about:blank") << false, this));
117 }
118
119 return true;
120}
121
122bool
123BookmarkItem::removeChildren(int position, int count)
124{
125 if (position < 0 || position > m_children.size())
126 return false;
127
128 for (int row = 0; row < count; ++row)
129 delete m_children.takeAt(i: position);
130
131 return true;
132}
133
134void
135BookmarkItem::dumpTree(int indent) const
136{
137 const QString tree(indent, ' ');
138 qDebug() << tree + (data(column: UserRoleFolder).toBool() ? "Folder" : "Bookmark")
139 << "Label:" << data(column: 0).toString() << "parent:" << m_parent << "this:"
140 << this;
141
142 for (BookmarkItem *item : m_children)
143 item->dumpTree(indent: indent + 4);
144}
145
146QT_END_NAMESPACE
147

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