From b1b27a434e55105e0bf0c4b204c7505655b81c34 Mon Sep 17 00:00:00 2001 From: nowrep Date: Sat, 5 May 2012 16:06:24 +0200 Subject: [PATCH] Possibility to delete history entries from address bar popup completion. - with Delete key --- .../completer/locationcompleterdelegate.cpp | 5 ++-- .../completer/locationcompletermodel.cpp | 30 ++++++++++--------- .../completer/locationcompletermodel.h | 5 ++++ .../completer/locationcompleterview.cpp | 14 +++++++++ 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/lib/navigation/completer/locationcompleterdelegate.cpp b/src/lib/navigation/completer/locationcompleterdelegate.cpp index a71a7c714..20a984590 100644 --- a/src/lib/navigation/completer/locationcompleterdelegate.cpp +++ b/src/lib/navigation/completer/locationcompleterdelegate.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "locationcompleterdelegate.h" #include "locationcompleterview.h" +#include "locationcompletermodel.h" #include "iconprovider.h" #include @@ -78,7 +79,7 @@ void LocationCompleterDelegate::paint(QPainter* painter, const QStyleOptionViewI const int leftTitleEdge = leftPosition + 2; const int rightTitleEdge = rightPosition - m_padding; QRect titleRect(leftTitleEdge, opt.rect.top() + m_padding, rightTitleEdge - leftTitleEdge, titleMetrics.height()); - QString title(titleMetrics.elidedText(index.data(Qt::UserRole).toString(), Qt::ElideRight, titleRect.width())); + QString title(titleMetrics.elidedText(index.data(LocationCompleterModel::TitleRole).toString(), Qt::ElideRight, titleRect.width())); painter->setFont(titleFont); style->drawItemText(painter, titleRect, Qt::AlignLeft | Qt::TextSingleLine, opt.palette, true, title, colorRole); @@ -90,7 +91,7 @@ void LocationCompleterDelegate::paint(QPainter* painter, const QStyleOptionViewI style->drawItemText(painter, linkRect, Qt::TextSingleLine | Qt::AlignLeft, opt.palette, true, link, colorLinkRole); // Draw star to bookmark items - if (index.data(Qt::UserRole + 1).toBool()) { + if (index.data(LocationCompleterModel::BookmarkRole).toBool()) { const QPixmap starPixmap = qIconProvider->bookmarkIcon(); QSize starSize = starPixmap.size(); QPoint pos(rightPosition - starSize.width(), opt.rect.top() + m_padding); diff --git a/src/lib/navigation/completer/locationcompletermodel.cpp b/src/lib/navigation/completer/locationcompletermodel.cpp index 368557331..7b46e8841 100644 --- a/src/lib/navigation/completer/locationcompletermodel.cpp +++ b/src/lib/navigation/completer/locationcompletermodel.cpp @@ -47,7 +47,7 @@ void LocationCompleterModel::refreshCompletions(const QString &string) QList urlList; QSqlQuery query; - query.prepare("SELECT url, title, icon FROM bookmarks WHERE title LIKE ? OR url LIKE ? LIMIT ?"); + query.prepare("SELECT id, url, title, icon FROM bookmarks WHERE title LIKE ? OR url LIKE ? LIMIT ?"); query.addBindValue(searchString); query.addBindValue(searchString); query.addBindValue(limit); @@ -55,20 +55,20 @@ void LocationCompleterModel::refreshCompletions(const QString &string) while (query.next()) { QStandardItem* item = new QStandardItem(); - const QUrl &url = query.value(0).toUrl(); + const QUrl &url = query.value(1).toUrl(); - item->setIcon(qIconProvider->iconFromImage(QImage::fromData(query.value(2).toByteArray()))); + item->setIcon(qIconProvider->iconFromImage(QImage::fromData(query.value(3).toByteArray()))); item->setText(url.toEncoded()); - item->setData(query.value(1), Qt::UserRole); - item->setData(QVariant(true), Qt::UserRole + 1); // From bookmarks - + item->setData(query.value(0), IdRole); + item->setData(query.value(2), TitleRole); + item->setData(QVariant(true), BookmarkRole); appendRow(item); urlList.append(url); } limit -= query.size(); - query.prepare("SELECT url, title FROM history WHERE title LIKE ? OR url LIKE ? ORDER BY count DESC LIMIT ?"); + query.prepare("SELECT id, url, title FROM history WHERE title LIKE ? OR url LIKE ? ORDER BY count DESC LIMIT ?"); query.addBindValue(searchString); query.addBindValue(searchString); query.addBindValue(limit); @@ -76,7 +76,7 @@ void LocationCompleterModel::refreshCompletions(const QString &string) while (query.next()) { QStandardItem* item = new QStandardItem(); - const QUrl &url = query.value(0).toUrl(); + const QUrl &url = query.value(1).toUrl(); if (urlList.contains(url)) { continue; @@ -84,8 +84,9 @@ void LocationCompleterModel::refreshCompletions(const QString &string) item->setIcon(_iconForUrl(url)); item->setText(url.toEncoded()); - item->setData(query.value(1), Qt::UserRole); - item->setData(QVariant(false), Qt::UserRole + 1); + item->setData(query.value(0), IdRole); + item->setData(query.value(2), TitleRole); + item->setData(QVariant(false), BookmarkRole); appendRow(item); } @@ -96,16 +97,17 @@ void LocationCompleterModel::showMostVisited() clear(); QSqlQuery query; - query.exec("SELECT url, title FROM history ORDER BY count DESC LIMIT 15"); + query.exec("SELECT id, url, title FROM history ORDER BY count DESC LIMIT 15"); while (query.next()) { QStandardItem* item = new QStandardItem(); - const QUrl &url = query.value(0).toUrl(); + const QUrl &url = query.value(1).toUrl(); item->setIcon(_iconForUrl(url)); item->setText(url.toEncoded()); - item->setData(query.value(1), Qt::UserRole); - item->setData(QVariant(false), Qt::UserRole + 1); + item->setData(query.value(0), IdRole); + item->setData(query.value(2), TitleRole); + item->setData(QVariant(false), BookmarkRole); appendRow(item); } diff --git a/src/lib/navigation/completer/locationcompletermodel.h b/src/lib/navigation/completer/locationcompletermodel.h index 672b7b54f..6ad18bd33 100644 --- a/src/lib/navigation/completer/locationcompletermodel.h +++ b/src/lib/navigation/completer/locationcompletermodel.h @@ -23,6 +23,11 @@ class LocationCompleterModel : public QStandardItemModel { public: + enum Role { + TitleRole = Qt::UserRole + 1, + BookmarkRole = Qt::UserRole + 2, + IdRole = Qt::UserRole + 3 + }; explicit LocationCompleterModel(QObject* parent = 0); void refreshCompletions(const QString &string); diff --git a/src/lib/navigation/completer/locationcompleterview.cpp b/src/lib/navigation/completer/locationcompleterview.cpp index 350b35bab..d54cbfa3e 100644 --- a/src/lib/navigation/completer/locationcompleterview.cpp +++ b/src/lib/navigation/completer/locationcompleterview.cpp @@ -16,6 +16,9 @@ * along with this program. If not, see . * ============================================================ */ #include "locationcompleterview.h" +#include "locationcompletermodel.h" +#include "mainapplication.h" +#include "history.h" #include #include @@ -116,6 +119,17 @@ bool LocationCompleterView::eventFilter(QObject* object, QEvent* event) } return false; + case Qt::Key_Delete: + if (viewport()->rect().contains(visualRect(curIndex)) && + !curIndex.data(LocationCompleterModel::BookmarkRole).toBool()) { + int id = curIndex.data(LocationCompleterModel::IdRole).toInt(); + model()->removeRow(curIndex.row(), curIndex.parent()); + + mApp->history()->deleteHistoryEntry(id); + return true; + } + break; + case Qt::Key_PageUp: case Qt::Key_PageDown: return false;