diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-01-04 13:02:40 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-01-04 11:17:12 +0100 |
| commit | 3d795469818177c5b9932df3577bb6e9c1d0ab2e (patch) | |
| tree | da974e41296f5084ca6b0c1567d741196cab696c /src | |
| parent | 7ec3ed15aa99148f1c8df86bb49202da67d6813d (diff) | |
| download | kristall-3d795469818177c5b9932df3577bb6e9c1d0ab2e.tar.gz | |
New popup when adding to favourites!
Diffstat (limited to 'src')
| -rw-r--r-- | src/browsertab.cpp | 36 | ||||
| -rw-r--r-- | src/browsertab.hpp | 4 | ||||
| -rw-r--r-- | src/favouritecollection.cpp | 31 | ||||
| -rw-r--r-- | src/favouritecollection.hpp | 3 | ||||
| -rw-r--r-- | src/kristall.pro | 6 | ||||
| -rw-r--r-- | src/mainwindow.cpp | 6 | ||||
| -rw-r--r-- | src/widgets/favouritepopup.cpp | 80 | ||||
| -rw-r--r-- | src/widgets/favouritepopup.hpp | 37 |
8 files changed, 199 insertions, 4 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp index e8b8443..c8fc307 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -21,6 +21,7 @@ #include "ioutil.hpp" #include "kristall.hpp" +#include "widgets/favouritepopup.hpp" #include <cassert> #include <QTabWidget> @@ -109,6 +110,14 @@ BrowserTab::BrowserTab(MainWindow *mainWindow) : QWidget(nullptr), QShortcut * sc = new QShortcut(QKeySequence("Escape"), this->ui->search_bar); connect(sc, &QShortcut::activated, this, &BrowserTab::on_close_search_clicked); } + + FavouritePopup * popup_menu = new FavouritePopup(this->ui->fav_button, this); + connect(popup_menu, &FavouritePopup::unfavourited, this, [this]() { + this->ui->fav_button->setChecked(false); + kristall::favourites.removeUrl(this->current_location); + }); + this->ui->fav_button->setPopupMode(QToolButton::DelayedPopup); + this->ui->fav_button->setMenu(popup_menu); } BrowserTab::~BrowserTab() @@ -864,9 +873,29 @@ void BrowserTab::pushToHistory(const QUrl &url) this->updateUI(); } +void BrowserTab::addToFavouritesPopup() +{ + // We add it to favourites immediately. + kristall::favourites.addUnsorted(this->current_location, this->page_title); + + // Show menu, this will block thread + this->ui->fav_button->setChecked(true); + FavouritePopup *popup = static_cast<FavouritePopup*>(this->ui->fav_button->menu()); + popup->fav_title->setText( + kristall::favourites.getFavourite(this->current_location).title + ); + popup->setFocus(Qt::PopupFocusReason); + popup->fav_title->setFocus(Qt::PopupFocusReason); + popup->fav_title->selectAll(); + this->ui->fav_button->showMenu(); + + // Update the favourites entry with what user inputted into menu + kristall::favourites.editFavouriteTitle(this->current_location, popup->fav_title->text()); +} + void BrowserTab::on_fav_button_clicked() { - toggleIsFavourite(this->ui->fav_button->isChecked()); + this->addToFavouritesPopup(); } void BrowserTab::on_text_browser_anchorClicked(const QUrl &url, bool open_in_new_tab) @@ -1106,6 +1135,11 @@ void BrowserTab::updateUI() this->ui->refresh_button->setVisible(not in_progress); this->ui->stop_button->setVisible(in_progress); + this->refreshFavButton(); +} + +void BrowserTab::refreshFavButton() +{ this->ui->fav_button->setEnabled(this->successfully_loaded); this->ui->fav_button->setChecked(kristall::favourites.containsUrl(this->current_location)); } diff --git a/src/browsertab.hpp b/src/browsertab.hpp index 52340d9..7b0b749 100644 --- a/src/browsertab.hpp +++ b/src/browsertab.hpp @@ -81,6 +81,10 @@ public: void updatePageTitle(); + void refreshFavButton(); + + void addToFavouritesPopup(); + void setUrlBarText(const QString & text); void updateUrlBarStyle(); diff --git a/src/favouritecollection.cpp b/src/favouritecollection.cpp index 8ff19e6..2472610 100644 --- a/src/favouritecollection.cpp +++ b/src/favouritecollection.cpp @@ -178,11 +178,42 @@ void FavouriteCollection::editFavouriteTitle(const QModelIndex &index, const QSt this->getMutableFavourite(index)->title = title; } +bool FavouriteCollection::editFavouriteTitle(const QUrl &url, const QString &new_title) +{ + for(auto const & group : this->root.children) + { + for(auto const & ident : group->children) + { + FavouriteNode* node = &ident->as<FavouriteNode>(); + if(node->favourite.destination == url) + { + node->favourite.title = new_title; + return true; + } + } + } + return false; +} + void FavouriteCollection::editFavouriteDest(const QModelIndex &index, const QUrl &url) { this->getMutableFavourite(index)->destination = url; } +Favourite FavouriteCollection::getFavourite(const QUrl &url) const +{ + for(auto const & group : this->root.children) + { + for(auto const & ident : group->children) + { + FavouriteNode* node = &ident->as<FavouriteNode>(); + if(node->favourite.destination == url) + return node->favourite; + } + } + return Favourite(); +} + Favourite FavouriteCollection::getFavourite(const QModelIndex &index) const { if (!index.isValid()) diff --git a/src/favouritecollection.hpp b/src/favouritecollection.hpp index 1316410..359f173 100644 --- a/src/favouritecollection.hpp +++ b/src/favouritecollection.hpp @@ -78,9 +78,12 @@ public: bool addFavourite(QString const & group, Favourite const & fav); void editFavouriteTitle(const QModelIndex &index, const QString &title); + bool editFavouriteTitle(const QUrl &url, const QString &new_title); void editFavouriteDest(const QModelIndex & index, const QUrl & url); + Favourite getFavourite(const QUrl &url) const; + Favourite getFavourite(QModelIndex const & index) const; Favourite * getMutableFavourite(QModelIndex const & index); diff --git a/src/kristall.pro b/src/kristall.pro index 5875a72..03bb58d 100644 --- a/src/kristall.pro +++ b/src/kristall.pro @@ -118,7 +118,8 @@ SOURCES += \ trustedhostcollection.cpp \ widgets/elidelabel.cpp \ widgets/searchbar.cpp \ - widgets/ssltrusteditor.cpp + widgets/ssltrusteditor.cpp \ + widgets/favouritepopup.cpp HEADERS += \ ../lib/luis-l-gist/interactiveview.hpp \ @@ -161,7 +162,8 @@ HEADERS += \ trustedhostcollection.hpp \ widgets/elidelabel.hpp \ widgets/searchbar.hpp \ - widgets/ssltrusteditor.hpp + widgets/ssltrusteditor.hpp \ + widgets/favouritepopup.hpp FORMS += \ browsertab.ui \ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b2b7b15..38077ee 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -226,6 +226,10 @@ void MainWindow::on_browser_tabs_currentChanged(int index) { tab->rerenderPage(); } + else + { + tab->refreshFavButton(); + } } else { this->ui->outline_view->setModel(nullptr); this->ui->history_view->setModel(nullptr); @@ -445,7 +449,7 @@ void MainWindow::on_actionAdd_to_favourites_triggered() { BrowserTab * tab = this->curTab(); if(tab != nullptr) { - tab->toggleIsFavourite(); + tab->addToFavouritesPopup(); } } diff --git a/src/widgets/favouritepopup.cpp b/src/widgets/favouritepopup.cpp new file mode 100644 index 0000000..b7e11fa --- /dev/null +++ b/src/widgets/favouritepopup.cpp @@ -0,0 +1,80 @@ +#include "favouritepopup.hpp" + +#include <QToolButton> +#include <QVBoxLayout> +#include <QGridLayout> +#include <QLabel> +#include <QKeyEvent> + +FavouritePopup::FavouritePopup(QToolButton *button, QWidget *parent) + : QMenu(parent), b(button) +{ + auto parent_layout = new QVBoxLayout(); + parent_layout->setContentsMargins(8, 8, 8, 8); + + auto layout = new QGridLayout(); + + // Title + auto title_lab = new QLabel("Title:"); + this->fav_title = new QLineEdit(); + layout->addWidget(title_lab, 0, 0); + layout->addWidget(this->fav_title, 0, 1); + + // Unfavourite + auto unfav_btn = new QPushButton("Unfavourite"); + layout->addWidget(unfav_btn); + connect(unfav_btn, &QPushButton::clicked, this, [this]() { + this->setVisible(false); + emit this->unfavourited(); + }); + + // Confirm + this->confirm_btn = new QPushButton("Confirm"); + layout->addWidget(this->confirm_btn); + connect(confirm_btn, &QPushButton::clicked, this, [this]() { + this->confirmPressed(); + }); + + parent_layout->addLayout(layout); + + this->setLayout(parent_layout); + this->setMinimumWidth(250); +} + +void FavouritePopup::confirmPressed() +{ + this->setVisible(false); + emit this->confirmed(); +} + +void FavouritePopup::showEvent(QShowEvent *event) +{ + QPoint p = this->pos(); + QRect geo = b->geometry(); + this->move( + p.x() + geo.width() - this->geometry().width(), + p.y()); +} + +void FavouritePopup::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Return) + { + this->confirmPressed(); + return; + } + else if (event->key() == Qt::Key_Escape) + { + this->setVisible(false); + return; + } + QMenu::keyPressEvent(event); +} + +void FavouritePopup::keyReleaseEvent(QKeyEvent *event) +{ + if (event->key() != Qt::Key_Escape) + { + QMenu::keyReleaseEvent(event); + } +} diff --git a/src/widgets/favouritepopup.hpp b/src/widgets/favouritepopup.hpp new file mode 100644 index 0000000..a07d2dd --- /dev/null +++ b/src/widgets/favouritepopup.hpp @@ -0,0 +1,37 @@ +#ifndef FAVOURITEPOPUP_HPP +#define FAVOURITEPOPUP_HPP + +#include <QMenu> +#include <QPushButton> +#include <QLineEdit> + +class QToolButton; + +class FavouritePopup : public QMenu +{ + Q_OBJECT; +public: + explicit FavouritePopup(QToolButton * button, QWidget * parent); + + void showEvent(QShowEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + void keyReleaseEvent(QKeyEvent *event) override; + +signals: + void confirmed(); + + void unfavourited(); + +private: + void confirmPressed(); + +private: + QToolButton *b; + +public: + QLineEdit *fav_title; + QPushButton *confirm_btn; + +}; + +#endif |
