summaryrefslogtreecommitdiff
path: root/src/panels/bookmarkspanel.cpp
blob: dab992032762c845f5d3ca3ce2f453847e4c85e2 (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
/* ============================================================
 *     The rekonq project
 * ============================================================
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================ */

#include "bookmarkspanel.hpp"
#include "bookmarks/bookmarkstreemodel.hpp"
#include <QMenu>

BookmarksPanel::BookmarksPanel(QWidget *parent) : QTreeView(parent)
{
  setEditTriggers(QAbstractItemView::NoEditTriggers);
  setContextMenuPolicy(Qt::CustomContextMenu);

  connect(this, &QTreeView::activated, this, [this](const QModelIndex &index) { open(index); });
  connect(this, &QTreeView::customContextMenuRequested, this, &BookmarksPanel::customContextMenu);
}

BookmarksTreeModel *BookmarksPanel::model() const
{
  auto *m = qobject_cast<BookmarksTreeModel *>(QTreeView::model());
  Q_CHECK_PTR(m);
  return m;
}

void BookmarksPanel::customContextMenu(const QPoint &pos)
{
  const auto index = indexAt(pos);
  auto *item = model()->item(indexAt(pos));
  Q_CHECK_PTR(item);

  auto *menu = new QMenu(this);
  menu->addAction(tr("Open in current tab"), this, [this, index]() { open(index); });
  menu->addAction(tr("Open in new tab"), this, [this, index]() { open(index, rekonq::NewTab); });
  menu->addAction(tr("Edit"));
  menu->addAction(tr("Remove"), this, [this, index]() { remove(index); });

  menu->popup(mapToGlobal(pos));
}

void BookmarksPanel::open(const QModelIndex &index, rekonq::OpenType type)
{
  auto *item = model()->item(index);
  const auto url = item->data(BookmarksTreeItem::Href).toUrl();
  emit loadUrl(url, type);
}

void BookmarksPanel::remove(const QModelIndex &index) { model()->removeRow(index.row(), index.parent()); }