/* ============================================================ * * This file is a part of the rekonq project * * Copyright (C) 2008-2009 by Andrea Diamantini * Copyright (C) 2009 by Domrachev Alexandr * Copyright (C) 2009 by Paweł Prażak * Copyright (C) 2009 by Lionel Chauvin * * * 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 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * 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 . * * ============================================================ */ // Self Includes #include "urlbar.h" #include "urlbar.moc" // Local Includes #include "application.h" #include "lineedit.h" #include "mainwindow.h" #include "webview.h" #include "urlresolver.h" // KDE Includes #include #include #include // Qt Includes #include #include #include #include #include QColor UrlBar::s_defaultBaseColor; UrlBar::UrlBar(QWidget *parent) : KComboBox(true, parent) , m_lineEdit(new LineEdit) , m_progress(0) { //cosmetic setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); setMinimumWidth(180); // signal handlings setTrapReturnKey(true); setUrlDropsEnabled(true); // Make m_lineEdit background transparent QPalette p = m_lineEdit->palette(); p.setColor(QPalette::Base, Qt::transparent); m_lineEdit->setPalette(p); if (!s_defaultBaseColor.isValid()) { s_defaultBaseColor = palette().color(QPalette::Base); } setLineEdit(m_lineEdit); // clear the URL bar m_lineEdit->clear(); // load urls on activated urlbar signal connect(this, SIGNAL(returnPressed(const QString&)), SLOT(activated(const QString&))); } UrlBar::~UrlBar() { } void UrlBar::selectAll() const { m_lineEdit->selectAll(); } KUrl UrlBar::url() const { return m_currentUrl; } void UrlBar::setUrl(const QUrl& url) { if(url.scheme() == "about") { m_currentUrl = KUrl(); setFocus(); } else { m_currentUrl = KUrl(url); } updateUrl(); } void UrlBar::setProgress(int progress) { m_progress = progress; repaint(); } void UrlBar::updateUrl() { // Don't change my typed url... // FIXME this is not a proper solution (also if it works...) if(hasFocus()) { kDebug() << "Don't change my typed url..."; return; } KIcon icon; if(m_currentUrl.isEmpty()) { icon = KIcon("arrow-right"); } else { icon = Application::icon(m_currentUrl); } if (count()) { changeUrl(0, icon, m_currentUrl); } else { insertUrl(0, icon, m_currentUrl); } setCurrentIndex(0); // important security consideration: always display the beginning // of the url rather than its end to prevent spoofing attempts. // Must be AFTER setCurrentIndex if (!hasFocus()) { m_lineEdit->setCursorPosition(0); } } void UrlBar::activated(const QString& urlString) { disconnect(this, SIGNAL(editTextChanged(const QString &)), this, SLOT(suggestUrls(const QString &))); if(!m_suggestedUrl.isEmpty()) { kDebug() << "+++++++++++++++++ suggested url NOT empty +++++++++++++++++++++++"; m_currentUrl = m_suggestedUrl; updateUrl(); m_suggestedUrl = KUrl(); } else { if (urlString.isEmpty()) return; setUrl(urlString); } Application::instance()->loadUrl(m_currentUrl); } void UrlBar::loadFinished(bool) { // reset progress bar after small delay m_progress = 0; QTimer::singleShot(200, this, SLOT(repaint())); } void UrlBar::updateProgress(int progress) { m_progress = progress; repaint(); } void UrlBar::paintEvent(QPaintEvent *event) { // set background color of UrlBar QPalette p = palette(); p.setColor(QPalette::Base, s_defaultBaseColor); setPalette(p); KComboBox::paintEvent(event); if (!hasFocus()) { QPainter painter(this); QColor loadingColor; if (m_currentUrl.scheme() == QLatin1String("https")) { loadingColor = QColor(248, 248, 100); } else { loadingColor = QColor(116, 192, 250); } painter.setBrush(generateGradient(loadingColor, height())); painter.setPen(Qt::transparent); QRect backgroundRect = m_lineEdit->frameGeometry(); int mid = backgroundRect.width() * m_progress / 100; QRect progressRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height()); painter.drawRect(progressRect); painter.end(); } } QSize UrlBar::sizeHint() const { return m_lineEdit->sizeHint(); } QLinearGradient UrlBar::generateGradient(const QColor &color, int height) { QColor base = s_defaultBaseColor; base.setAlpha(0); QColor barColor = color; barColor.setAlpha(200); QLinearGradient gradient(0, 0, 0, height); gradient.setColorAt(0, base); gradient.setColorAt(0.25, barColor.lighter(120)); gradient.setColorAt(0.5, barColor); gradient.setColorAt(0.75, barColor.lighter(120)); gradient.setColorAt(1, base); return gradient; } void UrlBar::setBackgroundColor(QColor c) { s_defaultBaseColor = c; repaint(); } bool UrlBar::isLoading() { if(m_progress == 0) { return false; } return true; } void UrlBar::keyPressEvent(QKeyEvent *event) { // this handles the up/down arrow with active suggestions if(m_box) { switch(event->key()) { case Qt::Key_Escape: m_box->hide(); setFocus(); break; case Qt::Key_Up: m_suggestedUrl = m_box->activatePrevious(); kDebug() << "oooooooooooooooooo Up Arrow"; break; case Qt::Key_Down: m_suggestedUrl = m_box->activateNext(); kDebug() << "oooooooooooooooooo Down Arrow"; break; default: break; } return KComboBox::keyPressEvent(event); } // this handles the Modifiers + Return key combinations QString currentText = m_lineEdit->text().trimmed(); if ((event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) && !currentText.startsWith(QLatin1String("http://"), Qt::CaseInsensitive)) { QString append; if (event->modifiers() == Qt::ControlModifier) { append = QLatin1String(".com"); } else if (event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) { append = QLatin1String(".org"); } else if (event->modifiers() == Qt::ShiftModifier) { append = QLatin1String(".net"); } QUrl url(QLatin1String("http://www.") + currentText); QString host = url.host(); if (!host.endsWith(append, Qt::CaseInsensitive)) { host += append; url.setHost(host); m_lineEdit->setText(url.toString()); } } KComboBox::keyPressEvent(event); } void UrlBar::suggestUrls(const QString &text) { if(text.isEmpty()) return; UrlResolver res(text); UrlSearchList list = res.orderedSearchItems(); if(list.count() > 0) { if (m_box) delete m_box; m_box=new CompletionWidget(this); m_box->insertSearchList(list); m_box->popup(); } } void UrlBar::focusInEvent(QFocusEvent *event) { // activate suggestions on edit text connect(this, SIGNAL(editTextChanged(const QString &)), this, SLOT(suggestUrls(const QString &))); KComboBox::focusInEvent(event); } void UrlBar::changeEvent(QEvent *event) { if (event->type()==QEvent::EnabledChange && !isEnabled() && m_box && m_box->isVisible()) m_box->hide(); KComboBox::changeEvent(event); }