From e7b2d97d99f53ed4ed68a9c1a7773e6d8df6c01e Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 13 Apr 2017 19:09:46 +0200 Subject: Address bar menu and search --- src/widgets/urllineedit.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++-- src/widgets/urllineedit.h | 11 ++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/widgets/urllineedit.cpp b/src/widgets/urllineedit.cpp index 5063191..b038da9 100644 --- a/src/widgets/urllineedit.cpp +++ b/src/widgets/urllineedit.cpp @@ -22,14 +22,31 @@ #include #include +#include +#include +#include "browser.h" + UrlLineEdit::UrlLineEdit(QWidget *parent) : QLineEdit(parent) { //setStyleSheet("color: #808080"); + setPlaceholderText(tr("Enter address")); + setContextMenuPolicy(Qt::NoContextMenu); QTextCharFormat hostnameFormat; hostnameFormat.setFontWeight(QFont::Bold); m_hostFormat.format = hostnameFormat; + + m_contextMenu = new QMenu(this); + m_contextMenu->addAction("Copy URL", this, SLOT(copyUrl())); + m_contextMenu->addAction("Paste URL", this, SLOT(pasteUrl())); + m_contextMenu->addAction("Paste URL and go", this, SLOT(pasteUrlAndGo())); + m_contextMenu->addSeparator(); + m_contextMenu->addAction("Bookmark this page", this, SLOT(bookmarkUrl()))->setEnabled(false); + + QAction *contextAction = addAction(style()->standardIcon(QStyle::SP_TitleBarMinButton), ActionPosition::TrailingPosition); + contextAction->setShortcut(QKeySequence::fromString("F3")); + connect(contextAction, SIGNAL(triggered()), this, SLOT(showMenu())); } void UrlLineEdit::setUrl(const QUrl &url) @@ -48,7 +65,7 @@ void UrlLineEdit::setUrl(const QUrl &url) QUrl UrlLineEdit::url() { - return QUrl::fromUserInput(text()); + return urlFromUserInput(text()); } void UrlLineEdit::focusInEvent(QFocusEvent *event) @@ -64,7 +81,7 @@ void UrlLineEdit::focusInEvent(QFocusEvent *event) void UrlLineEdit::focusOutEvent(QFocusEvent *event) { - setUrl(QUrl(text())); + setUrl(urlFromUserInput(text())); QLineEdit::focusOutEvent(event); } @@ -82,3 +99,45 @@ void UrlLineEdit::clearTextFormat() { setTextFormat(QTextLayout::FormatRange()); } + +QUrl UrlLineEdit::urlFromUserInput(const QString &input) +{ + if(input.startsWith('#')) { + return QUrl::fromUserInput(qApp->settings()->value("general.search").toString().replace("$term", input.mid(1))); + } + return QUrl::fromUserInput(input); +} + +// Menu + +void UrlLineEdit::showMenu() +{ + m_contextMenu->exec(mapToGlobal(QPoint(width() - m_contextMenu->width(), height()))); +} + +void UrlLineEdit::copyUrl() +{ + selectAll(); + copy(); + deselect(); +} + +void UrlLineEdit::pasteUrl() +{ + clear(); + paste(); + setUrl(urlFromUserInput(text())); +} + +void UrlLineEdit::pasteUrlAndGo() +{ + clear(); + paste(); + setUrl(urlFromUserInput(text())); + emit returnPressed(); +} + +void UrlLineEdit::bookmarkUrl() +{ + qDebug("TODO: bookmarkUrl()"); +} diff --git a/src/widgets/urllineedit.h b/src/widgets/urllineedit.h index d9d11ae..959461c 100644 --- a/src/widgets/urllineedit.h +++ b/src/widgets/urllineedit.h @@ -23,6 +23,7 @@ #include #include +#include class UrlLineEdit : public QLineEdit { @@ -40,11 +41,21 @@ protected: void focusInEvent(QFocusEvent *event); void focusOutEvent(QFocusEvent *event); +private slots: + void showMenu(); + void copyUrl(); + void pasteUrl(); + void pasteUrlAndGo(); + void bookmarkUrl(); + private: void setTextFormat(const QTextLayout::FormatRange &format); void clearTextFormat(); + QUrl urlFromUserInput(const QString &input); + QTextLayout::FormatRange m_hostFormat; + QMenu *m_contextMenu; }; #endif // URLLINEEDIT_H -- cgit v1.2.1