diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-01 15:54:49 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-01 15:54:49 +0200 |
commit | 1ee841364215042f1f284e692ae191ebf7a64156 (patch) | |
tree | 48c1f49e29fe6b13cef68cd73dd2cab039fea822 /src | |
parent | Window::session (diff) | |
download | smolbote-1ee841364215042f1f284e692ae191ebf7a64156.tar.xz |
Split off addressbar into lib/
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/addressbar/addressbar.cpp | 87 | ||||
-rw-r--r-- | src/addressbar/addressbar.h | 40 | ||||
-rw-r--r-- | src/addressbar/completer.cpp | 68 | ||||
-rw-r--r-- | src/addressbar/completer.h | 32 | ||||
-rw-r--r-- | src/addressbar/urllineedit.cpp | 127 | ||||
-rw-r--r-- | src/addressbar/urllineedit.h | 51 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 43 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 9 | ||||
-rw-r--r-- | src/webengine/webview.h | 5 |
10 files changed, 38 insertions, 433 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dd325ec..5757a6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,14 +28,6 @@ add_executable(poi mainwindow/widgets/tabwidget.cpp mainwindow/widgets/tabwidget.h - # address bar - addressbar/addressbar.cpp - addressbar/addressbar.h - addressbar/completer.cpp - addressbar/completer.h - addressbar/urllineedit.cpp - addressbar/urllineedit.h - # webengine webengine/urlinterceptor.cpp webengine/urlinterceptor.h @@ -63,6 +55,7 @@ target_link_libraries(poi Qt5::Core Qt5::Widgets Qt5::Concurrent Qt5::WebEngineWidgets ${Boost_LIBRARIES} about + addressbar configuration bookmarks downloads) diff --git a/src/addressbar/addressbar.cpp b/src/addressbar/addressbar.cpp deleted file mode 100644 index b86230c..0000000 --- a/src/addressbar/addressbar.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "addressbar.h" -#include "urllineedit.h" -#include "webengine/webview.h" -#include <QProgressBar> -#include <QShortcut> -#include <QVBoxLayout> - -AddressBar::AddressBar(const QHash<QString, QString> &config, QWidget *parent) - : QWidget(parent) -{ - setLayout(new QVBoxLayout()); - layout()->setContentsMargins(0, 0, 0, 0); - layout()->setSpacing(0); - - urlBar = new UrlLineEdit(this); - layout()->addWidget(urlBar); - - auto *focusShortcut = new QShortcut(QKeySequence(config.value("addressbar.shortcuts.focus")), parent); - connect(focusShortcut, &QShortcut::activated, urlBar, [=]() { - urlBar->setFocus(); - 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<void(QStringList &)> callback = std::bind(&UrlLineEdit::updateCompleter, urlBar, std::placeholders::_1); - emit complete(text, callback); - }); - - progressBar = new QProgressBar(this); - progressBar->setMaximumHeight(5); - progressBar->setTextVisible(false); - layout()->addWidget(progressBar); -} - -AddressBar::~AddressBar() -{ - disconnect(urlChangedConnection); - disconnect(loadUrlConnection); - disconnect(progressBarConnection); -} - -void AddressBar::connectWebView(WebView *view) -{ - disconnect(urlChangedConnection); - disconnect(loadUrlConnection); - disconnect(progressBarConnection); - - if(view == nullptr) { - urlBar->clear(); - urlBar->pageMenu_action->setMenu(nullptr); - urlBar->toolsMenu_action->setMenu(nullptr); - - progressBar->setValue(100); - return; - } - - urlBar->setUrl(view->url()); - urlBar->pageMenu_action->setMenu(view->pageMenu()); - urlBar->toolsMenu_action->setMenu(view->toolsMenu()); - urlChangedConnection = connect(view, &WebView::urlChanged, urlBar, &UrlLineEdit::setUrl); - loadUrlConnection = connect(urlBar, &UrlLineEdit::returnPressed, [=]() { - if(urlBar->text().startsWith('#')) { - ; - view->search(urlBar->text().mid(1)); - } else { - view->load(QUrl::fromUserInput(urlBar->text())); - } - view->setFocus(); - }); - - progressBar->setValue(view->loadProgress()); - progressBarConnection = connect(view, &QWebEngineView::loadProgress, progressBar, &QProgressBar::setValue); -} diff --git a/src/addressbar/addressbar.h b/src/addressbar/addressbar.h deleted file mode 100644 index 9b8b360..0000000 --- a/src/addressbar/addressbar.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef ADDRESSBAR_H -#define ADDRESSBAR_H - -#include <QWidget> -#include <functional> - -class WebView; -class UrlLineEdit; -class QProgressBar; -class AddressBar : public QWidget -{ - Q_OBJECT - -public: - AddressBar(const QHash<QString, QString> &config, QWidget *parent = nullptr); - ~AddressBar() override; - -signals: - void complete(const QString &term, std::function<void(QStringList &)> callback); - -public slots: - void connectWebView(WebView *view); - -private: - UrlLineEdit *urlBar; - QProgressBar *progressBar; - - QMetaObject::Connection urlChangedConnection, loadUrlConnection; - QMetaObject::Connection progressBarConnection; -}; - -#endif // ADDRESSBAR_H diff --git a/src/addressbar/completer.cpp b/src/addressbar/completer.cpp deleted file mode 100644 index 9b95ac0..0000000 --- a/src/addressbar/completer.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "completer.h" -#include <QKeyEvent> - -Completer::Completer(QWidget *parent) - : QListView(parent) -{ - setWindowFlags(Qt::ToolTip); -} - -bool Completer::updateItems(const QStringList &list) -{ - if(list.isEmpty()) - return false; - - auto *model = new QStringListModel(list, this); - setModel(model); - - delete completionModel; - completionModel = model; - - return true; -} - -bool Completer::keyPressed(QKeyEvent *event) -{ - if(isHidden()) - return false; - - Q_CHECK_PTR(completionModel); - - int count = completionModel->rowCount(); - const QModelIndex currentIndex = this->currentIndex(); - - switch(event->key()) { - case Qt::Key_Down: - if(currentIndex.row() + 1 >= count) { - setCurrentIndex(completionModel->index(0, 0)); - } else { - setCurrentIndex(completionModel->index(currentIndex.row() + 1, 0)); - } - break; - - case Qt::Key_Up: - if(currentIndex.row() == 0) { - setCurrentIndex(completionModel->index(count - 1, 0)); - } else { - setCurrentIndex(completionModel->index(currentIndex.row() - 1, 0)); - } - break; - - case Qt::Key_Escape: - hide(); - break; - - default: - break; - } - - return true; -} diff --git a/src/addressbar/completer.h b/src/addressbar/completer.h deleted file mode 100644 index 03ff317..0000000 --- a/src/addressbar/completer.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_COMPLETER_H -#define SMOLBOTE_COMPLETER_H - -#include <QListView> -#include <QStringListModel> -#include <QTreeWidgetItem> - -class Completer : public QListView -{ - - Q_OBJECT - -public: - explicit Completer(QWidget *parent = nullptr); - - bool updateItems(const QStringList &list); - - bool keyPressed(QKeyEvent *event); - -private: - QStringListModel *completionModel = nullptr; -}; - -#endif //SMOLBOTE_COMPLETER_H diff --git a/src/addressbar/urllineedit.cpp b/src/addressbar/urllineedit.cpp deleted file mode 100644 index fa65e5b..0000000 --- a/src/addressbar/urllineedit.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "urllineedit.h" -#include <QLabel> -#include <QMenu> -#include <QShortcut> -#include <QTimer> -#include <QWidgetAction> -#include <bookmarks/bookmarkswidget.h> - -UrlLineEdit::UrlLineEdit(QWidget *parent) - : QLineEdit(parent) - , m_listView(new Completer(this)) -{ - setPlaceholderText(tr("Enter address")); - - 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()))); - } - }); - - 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()))); - } - }); - - QTextCharFormat hostnameFormat; - hostnameFormat.setFontWeight(QFont::Bold); - m_hostFormat.format = hostnameFormat; -} - -void UrlLineEdit::setUrl(const QUrl &url) -{ - QString urlText = url.toString(); - QString domain = url.host(); - - m_hostFormat.start = urlText.indexOf(domain); - m_hostFormat.length = domain.length(); - - clear(); - clearTextFormat(); - setTextFormat(m_hostFormat); - setText(urlText); -} - -void UrlLineEdit::updateCompleter(const QStringList &l) -{ - if(!m_listView->updateItems(l)) { - m_listView->hide(); - return; - } - - // positioning - m_listView->setFixedWidth(width()); - m_listView->move(mapToGlobal(QPoint(0, height()))); - m_listView->show(); -} - -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); -} - -void UrlLineEdit::keyPressEvent(QKeyEvent *event) -{ - if(m_listView->keyPressed(event)) { - int key = event->key(); - QModelIndex currentIndex = m_listView->currentIndex(); - - if(key == Qt::Key::Key_Enter || key == Qt::Key_Return) { - - if(currentIndex.isValid()) { - 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); -} - -// formatting taken from: https://forum.qt.io/topic/60962/setting-qlineedit-text-bold -void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format) -{ - QList<QInputMethodEvent::Attribute> attributes; - attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, format.start, format.length, format.format)); - QInputMethodEvent ev(QString(), attributes); - event(&ev); -} - -void UrlLineEdit::clearTextFormat() -{ - setTextFormat(QTextLayout::FormatRange()); -} diff --git a/src/addressbar/urllineedit.h b/src/addressbar/urllineedit.h deleted file mode 100644 index f27addc..0000000 --- a/src/addressbar/urllineedit.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_URLLINEEDIT_H -#define SMOLBOTE_URLLINEEDIT_H - -#include "completer.h" -#include <QAction> -#include <QLineEdit> -#include <QTextLayout> - -class QMenu; -class WebView; -class UrlLineEdit : public QLineEdit -{ - Q_OBJECT -public: - explicit UrlLineEdit(QWidget *parent = nullptr); - -public slots: - void setUrl(const QUrl &url); - - void updateCompleter(const QStringList &l); - -public: - // pageMenu action: zoom, print - QAction *pageMenu_action = nullptr; - // devMenu action: scripts, etc - QAction *toolsMenu_action = nullptr; - -protected: - void focusInEvent(QFocusEvent *event) override; - void focusOutEvent(QFocusEvent *event) override; - void keyPressEvent(QKeyEvent *event) override; - -private: - void setTextFormat(const QTextLayout::FormatRange &format); - void clearTextFormat(); - - QTextLayout::FormatRange m_hostFormat; - - // completer - Completer *m_listView; -}; - -#endif // SMOLBOTE_URLLINEEDIT_H diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index be5e6e4..35cc598 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -46,7 +46,7 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) createMenuBar(); - auto *navigationToolBar = new NavigationBar(config->section("navigation"), this); + navigationToolBar = new NavigationBar(config->section("navigation"), this); navigationToolBar->setMovable(config->value<bool>("navigation.movable").value()); addToolBar(Qt::TopToolBarArea, navigationToolBar); navigationToolBar->connectWebView(nullptr); @@ -63,9 +63,8 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) searchBox->setVisible(false); // connect signlas - connect(mdiArea, &QMdiArea::subWindowActivated, this, [this, navigationToolBar](QMdiSubWindow *window) { - disconnect(addressBarConnection); - disconnect(navigationBarConnection); + connect(mdiArea, &QMdiArea::subWindowActivated, this, [this](QMdiSubWindow *window) { + disconnect(viewChangedConnection); disconnect(searchBoxConnection); disconnect(statusBarConnection); subWindowAction->setMenu(nullptr); @@ -73,18 +72,12 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) auto *w = qobject_cast<Window *>(window); if(w == nullptr) { // no current subwindow, clear everything - setWindowTitle(tr("smolbote")); - addressBar->connectWebView(nullptr); - navigationToolBar->connectWebView(nullptr); - searchBox->setView(nullptr); + setView(nullptr); + subWindowAction->setMenu(nullptr); } else { + setView(w->currentView()); subWindowAction->setMenu(w->systemMenu()); - addressBar->connectWebView(w->currentView()); - addressBarConnection = connect(w, &Window::currentViewChanged, addressBar, &AddressBar::connectWebView); - navigationToolBar->connectWebView(w->currentView()); - navigationBarConnection = connect(w, &Window::currentViewChanged, navigationToolBar, &NavigationBar::connectWebView); - searchBox->setView(w->currentView()); - searchBoxConnection = connect(w, &Window::currentViewChanged, searchBox, &SearchForm::setView); + viewChangedConnection = connect(w, &Window::currentViewChanged, this, &MainWindow::setView); statusBarConnection = connect(w, &Window::showStatusMessage, statusBar(), &QStatusBar::showMessage); } }); @@ -97,8 +90,8 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) MainWindow::~MainWindow() { - disconnect(addressBarConnection); - disconnect(navigationBarConnection); + disconnect(viewChangedConnection); + disconnect(searchConnection); disconnect(searchBoxConnection); disconnect(statusBarConnection); } @@ -192,6 +185,24 @@ Window *MainWindow::createSubWindow(const QUrl &url) return w; } +void MainWindow::setView(WebView *view) +{ + disconnect(searchConnection); + + addressBar->setView(view); + if(view) { + addressBar->setPageMenu(view->pageMenu()); + addressBar->setToolsMenu(view->toolsMenu()); + searchConnection = connect(addressBar, &AddressBar::search, view, &WebView::search); + } else { + addressBar->setPageMenu(nullptr); + addressBar->setToolsMenu(nullptr); + } + + navigationToolBar->connectWebView(view); + searchBox->setView(view); +} + void MainWindow::closeEvent(QCloseEvent *event) { if(mdiArea->subWindowList().count() > 1) { diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 1257a56..489075f 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -18,6 +18,8 @@ class Configuration; class Window; class AddressBar; class SearchForm; +class WebView; +class NavigationBar; class MainWindow : public QMainWindow { friend class Browser; @@ -42,20 +44,23 @@ public slots: void createTab(const QUrl &url); Window *createSubWindow(const QUrl &url); + void setView(WebView *view); + protected: void closeEvent(QCloseEvent *event) override; private: QAction *subWindowAction = nullptr; QMenu *toolsMenu = nullptr; + NavigationBar *navigationToolBar = nullptr; AddressBar *addressBar = nullptr; SearchForm *searchBox = nullptr; QMdiArea *mdiArea; std::shared_ptr<Configuration> m_config; - QMetaObject::Connection addressBarConnection, navigationBarConnection; - QMetaObject::Connection searchBoxConnection; + QMetaObject::Connection viewChangedConnection; + QMetaObject::Connection searchConnection, searchBoxConnection; QMetaObject::Connection statusBarConnection; }; diff --git a/src/webengine/webview.h b/src/webengine/webview.h index 5ab04cd..2a99fdd 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -41,10 +41,11 @@ public: bool isLoaded() const; int loadProgress() const; - void search(const QString &term); - void triggerViewAction(ViewAction action); +public slots: + void search(const QString &term); + signals: // loadStarted is always emitted, be it page load or in-page request, // but loadFinished is only emitted when it's a page load |