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/ --- 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 ----------------- 6 files changed, 405 deletions(-) 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 (limited to 'src/addressbar') 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 -- cgit v1.2.1