From fe3785a986bea374b6ee52aa92d5774860fd282b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 3 May 2018 18:38:02 +0200 Subject: Merge address bar menus --- lib/addressbar/addressbar.cpp | 14 ++++------ lib/addressbar/urllineedit.cpp | 61 +++++++++++++++++++++++++++++------------- lib/addressbar/urllineedit.h | 7 +++-- 3 files changed, 52 insertions(+), 30 deletions(-) (limited to 'lib/addressbar') diff --git a/lib/addressbar/addressbar.cpp b/lib/addressbar/addressbar.cpp index e8dcc6a..a923e72 100644 --- a/lib/addressbar/addressbar.cpp +++ b/lib/addressbar/addressbar.cpp @@ -21,6 +21,7 @@ AddressBar::AddressBar(const QHash &config, QWidget *parent) layout()->setSpacing(0); urlBar = new UrlLineEdit(this); + urlBar->menuAction->setShortcut(QKeySequence(config.value("addressbar.shortcuts.menu"))); layout()->addWidget(urlBar); auto *focusShortcut = new QShortcut(QKeySequence(config.value("addressbar.shortcuts.focus")), parent); @@ -29,11 +30,6 @@ AddressBar::AddressBar(const QHash &config, QWidget *parent) urlBar->selectAll(); }); - urlBar->pageMenu_action->setShortcut(QKeySequence(config.value("addressbar.shortcuts.pageMenu"))); - urlBar->pageMenu_action->setToolTip(tr("Page Actions (%1)").arg(urlBar->pageMenu_action->shortcut().toString())); - - urlBar->toolsMenu_action->setShortcut(QKeySequence(config.value("addressbar.shortcuts.toolsMenu"))); - urlBar->toolsMenu_action->setToolTip(tr("Tools (%1)").arg(urlBar->toolsMenu_action->shortcut().toString())); connect(urlBar, &UrlLineEdit::textEdited, [=](const QString &text) { std::function callback = std::bind(&UrlLineEdit::updateCompleter, urlBar, std::placeholders::_1); @@ -63,8 +59,8 @@ void AddressBar::setView(QWebEngineView *view) if(view == nullptr) { urlBar->clear(); - urlBar->pageMenu_action->setMenu(nullptr); - urlBar->toolsMenu_action->setMenu(nullptr); + urlBar->pageMenu = nullptr; + urlBar->toolsMenu = nullptr; return; } @@ -85,10 +81,10 @@ void AddressBar::setView(QWebEngineView *view) void AddressBar::setPageMenu(QMenu *menu) { - urlBar->pageMenu_action->setMenu(menu); + urlBar->pageMenu = menu; } void AddressBar::setToolsMenu(QMenu *menu) { - urlBar->toolsMenu_action->setMenu(menu); + urlBar->toolsMenu = menu; } diff --git a/lib/addressbar/urllineedit.cpp b/lib/addressbar/urllineedit.cpp index 27acf60..f92518e 100644 --- a/lib/addressbar/urllineedit.cpp +++ b/lib/addressbar/urllineedit.cpp @@ -7,11 +7,10 @@ */ #include "urllineedit.h" -#include #include #include -#include -#include +#include +#include UrlLineEdit::UrlLineEdit(QWidget *parent) : QLineEdit(parent) @@ -21,18 +20,49 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) m_listView->setVisible(false); - pageMenu_action = addAction(style()->standardIcon(QStyle::SP_DriveNetIcon), QLineEdit::LeadingPosition); - connect(pageMenu_action, &QAction::triggered, pageMenu_action, [&]() { - if(pageMenu_action->menu()) { - pageMenu_action->menu()->exec(this->mapToGlobal(QPoint(0, height()))); - } + auto *copyAction = new QAction(tr("Copy URL"), this); + connect(copyAction, &QAction::triggered, this, [this]() { + qApp->clipboard()->setText(this->text()); + }); + actions.append(copyAction); + + auto *pasteAction = new QAction(tr("Paste URL"), this); + connect(pasteAction, &QAction::triggered, this, [this]() { + this->setText(qApp->clipboard()->text()); + this->setFocus(); }); + actions.append(pasteAction); - toolsMenu_action = addAction(style()->standardIcon(QStyle::SP_FileIcon), QLineEdit::TrailingPosition); - connect(toolsMenu_action, &QAction::triggered, toolsMenu_action, [&]() { - if(toolsMenu_action->menu()) { - toolsMenu_action->menu()->exec(this->mapToGlobal(QPoint(width(), height()))); + auto *loadAction = new QAction(tr("Paste and load"), this); + connect(loadAction, &QAction::triggered, this, [this]() { + this->setText(qApp->clipboard()->text()); + emit returnPressed(); + }); + actions.append(loadAction); + + auto *searchAction = new QAction(tr("Paste and search"), this); + connect(searchAction, &QAction::triggered, this, [this]() { + this->setText("#" + qApp->clipboard()->text()); + emit returnPressed(); + }); + actions.append(searchAction); + + menuAction = addAction(style()->standardIcon(QStyle::SP_DriveNetIcon), QLineEdit::LeadingPosition); + connect(menuAction, &QAction::triggered, this, [this]() { + auto *menu = new QMenu(); + menu->setAttribute(Qt::WA_DeleteOnClose, true); + menu->setMinimumWidth(240); + menu->addActions(actions); + menu->addSeparator(); + + if(pageMenu) { + menu->addMenu(pageMenu); } + if(toolsMenu) { + menu->addMenu(toolsMenu); + } + + menu->exec(this->mapToGlobal(QPoint(0, height()))); }); QTextCharFormat hostnameFormat; @@ -70,20 +100,13 @@ void UrlLineEdit::updateCompleter(const QStringList &l) void UrlLineEdit::focusInEvent(QFocusEvent *event) { clearTextFormat(); - QLineEdit::focusInEvent(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())); } void UrlLineEdit::focusOutEvent(QFocusEvent *event) { if(!text().startsWith('#')) setUrl(QUrl::fromUserInput(text())); - QLineEdit::focusOutEvent(event); } diff --git a/lib/addressbar/urllineedit.h b/lib/addressbar/urllineedit.h index f27addc..f28983d 100644 --- a/lib/addressbar/urllineedit.h +++ b/lib/addressbar/urllineedit.h @@ -28,10 +28,11 @@ public slots: void updateCompleter(const QStringList &l); public: + QAction *menuAction = nullptr; // pageMenu action: zoom, print - QAction *pageMenu_action = nullptr; + QMenu *pageMenu = nullptr; // devMenu action: scripts, etc - QAction *toolsMenu_action = nullptr; + QMenu *toolsMenu = nullptr; protected: void focusInEvent(QFocusEvent *event) override; @@ -42,6 +43,8 @@ private: void setTextFormat(const QTextLayout::FormatRange &format); void clearTextFormat(); + QList actions; + QTextLayout::FormatRange m_hostFormat; // completer -- cgit v1.2.1