From 1ee841364215042f1f284e692ae191ebf7a64156 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 1 May 2018 15:54:49 +0200 Subject: Split off addressbar into lib/ --- CMakeLists.txt | 1 + lib/addressbar/CMakeLists.txt | 21 +++++++ lib/addressbar/addressbar.cpp | 94 ++++++++++++++++++++++++++++++ lib/addressbar/addressbar.h | 44 ++++++++++++++ lib/addressbar/completer.cpp | 68 ++++++++++++++++++++++ lib/addressbar/completer.h | 32 +++++++++++ lib/addressbar/urllineedit.cpp | 126 ++++++++++++++++++++++++++++++++++++++++ lib/addressbar/urllineedit.h | 51 +++++++++++++++++ src/CMakeLists.txt | 9 +-- src/addressbar/addressbar.cpp | 87 ---------------------------- src/addressbar/addressbar.h | 40 ------------- src/addressbar/completer.cpp | 68 ---------------------- src/addressbar/completer.h | 32 ----------- src/addressbar/urllineedit.cpp | 127 ----------------------------------------- src/addressbar/urllineedit.h | 51 ----------------- src/mainwindow/mainwindow.cpp | 43 ++++++++------ src/mainwindow/mainwindow.h | 9 ++- src/webengine/webview.h | 5 +- 18 files changed, 475 insertions(+), 433 deletions(-) create mode 100644 lib/addressbar/CMakeLists.txt create mode 100644 lib/addressbar/addressbar.cpp create mode 100644 lib/addressbar/addressbar.h create mode 100644 lib/addressbar/completer.cpp create mode 100644 lib/addressbar/completer.h create mode 100644 lib/addressbar/urllineedit.cpp create mode 100644 lib/addressbar/urllineedit.h delete mode 100644 src/addressbar/addressbar.cpp delete mode 100644 src/addressbar/addressbar.h delete mode 100644 src/addressbar/completer.cpp delete mode 100644 src/addressbar/completer.h delete mode 100644 src/addressbar/urllineedit.cpp delete mode 100644 src/addressbar/urllineedit.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 673a9d0..f084316 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,7 @@ endif() configure_file("${PROJECT_SOURCE_DIR}/src/version.h.in" "${PROJECT_BINARY_DIR}/src/version.h") add_subdirectory(lib/about) +add_subdirectory(lib/addressbar) add_subdirectory(lib/bookmarks) add_subdirectory(lib/downloads) add_subdirectory(lib/configuration) diff --git a/lib/addressbar/CMakeLists.txt b/lib/addressbar/CMakeLists.txt new file mode 100644 index 0000000..1122bc3 --- /dev/null +++ b/lib/addressbar/CMakeLists.txt @@ -0,0 +1,21 @@ +# Find includes in corresponding build directories +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +# Instruct CMake to run moc automatically when needed. +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTOUIC ON) +set(CMAKE_AUTORCC ON) + +add_library(addressbar + addressbar.cpp + addressbar.h + completer.cpp + completer.h + urllineedit.cpp + urllineedit.h +) + +#target_include_directories(addressbar +# PRIVATE ${Boost_INCLUDE_DIRS}) + +target_link_libraries(addressbar Qt5::Widgets Qt5::WebEngineWidgets) diff --git a/lib/addressbar/addressbar.cpp b/lib/addressbar/addressbar.cpp new file mode 100644 index 0000000..9b3970d --- /dev/null +++ b/lib/addressbar/addressbar.cpp @@ -0,0 +1,94 @@ +/* + * 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 +#include +#include +#include + +AddressBar::AddressBar(const QHash &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 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::setView(QWebEngineView *view) +{ + disconnect(urlChangedConnection); + disconnect(loadUrlConnection); + disconnect(progressBarConnection); + + progressBar->setValue(100); + + if(view == nullptr) { + urlBar->clear(); + urlBar->pageMenu_action->setMenu(nullptr); + urlBar->toolsMenu_action->setMenu(nullptr); + return; + } + + urlBar->setUrl(view->url()); + urlChangedConnection = connect(view, &QWebEngineView::urlChanged, urlBar, &UrlLineEdit::setUrl); + loadUrlConnection = connect(urlBar, &UrlLineEdit::returnPressed, [=]() { + if(urlBar->text().startsWith('#')) { + emit 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); +} + +void AddressBar::setPageMenu(QMenu *menu) +{ + urlBar->pageMenu_action->setMenu(menu); +} + +void AddressBar::setToolsMenu(QMenu *menu) +{ + urlBar->toolsMenu_action->setMenu(menu); +} diff --git a/lib/addressbar/addressbar.h b/lib/addressbar/addressbar.h new file mode 100644 index 0000000..861f985 --- /dev/null +++ b/lib/addressbar/addressbar.h @@ -0,0 +1,44 @@ +/* + * 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_ADDRESSBAR_H +#define SMOLBOTE_ADDRESSBAR_H + +#include +#include + +class QWebEngineView; +class UrlLineEdit; +class QProgressBar; +class QMenu; +class AddressBar : public QWidget +{ + Q_OBJECT + +public: + AddressBar(const QHash &config, QWidget *parent = nullptr); + ~AddressBar() override; + +signals: + void complete(const QString &term, std::function callback); + void search(const QString &term); + +public slots: + void setView(QWebEngineView *view); + void setPageMenu(QMenu *menu); + void setToolsMenu(QMenu *menu); + +private: + UrlLineEdit *urlBar; + QProgressBar *progressBar; + + QMetaObject::Connection urlChangedConnection, loadUrlConnection; + QMetaObject::Connection progressBarConnection; +}; + +#endif // SMOLBOTE_ADDRESSBAR_H diff --git a/lib/addressbar/completer.cpp b/lib/addressbar/completer.cpp new file mode 100644 index 0000000..9b95ac0 --- /dev/null +++ b/lib/addressbar/completer.cpp @@ -0,0 +1,68 @@ +/* + * 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 + +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/lib/addressbar/completer.h b/lib/addressbar/completer.h new file mode 100644 index 0000000..03ff317 --- /dev/null +++ b/lib/addressbar/completer.h @@ -0,0 +1,32 @@ +/* + * 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 +#include +#include + +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/lib/addressbar/urllineedit.cpp b/lib/addressbar/urllineedit.cpp new file mode 100644 index 0000000..27acf60 --- /dev/null +++ b/lib/addressbar/urllineedit.cpp @@ -0,0 +1,126 @@ +/* + * 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 +#include +#include +#include +#include + +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 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/lib/addressbar/urllineedit.h b/lib/addressbar/urllineedit.h new file mode 100644 index 0000000..f27addc --- /dev/null +++ b/lib/addressbar/urllineedit.h @@ -0,0 +1,51 @@ +/* + * 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 +#include +#include + +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/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 -#include -#include - -AddressBar::AddressBar(const QHash &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 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 -#include - -class WebView; -class UrlLineEdit; -class QProgressBar; -class AddressBar : public QWidget -{ - Q_OBJECT - -public: - AddressBar(const QHash &config, QWidget *parent = nullptr); - ~AddressBar() override; - -signals: - void complete(const QString &term, std::function 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 - -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 -#include -#include - -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 -#include -#include -#include -#include -#include - -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 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 -#include -#include - -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 &config, QWidget *parent) createMenuBar(); - auto *navigationToolBar = new NavigationBar(config->section("navigation"), this); + navigationToolBar = new NavigationBar(config->section("navigation"), this); navigationToolBar->setMovable(config->value("navigation.movable").value()); addToolBar(Qt::TopToolBarArea, navigationToolBar); navigationToolBar->connectWebView(nullptr); @@ -63,9 +63,8 @@ MainWindow::MainWindow(std::shared_ptr &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 &config, QWidget *parent) auto *w = qobject_cast(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 &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 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 -- cgit v1.2.1