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"
# 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 <QTreeView>
# include <QHeaderView>
# include <QSqlQuery>
2011-12-12 21:14:43 +01:00
LocationCompleter : : LocationCompleter ( QObject * parent )
: QCompleter ( parent )
2011-03-02 16:57:41 +01:00
{
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 ) ;
2011-11-06 17:01:23 +01:00
treeView - > header ( ) - > resizeSection ( 1 , 0 ) ;
2011-03-02 16:57:41 +01:00
setCompletionMode ( QCompleter : : PopupCompletion ) ;
setCaseSensitivity ( Qt : : CaseInsensitive ) ;
setWrapAround ( true ) ;
setCompletionColumn ( 1 ) ;
}
QStringList LocationCompleter : : splitPath ( const QString & path ) const
{
Q_UNUSED ( path ) ;
return QStringList ( " " ) ;
#if 0
QStringList returned = QCompleter : : splitPath ( path ) ;
QStringList returned2 ;
QSqlQuery query ;
2011-11-06 17:01:23 +01:00
query . exec ( " SELECT url FROM history WHERE title LIKE '% " + path + " %' OR url LIKE '% " + path + " %' ORDER BY count DESC LIMIT 1 " ) ;
2011-03-02 16:57:41 +01:00
if ( query . next ( ) ) {
QString url = query . value ( 0 ) . toString ( ) ;
bool titleSearching = false ;
2011-11-06 17:01:23 +01:00
if ( ! url . contains ( path ) ) {
2011-03-02 16:57:41 +01:00
titleSearching = true ;
2011-11-06 17:01:23 +01:00
}
QString prefix = url . mid ( 0 , url . indexOf ( path ) ) ;
2012-01-24 19:12:31 +01:00
foreach ( const QString & string , returned ) {
2011-11-06 17:01:23 +01:00
if ( titleSearching ) {
2011-03-02 16:57:41 +01:00
returned2 . append ( url ) ;
2011-11-06 17:01:23 +01:00
}
else {
returned2 . append ( prefix + string ) ;
}
2011-03-02 16:57:41 +01:00
}
return returned2 ;
2011-11-06 17:01:23 +01:00
}
else {
2012-01-24 19:12:31 +01:00
foreach ( const QString & string , returned )
2011-11-06 17:01:23 +01:00
returned2 . append ( " http://www.google.com/search?client=qupzilla&q= " + string ) ;
2011-03-02 16:57:41 +01:00
return returned2 ;
}
# endif
}
2011-12-11 14:12:40 +01:00
void LocationCompleter : : showMostVisited ( )
{
QSqlQuery query ;
query . exec ( " SELECT title, url FROM history ORDER BY count DESC LIMIT 15 " ) ;
int i = 0 ;
QStandardItemModel * cModel = qobject_cast < QStandardItemModel * > ( model ( ) ) ;
QTreeView * treeView = qobject_cast < QTreeView * > ( popup ( ) ) ;
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 ) ) ;
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 + + ;
}
treeView - > header ( ) - > setResizeMode ( 0 , QHeaderView : : Stretch ) ;
treeView - > header ( ) - > resizeSection ( 1 , 0 ) ;
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
{
int limit ;
2011-11-06 17:01:23 +01:00
if ( string . size ( ) < 3 ) {
2011-03-02 16:57:41 +01:00
limit = 25 ;
2011-11-06 17:01:23 +01:00
}
else {
2011-03-02 16:57:41 +01:00
limit = 15 ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
2011-09-30 21:44:18 +02:00
QSqlQuery query ;
2011-11-06 17:01:23 +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 ) ) ;
2011-03-02 16:57:41 +01:00
int i = 0 ;
2011-10-18 21:07:58 +02:00
QStandardItemModel * cModel = qobject_cast < QStandardItemModel * > ( model ( ) ) ;
QTreeView * treeView = qobject_cast < QTreeView * > ( popup ( ) ) ;
2011-03-02 16:57:41 +01:00
cModel - > clear ( ) ;
2011-11-06 17:01:23 +01:00
while ( query . next ( ) ) {
2011-03-02 16:57:41 +01:00
QStandardItem * iconText = new QStandardItem ( ) ;
QStandardItem * findUrl = new QStandardItem ( ) ;
QString url = query . value ( 1 ) . toUrl ( ) . toEncoded ( ) ;
2011-11-06 17:01:23 +01:00
iconText - > setIcon ( _iconForUrl ( query . value ( 1 ) . toUrl ( ) ) . pixmap ( 16 , 16 ) ) ;
iconText - > setText ( query . value ( 0 ) . toString ( ) . replace ( " \n " , " " ) . append ( " \n " + url ) ) ;
2011-03-02 16:57:41 +01:00
findUrl - > setText ( url ) ;
QList < QStandardItem * > items ;
items . append ( iconText ) ;
items . append ( findUrl ) ;
cModel - > insertRow ( i , items ) ;
i + + ;
}
2011-10-21 23:26:34 +02:00
// 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 ) ;
2011-11-06 17:01:23 +01:00
treeView - > header ( ) - > resizeSection ( 1 , 0 ) ;
2011-03-02 16:57:41 +01:00
2011-11-06 17:01:23 +01:00
if ( i > 6 ) {
2011-03-02 16:57:41 +01:00
popup ( ) - > setMinimumHeight ( 190 ) ;
2011-11-06 17:01:23 +01:00
}
else {
2011-03-02 16:57:41 +01:00
popup ( ) - > setMinimumHeight ( 0 ) ;
2011-11-06 17:01:23 +01:00
}
2011-09-30 21:44:18 +02:00
2011-03-02 16:57:41 +01:00
popup ( ) - > setUpdatesEnabled ( true ) ;
}