From 35cdc4f30f167037ef25f8f0a2d134cff74a599a Mon Sep 17 00:00:00 2001 From: Andrea Diamantini Date: Sun, 14 Oct 2012 21:52:46 +0200 Subject: clean up urlbar url loading. We really are lighter now :) --- src/urlbar/completionwidget.cpp | 106 ++++++++++++++++++++++++---------------- src/urlbar/completionwidget.h | 2 + src/urlbar/urlbar.cpp | 42 ++++++++++++---- src/urlbar/urlbar.h | 8 ++- 4 files changed, 103 insertions(+), 55 deletions(-) (limited to 'src/urlbar') diff --git a/src/urlbar/completionwidget.cpp b/src/urlbar/completionwidget.cpp index 12051f80..cf00a77c 100644 --- a/src/urlbar/completionwidget.cpp +++ b/src/urlbar/completionwidget.cpp @@ -261,63 +261,68 @@ bool CompletionWidget::eventFilter(QObject *obj, QEvent *ev) case Qt::Key_Enter: case Qt::Key_Return: + { w = qobject_cast(parent()); - if (kev->modifiers() == Qt::AltModifier) - { - if (kev->key() == Qt::Key_Return || kev->key() == Qt::Key_Enter) - { - emit chosenUrl(w->text(), Rekonq::NewFocusedTab); - } - } - - if (!w->text().startsWith(QL1S("http://"), Qt::CaseInsensitive)) - { - QString append; - if (kev->modifiers() == Qt::ControlModifier) - { - append = QL1S(".com"); - } - else if (kev->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) - { - append = QL1S(".org"); - } - else if (kev->modifiers() == Qt::ShiftModifier) - { - append = QL1S(".net"); - } - - if (!append.isEmpty()) - { - QUrl url(QL1S("http://") + w->text()); - QString host = url.host(); - if (!host.endsWith(append, Qt::CaseInsensitive)) - { - host += append; - url.setHost(host); - } - - emit chosenUrl(url, Rekonq::CurrentTab); - } - } +// if (!w->text().startsWith(QL1S("http://"), Qt::CaseInsensitive)) +// { +// QString append; +// if (kev->modifiers() == Qt::ControlModifier) +// { +// append = QL1S(".com"); +// } +// else if (kev->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) +// { +// append = QL1S(".org"); +// } +// else if (kev->modifiers() == Qt::ShiftModifier) +// { +// append = QL1S(".net"); +// } +// +// if (!append.isEmpty()) +// { +// QUrl url(QL1S("http://") + w->text()); +// QString host = url.host(); +// if (!host.endsWith(append, Qt::CaseInsensitive)) +// { +// host += append; +// url.setHost(host); +// } +// +// emit chosenUrl(url, Rekonq::CurrentTab); +// } +// } + + KUrl urlToLoad; + Rekonq::OpenType type = Rekonq::CurrentTab; + if (_currentIndex == -1) _currentIndex = 0; child = findChild(QString::number(_currentIndex)); if (child) //the completionwidget is visible and the user had press down { - //we can use the url of the listitem - emit chosenUrl(child->url(), Rekonq::CurrentTab); + urlToLoad = child->url(); } else //the user type too fast (completionwidget not visible or suggestion not downloaded) { - KUrl u = UrlResolver::urlFromTextTyped(w->text()); - emit chosenUrl(u, Rekonq::CurrentTab); + urlToLoad = UrlResolver::urlFromTextTyped(w->text()); } - kev->accept(); + + if (kev->modifiers() == Qt::AltModifier) + { + if (kev->key() == Qt::Key_Return || kev->key() == Qt::Key_Enter) + { + type = Rekonq::NewFocusedTab; + } + } + hide(); + emit chosenUrl(urlToLoad, type); + kev->accept(); return true; - + } case Qt::Key_Escape: hide(); return true; @@ -387,3 +392,18 @@ void CompletionWidget::suggestUrls(const QString &text) // NOTE: It's important to call this AFTER orderedSearchItems() to let everything work res->computeSuggestions(); } + + +KUrl CompletionWidget::activeSuggestion() +{ + int index = _currentIndex; + if (_currentIndex == -1) + index = 0; + + ListItem *child = findChild(QString::number(index)); + if (child) + return child->url(); + + kDebug() << "WARNING: NO URL to LOAD..."; + return KUrl(); +} diff --git a/src/urlbar/completionwidget.h b/src/urlbar/completionwidget.h index 098aa9f9..bd73584a 100644 --- a/src/urlbar/completionwidget.h +++ b/src/urlbar/completionwidget.h @@ -56,6 +56,8 @@ public: void suggestUrls(const QString &text); + KUrl activeSuggestion(); + private Q_SLOTS: void itemChosen(ListItem *item, Qt::MouseButton = Qt::LeftButton, Qt::KeyboardModifiers = Qt::NoModifier); void updateSuggestionList(const UrlSuggestionList &list, const QString& text); diff --git a/src/urlbar/urlbar.cpp b/src/urlbar/urlbar.cpp index 56d1370d..f10360b0 100644 --- a/src/urlbar/urlbar.cpp +++ b/src/urlbar/urlbar.cpp @@ -159,8 +159,6 @@ UrlBar::UrlBar(QWidget *parent) _suggestionTimer->setSingleShot(true); connect(_suggestionTimer, SIGNAL(timeout()), this, SLOT(suggest())); - - activateSuggestions(true); } @@ -200,10 +198,22 @@ void UrlBar::loadRequestedUrl(const KUrl& url, Rekonq::OpenType type) } -void UrlBar::loadDigitedUrl() +void UrlBar::loadTypedUrl() { - KUrl u = UrlResolver::urlFromTextTyped(text()); - loadRequestedUrl(u); + KUrl urlToLoad; + if (!_box.isNull()) + { + urlToLoad = _box.data()->activeSuggestion(); + if (!urlToLoad.isEmpty()) + { + loadRequestedUrl(urlToLoad); + return; + } + } + + // fallback here + urlToLoad = UrlResolver::urlFromTextTyped(text()); + loadRequestedUrl(urlToLoad); } @@ -333,11 +343,19 @@ void UrlBar::keyPressEvent(QKeyEvent *event) void UrlBar::focusInEvent(QFocusEvent *event) { + emit focusIn(); activateSuggestions(true); KLineEdit::focusInEvent(event); } +void UrlBar::focusOutEvent(QFocusEvent *event) +{ + activateSuggestions(false); + KLineEdit::focusOutEvent(event); +} + + void UrlBar::dropEvent(QDropEvent *event) { // handles only plain-text with url format @@ -429,7 +447,6 @@ void UrlBar::activateSuggestions(bool b) if (_box.isNull()) { _box = new CompletionWidget(this); - installEventFilter(_box.data()); connect(_box.data(), SIGNAL(chosenUrl(KUrl, Rekonq::OpenType)), this, SLOT(loadRequestedUrl(KUrl, Rekonq::OpenType))); // activate suggestions on edit text @@ -439,9 +456,13 @@ void UrlBar::activateSuggestions(bool b) else { disconnect(this, SIGNAL(textChanged(QString)), this, SLOT(detectTypedString(QString))); - removeEventFilter(_box.data()); + if (!_box.isNull()) - _box.data()->deleteLater(); + { + // This was just deleted later because of a crash in completionwidget... + delete _box.data(); + _box.clear(); + } } } @@ -637,15 +658,16 @@ void UrlBar::detectTypedString(const QString &typed) if (_suggestionTimer->isActive()) _suggestionTimer->stop(); - _suggestionTimer->start(50); + _suggestionTimer->start(100); } void UrlBar::suggest() { - kDebug() << "SUGGEST ABOUT DIGITED: " << text(); if (!_box.isNull()) + { _box.data()->suggestUrls(text()); + } } diff --git a/src/urlbar/urlbar.h b/src/urlbar/urlbar.h index 3f283e39..304afbe4 100644 --- a/src/urlbar/urlbar.h +++ b/src/urlbar/urlbar.h @@ -122,9 +122,9 @@ private Q_SLOTS: bool isValidURL(QString url); /** - * Load digited url + * Load typed url */ - void loadDigitedUrl(); + void loadTypedUrl(); void showRSSInfo(const QPoint &); @@ -132,11 +132,15 @@ protected: void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent *event); void focusInEvent(QFocusEvent *event); + void focusOutEvent(QFocusEvent *event); void dropEvent(QDropEvent *event); void mouseDoubleClickEvent(QMouseEvent *); void contextMenuEvent(QContextMenuEvent *event); void resizeEvent(QResizeEvent *); +Q_SIGNALS: + void focusIn(); + private: /** * Updates right icon position, given its number in the right icons list -- cgit v1.2.1