1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

LocationBar: Add LoadAction

It represents action to be taken after submitting text in LocationBar.
Use it from LocationCompleterDelegate instead of duplicating the logic.

This fixes showing incorrect indication when typing bookmark keyword
and when search suggestions begins with search engine shortcut.
This commit is contained in:
David Rosca 2018-01-25 15:19:10 +01:00
parent 4140aae6d2
commit 8d45c910bf
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
4 changed files with 97 additions and 62 deletions

View File

@ -92,7 +92,19 @@ void LocationCompleterDelegate::paint(QPainter* painter, const QStyleOptionViewI
const bool isVisitSearchItem = index.data(LocationCompleterModel::VisitSearchItemRole).toBool();
const bool isSearchSuggestion = index.data(LocationCompleterModel::SearchSuggestionRole).toBool();
const bool isWebSearch = qzSettings->searchFromAddressBar && !isUrlOrDomain(m_originalText.trimmed());
bool isWebSearch = false;
LocationBar::LoadAction loadAction;
if (isVisitSearchItem) {
loadAction = LocationBar::loadAction(m_originalText);
isWebSearch = loadAction.type == LocationBar::LoadAction::Search;
if (!isWebSearch && loadAction.type == LocationBar::LoadAction::Url) {
isWebSearch = !isUrlOrDomain(m_originalText.trimmed());
}
} else if (isSearchSuggestion) {
isWebSearch = true;
}
// Draw icon
const int iconSize = 16;
@ -102,6 +114,9 @@ void LocationCompleterDelegate::paint(QPainter* painter, const QStyleOptionViewI
if (isSearchSuggestion || (isVisitSearchItem && isWebSearch)) {
pixmap = QIcon::fromTheme(QSL("edit-find"), QIcon(QSL(":icons/menu/search-icon.svg"))).pixmap(iconSize, iconMode);
}
if (loadAction.type == LocationBar::LoadAction::Bookmark) {
pixmap = IconProvider::instance()->bookmarkIcon().pixmap(iconSize, iconMode);
}
painter->drawPixmap(iconRect, pixmap);
leftPosition = iconRect.right() + m_padding * 2;
@ -151,12 +166,7 @@ void LocationCompleterDelegate::paint(QPainter* painter, const QStyleOptionViewI
if (!isSearchSuggestion && !isWebSearch) {
link = tr("Visit");
} else if (opt.state.testFlag(QStyle::State_Selected) || opt.state.testFlag(QStyle::State_MouseOver)) {
QString searchEngineName;
const int firstSpacePos = title.indexOf(QL1C(' '));
if (firstSpacePos != -1) {
const QString shortcut = title.left(firstSpacePos);
searchEngineName = mApp->searchEnginesManager()->engineForShortcut(shortcut).name;
}
QString searchEngineName = loadAction.searchEngine.name;
if (searchEngineName.isEmpty()) {
searchEngineName = LocationBar::searchEngine().name;
}

View File

@ -179,54 +179,6 @@ void LocationBar::showDomainCompletion(const QString &completion)
completer()->complete();
}
LoadRequest LocationBar::createLoadRequest() const
{
LoadRequest req;
const QString &t = text().trimmed();
// Check for Search Engine shortcut
int firstSpacePos = t.indexOf(QLatin1Char(' '));
if (firstSpacePos != -1) {
const QString shortcut = t.left(firstSpacePos);
const QString searchedString = t.mid(firstSpacePos).trimmed();
SearchEngine en = mApp->searchEnginesManager()->engineForShortcut(shortcut);
if (!en.name.isEmpty()) {
req = mApp->searchEnginesManager()->searchResult(en, searchedString);
}
}
// Check for Bookmark keyword
QList<BookmarkItem*> items = mApp->bookmarks()->searchKeyword(t);
if (!items.isEmpty()) {
BookmarkItem* item = items.at(0);
item->updateVisitCount();
req.setUrl(item->url());
}
if (!req.isValid()) {
// One word needs special handling, because QUrl::fromUserInput
// would convert it to QUrl("http://WORD")
if (t != QL1S("localhost") && !t.contains(QL1C(' ')) && !t.contains(QL1C('.'))) {
req.setUrl(QUrl(t));
} else {
const QUrl &guessed = QUrl::fromUserInput(t);
if (!guessed.isEmpty())
req.setUrl(guessed);
else
req.setUrl(QUrl::fromEncoded(t.toUtf8()));
}
}
// Search when creating url failed
if (!req.isValid()) {
req = mApp->searchEnginesManager()->searchResult(t);
}
return req;
}
QString LocationBar::convertUrlToText(const QUrl &url)
{
// It was most probably entered by user, so don't urlencode it
@ -246,7 +198,7 @@ QString LocationBar::convertUrlToText(const QUrl &url)
SearchEnginesManager::Engine LocationBar::searchEngine()
{
if (!qzSettings->searchFromAddressBar) {
return SearchEnginesManager::Engine();
return SearchEngine();
} else if (qzSettings->searchWithDefaultEngine) {
return mApp->searchEnginesManager()->defaultEngine();
} else {
@ -254,6 +206,61 @@ SearchEnginesManager::Engine LocationBar::searchEngine()
}
}
LocationBar::LoadAction LocationBar::loadAction(const QString &text)
{
LoadAction action;
const QString &t = text.trimmed();
// Check for Search Engine shortcut
const int firstSpacePos = t.indexOf(QLatin1Char(' '));
if (qzSettings->searchFromAddressBar && firstSpacePos != -1) {
const QString shortcut = t.left(firstSpacePos);
const QString searchedString = t.mid(firstSpacePos).trimmed();
SearchEngine en = mApp->searchEnginesManager()->engineForShortcut(shortcut);
if (en.isValid()) {
action.type = LoadAction::Search;
action.searchEngine = en;
action.loadRequest = mApp->searchEnginesManager()->searchResult(en, searchedString);
return action;
}
}
// Check for Bookmark keyword
const QList<BookmarkItem*> items = mApp->bookmarks()->searchKeyword(t);
if (!items.isEmpty()) {
BookmarkItem* item = items.at(0);
action.type = LoadAction::Bookmark;
action.bookmark = item;
action.loadRequest.setUrl(item->url());
return action;
}
// Otherwise load as url
action.type = LoadAction::Url;
// One word needs special handling, because QUrl::fromUserInput
// would convert it to QUrl("http://WORD")
if (t != QL1S("localhost") && !QzTools::containsSpace(t) && !t.contains(QL1C('.'))) {
action.loadRequest.setUrl(QUrl(t));
} else {
const QUrl &guessed = QUrl::fromUserInput(t);
if (!guessed.isEmpty())
action.loadRequest.setUrl(guessed);
else
action.loadRequest.setUrl(QUrl::fromEncoded(t.toUtf8()));
}
// Search when creating url failed
if (qzSettings->searchFromAddressBar && !action.loadRequest.isValid()) {
action.type = LoadAction::Search;
action.loadRequest = mApp->searchEnginesManager()->searchResult(t);
}
return action;
}
void LocationBar::refreshTextFormat()
{
if (!m_webView) {
@ -295,7 +302,7 @@ void LocationBar::refreshTextFormat()
void LocationBar::requestLoadUrl()
{
loadRequest(createLoadRequest());
loadRequest(loadAction(text()).loadRequest);
}
void LocationBar::textEdited(const QString &text)
@ -531,7 +538,7 @@ void LocationBar::keyPressEvent(QKeyEvent* event)
case Qt::AltModifier:
m_completer->closePopup();
m_window->tabWidget()->addView(createLoadRequest());
m_window->tabWidget()->addView(loadAction(text()).loadRequest);
m_holdingAlt = false;
break;

View File

@ -21,6 +21,7 @@
#include "qzcommon.h"
#include "lineedit.h"
#include "searchenginesmanager.h"
#include "loadrequest.h"
class QStringListModel;
@ -32,7 +33,7 @@ class BookmarksIcon;
class SiteIcon;
class GoIcon;
class AutoFillIcon;
class LoadRequest;
class BookmarkItem;
class FALKON_EXPORT LocationBar : public LineEdit
{
@ -41,11 +42,25 @@ class FALKON_EXPORT LocationBar : public LineEdit
public:
explicit LocationBar(BrowserWindow* window);
struct LoadAction {
enum Type {
Invalid = 0,
Search,
Bookmark,
Url
};
Type type = Invalid;
SearchEngine searchEngine;
BookmarkItem *bookmark = nullptr;
LoadRequest loadRequest;
};
TabbedWebView* webView() const;
void setWebView(TabbedWebView* view);
static QString convertUrlToText(const QUrl &url);
static SearchEnginesManager::Engine searchEngine();
static SearchEngine searchEngine();
static LoadAction loadAction(const QString &text);
public slots:
void setText(const QString &text);
@ -89,7 +104,6 @@ private:
void dropEvent(QDropEvent* event);
void paintEvent(QPaintEvent* event);
LoadRequest createLoadRequest() const;
void refreshTextFormat();
LocationCompleter* m_completer;

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
*
* 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
@ -47,6 +47,10 @@ public:
QByteArray suggestionsParameters;
QByteArray postData;
bool isValid() const {
return !name.isEmpty() && !url.isEmpty();
}
bool operator==(const Engine &other) const {
return (this->name == other.name &&
this->url == other.url &&