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

136 lines
4.5 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2011-10-17 09:57:07 +02:00
* Copyright (C) 2010-2011 David Rosca
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 "locationbar.h"
#include "iconprovider.h"
#include "mainapplication.h"
2011-03-02 16:57:41 +01:00
2011-03-17 17:03:04 +01:00
LocationCompleter::LocationCompleter(QObject* parent) :
2011-03-02 16:57:41 +01:00
QCompleter(parent)
{
setMaxVisibleItems(6);
QStandardItemModel* completeModel = new QStandardItemModel();
setModel(completeModel);
2011-03-17 17:03:04 +01:00
QTreeView* treeView = new QTreeView;
2011-03-02 16:57:41 +01:00
setPopup(treeView);
treeView->setRootIsDecorated(false);
treeView->header()->hide();
treeView->header()->setStretchLastSection(false);
treeView->header()->setResizeMode(0, QHeaderView::Stretch);
treeView->header()->resizeSection(1,0);
setCompletionMode(QCompleter::PopupCompletion);
setCaseSensitivity(Qt::CaseInsensitive);
setWrapAround(true);
setCompletionColumn(1);
}
//QString LocationCompleter::pathFromIndex(const QModelIndex &index) const
//{
// qDebug() << __FUNCTION__ << "called";
// return QCompleter::pathFromIndex(index);
//}
QStringList LocationCompleter::splitPath(const QString &path) const
{
Q_UNUSED(path);
return QStringList("");
#if 0
QStringList returned = QCompleter::splitPath(path);
QStringList returned2;
QSqlQuery query;
query.exec("SELECT url FROM history WHERE title LIKE '%"+path+"%' OR url LIKE '%"+path+"%' ORDER BY count DESC LIMIT 1");
if (query.next()) {
QString url = query.value(0).toString();
bool titleSearching = false;
if (!url.contains(path))
titleSearching = true;
QString prefix = url.mid(0,url.indexOf(path));
foreach (QString string, returned) {
if (titleSearching)
returned2.append(url);
else
returned2.append(prefix+string);
}
return returned2;
} else {
foreach (QString string, returned)
returned2.append("http://www.google.com/search?client=qupzilla&q=" + string);
return returned2;
}
#endif
}
void LocationCompleter::refreshCompleter(QString string)
{
int limit;
if (string.size() < 3)
limit = 25;
else
limit = 15;
QSqlQuery query;
2011-03-02 16:57:41 +01:00
query.exec("SELECT title, url FROM history WHERE title LIKE '%"+string+"%' OR url LIKE '%"+string+"%' ORDER BY count DESC LIMIT "+QString::number(limit));
int i = 0;
QStandardItemModel* cModel = qobject_cast<QStandardItemModel*>(model());
QTreeView* treeView = qobject_cast<QTreeView*>(popup());
2011-03-02 16:57:41 +01:00
cModel->clear();
while(query.next()) {
QStandardItem* iconText = new QStandardItem();
QStandardItem* findUrl = new QStandardItem();
QString url = query.value(1).toUrl().toEncoded();
iconText->setIcon(_iconForUrl(query.value(1).toUrl()).pixmap(16,16));
2011-03-02 16:57:41 +01:00
iconText->setText(query.value(0).toString().replace("\n","").append("\n"+url));
findUrl->setText(url);
QList<QStandardItem*> items;
items.append(iconText);
items.append(findUrl);
cModel->insertRow(i, items);
i++;
}
// if (i == 0) {
// QStandardItem* iconText = new QStandardItem();
// QStandardItem* findUrl = new QStandardItem();
// QString url("http://www.google.com/search?client=qupzilla&q="+string);
// iconText->setIcon(QIcon(":/icons/menu/google.png"));
// iconText->setText(tr("Search %1 on Google.com\n..........").arg(string));
// findUrl->setText(url);
// QList<QStandardItem*> items;
// items.append(iconText);
// items.append(findUrl);
// cModel->insertRow(i, items);
// }
2011-03-02 16:57:41 +01:00
treeView->header()->setResizeMode(0, QHeaderView::Stretch);
treeView->header()->resizeSection(1,0);
if (i>6)
popup()->setMinimumHeight(190);
else
popup()->setMinimumHeight(0);
2011-03-02 16:57:41 +01:00
popup()->setUpdatesEnabled(true);
}