1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

[Bookmarks] Implement searching in BookmarksTreeView

This commit is contained in:
nowrep 2014-02-09 15:44:38 +01:00
parent 395b865d55
commit 549e20a31d
10 changed files with 111 additions and 42 deletions

View File

@ -69,6 +69,11 @@ QList<BookmarkItem*> BookmarkItem::children() const
return m_children;
}
QString BookmarkItem::urlString() const
{
return QString::fromUtf8(m_url.toEncoded());
}
QUrl BookmarkItem::url() const
{
return m_url;

View File

@ -48,6 +48,7 @@ public:
BookmarkItem* parent() const;
QList<BookmarkItem*> children() const;
QString urlString() const;
QUrl url() const;
void setUrl(const QUrl &url);

View File

@ -241,7 +241,7 @@ void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const
case BookmarkItem::Url:
if (parent->title().contains(string, sensitive) ||
parent->url().toString().contains(string, sensitive) ||
parent->urlString().contains(string, sensitive) ||
parent->description().contains(string, sensitive) ||
parent->keyword().compare(string, sensitive) == 0) {
items->append(parent);

View File

@ -68,8 +68,7 @@ void BookmarksManager::setMainWindow(QupZilla* window)
void BookmarksManager::search(const QString &string)
{
// TODO: Enable searching
Q_UNUSED(string)
ui->tree->search(string);
}
void BookmarksManager::bookmarkActivated(BookmarkItem* item)

View File

@ -22,6 +22,7 @@
#include <QApplication>
#include <QMimeData>
#include <QTimer>
#include <QStyle>
//#define BOOKMARKSMODEL_DEBUG
@ -99,6 +100,8 @@ QVariant BookmarksModel::data(const QModelIndex &index, int role) const
return itm->type();
case UrlRole:
return itm->url();
case UrlStringRole:
return itm->urlString();
case TitleRole:
return itm->title();
case DescriptionRole:
@ -314,3 +317,45 @@ void BookmarksModel::bookmarkChanged(BookmarkItem* item)
QModelIndex idx = index(item);
emit dataChanged(idx, idx);
}
// BookmarksFilterModel
BookmarksFilterModel::BookmarksFilterModel(QAbstractItemModel* parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(parent);
setFilterCaseSensitivity(Qt::CaseInsensitive);
m_filterTimer = new QTimer(this);
m_filterTimer->setSingleShot(true);
m_filterTimer->setInterval(500);
connect(m_filterTimer, SIGNAL(timeout()), this, SLOT(startFiltering()));
}
void BookmarksFilterModel::setFilterFixedString(const QString &pattern)
{
m_pattern = pattern;
m_filterTimer->stop();
m_filterTimer->start();
}
bool BookmarksFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
if (index.data(BookmarksModel::TypeRole).toInt() == BookmarkItem::Folder) {
return true;
}
return (index.data(BookmarksModel::TitleRole).toString().contains(m_pattern, filterCaseSensitivity()) ||
index.data(BookmarksModel::UrlStringRole).toString().contains(m_pattern, filterCaseSensitivity()) ||
index.data(BookmarksModel::DescriptionRole).toString().contains(m_pattern, filterCaseSensitivity()) ||
index.data(BookmarksModel::KeywordRole).toString().compare(m_pattern, filterCaseSensitivity()) == 0);
}
void BookmarksFilterModel::startFiltering()
{
QSortFilterProxyModel::setFilterFixedString(m_pattern);
}

View File

