Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
bookmarkitem.cpp
Go to the documentation of this file.
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
10
11using namespace Qt::StringLiterals;
12
14 : m_data(data)
15 , m_parent(parent)
16{
17}
18
20{
21 qDeleteAll(m_children);
22}
23
26{
27 return m_parent;
28}
29
30void
32{
33 m_parent = parent;
34}
35
36void
38{
39 child->setParent(this);
40 m_children.append(child);
41}
42
44BookmarkItem::child(int number) const
45{
46 if (number >= 0 && number < m_children.size())
47 return m_children[number];
48 return nullptr;
49}
50
52{
53 return m_children.size();
54}
55
57{
58 if (m_parent)
59 return m_parent->m_children.indexOf(const_cast<BookmarkItem*>(this));
60 return 0;
61}
62
64BookmarkItem::data(int column) const
65{
66 if (column == 0)
67 return m_data[0];
68
69 if (column == 1 || column == UserRoleUrl)
70 return m_data[1];
71
72 if (column == UserRoleFolder)
73 return m_data[1].toString() == "Folder"_L1;
74
75 if (column == UserRoleExpanded)
76 return m_data[2];
77
78 return QVariant();
79}
80
81void
83{
84 m_data = data;
85}
86
87bool
88BookmarkItem::setData(int column, const QVariant &newValue)
89{
90 int index = -1;
91 if (column == 0 || column == 1)
92 index = column;
93
94 if (column == UserRoleUrl || column == UserRoleFolder)
95 index = 1;
96
97 if (column == UserRoleExpanded)
98 index = 2;
99
100 if (index < 0)
101 return false;
102
103 m_data[index] = newValue;
104 return true;
105}
106
107bool
108BookmarkItem::insertChildren(bool isFolder, int position, int count)
109{
110 if (position < 0 || position > m_children.size())
111 return false;
112
113 for (int row = 0; row < count; ++row) {
114 m_children.insert(position, new BookmarkItem(DataVector()
115 << (isFolder
116 ? QCoreApplication::translate("BookmarkItem", "New Folder")
117 : QCoreApplication::translate("BookmarkItem", "Untitled"))
118 << (isFolder ? "Folder" : "about:blank") << false, this));
119 }
120
121 return true;
122}
123
124bool
125BookmarkItem::removeChildren(int position, int count)
126{
127 if (position < 0 || position > m_children.size())
128 return false;
129
130 for (int row = 0; row < count; ++row)
131 delete m_children.takeAt(position);
132
133 return true;
134}
135
136void
137BookmarkItem::dumpTree(int indent) const
138{
139 const QString tree(indent, ' ');
140 qDebug() << tree + (data(UserRoleFolder).toBool() ? "Folder" : "Bookmark")
141 << "Label:" << data(0).toString() << "parent:" << m_parent << "this:"
142 << this;
143
144 for (BookmarkItem *item : m_children)
145 item->dumpTree(indent + 4);
146}
147
148QT_END_NAMESPACE
QList< QVariant > DataVector
QVariant data(int column) const
BookmarkItem * child(int number) const
bool insertChildren(bool isFolder, int position, int count)
void addChild(BookmarkItem *child)
void setParent(BookmarkItem *parent)
bool setData(int column, const QVariant &value)
bool removeChildren(int position, int count)
int childCount() const
BookmarkItem * parent() const
void setData(const DataVector &data)
BookmarkItem(const DataVector &data, BookmarkItem *parent=nullptr)
int childNumber() const
void dumpTree(int indent) const
Combined button and popup list for selecting options.