aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarksmodel.h
blob: 7ae20faeeb2bbced8728f46c54df2c3f6772ed8b (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
/*
 * 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 "bookmarkitem.h"
#include "xbel.h"
#include <QAbstractItemModel>
#include <QIcon>
#include <QTreeView>

#ifndef BOOKMARKSMODEL_H
#define BOOKMARKSMODEL_H

class QStyle;
class BookmarksModel : public QAbstractItemModel
{
    Q_OBJECT

public:
    enum {
        TitleRole = Qt::UserRole + 1,
        OpenUrlRole = Qt::UserRole + 2
    };

    explicit BookmarksModel(QStyle *style, QObject *parent = nullptr);
    ~BookmarksModel() override;

    bool isModified() const;

    bool read(Xbel *xbel);
    bool write(Xbel *xbel);

    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

    QModelIndex index(BookmarkItem *node, int column = 0) const;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex insertItem(BookmarkItem::BookmarkItemType type, const QModelIndex &parent);
    bool removeItem(const QModelIndex &node);

    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    Qt::ItemFlags flags(const QModelIndex &index) const override;
    BookmarkItem::BookmarkItemType type(const QModelIndex &index) const;
    QVariant data(const QModelIndex &index, int role) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;

    bool hasChildren(const QModelIndex &parent) const override;

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

    void expandItems(QTreeView *view, BookmarkItem *root = nullptr);

    // drag and drop
    Qt::DropActions supportedDropActions() const override
    {
        return Qt::CopyAction | Qt::MoveAction;
    }
    QStringList mimeTypes() const override
    {
        return {"application/bookmarks.xbel"};
    }
    QMimeData *mimeData(const QModelIndexList &indexes) const override;
    bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;

private:
    QIcon folderIcon;
    QIcon bookmarkIcon;
    bool modified = false;

    BookmarkItem *m_rootItem;
};

#endif //BOOKMARKSMODEL_H