@ -19,9 +19,12 @@
#define BOOKMARKSMODEL_H
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include "qz_namespace.h"
class QTimer;
class Bookmarks;
class BookmarkItem;
@ -33,13 +36,14 @@ public:
enum Roles {
TypeRole = Qt::UserRole + 1,
UrlRole = Qt::UserRole + 2,
TitleRole = Qt::UserRole + 3,
DescriptionRole = Qt::UserRole + 4,
KeywordRole = Qt::UserRole + 5,
VisitCountRole = Qt::UserRole + 6,
ExpandedRole = Qt::UserRole + 7,
SidebarExpandedRole = Qt::UserRole + 8,
MaxRole = ExpandedRole
UrlStringRole = Qt::UserRole + 3,
TitleRole = Qt::UserRole + 4,
DescriptionRole = Qt::UserRole + 5,
KeywordRole = Qt::UserRole + 6,
VisitCountRole = Qt::UserRole + 7,
ExpandedRole = Qt::UserRole + 8,
SidebarExpandedRole = Qt::UserRole + 9,
MaxRole = SidebarExpandedRole
};
explicit BookmarksModel(Bookmarks* bookmarks, QObject* parent = 0);
@ -70,7 +74,27 @@ private slots:
private:
Bookmarks* m_bookmarks;
};
class QT_QUPZILLA_EXPORT BookmarksFilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit BookmarksFilterModel(QAbstractItemModel* parent);
public slots:
void setFilterFixedString(const QString &pattern);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
private slots:
void startFiltering();
private:
QString m_pattern;
QTimer* m_filterTimer;
};
#endif // BOOKMARKSMODEL_H

View File

