aboutsummaryrefslogtreecommitdiff
path: root/src/bookmarks/bookmarkstoolbar.cpp
blob: 52dcae2795949f6392158efccd88f4f888a0b88a (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
/*
 * 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 "bookmarkstoolbar.h"
#include "bookmarkmodel.h"
#include "configuration.h"
#include "mainwindow/mainwindow.h"
#include <QMenu>

BookmarksToolbar::BookmarksToolbar(const BookmarkModel *model, MainWindow *parent)
    : QToolBar(parent)
{
    m_window = parent;
    Configuration conf;

    setObjectName("bookmarks.toolbar");
    setWindowTitle(tr("Bookmarks Toolbar"));
    setMovable(conf.value<bool>("bookmarks.toolbar.movable").value());
    setVisible(conf.value<bool>("bookmarks.toolbar.visible").value());
    setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    const auto *root = model->root();
    for(int i = 0; i < root->childCount(); ++i) {
        const auto *child = root->child(i);
        if(child->type() == BookmarkItem::Folder && child->data(BookmarkItem::Title).toString() == "Bookmarks Toolbar") {
            addFolder(child);
            break;
        }
    }
}

void BookmarksToolbar::addBookmark(const BookmarkItem *item, QMenu *where)
{
    QAction *action = nullptr;
    if(where)
        action = where->addAction(item->data(BookmarkItem::Title).toString());
    else
        action = addAction(item->data(BookmarkItem::Title).toString());

    action->setIcon(item->icon());

    const auto url = item->data(BookmarkItem::Href).toUrl();
    connect(action, &QAction::triggered, m_window, [this, url]() {
        m_window->createTab(url);
    });
}

void BookmarksToolbar::addFolder(const BookmarkItem *item, QMenu *where)
{
    for(int i = 0; i < item->childCount(); ++i) {
        auto *child = item->child(i);

        if(child->type() == BookmarkItem::Bookmark)
            addBookmark(child, where);

        else if(child->type() == BookmarkItem::Folder) {
            auto *action = addAction(child->data(BookmarkItem::Title).toString());
            action->setIcon(child->icon());

            auto *menu = new QMenu(this);
            addFolder(child, menu);
            action->setMenu(menu);

            connect(action, &QAction::triggered, this, [this, action, menu]() {
                const auto *widget = this->widgetForAction(action);
                const auto pos = widget->mapToGlobal(QPoint(0, widget->height()));
                menu->popup(pos);
            });
        }
    }
}