summaryrefslogtreecommitdiff
path: root/src/urlbar/urlbar.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/urlbar/urlbar.cpp')
-rw-r--r--src/urlbar/urlbar.cpp366
1 files changed, 145 insertions, 221 deletions
diff --git a/src/urlbar/urlbar.cpp b/src/urlbar/urlbar.cpp
index adeba6ae..6d1b19c9 100644
--- a/src/urlbar/urlbar.cpp
+++ b/src/urlbar/urlbar.cpp
@@ -31,12 +31,16 @@
#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 "webview.h"
-#include "historymanager.h"
+#include "completionwidget.h"
// KDE Includes
#include <KDebug>
@@ -48,299 +52,219 @@
#include <QPaintEvent>
#include <QPalette>
#include <QTimer>
+#include <QVBoxLayout>
-
-QColor UrlBar::s_defaultBaseColor;
+// Defines
+#define QL1S(x) QLatin1String(x)
UrlBar::UrlBar(QWidget *parent)
- : KHistoryComboBox(true, parent)
- , m_lineEdit(new LineEdit)
- , m_progress(0)
+ : LineEdit(parent)
+ , _tab(0)
+ , _privateMode(false)
{
- setUrlDropsEnabled(true);
- setAutoDeleteCompletionObject(true);
-
- //cosmetic
- setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
- setMinimumWidth(180);
+ _tab = qobject_cast<WebTab *>(parent);
- 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()));
-
- // setup completion box
- setCompletionObject( Application::historyManager()->completionObject() );
+ 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()));
- // set dropdown list background
- QPalette p = view()->palette();
- p.setColor(QPalette::Base, palette().color(QPalette::Base));
- view()->setPalette(p);
+ // load typed urls
+ connect(this, SIGNAL(returnPressed(const QString &)), this, SLOT(loadTyped(const QString &)));
- // load urls on activated urlbar signal
- connect(this, SIGNAL(activated(const KUrl&)), Application::instance(), SLOT(loadUrl(const KUrl&)));
+ activateSuggestions(true);
}
UrlBar::~UrlBar()
{
+ activateSuggestions(false);
+ _box.clear();
}
-void UrlBar::selectAll() const
-{
- lineEdit()->selectAll();
-}
-
-
-KUrl UrlBar::url() const
-{
- return m_currentUrl;
-}
-
-
-KLineEdit *UrlBar::lineEdit() const
-{
- return m_lineEdit;
-}
-
-
-void UrlBar::setupLineEdit()
-{
- // 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();
-}
-
-
-void UrlBar::setUrl(const QUrl& url)
+void UrlBar::setQUrl(const QUrl& url)
{
- if(url.scheme() == "about")
+ if(url.scheme() == QL1S("about") )
{
- m_currentUrl = KUrl();
- updateUrl(); // updateUrl before setFocus
+ iconButton()->setIcon( KIcon("arrow-right") );
+ clear();
setFocus();
}
else
{
- m_currentUrl = KUrl(url);
- updateUrl();
+ clearFocus();
+ LineEdit::setUrl(url);
+ setCursorPosition(0);
+ iconButton()->setIcon( Application::icon(url) );
}
-
}
-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())
- {
- icon = KIcon("arrow-right");
- }
- else
- {
- icon = Application::icon(m_currentUrl);
- }
-
- if (count())
+ QColor backgroundColor;
+ if( _privateMode )
{
- changeUrl(0, icon, m_currentUrl);
+ backgroundColor = QColor(220, 220, 220); // light gray
}
else
{
- insertUrl(0, icon, m_currentUrl);
+ backgroundColor = Application::palette().color(QPalette::Base);
}
+
+ // set background color of UrlBar
+ QPalette p = palette();
- 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())
+ int progr = _tab->progress();
+ if (progr == 0)
+ {
+ if( _tab->url().scheme() == QL1S("https") )
+ {
+ backgroundColor = QColor(255, 255, 171); // light yellow
+ }
+ p.setBrush(QPalette::Base, backgroundColor);
+ }
+ else
{
- lineEdit()->setCursorPosition(0);
+ 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);
+
+ LineEdit::paintEvent(event);
}
-void UrlBar::activated(const QString& urlString)
-{
- if (urlString.isEmpty())
- return;
-
- setUrl(urlString);
- emit activated(m_currentUrl);
-}
-
-
-void UrlBar::cleared()
-{
- // clear the history on user's request from context menu
- clear();
-}
-
-
-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)
+void UrlBar::keyPressEvent(QKeyEvent *event)
{
- // set background color of UrlBar
- QPalette p = palette();
- p.setColor(QPalette::Base, s_defaultBaseColor);
- setPalette(p);
-
- KHistoryComboBox::paintEvent(event);
-
- if (!hasFocus())
+ // this handles the Modifiers + Return key combinations
+ QString currentText = text().trimmed();
+ if ((event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
+ && !currentText.startsWith(QLatin1String("http://"), Qt::CaseInsensitive))
{
- QPainter painter(this);
-
- QColor loadingColor;
- if (m_currentUrl.scheme() == QLatin1String("https"))
+ QString append;
+ if (event->modifiers() == Qt::ControlModifier)
+ {
+ append = QLatin1String(".com");
+ }
+ else if (event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier))
{
- loadingColor = QColor(248, 248, 100);
+ append = QLatin1String(".org");
}
- else
+ 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))
{
- loadingColor = QColor(116, 192, 250);
+ host += append;
+ url.setHost(host);
+ setText(url.toString());
}
- 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();
}
+
+ LineEdit::keyPressEvent(event);
}
-QSize UrlBar::sizeHint() const
+void UrlBar::focusInEvent(QFocusEvent *event)
{
- return lineEdit()->sizeHint();
+ activateSuggestions(true);
+
+ LineEdit::focusInEvent(event);
}
-QLinearGradient UrlBar::generateGradient(const QColor &color, int height)
+void UrlBar::setPrivateMode(bool on)
{
- 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;
+ _privateMode = on;
}
-void UrlBar::setBackgroundColor(QColor c)
+void UrlBar::dropEvent(QDropEvent *event)
{
- s_defaultBaseColor = c;
- repaint();
+ LineEdit::dropEvent(event);
+ activated(text());
}
-bool UrlBar::isLoading()
+void UrlBar::loadFinished()
{
- if(m_progress == 0)
+ if(_tab->progress() != 0)
+ return;
+
+ if(_tab->url().scheme() == QL1S("about") )
+ {
+ update();
+ return;
+ }
+
+ // show KGet downloads??
+ if(ReKonfig::kgetList())
+ {
+ IconButton *bt = addRightIcon(LineEdit::KGet);
+ connect(bt, SIGNAL(clicked()), _tab->page(), SLOT(downloadAllContentsWithKGet()));
+ }
+
+ // show RSS
+ if(_tab->hasRSSInfo())
+ {
+ IconButton *bt = addRightIcon(LineEdit::RSS);
+ connect(bt, SIGNAL(clicked()), _tab, SLOT(showRSSInfo()));
+ }
+
+ // show SSL
+ if(_tab->url().scheme() == QL1S("https") )
{
- return false;
+ IconButton *bt = addRightIcon(LineEdit::SSL);
+ connect(bt, SIGNAL(clicked()), _tab->page(), SLOT(showSSLInfo()));
}
- return true;
+
+ update();
}
-void UrlBar::keyPressEvent(QKeyEvent *event)
+void UrlBar::loadTyped(const QString &text)
{
- QString currentText = m_lineEdit->text().trimmed();
- if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
+ activated(KUrl(text));
+}
+
+
+void UrlBar::activateSuggestions(bool b)
+{
+ if(b)
{
- if( !currentText.startsWith(QLatin1String("http://"), Qt::CaseInsensitive) )
+ if(_box.isNull())
{
- 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());
- }
- }
- else
- {
- // fill lineEdit with its stripped contents to remove trailing spaces
- m_lineEdit->setText(currentText);
+ _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 &)), _box.data(), SLOT(suggestUrls(const QString &)));
}
}
-
- KHistoryComboBox::keyPressEvent(event);
+ else
+ {
+ removeEventFilter(_box.data());
+ _box.data()->deleteLater();
+ }
}