@ -28,9 +28,10 @@ BookmarksTreeView::BookmarksTreeView(QWidget* parent)
: QTreeView(parent)
, m_bookmarks(mApp->bookmarks())
, m_model(m_bookmarks->model())
, m_filter(new BookmarksFilterModel(m_model))
, m_type(BookmarksManagerViewType)
{
setModel(m_model);
setModel(m_filter);
setDragEnabled(true);
setAcceptDrops(true);
setDropIndicatorShown(true);
@ -70,11 +71,6 @@ void BookmarksTreeView::setViewType(BookmarksTreeView::ViewType type)
restoreExpandedState(QModelIndex());
}
BookmarksModel* BookmarksTreeView::model() const
{
return m_model;
}
BookmarkItem* BookmarksTreeView::selectedBookmark() const
{
QList<BookmarkItem*> items = selectedBookmarks();
@ -86,7 +82,7 @@ QList<BookmarkItem*> BookmarksTreeView::selectedBookmarks() const
QList<BookmarkItem*> items;
foreach (const QModelIndex &index, selectionModel()->selectedRows()) {
BookmarkItem* item = m_model->item(index);
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
items.append(item);
}
@ -95,8 +91,8 @@ QList<BookmarkItem*> BookmarksTreeView::selectedBookmarks() const
void BookmarksTreeView::selectBookmark(BookmarkItem* item)
{
QModelIndex col0 = m_model->index(item, 0);
QModelIndex col1 = m_model->index(item, 1);
QModelIndex col0 = m_filter->mapFromSource(m_model->index(item, 0));
QModelIndex col1 = m_filter->mapFromSource(m_model->index(item, 1));
selectionModel()->clearSelection();
selectionModel()->select(col0, QItemSelectionModel::Select);
@ -105,18 +101,23 @@ void BookmarksTreeView::selectBookmark(BookmarkItem* item)
void BookmarksTreeView::ensureBookmarkVisible(BookmarkItem* item)
{
QModelIndex index = m_model->index(item);
QModelIndex parent = m_model->parent(index);
QModelIndex index = m_filter->mapFromSource(m_model->index(item));
QModelIndex parent = m_filter->parent(index);
while (parent.isValid()) {
setExpanded(parent, true);
parent = m_model->parent(parent);
parent = m_filter->parent(parent);
}
}
void BookmarksTreeView::search(const QString &string)
{
m_filter->setFilterFixedString(string);
}
void BookmarksTreeView::indexExpanded(const QModelIndex &parent)
{
BookmarkItem* item = m_model->item(parent);
BookmarkItem* item = m_model->item(m_filter->mapToSource(parent));
switch (m_type) {
case BookmarksManagerViewType:
@ -132,7 +133,7 @@ void BookmarksTreeView::indexExpanded(const QModelIndex &parent)
void BookmarksTreeView::indexCollapsed(const QModelIndex &parent)
{
BookmarkItem* item = m_model->item(parent);
BookmarkItem* item = m_model->item(m_filter->mapToSource(parent));
switch (m_type) {
case BookmarksManagerViewType:
@ -158,9 +159,9 @@ void BookmarksTreeView::createContextMenu(const QPoint &point)
void BookmarksTreeView::restoreExpandedState(const QModelIndex &parent)
{
for (int i = 0; i < m_model->rowCount(parent); ++i) {
QModelIndex index = m_model->index(i, 0, parent);
BookmarkItem* item = m_model->item(index);
for (int i = 0; i < m_filter->rowCount(parent); ++i) {
QModelIndex index = m_filter->index(i, 0, parent);
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
setExpanded(index, m_type == BookmarksManagerViewType ? item->isExpanded() : item->isSidebarExpanded());
restoreExpandedState(index);
}
@ -180,7 +181,7 @@ void BookmarksTreeView::mousePressEvent(QMouseEvent* event)
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
BookmarkItem* item = m_model->item(index);
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
Qt::MouseButtons buttons = event->buttons();
Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
@ -202,7 +203,7 @@ void BookmarksTreeView::mouseDoubleClickEvent(QMouseEvent* event)
QModelIndex index = indexAt(event->pos());
if (index.isValid()) {
BookmarkItem* item = m_model->item(index);
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
Qt::MouseButtons buttons = event->buttons();
Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
@ -222,7 +223,7 @@ void BookmarksTreeView::keyPressEvent(QKeyEvent* event)
if (selectionModel()->selectedRows().count() == 1) {
QModelIndex index = selectionModel()->selectedRows().first();
BookmarkItem* item = m_model->item(index);
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
switch (event->key()) {
case Qt::Key_Return:

View File

@ -25,6 +25,7 @@
class Bookmarks;
class BookmarkItem;
class BookmarksModel;
class BookmarksFilterModel;
class QT_QUPZILLA_EXPORT BookmarksTreeView : public QTreeView
{
@ -41,8 +42,6 @@ public:
ViewType viewType() const;
void setViewType(ViewType type);
BookmarksModel* model() const;
// Returns null if more than one (or zero) bookmarks are selected
BookmarkItem* selectedBookmark() const;
// Returns all selected bookmarks
@ -52,6 +51,9 @@ public:
// Expand up to root item
void ensureBookmarkVisible(BookmarkItem* item);
public slots:
void search(const QString &string);
signals:
// Open bookmark in current tab
void bookmarkActivated(BookmarkItem* item);
@ -81,6 +83,7 @@ private:
Bookmarks* m_bookmarks;
BookmarksModel* m_model;
BookmarksFilterModel* m_filter;
ViewType m_type;
};

View File

@ -38,7 +38,7 @@ BookmarksSidebar::BookmarksSidebar(QupZilla* mainClass, QWidget* parent)
connect(ui->tree, SIGNAL(bookmarkShiftActivated(BookmarkItem*)), this, SLOT(bookmarkShiftActivated(BookmarkItem*)));
connect(ui->tree, SIGNAL(contextMenuRequested(QPoint)), this, SLOT(createContextMenu(QPoint)));
connect(ui->search, SIGNAL(textChanged(QString)), this, SLOT(search(QString)));
connect(ui->search, SIGNAL(textChanged(QString)), ui->tree, SLOT(search(QString)));
}
BookmarksSidebar::~BookmarksSidebar()
@ -46,12 +46,6 @@ BookmarksSidebar::~BookmarksSidebar()
delete ui;
}
void BookmarksSidebar::search(const QString &string)
{
Q_UNUSED(string)
// TODO: Enable searching
}
void BookmarksSidebar::bookmarkActivated(BookmarkItem* item)
{
openBookmark(item);

View File

@ -39,9 +39,6 @@ public:
explicit BookmarksSidebar(QupZilla* mainClass, QWidget* parent = 0);
~BookmarksSidebar();
public slots:
void search(const QString &string);
private slots:
void bookmarkActivated(BookmarkItem* item);
void bookmarkCtrlActivated(BookmarkItem* item);