aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarkitem.cpp
blob: b2ee78b49fdd62d8cad54386ff582407d976c605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "bookmarkitem.h"
#include <QApplication>
#include <QStyle>

BookmarkItem::BookmarkItem(const QVector<QVariant> &data, Type type, BookmarkItem *parent)
{
    m_parentItem = parent;
    m_type = type;

    m_data.resize(FieldCount);
    for(int i = 0; i < FieldCount; ++i) {
        m_data[i] = data.value(i, QVariant());
    }

    // set a default style if any can be set
    if(qApp) {
        if(m_type == Folder) {
            m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off);
            m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On);
        } else if(m_type == Bookmark)
            m_icon.addPixmap(qApp->style()->standardPixmap(QStyle::SP_FileIcon));
    }
}

BookmarkItem::~BookmarkItem()
{
    qDeleteAll(m_children);
}

BookmarkItem *BookmarkItem::parent() const
{
    return m_parentItem;
}

bool BookmarkItem::appendChild(BookmarkItem *childItem)
{
    // Only folders can have children, so only append them on folders
    // This way, we don't need to add checks to the other methods
    if(m_type == Folder || m_type == Root) {
        m_children.append(childItem);
        return true;
    }

    return false;
}

bool BookmarkItem::insertChild(int position, BookmarkItem *childItem)
{
    // position is invalid (-1) when dropping an item onto the folder, which leads to crash
    // make sure that position passed is >= 0 (insert item at first position)

    if(m_type == Folder || m_type == Root) {
        m_children.insert(qMax(position, 0), childItem);
        return true;
    }

    return false;
}

bool BookmarkItem::removeChildAt(int index, int count)
{
    if(index < 0 || index + count > m_children.size())
        return false;

    // delete the item at index count times
    for(int i = 0; i < count; ++i) {
        delete m_children.takeAt(index);
    }
    return true;
}

BookmarkItem *BookmarkItem::takeChild(int index, BookmarkItem *newParent)
{
    m_children[index]->m_parentItem = newParent;
    return m_children.takeAt(index);
}

BookmarkItem *BookmarkItem::child(int index) const
{
    return m_children.value(index);
}

int BookmarkItem::childCount() const
{
    return m_children.count();
}

QVariant BookmarkItem::data(Fields column) const
{
    return m_data.value(column);
}

bool BookmarkItem::setData(Fields column, const QVariant &data)
{
    if(column >= FieldCount)
        return false;

    m_data[column] = data;
    return true;
}

QIcon BookmarkItem::icon() const
{
    return m_icon;
}

bool BookmarkItem::isExpanded() const
{
    return m_isExpanded;
}

void BookmarkItem::setExpanded(bool expanded)
{
    if(m_type == BookmarkItem::Folder)
        m_isExpanded = expanded;
}

QString BookmarkItem::tooltip() const
{
    return m_data.value(Tags).toStringList().join(", ");
}

BookmarkItem::Type BookmarkItem::type() const
{
    return m_type;
}

int BookmarkItem::row() const
{
    if(m_parentItem)
        return m_parentItem->m_children.indexOf(const_cast<BookmarkItem *>(this));

    return 0;
}