diff options
Diffstat (limited to 'src/urlbar/urlbar.cpp')
-rw-r--r-- | src/urlbar/urlbar.cpp | 456 |
1 files changed, 266 insertions, 190 deletions
diff --git a/src/urlbar/urlbar.cpp b/src/urlbar/urlbar.cpp index be19dae4..e1e542b7 100644 --- a/src/urlbar/urlbar.cpp +++ b/src/urlbar/urlbar.cpp @@ -13,9 +13,9 @@ * 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 +* 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 @@ -31,307 +31,383 @@ #include "urlbar.h" #include "urlbar.moc" +// Auto Includes +#include "rekonq.h" + // Local Includes #include "application.h" -#include "lineedit.h" #include "mainwindow.h" +#include "webtab.h" +#include "webpage.h" #include "webview.h" -#include "historymanager.h" +#include "completionwidget.h" // KDE Includes -#include <KDebug> #include <KCompletionBox> -#include <KUrl> // Qt Includes -#include <QPainter> -#include <QPaintEvent> -#include <QPalette> -#include <QTimer> +#include <QtGui/QPainter> +#include <QtGui/QPaintEvent> +#include <QtGui/QPalette> +#include <QtGui/QVBoxLayout> -QColor UrlBar::s_defaultBaseColor; +IconButton::IconButton(QWidget *parent) + : QToolButton(parent) +{ + setToolButtonStyle(Qt::ToolButtonIconOnly); + setStyleSheet("IconButton { background-color:transparent; border: none; padding: 0px}"); + setCursor(Qt::ArrowCursor); +} -UrlBar::UrlBar(QWidget *parent) - : KHistoryComboBox(true, parent) - , m_lineEdit(new LineEdit) - , m_progress(0) +void IconButton::mouseReleaseEvent(QMouseEvent* event) { - setUrlDropsEnabled(true); - setAutoDeleteCompletionObject(true); + emit clicked(event->globalPos()); +} - //cosmetic - setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - setMinimumWidth(180); - - setTrapReturnKey(true); - setupLineEdit(); +// ----------------------------------------------------------------------------------------------------------- - // add every item to history - connect(this, SIGNAL(returnPressed(const QString&)), SLOT(activated(const QString&))); - connect(completionBox(), SIGNAL(activated(const QString&)), SLOT(activated(const QString&))); - connect(this, SIGNAL(cleared()), SLOT(cleared())); +UrlBar::UrlBar(QWidget *parent) + : KLineEdit(parent) + , _tab(0) + , _privateMode(false) + , _icon(new IconButton(this)) + , _suggestionTimer(new QTimer(this)) +{ + // initial style + setStyleSheet(QString("UrlBar { padding: 0 0 0 %1px;} ").arg(_icon->sizeHint().width())); - // setup completion box - setCompletionObject( Application::historyManager()->completionObject() ); - - // set dropdown list background - QPalette p = view()->palette(); - p.setColor(QPalette::Base, palette().color(QPalette::Base)); - view()->setPalette(p); + // cosmetic + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + setMinimumWidth(200); + setMinimumHeight(26); - // load urls on activated urlbar signal - connect(this, SIGNAL(activated(const KUrl&)), Application::instance(), SLOT(loadUrl(const KUrl&))); -} + // doesn't show the clear button + setClearButtonShown(false); + // trap Key_Enter & Key_Return events, while emitting the returnPressed signal + setTrapReturnKey(true); -UrlBar::~UrlBar() -{ -} + // insert decoded URLs + setUrlDropsEnabled(true); + // accept focus, via tabbing, clicking & wheeling + setFocusPolicy(Qt::WheelFocus); -void UrlBar::selectAll() const -{ - lineEdit()->selectAll(); -} + // disable completion object (we have our own :) ) + setCompletionObject(0); + _tab = qobject_cast<WebTab *>(parent); -KUrl UrlBar::url() const -{ - return m_currentUrl; -} + connect(_tab->view(), SIGNAL(urlChanged(const QUrl &)), this, SLOT(setQUrl(const QUrl &))); + connect(_tab->view(), SIGNAL(loadFinished(bool)), this, SLOT(loadFinished())); + connect(_tab->view(), SIGNAL(loadStarted()), this, SLOT(clearRightIcons())); + // load typed urls + connect(this, SIGNAL(returnPressed(const QString &)), this, SLOT(loadTyped(const QString &))); -KLineEdit *UrlBar::lineEdit() const -{ - return m_lineEdit; + _suggestionTimer->setSingleShot(true); + connect(_suggestionTimer, SIGNAL(timeout()), this, SLOT(suggest())); + + activateSuggestions(true); } -void UrlBar::setupLineEdit() +UrlBar::~UrlBar() { - // 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); - - // Make the lineedit consume the Qt::Key_Enter event... - lineEdit()->setTrapReturnKey(true); - - lineEdit()->setHandleSignals(true); - - // clear the URL bar - lineEdit()->clear(); + activateSuggestions(false); + delete _icon; + _box.clear(); } -void UrlBar::setUrl(const QUrl& url) +void UrlBar::setQUrl(const QUrl& url) { - if(url.scheme() == "about") + if (url.scheme() == QL1S("about")) { - m_currentUrl = KUrl(); + _icon->setIcon(KIcon("arrow-right")); + clear(); setFocus(); } else { - m_currentUrl = KUrl(url); + clearFocus(); + KLineEdit::setUrl(url); + setCursorPosition(0); + _icon->setIcon(Application::icon(url)); } - updateUrl(); } -void UrlBar::setProgress(int progress) +void UrlBar::activated(const KUrl& url, Rekonq::OpenType type) { - m_progress = progress; - repaint(); + activateSuggestions(false); + + clearFocus(); + setUrl(url); + Application::instance()->loadUrl(url, type); } -void UrlBar::updateUrl() +void UrlBar::paintEvent(QPaintEvent *event) { - // 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()) + QColor backgroundColor; + if (_privateMode) { - icon = KIcon("arrow-right"); + backgroundColor = QColor(220, 220, 220); // light gray } - else + else { - icon = Application::icon(m_currentUrl); + backgroundColor = Application::palette().color(QPalette::Base); } - if (count()) + // set background color of UrlBar + QPalette p = palette(); + + int progr = _tab->progress(); + if (progr == 0) { - changeUrl(0, icon, m_currentUrl); + if (_tab->url().scheme() == QL1S("https")) + { + backgroundColor = QColor(255, 255, 171); // light yellow + } + p.setBrush(QPalette::Base, backgroundColor); } else { - insertUrl(0, icon, m_currentUrl); + QColor loadingColor = QColor(116, 192, 250); + + QLinearGradient gradient(0, 0, width(), 0); + gradient.setColorAt(0, loadingColor); + gradient.setColorAt(((double)progr) / 100, backgroundColor); + p.setBrush(QPalette::Base, gradient); } + setPalette(p); - setCurrentIndex(0); + // you need this before our code to draw inside the line edit.. + KLineEdit::paintEvent(event); - // important security consideration: always display the beginning - // of the url rather than its end to prevent spoofing attempts. - // Must be AFTER setCurrentIndex - if (!hasFocus()) + if (text().isEmpty()) { - lineEdit()->setCursorPosition(0); + QStyleOptionFrame option; + initStyleOption(&option); + QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &option, this); + QPainter painter(this); + painter.setPen(Qt::gray); + painter.drawText(textRect, + Qt::AlignCenter, + i18n("Start typing here to search your bookmarks, history and the web...") + ); } } -void UrlBar::activated(const QString& urlString) +void UrlBar::keyPressEvent(QKeyEvent *event) { - if (urlString.isEmpty()) - return; + // this handles the Modifiers + Return key combinations + QString currentText = text().trimmed(); + if ((event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) + && !currentText.startsWith(QL1S("http://"), Qt::CaseInsensitive)) + { + QString append; + if (event->modifiers() == Qt::ControlModifier) + { + append = QL1S(".com"); + } + else if (event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) + { + append = QL1S(".org"); + } + else if (event->modifiers() == Qt::ShiftModifier) + { + append = QL1S(".net"); + } - setUrl(urlString); - emit activated(m_currentUrl); + QUrl url(QL1S("http://www.") + currentText); + QString host = url.host(); + if (!host.endsWith(append, Qt::CaseInsensitive)) + { + host += append; + url.setHost(host); + setText(url.toString()); + } + } + + if (event->key() == Qt::Key_Escape) + { + clearFocus(); + event->accept(); + } + + KLineEdit::keyPressEvent(event); } -void UrlBar::cleared() +void UrlBar::focusInEvent(QFocusEvent *event) { - // clear the history on user's request from context menu - clear(); + activateSuggestions(true); + + KLineEdit::focusInEvent(event); } -void UrlBar::loadFinished(bool) +void UrlBar::setPrivateMode(bool on) { - // reset progress bar after small delay - m_progress = 0; - QTimer::singleShot(200, this, SLOT(repaint())); + _privateMode = on; } -void UrlBar::updateProgress(int progress) +void UrlBar::dropEvent(QDropEvent *event) { - m_progress = progress; - repaint(); + KLineEdit::dropEvent(event); + activated(text()); } -void UrlBar::paintEvent(QPaintEvent *event) +void UrlBar::loadFinished() { - // set background color of UrlBar - QPalette p = palette(); - p.setColor(QPalette::Base, s_defaultBaseColor); - setPalette(p); + if (_tab->progress() != 0) + return; - KHistoryComboBox::paintEvent(event); + if (_tab->url().scheme() == QL1S("about")) + { + update(); + return; + } - if (!hasFocus()) + // show KGet downloads?? + if (ReKonfig::kgetList()) { - QPainter painter(this); + IconButton *bt = addRightIcon(UrlBar::KGet); + connect(bt, SIGNAL(clicked(QPoint)), _tab->page(), SLOT(downloadAllContentsWithKGet(QPoint))); + } - 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 = lineEdit()->frameGeometry(); - int mid = backgroundRect.width() * m_progress / 100; - QRect progressRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height()); - painter.drawRect(progressRect); - painter.end(); + // show RSS + if (_tab->hasRSSInfo()) + { + IconButton *bt = addRightIcon(UrlBar::RSS); + connect(bt, SIGNAL(clicked(QPoint)), _tab, SLOT(showRSSInfo(QPoint))); + } + + // show SSL + if (_tab->url().scheme() == QL1S("https")) + { + IconButton *bt = addRightIcon(UrlBar::SSL); + connect(bt, SIGNAL(clicked(QPoint)), _tab->page(), SLOT(showSSLInfo(QPoint))); } + + update(); } -QSize UrlBar::sizeHint() const +void UrlBar::loadTyped(const QString &text) { - return lineEdit()->sizeHint(); + activated(KUrl(text)); } -QLinearGradient UrlBar::generateGradient(const QColor &color, int height) +void UrlBar::activateSuggestions(bool b) { - 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; + if (b) + { + if (_box.isNull()) + { + _box = new CompletionWidget(this); + installEventFilter(_box.data()); + connect(_box.data(), SIGNAL(chosenUrl(const KUrl &, Rekonq::OpenType)), this, SLOT(activated(const KUrl &, Rekonq::OpenType))); + + // activate suggestions on edit text + connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(detectTypedString(const QString &))); + } + } + else + { + disconnect(this, SIGNAL(textChanged(const QString &)), this, SLOT(detectTypedString(const QString &))); + removeEventFilter(_box.data()); + _box.data()->deleteLater(); + } } -void UrlBar::setBackgroundColor(QColor c) +void UrlBar::mouseDoubleClickEvent(QMouseEvent *) { - s_defaultBaseColor = c; - repaint(); + selectAll(); } -bool UrlBar::isLoading() +IconButton *UrlBar::addRightIcon(UrlBar::icon ic) { - if(m_progress == 0) + IconButton *rightIcon = new IconButton(this); + + switch (ic) { - return false; + case UrlBar::KGet: + rightIcon->setIcon(KIcon("download")); + rightIcon->setToolTip(i18n("List all links with KGet")); + break; + case UrlBar::RSS: + rightIcon->setIcon(KIcon("application-rss+xml")); + rightIcon->setToolTip(i18n("List all available RSS feeds")); + break; + case UrlBar::SSL: + rightIcon->setIcon(KIcon("object-locked")); + rightIcon->setToolTip(i18n("Show SSL Info")); + break; + default: + kDebug() << "ERROR.. default non extant case!!"; + break; } - return true; + + _rightIconsList << rightIcon; + int iconsCount = _rightIconsList.count(); + rightIcon->move(width() - 23*iconsCount, 6); + rightIcon->show(); + + return rightIcon; } -void UrlBar::keyPressEvent(QKeyEvent *event) + +void UrlBar::clearRightIcons() { - 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"); - } + qDeleteAll(_rightIconsList); + _rightIconsList.clear(); +} - 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()); - } + +void UrlBar::resizeEvent(QResizeEvent *event) +{ + int newHeight = (height() - 19) / 2; + _icon->move(4, newHeight); + + int iconsCount = _rightIconsList.count(); + int w = width(); + + for (int i = 0; i < iconsCount; ++i) + { + IconButton *bt = _rightIconsList.at(i); + bt->move(w - 25*(i + 1), newHeight); } - KHistoryComboBox::keyPressEvent(event); + KLineEdit::resizeEvent(event); + +} + + +void UrlBar::detectTypedString(const QString &typed) +{ + Q_UNUSED(typed); + + if(_suggestionTimer->isActive()) + _suggestionTimer->stop(); + _suggestionTimer->start(200); } + +void UrlBar::suggest() +{ + if(!_box.isNull()) + _box.data()->suggestUrls( text() ); +}
\ No newline at end of file |