From 4d958ae602315fd9d78af35c40b27d9c6ae6e656 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 7 Jan 2018 20:02:33 +0100 Subject: Searching through BookmarkModel elements --- lib/navigation/CMakeLists.txt | 4 +-- lib/navigation/urlcompleter.cpp | 26 -------------- lib/navigation/urlcompleter.h | 25 ------------- lib/navigation/urllineedit.cpp | 79 ++++++++++++++++++++++++++++++++++++++--- lib/navigation/urllineedit.h | 9 +++-- 5 files changed, 83 insertions(+), 60 deletions(-) delete mode 100644 lib/navigation/urlcompleter.cpp delete mode 100644 lib/navigation/urlcompleter.h (limited to 'lib/navigation') diff --git a/lib/navigation/CMakeLists.txt b/lib/navigation/CMakeLists.txt index 4a4cf94..a81818e 100644 --- a/lib/navigation/CMakeLists.txt +++ b/lib/navigation/CMakeLists.txt @@ -3,9 +3,7 @@ cmake_minimum_required(VERSION 3.1.0) add_library(navigation navigationbutton.cpp navigationbutton.h - urlcompleter.cpp - urlcompleter.h urllineedit.cpp urllineedit.h) -target_link_libraries(navigation Qt5::Widgets Qt5::WebEngineWidgets) \ No newline at end of file +target_link_libraries(navigation Qt5::Widgets Qt5::WebEngineWidgets) diff --git a/lib/navigation/urlcompleter.cpp b/lib/navigation/urlcompleter.cpp deleted file mode 100644 index bbde297..0000000 --- a/lib/navigation/urlcompleter.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "urlcompleter.h" - -UrlCompleter::UrlCompleter(QAbstractItemModel *model, QObject *parent) : - QCompleter(model, parent) -{ - setCompletionMode(QCompleter::PopupCompletion); - setFilterMode(Qt::MatchContains); -} - -QStringList UrlCompleter::splitPath(const QString &path) const -{ - return path.split('.'); -} - -QString UrlCompleter::pathFromIndex(const QModelIndex &index) const -{ - return model()->data(index, completionRole()).toString(); -} diff --git a/lib/navigation/urlcompleter.h b/lib/navigation/urlcompleter.h deleted file mode 100644 index f2c52ff..0000000 --- a/lib/navigation/urlcompleter.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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: git://neueland.iserlohn-fortress.net/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef URLCOMPLETER_H -#define URLCOMPLETER_H - -#include - -class UrlCompleter : public QCompleter -{ - Q_OBJECT -public: - explicit UrlCompleter(QAbstractItemModel *model, QObject *parent = nullptr); - -protected: - QStringList splitPath(const QString &path) const override; - QString pathFromIndex(const QModelIndex &index) const override; -}; - -#endif // URLCOMPLETER_H diff --git a/lib/navigation/urllineedit.cpp b/lib/navigation/urllineedit.cpp index 095431b..85f443d 100644 --- a/lib/navigation/urllineedit.cpp +++ b/lib/navigation/urllineedit.cpp @@ -15,11 +15,19 @@ // ssl menu #include +#include +#include + UrlLineEdit::UrlLineEdit(QWidget *parent) : - QLineEdit(parent) + QLineEdit(parent), + m_listView(new QListView(this)) { setPlaceholderText(tr("Enter address")); + m_listView->setWindowFlags(Qt::ToolTip); + m_listView->setVisible(false); + connect(this, &UrlLineEdit::textEdited, this, &UrlLineEdit::updateCompleter); + // ssl menu m_sslMenu = new QMenu(this); m_sslLabel = new QLabel(m_sslMenu); @@ -70,9 +78,7 @@ QAction *UrlLineEdit::pageAction() void UrlLineEdit::setCompleterModel(QAbstractItemModel *model) { Q_CHECK_PTR(model); - m_completer = new UrlCompleter(model, this); - m_completer->setCompletionColumn(1); - this->setCompleter(m_completer); + m_bookmarksModel = model; } void UrlLineEdit::setUrl(const QUrl &url) @@ -95,6 +101,31 @@ void UrlLineEdit::showSslError(const QString &message) m_sslAction->trigger(); } +void UrlLineEdit::updateCompleter(const QString &text) +{ + if(m_bookmarksModel == nullptr) { + return; + } + + const QModelIndexList res = m_bookmarksModel->match(QModelIndex(), Qt::EditRole, text, 7); + QStringList l; + for(const QModelIndex &idx : res) { + l.append(idx.data(Qt::EditRole).toString()); + } + + if(!text.isEmpty()) { + l.append(text); + } + QStringListModel *m = new QStringListModel(l, this); + + m_listView->setModel(m); + + // positioning + m_listView->setFixedWidth(width()); + m_listView->move(mapToGlobal(QPoint(0, height()))); + m_listView->show(); +} + void UrlLineEdit::focusInEvent(QFocusEvent *event) { clearTextFormat(); @@ -107,6 +138,46 @@ void UrlLineEdit::focusInEvent(QFocusEvent *event) //QTimer::singleShot(0, this, SLOT(selectAll())); } +void UrlLineEdit::keyPressEvent(QKeyEvent *event) +{ + if(!m_listView->isHidden()) { + int key = event->key(); + int count = m_listView->model()->rowCount(); + QModelIndex currentIndex = m_listView->currentIndex(); + + switch (key) { + case Qt::Key_Down: + if(currentIndex.row() + 1 >= count) { + m_listView->setCurrentIndex(m_listView->model()->index(0, 0)); + } else { + m_listView->setCurrentIndex(m_listView->model()->index(currentIndex.row() + 1, 0)); + } + break; + case Qt::Key_Up: + if(currentIndex.row() == 0) { + m_listView->setCurrentIndex(m_listView->model()->index(count - 1, 0)); + } else { + m_listView->setCurrentIndex(m_listView->model()->index(currentIndex.row() - 1, 0)); + } + break; + + case Qt::Key_Enter: + case Qt::Key_Return: + if(currentIndex.isValid()) { + setText(currentIndex.data().toString()); + } + m_listView->hide(); + return; + case Qt::Key_Escape: + m_listView->hide(); + break; + default: + break; + } + } + QLineEdit::keyPressEvent(event); +} + // formatting taken from: https://forum.qt.io/topic/60962/setting-qlineedit-text-bold void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format) { diff --git a/lib/navigation/urllineedit.h b/lib/navigation/urllineedit.h index dee4938..a06ba1b 100644 --- a/lib/navigation/urllineedit.h +++ b/lib/navigation/urllineedit.h @@ -12,11 +12,11 @@ #include #include #include -#include "urlcompleter.h" class QAbstractItemModel; class QMenu; class QLabel; +class QListView; class UrlLineEdit : public QLineEdit { Q_OBJECT @@ -35,8 +35,11 @@ public slots: void setUrl(const QUrl &url); void showSslError(const QString &message); + void updateCompleter(const QString &text); + protected: void focusInEvent(QFocusEvent *event); + void keyPressEvent(QKeyEvent *event); private: void setTextFormat(const QTextLayout::FormatRange &format); @@ -51,7 +54,9 @@ private: QMenu *m_sslMenu; QLabel *m_sslLabel; - UrlCompleter *m_completer; + // completer + QAbstractItemModel *m_bookmarksModel = nullptr; + QListView *m_listView; }; #endif // URLLINEEDIT_H -- cgit v1.2.1