From 1111d9fc2c11a351e3011d692293fa45eb9a5d86 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 31 Dec 2017 11:28:08 +0100 Subject: UrlCompleter searches the bookmarks tree (folder href) --- src/lib/bookmarks/xbel.cpp | 7 ++++++- src/lib/navigation/navigation.qbs | 2 ++ src/lib/navigation/urlcompleter.cpp | 26 ++++++++++++++++++++++++++ src/lib/navigation/urlcompleter.h | 25 +++++++++++++++++++++++++ src/lib/navigation/urllineedit.cpp | 5 +---- src/lib/navigation/urllineedit.h | 4 +++- 6 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/lib/navigation/urlcompleter.cpp create mode 100644 src/lib/navigation/urlcompleter.h (limited to 'src/lib') diff --git a/src/lib/bookmarks/xbel.cpp b/src/lib/bookmarks/xbel.cpp index 55392a1..523ed99 100644 --- a/src/lib/bookmarks/xbel.cpp +++ b/src/lib/bookmarks/xbel.cpp @@ -88,7 +88,9 @@ void Xbel::readChildElements(QTreeWidgetItem *parentItem) if(xmlReader.name() == "title") { readTitle(parentItem); } else if(xmlReader.name() == "folder") { - readChildElements(addFolder(parentItem)); + QTreeWidgetItem *item = addFolder(parentItem); + item->setText(1, xmlReader.attributes().value("href").toString()); + readChildElements(item); } else if(xmlReader.name() == "bookmark") { QTreeWidgetItem *item = addBookmark(parentItem); item->setText(1, xmlReader.attributes().value("href").toString()); @@ -169,6 +171,9 @@ void Xbel::writeItem(QTreeWidgetItem *item) QString tagName = item->data(0, Qt::UserRole).toString(); if (tagName == "folder") { xmlWriter.writeStartElement(tagName); + if(!item->text(1).isEmpty()) { + xmlWriter.writeAttribute("href", item->text(1)); + } xmlWriter.writeAttribute("folded", treeWidget->isItemExpanded(item) ? "no" : "yes"); xmlWriter.writeTextElement("title", item->text(0)); for (int i = 0; i < item->childCount(); ++i) { diff --git a/src/lib/navigation/navigation.qbs b/src/lib/navigation/navigation.qbs index a58f86d..7719fbc 100644 --- a/src/lib/navigation/navigation.qbs +++ b/src/lib/navigation/navigation.qbs @@ -17,6 +17,8 @@ Project { files: [ "navigationbutton.cpp", "navigationbutton.h", + "urlcompleter.cpp", + "urlcompleter.h", "urllineedit.cpp", "urllineedit.h", ] diff --git a/src/lib/navigation/urlcompleter.cpp b/src/lib/navigation/urlcompleter.cpp new file mode 100644 index 0000000..bbde297 --- /dev/null +++ b/src/lib/navigation/urlcompleter.cpp @@ -0,0 +1,26 @@ +/* + * 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/src/lib/navigation/urlcompleter.h b/src/lib/navigation/urlcompleter.h new file mode 100644 index 0000000..f2c52ff --- /dev/null +++ b/src/lib/navigation/urlcompleter.h @@ -0,0 +1,25 @@ +/* + * 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/src/lib/navigation/urllineedit.cpp b/src/lib/navigation/urllineedit.cpp index c9f11c6..4dc6b39 100644 --- a/src/lib/navigation/urllineedit.cpp +++ b/src/lib/navigation/urllineedit.cpp @@ -83,11 +83,8 @@ QAction *UrlLineEdit::pageAction() void UrlLineEdit::setCompleterModel(QAbstractItemModel *model) { Q_CHECK_PTR(model); - QCompleter *m_completer = new QCompleter(this); - m_completer->setCompletionMode(QCompleter::PopupCompletion); - m_completer->setModel(model); + m_completer = new UrlCompleter(model, this); m_completer->setCompletionColumn(1); - this->setCompleter(m_completer); } diff --git a/src/lib/navigation/urllineedit.h b/src/lib/navigation/urllineedit.h index 9dd73ab..46366a7 100644 --- a/src/lib/navigation/urllineedit.h +++ b/src/lib/navigation/urllineedit.h @@ -13,6 +13,8 @@ #include #include +#include "urlcompleter.h" + class QAbstractItemModel; class QMenu; class QLabel; @@ -51,7 +53,7 @@ private: QMenu *m_sslMenu; QLabel *m_sslLabel; - QCompleter *m_completer; + UrlCompleter *m_completer; }; #endif // URLLINEEDIT_H -- cgit v1.2.1