aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks/bookmarksview.cpp
blob: a92a3a7ccf6da39ff8c910813bc8b25409f50cb1 (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
/*
 * 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 "bookmarksview.h"

inline QTreeWidgetItem *createTreeWidgetItem(QTreeWidget *widget, QTreeWidgetItem *parentItem)
{
    if(parentItem)
        return new QTreeWidgetItem(parentItem);
    else
        return new QTreeWidgetItem(widget);
}

BookmarksView::BookmarksView(QWidget *parent)
    : QTreeWidget(parent)
{
    QStyle *qStyle = style();
    folderIcon.addPixmap(qStyle->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off);
    folderIcon.addPixmap(qStyle->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On);
    bookmarkIcon.addPixmap(qStyle->standardPixmap(QStyle::SP_FileIcon));
}

QTreeWidgetItem *BookmarksView::createBookmark(QTreeWidgetItem *parentItem)
{
    QTreeWidgetItem *item = createTreeWidgetItem(this, parentItem);
    item->setFlags(item->flags().setFlag(Qt::ItemIsDropEnabled, false) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled);
    item->setData(0, Qt::UserRole, Type::Bookmark);
    item->setIcon(0, bookmarkIcon);
    return item;
}

QTreeWidgetItem *BookmarksView::createFolder(QTreeWidgetItem *parentItem)
{
    QTreeWidgetItem *item = createTreeWidgetItem(this, parentItem);
    item->setFlags(item->flags() | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
    item->setData(0, Qt::UserRole, Type::Folder);
    item->setIcon(0, folderIcon);
    return item;
}

BookmarksView::Type BookmarksView::itemType(QTreeWidgetItem *item) const
{
    if(item == nullptr)
        return Invalid;

    return item->data(0, Qt::UserRole).value<Type>();
}