From 8e1ac75749d4913ec9407844f2fab0eba0a0bb5b Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 31 Mar 2018 17:31:59 +0200 Subject: Add keyboard shortcuts for address bar menus --- src/addressbar/urllineedit.cpp | 46 ++++++++++++++++++++++++++++++------------ src/addressbar/urllineedit.h | 13 +++++++----- src/mainwindow/mainwindow.cpp | 14 +------------ 3 files changed, 42 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/addressbar/urllineedit.cpp b/src/addressbar/urllineedit.cpp index cfc2ab0..a36e1fc 100644 --- a/src/addressbar/urllineedit.cpp +++ b/src/addressbar/urllineedit.cpp @@ -9,29 +9,41 @@ #include "urllineedit.h" #include #include +#include #include #include #include -UrlLineEdit::UrlLineEdit(QWidget *parent) +UrlLineEdit::UrlLineEdit(const QHash &config, QWidget *parent) : QLineEdit(parent) , m_listView(new Completer(this)) { + auto *focusShortcut = new QShortcut(QKeySequence(config.value("addressbar.shortcuts.focus")), parent); + connect(focusShortcut, &QShortcut::activated, this, [this]() { + setFocus(); + selectAll(); + }); + setPlaceholderText(tr("Enter address")); m_listView->setVisible(false); connect(this, &UrlLineEdit::textEdited, this, &UrlLineEdit::updateCompleter); - // leading position action - //addAction(style()->standardIcon(QStyle::SP_DriveNetIcon), QLineEdit::LeadingPosition); + m_pageMenuAction = addAction(style()->standardIcon(QStyle::SP_DriveNetIcon), QLineEdit::LeadingPosition); + m_pageMenuAction->setShortcut(QKeySequence(config.value("addressbar.shortcuts.pageMenu"))); + m_pageMenuAction->setToolTip(tr("Page Actions (%1)").arg(m_pageMenuAction->shortcut().toString())); + connect(m_pageMenuAction, &QAction::triggered, m_pageMenuAction, [&]() { + if(m_pageMenuAction->menu()) { + m_pageMenuAction->menu()->exec(this->mapToGlobal(QPoint(0, height()))); + } + }); - 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()))); + m_toolsMenuAction = addAction(style()->standardIcon(QStyle::SP_FileIcon), QLineEdit::TrailingPosition); + m_toolsMenuAction->setShortcut(QKeySequence(config.value("addressbar.shortcuts.toolsMenu"))); + m_toolsMenuAction->setToolTip(tr("Tools (%1)").arg(m_toolsMenuAction->shortcut().toString())); + connect(m_toolsMenuAction, &QAction::triggered, m_toolsMenuAction, [&]() { + if(m_toolsMenuAction->menu()) { + m_toolsMenuAction->menu()->exec(this->mapToGlobal(QPoint(width(), height()))); } }); @@ -63,7 +75,7 @@ void UrlLineEdit::connectWebView(WebView *view) disconnect(urlChangedConnection); setUrl(view->url()); - m_pageAction->setMenu(view->pageMenu()); + m_pageMenuAction->setMenu(view->pageMenu()); urlChangedConnection = connect(view, &WebView::urlChanged, this, &UrlLineEdit::setUrl); } @@ -88,7 +100,7 @@ void UrlLineEdit::updateCompleter(const QString &text) return; } - const QList res = m_bookmarksModel->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1); + const QList res = m_bookmarksModel->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1); if(!m_listView->updateItems(res)) { m_listView->hide(); @@ -115,7 +127,9 @@ void UrlLineEdit::focusInEvent(QFocusEvent *event) void UrlLineEdit::focusOutEvent(QFocusEvent *event) { - setUrl(QUrl::fromUserInput(text())); + if(!text().startsWith('#')) + setUrl(QUrl::fromUserInput(text())); + QLineEdit::focusOutEvent(event); } @@ -131,9 +145,15 @@ void UrlLineEdit::keyPressEvent(QKeyEvent *event) setText(currentIndex.data().toString()); } m_listView->hide(); + event->accept(); return; } + } else if(event->key() == Qt::Key::Key_Escape) { + clearFocus(); + event->accept(); + return; } + QLineEdit::keyPressEvent(event); } diff --git a/src/addressbar/urllineedit.h b/src/addressbar/urllineedit.h index b726973..17465bc 100644 --- a/src/addressbar/urllineedit.h +++ b/src/addressbar/urllineedit.h @@ -6,8 +6,8 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef URLLINEEDIT_H -#define URLLINEEDIT_H +#ifndef SMOLBOTE_URLLINEEDIT_H +#define SMOLBOTE_URLLINEEDIT_H #include #include @@ -22,7 +22,7 @@ class UrlLineEdit : public QLineEdit { Q_OBJECT public: - explicit UrlLineEdit(QWidget *parent = nullptr); + explicit UrlLineEdit(const QHash &config, QWidget *parent = nullptr); void setCompleterModel(BookmarksView *model); @@ -47,7 +47,10 @@ private: QTextLayout::FormatRange m_hostFormat; - QAction *m_pageAction = nullptr; + // pageMenu action: zoom, print + QAction *m_pageMenuAction = nullptr; + // devMenu action: scripts, etc + QAction *m_toolsMenuAction = nullptr; // completer BookmarksView *m_bookmarksModel = nullptr; @@ -56,4 +59,4 @@ private: QMetaObject::Connection urlChangedConnection; }; -#endif // URLLINEEDIT_H +#endif // SMOLBOTE_URLLINEEDIT_H diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index e280fee..6dc7635 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -17,14 +17,13 @@ #include #include "addressbar/urllineedit.h" #include -//#include MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , tabBar(new MainWindowTabBar(config, this)) , menuBar(new MainWindowMenuBar(config, this)) - , m_addressBar(new UrlLineEdit(this)) + , m_addressBar(new UrlLineEdit(config->section("addressbar"), this)) , m_progressBar(new LoadingBar(this)) { Q_ASSERT(config); @@ -84,17 +83,6 @@ MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) ui->statusBar->addWidget(m_searchBox); m_searchBox->setVisible(false); - // shortcuts - QAction *focusAddressAction = new QAction(this); - focusAddressAction->setShortcut(QKeySequence(QString::fromStdString(m_config->value("browser.shortcuts.focusAddress").value()))); - //focusAddressAction->setShortcut(QKeySequence::fromString(browser->settings()->value("window.shortcuts.focusAddress").toString())); - //connect(focusAddressAction, SIGNAL(triggered(bool)), this, SLOT(focusAddress())); - connect(focusAddressAction, &QAction::triggered, this, [this]() { - m_addressBar->setFocus(); - m_addressBar->selectAll(); - }); - addAction(focusAddressAction); - resize(m_config->value("browser.window.width").value(), m_config->value("browser.window.height").value()); if(m_config->value("browser.window.maximized").value()) { showMaximized(); -- cgit v1.2.1