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 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | BookmarkItem::BookmarkItem(const DataVector &data, BookmarkItem *parent) |
12 | : m_data(data) |
13 | , m_parent(parent) |
14 | { |
15 | } |
16 | |
17 | BookmarkItem::~BookmarkItem() |
18 | { |
19 | qDeleteAll(c: m_children); |
20 | } |
21 | |
22 | BookmarkItem* |
23 | BookmarkItem::parent() const |
24 | { |
25 | return m_parent; |
26 | } |
27 | |
28 | void |
29 | BookmarkItem::setParent(BookmarkItem *parent) |
30 | { |
31 | m_parent = parent; |
32 | } |
33 | |
34 | void |
35 | BookmarkItem::addChild(BookmarkItem *child) |
36 | { |
37 | child->setParent(this); |
38 | m_children.append(t: child); |
39 | } |
40 | |
41 | BookmarkItem* |
42 | BookmarkItem::child(int number) const |
43 | { |
44 | if (number >= 0 && number < m_children.size()) |
45 | return m_children[number]; |
46 | return nullptr; |
47 | } |
48 | |
49 | int BookmarkItem::childCount() const |
50 | { |
51 | return m_children.size(); |
52 | } |
53 | |
54 | int 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 | |
61 | QVariant |
62 | BookmarkItem::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 | |
79 | void |
80 | BookmarkItem::setData(const DataVector &data) |
81 | { |
82 | m_data = data; |
83 | } |
84 | |
85 | bool |
86 | BookmarkItem::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 | |
105 | bool |
106 | BookmarkItem::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 | |
122 | bool |
123 | BookmarkItem::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 | |
134 | void |
135 | BookmarkItem::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 | |
146 | QT_END_NAMESPACE |
147 | |