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

311 lines
8.6 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 "locationbar.h"
#include "qupzilla.h"
#include "webview.h"
#include "rssmanager.h"
#include "mainapplication.h"
#include "locationcompleter.h"
#include "clickablelabel.h"
#include "siteinfowidget.h"
#include "rsswidget.h"
#include "webpage.h"
#include "bookmarkicon.h"
#include "progressbar.h"
#include "statusbarmessage.h"
#include "locationbarsettings.h"
#include "toolbutton.h"
#include "searchenginesmanager.h"
2011-03-02 16:57:41 +01:00
LocationBar::LocationBar(QupZilla* mainClass)
: LineEdit()
, p_QupZilla(mainClass)
, m_webView(0)
, m_locationBarSettings(LocationBarSettings::instance())
2011-03-02 16:57:41 +01:00
{
setObjectName("locationbar");
m_siteIcon = new ToolButton(this);
m_siteIcon->setObjectName("locationbar-siteicon");
2011-03-02 16:57:41 +01:00
m_siteIcon->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
m_siteIcon->setCursor(Qt::ArrowCursor);
m_siteIcon->setToolTip(tr("Show informations about this page"));
2011-03-02 17:54:22 +01:00
m_siteIcon->setFocusPolicy(Qt::ClickFocus);
2011-03-02 16:57:41 +01:00
m_rssIcon = new ClickableLabel(this);
m_rssIcon->setObjectName("locationbar-rss-icon");
m_rssIcon->setCursor(Qt::PointingHandCursor);
2011-03-02 16:57:41 +01:00
m_rssIcon->setToolTip(tr("Add RSS from this page..."));
2011-03-02 17:54:22 +01:00
m_rssIcon->setFocusPolicy(Qt::ClickFocus);
2011-03-03 18:29:20 +01:00
m_rssIcon->setVisible(false);
2011-03-02 16:57:41 +01:00
m_goButton = new ClickableLabel(this);
m_goButton->setObjectName("locationbar-goicon");
2011-03-02 16:57:41 +01:00
m_goButton->setCursor(Qt::PointingHandCursor);
m_goButton->setHidden(true);
m_bookmarkIcon = new BookmarkIcon(p_QupZilla);
2011-03-02 16:57:41 +01:00
ClickableLabel* down = new ClickableLabel(this);
down->setObjectName("locationbar-down-icon");
2011-03-02 16:57:41 +01:00
down->setCursor(Qt::ArrowCursor);
addWidget(down, LineEdit::RightSide);
addWidget(m_bookmarkIcon, LineEdit::RightSide);
2011-03-02 16:57:41 +01:00
addWidget(m_goButton, LineEdit::RightSide);
addWidget(m_rssIcon, LineEdit::RightSide);
setWidgetSpacing(0);
m_locationCompleter = new LocationCompleter();
setCompleter(m_locationCompleter);
2011-03-04 13:59:07 +01:00
// LocationPopup* com = new LocationPopup(this);
// connect(down, SIGNAL(clicked(QPoint)), com, SLOT(show()));
2011-03-02 16:57:41 +01:00
connect(this, SIGNAL(textEdited(QString)), this, SLOT(textEdit()));
connect(this, SIGNAL(textEdited(QString)), m_locationCompleter, SLOT(refreshCompleter(QString)));
connect(this, SIGNAL(returnPressed()), this, SLOT(urlEnter()));
2011-03-02 16:57:41 +01:00
connect(m_locationCompleter->popup(), SIGNAL(clicked(QModelIndex)), p_QupZilla, SLOT(urlEnter()));
connect(m_siteIcon, SIGNAL(clicked()), this, SLOT(showSiteInfo()));
connect(m_goButton, SIGNAL(clicked(QPoint)), this, SLOT(urlEnter()));
connect(m_rssIcon, SIGNAL(clicked(QPoint)), this, SLOT(rssIconClicked()));
connect(mApp->searchEnginesManager(), SIGNAL(activeEngineChanged()), this, SLOT(updatePlaceHolderText()));
2011-03-02 16:57:41 +01:00
clearIcon();
updatePlaceHolderText();
}
void LocationBar::updatePlaceHolderText()
{
setPlaceholderText(tr("Enter URL address or search on %1").arg(mApp->searchEnginesManager()->activeEngine().name));
2011-03-02 16:57:41 +01:00
}
void LocationBar::urlEnter()
{
QUrl urlToLoad;
//Check for Search Engine shortcut
int firstSpacePos = text().indexOf(" ");
if (firstSpacePos != -1) {
QString shortcut = text().mid(0, firstSpacePos);
QString searchedString = text().mid(firstSpacePos).trimmed();
SearchEngine en = mApp->searchEnginesManager()->engineForShortcut(shortcut);
if (!en.name.isEmpty()) {
urlToLoad = en.url.replace("%s", searchedString);
}
}
if (urlToLoad.isEmpty()) {
QUrl guessedUrl = WebView::guessUrlFromString(text());
if (!guessedUrl.isEmpty()) {
urlToLoad = guessedUrl;
}
else {
urlToLoad = text();
}
}
m_webView->load(urlToLoad);
setText(urlToLoad.toEncoded());
m_webView->setFocus();
}
2011-03-02 16:57:41 +01:00
void LocationBar::textEdit()
{
m_locationCompleter->popup()->setUpdatesEnabled(false);
showGoButton();
}
void LocationBar::showGoButton()
{
if (m_goButton->isVisible()) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
m_rssIconVisible = m_rssIcon->isVisible();
m_bookmarkIcon->hide();
2011-03-02 16:57:41 +01:00
m_rssIcon->hide();
m_goButton->show();
}
void LocationBar::hideGoButton()
{
if (!m_goButton->isVisible()) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
m_rssIcon->setVisible(m_rssIconVisible);
m_bookmarkIcon->show();
2011-03-02 16:57:41 +01:00
m_goButton->hide();
}
void LocationBar::showPopup()
{
// TODO: Fix to next version
// return;
// emit textEdited("");
// m_locationCompleter->popup()->showNormal();
2011-03-02 16:57:41 +01:00
}
void LocationBar::showSiteInfo()
{
QUrl url = p_QupZilla->weView()->url();
if (url.isEmpty() || url.scheme() == "qupzilla") {
return;
}
2011-03-02 16:57:41 +01:00
SiteInfoWidget* info = new SiteInfoWidget(p_QupZilla);
info->showAt(this);
}
void LocationBar::rssIconClicked()
{
RSSWidget* rss = new RSSWidget(m_webView, this);
rss->showAt(this);
}
void LocationBar::showRSSIcon(bool state)
{
m_rssIcon->setVisible(state);
}
2011-03-02 16:57:41 +01:00
void LocationBar::showUrl(const QUrl &url, bool empty)
{
if (hasFocus() || (url.isEmpty() && empty)) {
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
if (url.toEncoded() != text()) {
2011-03-02 16:57:41 +01:00
setText(url.toEncoded());
setCursorPosition(0);
}
p_QupZilla->statusBarMessage()->clearMessage();
2011-03-02 16:57:41 +01:00
hideGoButton();
m_bookmarkIcon->checkBookmark(url);
2011-03-02 16:57:41 +01:00
}
void LocationBar::siteIconChanged()
{
QIcon icon_ = m_webView->siteIcon();
2011-03-02 16:57:41 +01:00
if (icon_.isNull()) {
clearIcon();
}
else {
m_siteIcon->setIcon(QIcon(icon_.pixmap(16, 16)));
2011-03-02 16:57:41 +01:00
}
}
void LocationBar::clearIcon()
{
m_siteIcon->setIcon(QIcon(QWebSettings::webGraphic(QWebSettings::DefaultFrameIconGraphic)));
}
2011-03-02 16:57:41 +01:00
void LocationBar::setPrivacy(bool state)
{
m_siteIcon->setProperty("secured", state);
m_siteIcon->style()->unpolish(m_siteIcon);
m_siteIcon->style()->polish(m_siteIcon);
2011-03-02 16:57:41 +01:00
}
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());
2011-03-02 16:57:41 +01:00
p_QupZilla->loadAddress(dropUrl);
QLineEdit::focusOutEvent(new QFocusEvent(QFocusEvent::FocusOut));
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());
2011-03-02 16:57:41 +01:00
p_QupZilla->loadAddress(dropUrl);
QLineEdit::focusOutEvent(new QFocusEvent(QFocusEvent::FocusOut));
return;
}
}
QLineEdit::dropEvent(event);
}
void LocationBar::focusOutEvent(QFocusEvent* e)
{
QLineEdit::focusOutEvent(e);
if (!selectedText().isEmpty() && e->reason() != Qt::TabFocusReason) {
return;
}
setCursorPosition(0);
hideGoButton();
}
2011-03-17 17:03:04 +01:00
void LocationBar::mouseDoubleClickEvent(QMouseEvent* event)
2011-03-02 16:57:41 +01:00
{
if (event->button() == Qt::LeftButton && m_locationBarSettings->selectAllOnDoubleClick) {
2011-03-02 16:57:41 +01:00
selectAll();
}
else {
2011-03-02 16:57:41 +01:00
QLineEdit::mouseDoubleClickEvent(event);
}
2011-03-02 16:57:41 +01:00
}
void LocationBar::mousePressEvent(QMouseEvent* event)
{
if (cursorPosition() == 0 && m_locationBarSettings->selectAllOnClick) {
selectAll();
return;
}
LineEdit::mousePressEvent(event);
}
void LocationBar::keyPressEvent(QKeyEvent* event)
2011-03-02 16:57:41 +01:00
{
if (event->key() == Qt::Key_Escape) {
setText(m_webView->url().toEncoded());
2011-03-02 16:57:41 +01:00
event->accept();
return;
}
QString localDomain = tr(".co.uk", "Append domain name on ALT + Enter = Should be different for every country");
if (event->key() == Qt::Key_Alt && m_locationBarSettings->addCountryWithAlt && !text().endsWith(localDomain) && !text().endsWith("/")) {
2011-03-02 16:57:41 +01:00
setText(text().append(localDomain));
}
2011-03-02 16:57:41 +01:00
QLineEdit::keyPressEvent(event);
}
LocationBar::~LocationBar()
{
delete m_bookmarkIcon;
2011-03-02 16:57:41 +01:00
delete m_goButton;
delete m_siteIcon;
delete m_rssIcon;
delete m_locationCompleter;
}