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

640 lines
18 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2014 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 "locationbar.h"
#include "browserwindow.h"
#include "tabbedwebview.h"
2011-03-02 16:57:41 +01:00
#include "mainapplication.h"
#include "webpage.h"
#include "tabwidget.h"
#include "bookmarksicon.h"
#include "siteicon.h"
#include "goicon.h"
#include "rssicon.h"
#include "downicon.h"
#include "qztools.h"
#include "iconprovider.h"
#include "qzsettings.h"
#include "colors.h"
#include "autofillicon.h"
#include "searchenginesmanager.h"
#include "completer/locationcompleter.h"
2011-03-02 16:57:41 +01:00
#include <QTimer>
#include <QMimeData>
#include <QCompleter>
#include <QStringListModel>
#include <QContextMenuEvent>
LocationBar::LocationBar(BrowserWindow* window)
: LineEdit(window)
, m_window(window)
, m_webView(0)
2012-01-21 19:26:47 +01:00
, m_pasteAndGoAction(0)
, m_clearAction(0)
, m_holdingAlt(false)
, m_loadProgress(0)
, m_progressVisible(false)
2011-03-02 16:57:41 +01:00
{
setObjectName("locationbar");
setDragEnabled(true);
2011-03-02 16:57:41 +01:00
// Disable Oxygen QLineEdit transitions, it breaks with setText() && home()
setProperty("_kde_no_animations", QVariant(true));
m_bookmarkIcon = new BookmarksIcon(this);
m_goIcon = new GoIcon(this);
m_rssIcon = new RssIcon(this);
m_siteIcon = new SiteIcon(m_window, this);
m_autofillIcon = new AutoFillIcon(this);
DownIcon* down = new DownIcon(this);
2011-03-02 16:57:41 +01:00
addWidget(m_siteIcon, LineEdit::LeftSide);
addWidget(m_autofillIcon, LineEdit::RightSide);
addWidget(m_bookmarkIcon, LineEdit::RightSide);
2011-03-02 16:57:41 +01:00
addWidget(m_rssIcon, LineEdit::RightSide);
addWidget(m_goIcon, LineEdit::RightSide);
addWidget(down, LineEdit::RightSide);
2011-03-02 16:57:41 +01:00
m_completer = new LocationCompleter(this);
m_completer->setMainWindow(m_window);
m_completer->setLocationBar(this);
connect(m_completer, SIGNAL(showCompletion(QString)), this, SLOT(showCompletion(QString)));
connect(m_completer, SIGNAL(showDomainCompletion(QString)), this, SLOT(showDomainCompletion(QString)));
connect(m_completer, SIGNAL(loadCompletion()), this, SLOT(requestLoadUrl()));
connect(m_completer, SIGNAL(clearCompletion()), this, SLOT(clearCompletion()));
m_domainCompleterModel = new QStringListModel(this);
QCompleter* domainCompleter = new QCompleter(this);
domainCompleter->setCompletionMode(QCompleter::InlineCompletion);
domainCompleter->setModel(m_domainCompleterModel);
setCompleter(domainCompleter);
2011-03-02 16:57:41 +01:00
connect(this, SIGNAL(textEdited(QString)), this, SLOT(textEditted()));
connect(m_goIcon, SIGNAL(clicked(QPoint)), this, SLOT(requestLoadUrl()));
connect(down, SIGNAL(clicked(QPoint)), m_completer, SLOT(showMostVisited()));
connect(mApp->searchEnginesManager(), SIGNAL(activeEngineChanged()), this, SLOT(updatePlaceHolderText()));
connect(mApp->searchEnginesManager(), SIGNAL(defaultEngineChanged()), this, SLOT(updatePlaceHolderText()));
connect(mApp, SIGNAL(settingsReloaded()), SLOT(loadSettings()));
loadSettings();
updateSiteIcon();
// Hide icons by default
m_goIcon->setVisible(qzSettings->alwaysShowGoIcon);
m_rssIcon->hide();
m_autofillIcon->hide();
QTimer::singleShot(0, this, SLOT(updatePlaceHolderText()));
}
TabbedWebView* LocationBar::webView() const
{
return m_webView;
}
void LocationBar::setWebView(TabbedWebView* view)
{
m_webView = view;
m_bookmarkIcon->setWebView(m_webView);
m_rssIcon->setWebView(m_webView);
m_siteIcon->setWebView(m_webView);
m_autofillIcon->setWebView(m_webView);
connect(m_webView, SIGNAL(loadStarted()), SLOT(loadStarted()));
connect(m_webView, SIGNAL(loadProgress(int)), SLOT(loadProgress(int)));
connect(m_webView, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
connect(m_webView, SIGNAL(iconChanged()), this, SLOT(updateSiteIcon()));
connect(m_webView, SIGNAL(urlChanged(QUrl)), this, SLOT(showUrl(QUrl)));
connect(m_webView, SIGNAL(rssChanged(bool)), this, SLOT(setRssIconVisible(bool)));
connect(m_webView, SIGNAL(privacyChanged(bool)), this, SLOT(setPrivacyState(bool)));
connect(this, SIGNAL(loadUrl(QUrl)), m_webView, SLOT(userLoadAction(QUrl)));
}
void LocationBar::setText(const QString &text)
{
LineEdit::setText(text);
refreshTextFormat();
}
void LocationBar::updatePlaceHolderText()
{
QString engineName = qzSettings->searchWithDefaultEngine ?
mApp->searchEnginesManager()->defaultEngine().name :
mApp->searchEnginesManager()->activeEngine().name;
setPlaceholderText(tr("Enter URL address or search on %1").arg(engineName));
2011-03-02 16:57:41 +01:00
}
void LocationBar::showCompletion(const QString &completion)
{
LineEdit::setText(completion);
// Move cursor to the end
end(false);
}
void LocationBar::clearCompletion()
{
m_webView->setFocus();
showUrl(m_webView->url());
}
void LocationBar::showDomainCompletion(const QString &completion)
{
m_domainCompleterModel->setStringList(QStringList() << completion);
}
QUrl LocationBar::createUrl() const
{
QUrl urlToLoad;
// Check for Search Engine shortcut
int firstSpacePos = text().indexOf(QLatin1Char(' '));
if (firstSpacePos != -1) {
QString shortcut = text().left(firstSpacePos);
QString searchedString = QUrl::toPercentEncoding(text().mid(firstSpacePos).trimmed());
SearchEngine en = mApp->searchEnginesManager()->engineForShortcut(shortcut);
if (!en.name.isEmpty()) {
urlToLoad = QUrl::fromEncoded(en.url.replace(QLatin1String("%s"), searchedString).toUtf8());
}
}
if (urlToLoad.isEmpty()) {
QUrl guessedUrl = WebView::guessUrlFromString(text());
if (!guessedUrl.isEmpty()) {
urlToLoad = guessedUrl;
}
else {
urlToLoad = QUrl::fromEncoded(text().toUtf8());
}
}
return urlToLoad;
}
QString LocationBar::convertUrlToText(const QUrl &url) const
{
// It was most probably entered by user, so don't urlencode it
if (url.scheme().isEmpty()) {
return url.toString();
}
QString stringUrl = QzTools::urlEncodeQueryString(url);
if (stringUrl == QLatin1String("qupzilla:speeddial") || stringUrl == QLatin1String("about:blank")) {
stringUrl.clear();
}
return stringUrl;
}
void LocationBar::refreshTextFormat()
{
if (!m_webView) {
return;
}
TextFormat textFormat;
const QString hostName = m_webView->url().isEmpty() ? QUrl(text()).host() : m_webView->url().host();
if (!hostName.isEmpty()) {
const int hostPos = text().indexOf(hostName);
if (hostPos > 0) {
QTextCharFormat format;
format.setForeground(Colors::mid(palette().color(QPalette::Base), palette().color(QPalette::Text), 1, 1));
QTextLayout::FormatRange schemePart;
schemePart.start = 0;
schemePart.length = hostPos;
schemePart.format = format;
QTextLayout::FormatRange hostPart;
hostPart.start = hostPos;
hostPart.length = hostName.size();
QTextLayout::FormatRange remainingPart;
remainingPart.start = hostPos + hostName.size();
remainingPart.length = text().size() - remainingPart.start;
remainingPart.format = format;
textFormat.append(schemePart);
textFormat.append(hostPart);
textFormat.append(remainingPart);
}
}
setTextFormat(textFormat);
}
void LocationBar::requestLoadUrl()
{
const QUrl url = createUrl();
const QString urlString = convertUrlToText(url);
m_completer->closePopup();
m_webView->setFocus();
if (urlString != text()) {
setText(urlString);
}
emit loadUrl(url);
}
void LocationBar::textEditted()
2011-03-02 16:57:41 +01:00
{
if (!text().isEmpty()) {
m_completer->complete(text());
}
else {
m_completer->closePopup();
}
setGoIconVisible(true);
2011-03-02 16:57:41 +01:00
}
void LocationBar::setGoIconVisible(bool state)
2011-03-02 16:57:41 +01:00
{
if (state) {
m_bookmarkIcon->hide();
m_rssIcon->hide();
m_goIcon->show();
}
else {
m_rssIcon->setVisible(m_webView->hasRss());
m_bookmarkIcon->show();
if (!qzSettings->alwaysShowGoIcon) {
m_goIcon->hide();
}
}
updateTextMargins();
2011-03-02 16:57:41 +01:00
}
void LocationBar::setRssIconVisible(bool state)
{
m_rssIcon->setVisible(state);
updateTextMargins();
}
void LocationBar::showUrl(const QUrl &url)
2011-03-02 16:57:41 +01:00
{
if (hasFocus() || url.isEmpty()) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
const QString stringUrl = convertUrlToText(url);
if (text() == stringUrl) {
home(false);
return;
2011-03-02 16:57:41 +01:00
}
// Set converted url as text
setText(convertUrlToText(url));
// Move cursor to the start
home(false);
m_bookmarkIcon->checkBookmark(url);
2011-03-02 16:57:41 +01:00
}
void LocationBar::updateSiteIcon()
2011-03-02 16:57:41 +01:00
{
const QIcon icn = m_webView ? m_webView->icon() : QIcon();
2011-03-02 16:57:41 +01:00
if (icn.isNull()) {
m_siteIcon->setIcon(IconProvider::emptyWebIcon());
}
else {
m_siteIcon->setIcon(QIcon(icn.pixmap(16, 16)));
2011-03-02 16:57:41 +01:00
}
}
void LocationBar::setPrivacyState(bool state)
2011-03-02 16:57:41 +01:00
{
m_siteIcon->setProperty("secured", QVariant(state));
m_siteIcon->style()->unpolish(m_siteIcon);
m_siteIcon->style()->polish(m_siteIcon);
setProperty("secured", QVariant(state));
style()->unpolish(this);
style()->polish(this);
2011-03-02 16:57:41 +01:00
}
2012-01-21 19:26:47 +01:00
void LocationBar::pasteAndGo()
{
clear();
paste();
requestLoadUrl();
2012-01-21 19:26:47 +01:00
}
void LocationBar::contextMenuEvent(QContextMenuEvent* event)
{
if (!m_pasteAndGoAction) {
m_pasteAndGoAction = new QAction(QIcon::fromTheme("edit-paste"), tr("Paste And &Go"), this);
m_pasteAndGoAction->setShortcut(QKeySequence("Ctrl+Shift+V"));
connect(m_pasteAndGoAction, SIGNAL(triggered()), this, SLOT(pasteAndGo()));
2012-01-21 19:26:47 +01:00
}
QMenu* menu = createContextMenu(m_pasteAndGoAction);
menu->setAttribute(Qt::WA_DeleteOnClose);
2012-01-21 19:26:47 +01:00
// Prevent choosing first option with double rightclick
QPoint pos = event->globalPos();
pos.setY(pos.y() + 1);
menu->popup(pos);
2012-01-21 19:26:47 +01:00
}
void LocationBar::showEvent(QShowEvent* event)
{
LineEdit::showEvent(event);
refreshTextFormat();
}
void LocationBar::focusInEvent(QFocusEvent* event)
{
if (m_webView) {
const QString stringUrl = convertUrlToText(m_webView->url());
// Text has been edited, let's show go button
if (stringUrl != text()) {
setGoIconVisible(true);
}
}
clearTextFormat();
LineEdit::focusInEvent(event);
}
void LocationBar::focusOutEvent(QFocusEvent* event)
{
// Context menu or completer popup were opened
// Let's block focusOutEvent to trick QLineEdit and paint cursor properly
if (event->reason() == Qt::PopupFocusReason) {
return;
}
LineEdit::focusOutEvent(event);
setGoIconVisible(false);
if (text().trimmed().isEmpty()) {
clear();
}
refreshTextFormat();
}
2011-03-17 17:03:04 +01:00
void LocationBar::dropEvent(QDropEvent* event)
2011-03-02 16:57:41 +01:00
{
if (event->mimeData()->hasUrls()) {
QUrl dropUrl = event->mimeData()->urls().at(0);
if (WebView::isUrlValid(dropUrl)) {
setText(dropUrl.toString());
m_webView->setFocus();
emit loadUrl(dropUrl);
QFocusEvent event(QFocusEvent::FocusOut);
LineEdit::focusOutEvent(&event);
2011-03-02 16:57:41 +01:00
return;
}
}
else if (event->mimeData()->hasText()) {
QUrl dropUrl = QUrl(event->mimeData()->text().trimmed());
2011-03-02 16:57:41 +01:00
if (WebView::isUrlValid(dropUrl)) {
setText(dropUrl.toString());
m_webView->setFocus();
emit loadUrl(dropUrl);
QFocusEvent event(QFocusEvent::FocusOut);
LineEdit::focusOutEvent(&event);
2011-03-02 16:57:41 +01:00
return;
}
}
LineEdit::dropEvent(event);
2011-03-02 16:57:41 +01:00
}
void LocationBar::keyPressEvent(QKeyEvent* event)
2011-03-02 16:57:41 +01:00
{
switch (event->key()) {
case Qt::Key_V:
if (event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) {
pasteAndGo();
event->accept();
return;
}
break;
case Qt::Key_Down:
m_completer->complete(text());
break;
case Qt::Key_Left:
m_completer->closePopup();
break;
case Qt::Key_Escape:
m_webView->setFocus();
showUrl(m_webView->url());
2011-03-02 16:57:41 +01:00
event->accept();
break;
2011-03-02 16:57:41 +01:00
case Qt::Key_Alt:
m_holdingAlt = true;
break;
2011-03-02 16:57:41 +01:00
case Qt::Key_Return:
case Qt::Key_Enter:
switch (event->modifiers()) {
case Qt::ControlModifier:
setText(text().append(QLatin1String(".com")));
requestLoadUrl();
m_holdingAlt = false;
break;
case Qt::AltModifier:
m_completer->closePopup();
m_window->tabWidget()->addView(createUrl());
m_holdingAlt = false;
break;
default:
requestLoadUrl();
m_holdingAlt = false;
}
break;
case Qt::Key_0:
case Qt::Key_1:
case Qt::Key_2:
case Qt::Key_3:
case Qt::Key_4:
case Qt::Key_5:
case Qt::Key_6:
case Qt::Key_7:
case Qt::Key_8:
case Qt::Key_9:
if (event->modifiers() & Qt::AltModifier || event->modifiers() & Qt::ControlModifier) {
event->ignore();
m_holdingAlt = false;
return;
}
break;
default:
m_holdingAlt = false;
}
LineEdit::keyPressEvent(event);
2011-03-02 16:57:41 +01:00
}
void LocationBar::keyReleaseEvent(QKeyEvent* event)
{
QString localDomain = tr(".co.uk", "Append domain name on ALT + Enter = Should be different for every country");
if (event->key() == Qt::Key_Alt && m_holdingAlt && qzSettings->addCountryWithAlt &&
!text().endsWith(localDomain) && !text().endsWith(QLatin1Char('/'))) {
LineEdit::setText(text().append(localDomain));
}
LineEdit::keyReleaseEvent(event);
}
void LocationBar::loadStarted()
{
m_progressVisible = true;
m_autofillIcon->hide();
m_siteIcon->setIcon(IconProvider::emptyWebIcon());
}
void LocationBar::loadProgress(int progress)
{
if (qzSettings->showLoadingProgress) {
m_loadProgress = progress;
update();
}
}
void LocationBar::loadFinished()
{
if (qzSettings->showLoadingProgress) {
QTimer::singleShot(700, this, SLOT(hideProgress()));
}
WebPage* page = qobject_cast<WebPage*>(m_webView->page());
if (page && page->hasMultipleUsernames()) {
m_autofillIcon->setFormData(page->autoFillData());
m_autofillIcon->show();
}
updateSiteIcon();
}
void LocationBar::loadSettings()
{
Settings settings;
settings.beginGroup("AddressBar");
m_progressStyle = static_cast<ProgressStyle>(settings.value("ProgressStyle", 0).toInt());
bool customColor = settings.value("UseCustomProgressColor", false).toBool();
m_progressColor = customColor ? settings.value("CustomProgressColor", palette().color(QPalette::Highlight)).value<QColor>() : QColor();
settings.endGroup();
}
void LocationBar::hideProgress()
{
if (qzSettings->showLoadingProgress && m_loadProgress == 100) {
m_progressVisible = false;
update();
}
}
void LocationBar::paintEvent(QPaintEvent* event)
{
if (qzSettings->showLoadingProgress && m_progressVisible) {
QStyleOptionFrameV3 option;
initStyleOption(&option);
int lm, tm, rm, bm;
getTextMargins(&lm, &tm, &rm, &bm);
QRect contentsRect = style()->subElementRect(QStyle::SE_LineEditContents, &option, this);
contentsRect.adjust(lm, tm, -rm, -bm);
LineEdit::paintEvent(event);
QColor bg = m_progressColor;
if (!bg.isValid() || bg.alpha() == 0) {
bg = Colors::mid(palette().color(QPalette::Base), palette().color(QPalette::Text), m_progressStyle > 0 ? 4 : 8, 1);
}
QPainter p(this);
p.setBrush(QBrush(bg));
QPen outlinePen(bg.darker(110), 0.8);
p.setPen(outlinePen);
switch (m_progressStyle) {
case ProgressFilled: {
QRect bar = contentsRect.adjusted(0, 1, 0, -1);
bar.setWidth(bar.width() * m_loadProgress / 100);
const int roundness = bar.height() / 4.0;
p.drawRoundedRect(bar, roundness, roundness);
break;
}
case ProgressBottom: {
outlinePen.setWidthF(0.3);
outlinePen.setColor(outlinePen.color().darker(130));
p.setPen(outlinePen);
QRect bar(contentsRect.x(), contentsRect.bottom() - 3,
contentsRect.width() * m_loadProgress / 100.0, 3);
p.drawRoundedRect(bar, 1, 1);
break;
}
case ProgressTop: {
outlinePen.setWidthF(0.3);
outlinePen.setColor(outlinePen.color().darker(130));
p.setPen(outlinePen);
QRect bar(contentsRect.x(), contentsRect.top() + 1, contentsRect.width() * m_loadProgress / 100.0, 3);
p.drawRoundedRect(bar, 1, 1);
break;
}
default:
break;
}
return;
}
LineEdit::paintEvent(event);
}