From f3a4607d6a722a862af0eb9747a15dcdf624b6fb Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 3 Nov 2019 00:18:10 +0200 Subject: Drop boost dependency - wrote not-invented-here config file parser and conf class - spent obscene amount of time plugging in said conf class --- lib/addressbar/addressbar.cpp | 61 ---------------- lib/addressbar/addressbar.h | 42 ----------- lib/addressbar/addressbar.ui | 59 --------------- lib/addressbar/completer.cpp | 81 --------------------- lib/addressbar/completer.h | 35 --------- lib/addressbar/meson.build | 15 ---- lib/addressbar/urllineedit.cpp | 158 ----------------------------------------- lib/addressbar/urllineedit.h | 51 ------------- 8 files changed, 502 deletions(-) delete mode 100644 lib/addressbar/addressbar.cpp delete mode 100644 lib/addressbar/addressbar.h delete mode 100644 lib/addressbar/addressbar.ui delete mode 100644 lib/addressbar/completer.cpp delete mode 100644 lib/addressbar/completer.h delete mode 100644 lib/addressbar/meson.build delete mode 100644 lib/addressbar/urllineedit.cpp delete mode 100644 lib/addressbar/urllineedit.h (limited to 'lib/addressbar') diff --git a/lib/addressbar/addressbar.cpp b/lib/addressbar/addressbar.cpp deleted file mode 100644 index 2ea6d5e..0000000 --- a/lib/addressbar/addressbar.cpp +++ /dev/null @@ -1,61 +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/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "addressbar.h" -#include "ui_addressbar.h" -#include - -AddressBar::AddressBar(const QHash &config, QWidget *parent) - : QWidget(parent) - , ui(new Ui::AddressBar) -{ - ui->setupUi(this); - - ui->urlBar->menuAction->setShortcut(QKeySequence(config.value("addressbar.shortcuts.menu"))); - - auto *focusShortcut = new QShortcut(QKeySequence(config.value("addressbar.shortcuts.focus")), parent); - connect(focusShortcut, &QShortcut::activated, ui->urlBar, [=]() { - ui->urlBar->setFocus(); - ui->urlBar->selectAll(); - }); - - connect(ui->urlBar, &UrlLineEdit::textEdited, [=](const QString &text) { - std::function callback = std::bind(&UrlLineEdit::updateCompleter, ui->urlBar, std::placeholders::_1); - emit complete(text, callback); - }); - - connect(ui->urlBar, &UrlLineEdit::returnPressed, [=]() { - const QUrl url = QUrl::fromUserInput(ui->urlBar->text()); - - // check if url contains \w+:// (matches protocol://) or contains a '.' (matches site.domain) - // this is because single words are valid URLs for QUrl (searchterm becomes http://searchterm) - // check for protocol://site because \. wouldn't match it (localhost is a search term; http://localhost is an address) - if(ui->urlBar->text().contains(QRegularExpression("\\w+://|\\.")) && url.isValid()) { - emit load(url); - } else { - emit search(ui->urlBar->text()); - } - }); -} - -AddressBar::~AddressBar() -{ - disconnect(this); -} - -void AddressBar::setUrl(const QUrl &url) -{ - if(url.isEmpty()) - ui->urlBar->clear(); - else - ui->urlBar->setUrl(url); -} - -void AddressBar::setProgress(int value) { - ui->loadingBar->setValue(std::min(value, 100)); -} diff --git a/lib/addressbar/addressbar.h b/lib/addressbar/addressbar.h deleted file mode 100644 index 4609ac1..0000000 --- a/lib/addressbar/addressbar.h +++ /dev/null @@ -1,42 +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/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_ADDRESSBAR_H -#define SMOLBOTE_ADDRESSBAR_H - -#include -#include - -namespace Ui { -class AddressBar; -} - -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); - void load(const QUrl &url); - void giveFocus(); - -public slots: - void setUrl(const QUrl &url); - void setProgress(int value); - -private: - Ui::AddressBar *ui; -}; - -#endif // SMOLBOTE_ADDRESSBAR_H diff --git a/lib/addressbar/addressbar.ui b/lib/addressbar/addressbar.ui deleted file mode 100644 index a5b4a4d..0000000 --- a/lib/addressbar/addressbar.ui +++ /dev/null @@ -1,59 +0,0 @@ - - - AddressBar - - - - 0 - 0 - 400 - 31 - - - - Form - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - - - - 16777215 - 5 - - - - false - - - - - - - - UrlLineEdit - QLineEdit -
urllineedit.h
-
-
- - -
diff --git a/lib/addressbar/completer.cpp b/lib/addressbar/completer.cpp deleted file mode 100644 index 578f745..0000000 --- a/lib/addressbar/completer.cpp +++ /dev/null @@ -1,81 +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/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "completer.h" -#include - -Completer::Completer(QWidget *parent) - : QListView(parent) -{ - setObjectName("Completer"); - setWindowFlags(Qt::ToolTip); - setEditTriggers(QAbstractItemView::NoEditTriggers); - - connect(this, &Completer::activated, [=](const QModelIndex &index) { - hide(); - emit completionActivated(index.data().toString()); - }); -} - -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; - - case Qt::Key_Enter: - case Qt::Key_Return: - hide(); - emit completionActivated(currentIndex.data().toString()); - break; - - default: - return false; - } - - return true; -} diff --git a/lib/addressbar/completer.h b/lib/addressbar/completer.h deleted file mode 100644 index 656a80f..0000000 --- a/lib/addressbar/completer.h +++ /dev/null @@ -1,35 +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/gitea/aqua/smolbote - * - * 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); - -signals: - void completionActivated(const QString &url); - -private: - QStringListModel *completionModel = nullptr; -}; - -#endif //SMOLBOTE_COMPLETER_H diff --git a/lib/addressbar/meson.build b/lib/addressbar/meson.build deleted file mode 100644 index 486d05d..0000000 --- a/lib/addressbar/meson.build +++ /dev/null @@ -1,15 +0,0 @@ -addressbar_inc = include_directories('.') -addressbar_moc = mod_qt5.preprocess( - moc_headers: ['addressbar.h', 'completer.h', 'urllineedit.h'], - ui_files: ['addressbar.ui'], - dependencies: dep_qt5 -) - -addressbar_lib = static_library('addressbar', ['addressbar.cpp', 'completer.cpp', 'urllineedit.cpp', addressbar_moc], - dependencies: dep_qt5, -) - -dep_addressbar = declare_dependency( - include_directories: addressbar_inc, - link_with: addressbar_lib -) diff --git a/lib/addressbar/urllineedit.cpp b/lib/addressbar/urllineedit.cpp deleted file mode 100644 index 1084f25..0000000 --- a/lib/addressbar/urllineedit.cpp +++ /dev/null @@ -1,158 +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/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "urllineedit.h" -#include -#include -#include -#include - -#include "addressbar.h" - -UrlLineEdit::UrlLineEdit(QWidget *parent) - : QLineEdit(parent) - , m_listView(new Completer(this)) -{ - setObjectName("UrlBar"); - setPlaceholderText(tr("Enter address")); - - m_listView->setVisible(false); - connect(m_listView, &Completer::completionActivated, this, &UrlLineEdit::setText); - - addressbar = qobject_cast(parent); - Q_CHECK_PTR(addressbar); - - 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); - - auto *loadAction = new QAction(tr("Paste and load"), this); - connect(loadAction, &QAction::triggered, this, [=]() { - emit addressbar->load(QUrl::fromUserInput(qApp->clipboard()->text())); - }); - actions.append(loadAction); - - auto *searchAction = new QAction(tr("Paste and search"), this); - connect(searchAction, &QAction::triggered, this, [=]() { - emit addressbar->search(qApp->clipboard()->text()); - }); - 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->exec(this->mapToGlobal(QPoint(0, height()))); - }); - - auto *goAction = addAction(style()->standardIcon(QStyle::SP_DialogOkButton), QLineEdit::TrailingPosition); - connect(goAction, &QAction::triggered, this, [this]() { - emit returnPressed(); - }); - - 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) -{ - // a context menu event also causes a focusInEvent, so if text is selected - // skip the formatting step - if(event->reason() == Qt::PopupFocusReason) { - QLineEdit::focusInEvent(event); - return; - } - - clearTextFormat(); - QLineEdit::focusInEvent(event); -} - -void UrlLineEdit::focusOutEvent(QFocusEvent *event) -{ - // a context menu event causes a focusOutEvent, and setUrl will clear the - // selection, and this would prevent the menu from working properly - if(event->reason() == Qt::PopupFocusReason) { - QLineEdit::focusOutEvent(event); - return; - } - - const QUrl url = QUrl::fromUserInput(text()); - if(url.isValid()) - setUrl(url); - - emit addressbar->giveFocus(); - QLineEdit::focusOutEvent(event); -} - -void UrlLineEdit::keyPressEvent(QKeyEvent *event) -{ - if(m_listView->keyPressed(event)) { - 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 deleted file mode 100644 index 4df8d21..0000000 --- a/lib/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/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_URLLINEEDIT_H -#define SMOLBOTE_URLLINEEDIT_H - -#include "completer.h" -#include "addressbar.h" -#include -#include -#include - -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: - QAction *menuAction = 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(); - - QList actions; - - QTextLayout::FormatRange m_hostFormat; - - // completer - Completer *m_listView; - AddressBar *addressbar; -}; - -#endif // SMOLBOTE_URLLINEEDIT_H -- cgit v1.2.1