diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-07 20:02:33 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-01-07 20:02:33 +0100 |
commit | 4d958ae602315fd9d78af35c40b27d9c6ae6e656 (patch) | |
tree | 18af9cfb87d2a432c850af39258d6c7294051df5 /lib | |
parent | Added BookmarksModel (diff) | |
download | smolbote-4d958ae602315fd9d78af35c40b27d9c6ae6e656.tar.xz |
Searching through BookmarkModel elements
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bookmarks/bookmarksmodel.cpp | 32 | ||||
-rw-r--r-- | lib/bookmarks/bookmarksmodel.h | 6 | ||||
-rw-r--r-- | lib/navigation/CMakeLists.txt | 4 | ||||
-rw-r--r-- | lib/navigation/urlcompleter.cpp | 26 | ||||
-rw-r--r-- | lib/navigation/urlcompleter.h | 25 | ||||
-rw-r--r-- | lib/navigation/urllineedit.cpp | 79 | ||||
-rw-r--r-- | lib/navigation/urllineedit.h | 9 |
7 files changed, 117 insertions, 64 deletions
diff --git a/lib/bookmarks/bookmarksmodel.cpp b/lib/bookmarks/bookmarksmodel.cpp index ddd136e..757612e 100644 --- a/lib/bookmarks/bookmarksmodel.cpp +++ b/lib/bookmarks/bookmarksmodel.cpp @@ -49,14 +49,14 @@ QVariant BookmarksModel::headerData(int section, Qt::Orientation orientation, in return QVariant(); } -QModelIndex BookmarksModel::index(BookmarkItem *node) const +QModelIndex BookmarksModel::index(BookmarkItem *node, int column) const { BookmarkItem *parentItem = node->parentItem(); if(!parentItem) { return QModelIndex(); } - return createIndex(parentItem->childIndex(node), 0, node); + return createIndex(parentItem->childIndex(node), column, node); } QModelIndex BookmarksModel::index(int row, int column, const QModelIndex &parent) const @@ -175,3 +175,31 @@ bool BookmarksModel::hasChildren(const QModelIndex &parent) const const BookmarkItem *parentItem = static_cast<BookmarkItem *>(parent.internalPointer()); return (parentItem->type() == BookmarkItem::Folder); } + +QModelIndexList BookmarksModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const +{ + QModelIndexList list; + + // search root + BookmarkItem *searchRoot; + if(!start.isValid()) { + searchRoot = m_rootItem; + } else { + searchRoot = static_cast<BookmarkItem *>(start.internalPointer()); + } + + // iterate through searchRoot children + for(int i = 0; i < searchRoot->childCount(); ++i) { + BookmarkItem *item = searchRoot->child(i); + if(item->type() == BookmarkItem::Bookmark) { + if(item->href.contains(value.toString())) { + list.append(index(item, 1)); + } + } else if(item->type() == BookmarkItem::Folder) { + list.append(match(index(item), role, value, hits, flags)); + } + } + + + return list; +} diff --git a/lib/bookmarks/bookmarksmodel.h b/lib/bookmarks/bookmarksmodel.h index 7493f1c..9d884fb 100644 --- a/lib/bookmarks/bookmarksmodel.h +++ b/lib/bookmarks/bookmarksmodel.h @@ -30,14 +30,16 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; - QModelIndex index(BookmarkItem *node) const; + QModelIndex index(BookmarkItem *node, int column = 0) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; - bool hasChildren(const QModelIndex &parent) const override ; + bool hasChildren(const QModelIndex &parent) const override; + + QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const override; private: QIcon folderIcon; 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 <QCompleter> - -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 <QLabel> +#include <QListView> +#include <QStringListModel> + 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 <QLineEdit> #include <QTextLayout> #include <QAction> -#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 |