2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* 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"
|
2012-04-19 22:42:35 +02:00
|
|
|
#include "locationcompleterdelegate.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "locationbar.h"
|
2011-04-25 20:56:45 +02:00
|
|
|
#include "iconprovider.h"
|
|
|
|
#include "mainapplication.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
|
2011-12-12 21:14:43 +01:00
|
|
|
LocationCompleter::LocationCompleter(QObject* parent)
|
|
|
|
: QCompleter(parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
|
|
|
QStandardItemModel* completeModel = new QStandardItemModel();
|
|
|
|
setModel(completeModel);
|
|
|
|
|
2012-04-20 14:03:08 +02:00
|
|
|
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);
|
2012-04-19 22:42:35 +02:00
|
|
|
setMaxVisibleItems(6);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList LocationCompleter::splitPath(const QString &path) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(path);
|
2012-04-20 14:03:08 +02:00
|
|
|
return QStringList();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-11 14:12:40 +01:00
|
|
|
void LocationCompleter::showMostVisited()
|
|
|
|
{
|
|
|
|
QStandardItemModel* cModel = qobject_cast<QStandardItemModel*>(model());
|
|
|
|
cModel->clear();
|
2012-04-20 14:03:08 +02:00
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.exec("SELECT url, title FROM history ORDER BY count DESC LIMIT 15");
|
|
|
|
|
2011-12-11 14:12:40 +01:00
|
|
|
while (query.next()) {
|
2012-04-20 14:03:08 +02:00
|
|
|
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);
|
2011-12-11 14:12:40 +01:00
|
|
|
|
2012-04-20 14:03:08 +02:00
|
|
|
cModel->appendRow(item);
|
|
|
|
}
|
2011-12-11 14:12:40 +01:00
|
|
|
|
|
|
|
popup()->setMinimumHeight(190);
|
|
|
|
|
|
|
|
QCompleter::complete();
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
void LocationCompleter::refreshCompleter(const QString &string)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2012-04-15 15:41:48 +02:00
|
|
|
int limit = string.size() < 3 ? 25 : 15;
|
|
|
|
QString searchString = QString("%%1%").arg(string);
|
2012-04-20 14:03:08 +02:00
|
|
|
QList<QUrl> urlList;
|
2012-04-15 15:41:48 +02:00
|
|
|
|
2011-10-18 21:07:58 +02:00
|
|
|
QStandardItemModel* cModel = qobject_cast<QStandardItemModel*>(model());
|
2011-03-02 16:57:41 +01:00
|
|
|
cModel->clear();
|
2012-04-15 15:41:48 +02:00
|
|
|
|
|
|
|
QSqlQuery query;
|
2012-04-20 14:03:08 +02:00
|
|
|
query.prepare("SELECT url, title, icon FROM bookmarks WHERE title LIKE ? OR url LIKE ? LIMIT ?");
|
2012-04-15 15:41:48 +02:00
|
|
|
query.addBindValue(searchString);
|
|
|
|
query.addBindValue(searchString);
|
|
|
|
query.addBindValue(limit);
|
|
|
|
query.exec();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
while (query.next()) {
|
2012-04-20 14:03:08 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-04-20 14:03:08 +02:00
|
|
|
limit -= query.size();
|
|
|
|
|
|
|
|
query.prepare("SELECT url, title FROM history WHERE title LIKE ? OR url LIKE ? ORDER BY count DESC LIMIT ?");
|
2012-04-15 15:41:48 +02:00
|
|
|
query.addBindValue(searchString);
|
|
|
|
query.addBindValue(searchString);
|
2012-04-20 14:03:08 +02:00
|
|
|
query.addBindValue(limit);
|
2012-04-15 15:41:48 +02:00
|
|
|
query.exec();
|
|
|
|
|
|
|
|
while (query.next()) {
|
2012-04-20 14:03:08 +02:00
|
|
|
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
|
|
|
|
2012-04-20 14:03:08 +02:00
|
|
|
cModel->appendRow(item);
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-04-20 14:03:08 +02:00
|
|
|
if (cModel->rowCount() > 6) {
|
|
|
|
m_listView->setMinimumHeight(6 * m_listView->rowHeight());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-04-20 14:03:08 +02:00
|
|
|
m_listView->setMinimumHeight(0);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 21:44:18 +02:00
|
|
|
|
2012-04-20 14:03:08 +02:00
|
|
|
m_listView->setUpdatesEnabled(true);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|