1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-23 10:42:11 +02:00
falkonOfficial/src/lib/navigation/locationcompleter.cpp

131 lines
3.9 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
2011-03-03 18:29:20 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
2011-03-02 16:57:41 +01:00
#include "locationcompleter.h"
#include "locationcompleterdelegate.h"
2011-03-02 16:57:41 +01:00
#include "locationbar.h"
#include "iconprovider.h"
#include "mainapplication.h"
2011-03-02 16:57:41 +01:00
#include <QStandardItemModel>
#include <QSqlQuery>
LocationCompleter::LocationCompleter(QObject* parent)
: QCompleter(parent)
2011-03-02 16:57:41 +01:00
{
QStandardItemModel* completeModel = new QStandardItemModel();
setModel(completeModel);
m_listView = new CompleterListView();
m_listView->setItemDelegateForColumn(0, new LocationCompleterDelegate(m_listView));
setPopup(m_listView);
2011-03-02 16:57:41 +01:00
setCompletionMode(QCompleter::PopupCompletion);
setMaxVisibleItems(6);
2011-03-02 16:57:41 +01:00
}
QStringList LocationCompleter::splitPath(const QString &path) const
{
Q_UNUSED(path);
return QStringList();
2011-03-02 16:57:41 +01:00
}
void LocationCompleter::showMostVisited()
{
QStandardItemModel* cModel = qobject_cast<QStandardItemModel*>(model());
cModel->clear();
QSqlQuery query;
query.exec("SELECT url, title FROM history ORDER BY count DESC LIMIT 15");
while (query.next()) {
QStandardItem* item = new QStandardItem();
const QUrl &url = query.value(0).toUrl();
item->setIcon(_iconForUrl(url));
item->setText(url.toEncoded());
item->setData(query.value(1), Qt::UserRole);
cModel->appendRow(item);
}
m_listView->setMinimumHeight(6 * m_listView->rowHeight());
QCompleter::complete();
}
void LocationCompleter::refreshCompleter(const QString &string)
2011-03-02 16:57:41 +01:00
{
int limit = string.size() < 3 ? 25 : 15;
QString searchString = QString("%%1%").arg(string);
QList<QUrl> urlList;
QStandardItemModel* cModel = qobject_cast<QStandardItemModel*>(model());
2011-03-02 16:57:41 +01:00
cModel->clear();
QSqlQuery query;
query.prepare("SELECT url, title, icon FROM bookmarks WHERE title LIKE ? OR url LIKE ? LIMIT ?");
query.addBindValue(searchString);
query.addBindValue(searchString);
query.addBindValue(limit);
query.exec();
while (query.next()) {
QStandardItem* item = new QStandardItem();
const QUrl &url = query.value(0).toUrl();
item->setText(url.toEncoded());
item->setData(query.value(1), Qt::UserRole);
item->setIcon(IconProvider::iconFromImage(QImage::fromData(query.value(2).toByteArray())));
cModel->appendRow(item);
urlList.append(url);
2011-03-02 16:57:41 +01:00
}
limit -= query.size();
query.prepare("SELECT url, title FROM history WHERE title LIKE ? OR url LIKE ? ORDER BY count DESC LIMIT ?");
query.addBindValue(searchString);
query.addBindValue(searchString);
query.addBindValue(limit);
query.exec();
while (query.next()) {
QStandardItem* item = new QStandardItem();
const QUrl &url = query.value(0).toUrl();
if (urlList.contains(url)) {
continue;
}
item->setIcon(_iconForUrl(url));
item->setText(url.toEncoded());
item->setData(query.value(1), Qt::UserRole);
2011-03-02 16:57:41 +01:00
cModel->appendRow(item);
}
2011-03-02 16:57:41 +01:00
if (cModel->rowCount() > 6) {
m_listView->setMinimumHeight(6 * m_listView->rowHeight());
}
else {
m_listView->setMinimumHeight(0);
}
m_listView->setUpdatesEnabled(true);
2011-03-02 16:57:41 +01:00
}