From 527756748468ddd3d5e7fa1bafcb2e32bf02144b Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sun, 9 Jan 2011 11:40:43 +0100 Subject: Update the completion list when the user switches search engine on the fly. Lionel Chauvin's work. Reviewed by me :) --- src/opensearch/opensearchmanager.cpp | 4 +- src/opensearch/opensearchmanager.h | 2 +- src/opensearch/searchengine.cpp | 9 ----- src/urlbar/completionwidget.cpp | 72 ++++++++++++++++-------------------- src/urlbar/completionwidget.h | 3 +- src/urlbar/listitem.cpp | 11 ++---- src/urlbar/listitem.h | 1 + src/urlbar/urlresolver.cpp | 31 +++++++++++----- src/urlbar/urlresolver.h | 9 +++-- 9 files changed, 68 insertions(+), 74 deletions(-) (limited to 'src') diff --git a/src/opensearch/opensearchmanager.cpp b/src/opensearch/opensearchmanager.cpp index 4ee1de33..9d2a52e4 100644 --- a/src/opensearch/opensearchmanager.cpp +++ b/src/opensearch/opensearchmanager.cpp @@ -171,7 +171,7 @@ void OpenSearchManager::jobFinished(KJob *job) { if (job->error()) { - emit suggestionReceived(_typedText, ResponseList()); + emit suggestionsReceived(_typedText, ResponseList()); m_state = IDLE; return; // just silently return } @@ -185,7 +185,7 @@ void OpenSearchManager::jobFinished(KJob *job) kDebug() << r.title; } - emit suggestionReceived(_typedText, suggestionsList); + emit suggestionsReceived(_typedText, suggestionsList); idleJob(); return; } diff --git a/src/opensearch/opensearchmanager.h b/src/opensearch/opensearchmanager.h index df8e5367..a0543c56 100644 --- a/src/opensearch/opensearchmanager.h +++ b/src/opensearch/opensearchmanager.h @@ -94,7 +94,7 @@ private Q_SLOTS: void jobFinished(KJob *job); Q_SIGNALS: - void suggestionReceived(const QString &text, const ResponseList &suggestion); + void suggestionsReceived(const QString &text, const ResponseList &suggestion); void openSearchEngineAdded(const QString &name, const QString &searchUrl, const QString &fileName); private: diff --git a/src/opensearch/searchengine.cpp b/src/opensearch/searchengine.cpp index 92b3293e..f407789b 100644 --- a/src/opensearch/searchengine.cpp +++ b/src/opensearch/searchengine.cpp @@ -135,15 +135,6 @@ KService::Ptr SearchEngine::fromString(const QString &text) } -QString SearchEngine::extractQuery(const QString &text) -{ - QString query = text; - KService::Ptr engine = SearchEngine::fromString(text); - - return query; -} - - QString SearchEngine::buildQuery(KService::Ptr engine, const QString &text) { if(!engine) diff --git a/src/urlbar/completionwidget.cpp b/src/urlbar/completionwidget.cpp index 087a10a6..0d2026af 100644 --- a/src/urlbar/completionwidget.cpp +++ b/src/urlbar/completionwidget.cpp @@ -66,26 +66,20 @@ CompletionWidget::CompletionWidget(QWidget *parent) } -void CompletionWidget::insertSearchList(const UrlSearchList &list, const QString& text) +void CompletionWidget::insertItems(const UrlSearchList &list, const QString& text, int offset) { - if (!isVisible()) - { - UrlResolver::setSearchEngine(SearchEngine::defaultEngine()); - } - - _list = list; - int i = 0; - foreach(const UrlSearchItem &item, _list) + Q_FOREACH(const UrlSearchItem &item, list) { ListItem *suggestion = ListItemFactory::create(item, text, this); - suggestion->setBackgroundRole(i % 2 ? QPalette::AlternateBase : QPalette::Base); - connect(suggestion, - SIGNAL(itemClicked(ListItem *, Qt::MouseButton, Qt::KeyboardModifiers)), - this, + suggestion->setBackgroundRole(offset % 2 ? QPalette::AlternateBase : QPalette::Base); + connect(suggestion, + SIGNAL(itemClicked(ListItem *, Qt::MouseButton, Qt::KeyboardModifiers)), + this, SLOT(itemChosen(ListItem *, Qt::MouseButton, Qt::KeyboardModifiers))); + connect(suggestion, SIGNAL(updateList()), this, SLOT(updateList())); connect(this, SIGNAL(nextItemSubChoice()), suggestion, SLOT(nextItemSubChoice())); - - suggestion->setObjectName(QString::number(i++)); + + suggestion->setObjectName(QString::number(offset++)); layout()->addWidget(suggestion); } } @@ -99,29 +93,16 @@ void CompletionWidget::updateSearchList(const UrlSearchList &list, const QString if(_hasSuggestions || _typedString != text) return; _hasSuggestions = true; - + if (_resList.count() > 0) { clear(); - insertSearchList(_resList, text); - - UrlSearchList sugList = list.mid(0,4); - // add new suggestions to the list - int offset = _list.count(); - Q_FOREACH(const UrlSearchItem &item, sugList) - { - ListItem *suggestion = ListItemFactory::create(item, text, this); - suggestion->setBackgroundRole(offset % 2 ? QPalette::AlternateBase : QPalette::Base); - connect(suggestion, - SIGNAL(itemClicked(ListItem *, Qt::MouseButton, Qt::KeyboardModifiers)), - this, - SLOT(itemChosen(ListItem *, Qt::MouseButton, Qt::KeyboardModifiers))); - connect(this, SIGNAL(nextItemSubChoice()), suggestion, SLOT(nextItemSubChoice())); - - suggestion->setObjectName(QString::number(offset++)); - layout()->addWidget(suggestion); - } + insertItems(_resList, text); + _list = _resList; + + UrlSearchList sugList = list.mid(0,4); + insertItems(sugList, text, _list.count()); _list.append(sugList); popup(); } @@ -239,7 +220,7 @@ bool CompletionWidget::eventFilter(QObject *obj, QEvent *ev) { ListItem *child; UrlBar *w; - + if (type == QEvent::KeyPress) { QKeyEvent *kev = static_cast(ev); @@ -307,12 +288,12 @@ bool CompletionWidget::eventFilter(QObject *obj, QEvent *ev) host += append; url.setHost(host); } - + emit chosenUrl(url, Rekonq::CurrentTab); } } - + if( _currentIndex == -1) _currentIndex = 0; child = findChild(QString::number(_currentIndex)); @@ -320,7 +301,7 @@ bool CompletionWidget::eventFilter(QObject *obj, QEvent *ev) { kDebug() << "USING LISTITEM URL: " << child->url(); kDebug() << "USING LISTITEM TITLE: " << child->text(); - + //we can use the url of the listitem emit chosenUrl(child->url(), Rekonq::CurrentTab); } @@ -332,7 +313,7 @@ bool CompletionWidget::eventFilter(QObject *obj, QEvent *ev) kev->accept(); hide(); return true; - + case Qt::Key_Escape: hide(); return true; @@ -375,10 +356,16 @@ void CompletionWidget::itemChosen(ListItem *item, Qt::MouseButton button, Qt::Ke } +void CompletionWidget::updateList() +{ + suggestUrls(_typedString); +} + + void CompletionWidget::suggestUrls(const QString &text) { _typedString = text; - + QWidget *w = qobject_cast(parent()); if (!w->hasFocus()) return; @@ -389,6 +376,11 @@ void CompletionWidget::suggestUrls(const QString &text) return; } + if (!isVisible()) + { + UrlResolver::setSearchEngine(SearchEngine::defaultEngine()); + } + UrlResolver *res = new UrlResolver(text); connect(res, SIGNAL(suggestionsReady(const UrlSearchList &, const QString &)), this, SLOT(updateSearchList(const UrlSearchList &, const QString &))); _resList = res->orderedSearchItems(); diff --git a/src/urlbar/completionwidget.h b/src/urlbar/completionwidget.h index 6545b11d..22dfa430 100644 --- a/src/urlbar/completionwidget.h +++ b/src/urlbar/completionwidget.h @@ -56,13 +56,14 @@ public: private slots: void itemChosen(ListItem *item, Qt::MouseButton = Qt::LeftButton, Qt::KeyboardModifiers = Qt::NoModifier); void updateSearchList(const UrlSearchList &list, const QString& text); + void updateList(); signals: void chosenUrl(const KUrl &, Rekonq::OpenType); void nextItemSubChoice(); private: - void insertSearchList(const UrlSearchList &list, const QString& text); + void insertItems(const UrlSearchList &list, const QString& text, int offset = 0); void popup(); void clear(); diff --git a/src/urlbar/listitem.cpp b/src/urlbar/listitem.cpp index d82613f1..f7514f2a 100644 --- a/src/urlbar/listitem.cpp +++ b/src/urlbar/listitem.cpp @@ -401,9 +401,8 @@ SearchListItem::SearchListItem(const UrlSearchItem &item, const QString &text, Q , m_text(text) { m_iconLabel = new IconLabel(SearchEngine::buildQuery(UrlResolver::searchEngine(), ""), this); - QString query = SearchEngine::extractQuery(text); m_titleLabel = new TextLabel(this); - m_titleLabel->setEngineText(item.title, query); + m_titleLabel->setEngineText(UrlResolver::searchEngine()->name(), item.title); m_engineBar = new EngineBar(UrlResolver::searchEngine(), parent); QHBoxLayout *hLayout = new QHBoxLayout; @@ -429,10 +428,8 @@ QString SearchListItem::text() void SearchListItem::changeSearchEngine(KService::Ptr engine) { - m_titleLabel->setEngineText(engine->name(), m_text); - m_iconLabel->setPixmap( Application::iconManager()->iconForUrl(KUrl(engine->property("Query").toString())).pixmap(16) ); - m_url = SearchEngine::buildQuery(engine, m_text); UrlResolver::setSearchEngine(engine); + emit updateList(); } @@ -548,7 +545,7 @@ VisualSuggestionListItem::VisualSuggestionListItem(const UrlSearchItem &item, co QHBoxLayout *hLayout = new QHBoxLayout; hLayout->setSpacing(4); QLabel *previewLabelIcon = new QLabel(this); - + if (!item.image.isEmpty()) { previewLabelIcon->setFixedSize(item.image_width+10, item.image_height+10); @@ -566,7 +563,7 @@ VisualSuggestionListItem::VisualSuggestionListItem(const UrlSearchItem &item, co QVBoxLayout *vLayout = new QVBoxLayout; vLayout->setMargin(0); vLayout->addItem(new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::MinimumExpanding)); - vLayout->addWidget(new TextLabel(item.title, SearchEngine::extractQuery(text), this)); + vLayout->addWidget(new TextLabel(item.title, text, this)); DescriptionLabel *d = new DescriptionLabel("", this); vLayout->addWidget(d); vLayout->addItem(new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::MinimumExpanding)); diff --git a/src/urlbar/listitem.h b/src/urlbar/listitem.h index 0d66a12c..30089ce9 100644 --- a/src/urlbar/listitem.h +++ b/src/urlbar/listitem.h @@ -69,6 +69,7 @@ public slots: signals: void itemClicked(ListItem *item, Qt::MouseButton, Qt::KeyboardModifiers); + void updateList(); protected: virtual void paintEvent(QPaintEvent *event); diff --git a/src/urlbar/urlresolver.cpp b/src/urlbar/urlresolver.cpp index 864eca04..3797f51f 100644 --- a/src/urlbar/urlresolver.cpp +++ b/src/urlbar/urlresolver.cpp @@ -69,6 +69,7 @@ QRegExp UrlResolver::_searchEnginesRegexp; UrlResolver::UrlResolver(const QString &typedUrl) : QObject() , _typedString(typedUrl.trimmed()) + , _typedQuery() { if (!_searchEngine ) setSearchEngine(SearchEngine::defaultEngine()); @@ -339,12 +340,12 @@ void UrlResolver::computeWebSearches() if (engine) { query = query.remove(0, _typedString.indexOf(SearchEngine::delimiter()) + 1); - _searchEngine = engine; + setSearchEngine(engine); } if(_searchEngine) { - UrlSearchItem item = UrlSearchItem(UrlSearchItem::Search, SearchEngine::buildQuery(_searchEngine, query), _searchEngine->name()); + UrlSearchItem item = UrlSearchItem(UrlSearchItem::Search, SearchEngine::buildQuery(_searchEngine, query), query); UrlSearchList list; list << item; _webSearches = list; @@ -394,30 +395,40 @@ void UrlResolver::computeSuggestions() return; } + QString query = _typedString; + KService::Ptr engine = SearchEngine::fromString(_typedString); + if (engine) + { + query = query.remove(0, _typedString.indexOf(SearchEngine::delimiter()) + 1); + setSearchEngine(engine); + } + connect(Application::opensearchManager(), - SIGNAL(suggestionReceived(const QString &, const ResponseList &)), + SIGNAL(suggestionsReceived(const QString &, const ResponseList &)), this, SLOT(suggestionsReceived(const QString &, const ResponseList &))); - Application::opensearchManager()->requestSuggestion(_typedString); + _typedQuery = query; + Application::opensearchManager()->requestSuggestion(query); } void UrlResolver::suggestionsReceived(const QString &text, const ResponseList &suggestions) { - if(text != _typedString) + if(text != _typedQuery) return; UrlSearchList sugList; - + QString urlString; Q_FOREACH(const Response &i, suggestions) { - QString url = i.url; - if (url.isEmpty()) + urlString = i.url; + if (urlString.isEmpty()) { - url = SearchEngine::buildQuery(searchEngine(), i.title); + urlString = SearchEngine::buildQuery(UrlResolver::searchEngine(),i.title); } - UrlSearchItem gItem(UrlSearchItem::Suggestion, url, i.title, i.description, i.image, i.image_width, i.image_height); + + UrlSearchItem gItem(UrlSearchItem::Suggestion, urlString, i.title, i.description, i.image, i.image_width, i.image_height); sugList << gItem; } emit suggestionsReady(sugList, _typedString); diff --git a/src/urlbar/urlresolver.h b/src/urlbar/urlresolver.h index e6ca7703..43a4a43e 100644 --- a/src/urlbar/urlresolver.h +++ b/src/urlbar/urlresolver.h @@ -67,7 +67,7 @@ public: int image_width; int image_height; QString bookmarkPath; - + UrlSearchItem(const UrlSearchItem &item) : type(item.type), url(item.url), title(item.title), @@ -85,7 +85,7 @@ public: image_width(0), image_height(0) {}; - + UrlSearchItem(const int &_type, const QString &_url, const QString &_title = QString(), @@ -106,7 +106,7 @@ public: inline bool operator==(const UrlSearchItem &i) const { return i.url == url;//TODO && i.title == title; - } + } }; typedef QList UrlSearchList; @@ -154,13 +154,14 @@ private: UrlSearchList orderLists(); QString _typedString; + QString _typedQuery; UrlSearchList _webSearches; UrlSearchList _qurlFromUserInput; UrlSearchList _history; UrlSearchList _bookmarks; UrlSearchList _suggestions; - + static QRegExp _browseRegexp; static QRegExp _searchEnginesRegexp; -- cgit v1.2.1