From 783621c89c6c4c9aeae1bcf907c4395b96c5babd Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 14 Dec 2017 11:51:51 +0100 Subject: Cleaned up UrlLineEdit --- src/lib/navigation/urllineedit.cpp | 81 ++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 46 deletions(-) (limited to 'src/lib/navigation/urllineedit.cpp') diff --git a/src/lib/navigation/urllineedit.cpp b/src/lib/navigation/urllineedit.cpp index 5e47223..4244866 100644 --- a/src/lib/navigation/urllineedit.cpp +++ b/src/lib/navigation/urllineedit.cpp @@ -21,7 +21,7 @@ #include "urllineedit.h" #include #include - +#include #include #include @@ -31,24 +31,27 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) : QLineEdit(parent) { setPlaceholderText(tr("Enter address")); - setContextMenuPolicy(Qt::NoContextMenu); + // test action + m_sslAction = addAction(style()->standardIcon(QStyle::SP_ComputerIcon), QLineEdit::LeadingPosition); + m_sslAction->setToolTip(tr("TODO: Display SSL Status popup here")); + + QAction *completerAction = addAction(style()->standardIcon(QStyle::SP_TitleBarMinButton), QLineEdit::TrailingPosition); + + m_pageAction = addAction(style()->standardIcon(QStyle::SP_FileIcon), QLineEdit::TrailingPosition); + m_pageAction->setShortcut(QKeySequence("F10")); + m_pageAction->setToolTip(tr("Page Actions")); + connect(m_pageAction, &QAction::triggered, m_pageAction, [&]() { + //this->deselect(); + if(m_pageAction->menu() != nullptr) { + m_pageAction->menu()->exec(this->mapToGlobal(QPoint(width(), height()))); + } + }); 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())); - m_menu = new QMenu(this); m_menu->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool); @@ -70,14 +73,32 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) : QAction *closeAction = m_menu->addAction("Close"); connect(closeAction, SIGNAL(triggered()), m_menu, SLOT(hide())); + connect(completerAction, &QAction::triggered, this, [this]() { + this->showCompleter(this->text()); + }); + // connect signals connect(this, SIGNAL(textEdited(QString)), this, SLOT(showCompleter(QString))); connect(this, &QLineEdit::returnPressed, [this]() { + emit addressEntered(QUrl::fromUserInput(this->text())); m_menu->hide(); + this->clearFocus(); }); } +QAction *UrlLineEdit::sslAction() +{ + Q_ASSERT(m_sslAction != nullptr); + return m_sslAction; +} + +QAction *UrlLineEdit::pageAction() +{ + Q_ASSERT(m_pageAction != nullptr); + return m_pageAction; +} + void UrlLineEdit::setUrl(const QUrl &url) { QString urlText = url.toString(); @@ -101,7 +122,7 @@ void UrlLineEdit::focusInEvent(QFocusEvent *event) // select the contents when receiving focus // http://stackoverflow.com/a/35725950/1054406 // mousePressEvent triggers right after focusInEvent so text selected in focusInEvent unselects by mousePressEvent - QTimer::singleShot(0, this, SLOT(selectAll())); + //QTimer::singleShot(0, this, SLOT(selectAll())); } void UrlLineEdit::resizeEvent(QResizeEvent *event) @@ -159,35 +180,3 @@ void UrlLineEdit::showCompleter(const QString &text) //listWidget->setCurrentRow(0, QItemSelectionModel::SelectCurrent); m_menu->exec(); } - -// 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(); -} - -void UrlLineEdit::pasteUrlAndGo() -{ - clear(); - paste(); - emit returnPressed(); -} - -void UrlLineEdit::bookmarkUrl() -{ - qDebug("TODO: bookmarkUrl()"); -} -- cgit v1.2.1