summaryrefslogtreecommitdiff
path: root/src/urlbar/completer.cpp
blob: df864e05d696d66978a2d8b72730eb617d79f0ae (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
/* ============================================================
 *     The rekonq project
 * ============================================================
 * SPDX-License-Identifier: BSD-3-Clause
 * Copyright (C) 2016 The Qt Company Ltd.
 * SPDX-License-Identifier: GPL-3.0-only
 * Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
 * ============================================================ */

#include "completer.hpp"
#include "urlbar.hpp"
#include <QHeaderView>
#include <QKeyEvent>
#include <QLineEdit>
#include <QTreeWidget>

static const QString searchUrl{"https://duckduckgo.com/?q=%1"};

// ------------------------------------------------------------------------
// Ctor and Dtor

UrlSuggester::UrlSuggester(UrlBar *parent) : QObject(parent), m_editor(parent), m_popup(new QTreeWidget)
{
  m_popup->setWindowFlags(Qt::Popup);
  m_popup->setFocusPolicy(Qt::NoFocus);
  m_popup->setFocusProxy(parent);
  m_popup->setMouseTracking(true);

  m_popup->setColumnCount(2);
  m_popup->setUniformRowHeights(true);
  m_popup->setRootIsDecorated(false);
  m_popup->setEditTriggers(QTreeWidget::NoEditTriggers);
  m_popup->setSelectionBehavior(QTreeWidget::SelectRows);
  m_popup->setFrameStyle(QFrame::Box | QFrame::Plain);
  m_popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  m_popup->header()->hide();

  m_popup->installEventFilter(this);
}

UrlSuggester::~UrlSuggester() { delete m_popup; }

bool UrlSuggester::eventFilter(QObject *obj, QEvent *ev)
{
  if (obj != m_popup) return false;

  if (ev->type() == QEvent::MouseButtonPress) {
    m_popup->hide();
    m_editor->setFocus();
    return true;
  }

  if (ev->type() == QEvent::KeyPress) {
    bool consumed = false;
    int key = static_cast<QKeyEvent *>(ev)->key();
    switch (key) {
    case Qt::Key_Enter:
    case Qt::Key_Return:
      doneCompletion();
      consumed = true;
      break;

    case Qt::Key_Escape:
      m_editor->setFocus();
      m_popup->hide();
      consumed = true;
      break;

    case Qt::Key_Up:
    case Qt::Key_Down:
    case Qt::Key_Home:
    case Qt::Key_End:
    case Qt::Key_PageUp:
    case Qt::Key_PageDown:
      break;

    default:
      m_editor->setFocus();
      m_editor->event(ev);
      // m_popup->hide();
      break;
    }

    return consumed;
  }

  return false;
}

// ------------------------------------------------------------------------
// slots

void UrlSuggester::showCompletions(const QString &text)
{
  if (text.isEmpty()) return;

  m_popup->setUpdatesEnabled(false);
  m_popup->clear();

  { // go item
    auto *item = new QTreeWidgetItem(m_popup);
    item->setIcon(0, QIcon::fromTheme("go-jump"));
    item->setText(0, tr("Go to %1").arg(text));
    item->setText(1, QUrl::fromUserInput(text).toString());
  }
  { // search item
    auto *item = new QTreeWidgetItem(m_popup);
    item->setIcon(0, QIcon::fromTheme("search"));
    item->setText(0, tr("Search for %1").arg(text));
    item->setText(1, searchUrl.arg(QUrl::toPercentEncoding(text)));
  }

  m_popup->setCurrentItem(m_popup->topLevelItem(0));
  m_popup->resizeColumnToContents(0);
  m_popup->setUpdatesEnabled(true);

  m_popup->move(m_editor->mapToGlobal(QPoint(0, m_editor->height())));
  m_popup->resize(m_editor->width(), 250);
  m_popup->setFocus();
  m_popup->show();
}

void UrlSuggester::doneCompletion()
{
  m_popup->hide();
  m_editor->setFocus();
  auto *item = m_popup->currentItem();
  if (item) {
    m_editor->setText(item->text(1));
    emit m_editor->returnPressed();
  }
}