aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarksmodel.cpp
blob: 3498f7156a85cf2b493bf16d2933b19173a3d00f (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * 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/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "bookmarksmodel.h"

BookmarksModel::BookmarksModel(QStyle *style, QObject *parent)
    : QAbstractItemModel(parent)
{
    folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On);
    folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off);
    bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));

    // create a root item
    m_rootItem = new BookmarkItem(BookmarkItem::Root, nullptr);
    m_rootItem->title = tr("Title");
    m_rootItem->href = tr("href");
}

BookmarksModel::~BookmarksModel()
{
    delete m_rootItem;
}

bool BookmarksModel::isModified() const
{
    return modified;
}

bool BookmarksModel::read(Xbel *xbel)
{
    Q_CHECK_PTR(xbel);
    // if there are items in the bookmark list, we're adding to them, so the model becomes modified
    if(m_rootItem->childCount() != 0) {
        modified = true;
    }
    return xbel->read(m_rootItem);
}

bool BookmarksModel::write(Xbel *xbel)
{
    Q_CHECK_PTR(xbel);
    return xbel->write(m_rootItem);
}

QVariant BookmarksModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        if(section == 0) {
            return m_rootItem->title;
        } else if(section == 1) {
            return m_rootItem->href;
        } else {
            return QVariant();
        }
    }

    return QVariant();
}

QModelIndex BookmarksModel::index(BookmarkItem *node, int column) const
{
    BookmarkItem *parentItem = node->parentItem();
    if(!parentItem) {
        return QModelIndex();
    }

    return createIndex(parentItem->childIndex(node), column, node);
}

QModelIndex BookmarksModel::index(int row, int column, const QModelIndex &parent) const
{
    // invalid item requested
    if(!hasIndex(row, column, parent)) {
        return QModelIndex();
    }

    // index exists
    BookmarkItem *parentItem;
    if(parent.isValid()) {
        parentItem = static_cast<BookmarkItem *>(parent.internalPointer());
    } else {
        parentItem = m_rootItem;
    }

    BookmarkItem *childItem = parentItem->child(row);
    if(childItem) {
        return createIndex(row, column, childItem);
    }
    return QModelIndex();
}

QModelIndex BookmarksModel::parent(const QModelIndex &index) const
{
    if(!index.isValid()) {
        return QModelIndex();
    }

    BookmarkItem *childItem = static_cast<BookmarkItem *>(index.internalPointer());
    BookmarkItem *parentItem = childItem->parentItem();

    if(parentItem == m_rootItem) {
        return QModelIndex();
    }

    return createIndex(parentItem->row(), 0, parentItem);
}

int BookmarksModel::rowCount(const QModelIndex &parent) const
{
    if(parent.column() > 0) {
        return 0;
    }

    BookmarkItem *parentItem;
    if(!parent.isValid()) {
        parentItem = m_rootItem;
    } else {
        parentItem = static_cast<BookmarkItem *>(parent.internalPointer());
    }

    return parentItem->childCount();
}

QModelIndex BookmarksModel::insertItem(BookmarkItem::BookmarkItemType type, const QModelIndex &parent)
{
    BookmarkItem *parentItem;
    if(!parent.isValid()) {
        parentItem = m_rootItem;
    } else {
        parentItem = static_cast<BookmarkItem *>(parent.internalPointer());
        if(parentItem->type() == BookmarkItem::Bookmark)
            parentItem = parentItem->parentItem();
    }

    beginInsertRows(index(parentItem), parentItem->row(), parentItem->row());
    auto *node = new BookmarkItem(type, parentItem);
    node->title = tr("title");
    node->href = tr("href");
    endInsertRows();

    return index(node);
}

int BookmarksModel::columnCount(const QModelIndex &parent) const
{
    if(parent.isValid()) {
        return static_cast<BookmarkItem *>(parent.internalPointer())->columnCount();
    } else {
        return m_rootItem->columnCount();
    }
}

Qt::ItemFlags BookmarksModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
        return Qt::NoItemFlags;

    Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    auto *node = static_cast<BookmarkItem *>(index.internalPointer());
    switch(node->type()) {
    case BookmarkItem::Root:
        break;
    case BookmarkItem::Folder:
        flags |= Qt::ItemIsEditable;
        break;
    case BookmarkItem::Bookmark:
        flags |= Qt::ItemIsEnabled | Qt::ItemNeverHasChildren;
        break;
    }
    return flags;
}

QVariant BookmarksModel::data(const QModelIndex &index, int role) const
{
    // get data of invalid index?
    if(!index.isValid()) {
        return QVariant();
    }

    const BookmarkItem *item = static_cast<BookmarkItem *>(index.internalPointer());
    switch(role) {
    case Qt::DisplayRole:
    case Qt::EditRole:
        if(index.column() == 0) {
            return item->title;
        } else if(index.column() == 1) {
            return item->href;
        } else {
            return QVariant();
        }

    case Qt::DecorationRole:
        if(index.column() == 0) {
            if(item->type() == BookmarkItem::Folder) {
                return folderIcon;
            } else if(item->type() == BookmarkItem::Bookmark) {
                return bookmarkIcon;
            } else {
                return QVariant();
            }
        } else {
            return QVariant();
        }

    case OpenUrlRole:
        if(item->type() == BookmarkItem::Bookmark) {
            return item->href;
        } else {
            return QVariant();
        }

    default:
        return QVariant();
    }
}

bool BookmarksModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!index.isValid())
        return false;

    auto *node = static_cast<BookmarkItem *>(index.internalPointer());
    switch(node->type()) {
    case BookmarkItem::Root:
        return false;

    case BookmarkItem::Folder:
        if(index.column() == 0) {
            node->title = value.toString();
            modified = true;
            emit dataChanged(index, index);
            return true;
        }
        return false;

    case BookmarkItem::Bookmark:
        if(index.column() == 0) {
            node->title = value.toString();
            modified = true;
            emit dataChanged(index, index);
            return true;
        } else if(index.column() == 1) {
            node->href = value.toString();
            modified = true;
            emit dataChanged(index, index);
            return true;
        }
        return false;
    }
}

bool BookmarksModel::hasChildren(const QModelIndex &parent) const
{
    if(!parent.isValid()) {
        return true;
    }

    const BookmarkItem *parentItem = static_cast<BookmarkItem *>(parent.internalPointer());
    return (parentItem->type() == BookmarkItem::Folder);
}

QModelIndexList BookmarksModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
{
    QModelIndexList list;

    // search root
    BookmarkItem *searchRoot;
    if(!start.isValid()) {
        searchRoot = m_rootItem;
    } else {
        searchRoot = static_cast<BookmarkItem *>(start.internalPointer());
    }

    // iterate through searchRoot children
    for(int i = 0; i < searchRoot->childCount(); ++i) {
        BookmarkItem *item = searchRoot->child(i);
        if(item->type() == BookmarkItem::Bookmark) {
            if(item->href.contains(value.toString())) {
                list.append(index(item, 1));
            }
        } else if(item->type() == BookmarkItem::Folder) {
            list.append(match(index(item), role, value, hits, flags));
        }
    }

    return list;
}

void BookmarksModel::expandItems(QTreeView *view, BookmarkItem *root)
{
    if(root == nullptr)
        root = m_rootItem;

    // iterate through children
    for(int i = 0; i < root->childCount(); ++i) {
        BookmarkItem *childNode = root->child(i);

        // and if it's a folder
        if(childNode->type() == BookmarkItem::Folder) {
            QModelIndex idx = index(childNode);
            view->setExpanded(idx, !childNode->folded);

            // only folders have children, so go through them
            expandItems(view, childNode);
        }
    }
}