diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-01-07 16:17:58 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-01-07 10:05:00 +0100 |
| commit | 19046566a62f9e616a23584ebbe045ddbc132023 (patch) | |
| tree | 903ed2a82eddd1c318574725b498fc1938f6c472 /src | |
| parent | 772cad15cef23559986869733f541b5f3ea4d130 (diff) | |
| download | kristall-19046566a62f9e616a23584ebbe045ddbc132023.tar.gz | |
favourite popup: adding group combobox!
Diffstat (limited to 'src')
| -rw-r--r-- | src/browsertab.cpp | 71 | ||||
| -rw-r--r-- | src/favouritecollection.cpp | 44 | ||||
| -rw-r--r-- | src/favouritecollection.hpp | 4 | ||||
| -rw-r--r-- | src/mainwindow.cpp | 25 | ||||
| -rw-r--r-- | src/mainwindow.hpp | 2 | ||||
| -rw-r--r-- | src/mainwindow.ui | 2 | ||||
| -rw-r--r-- | src/widgets/favouritepopup.cpp | 23 | ||||
| -rw-r--r-- | src/widgets/favouritepopup.hpp | 6 |
8 files changed, 159 insertions, 18 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 008ec80..cdc346b 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -114,13 +114,45 @@ BrowserTab::BrowserTab(MainWindow *mainWindow) : QWidget(nullptr), 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]() { + FavouritePopup * popup = new FavouritePopup(this->ui->fav_button, this); + connect(popup, &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); + this->ui->fav_button->setMenu(popup); + + connect(popup, &FavouritePopup::newGroupClicked, this, [this, popup]() { + // Dialog to create new group + QString v = this->mainWindow->newGroupDialog(); + + // Update combobox + popup->fav_group->clear(); + QStringList groups = kristall::favourites.groups(); + for (int i = 0; i < groups.length(); ++i) + { + popup->fav_group->addItem(groups[i]); + + // Select this group if it is current one + if (!v.isEmpty() && groups[i] == v) + { + popup->fav_group->setCurrentIndex(i); + } + } + + // Show the menu again + this->ui->fav_button->showMenu(); + }); + + connect(popup->fav_group, QOverload<int>::of(&QComboBox::currentIndexChanged), + [this, popup](int index) + { + if (!popup->is_ready || index == -1) return; + + // Change favourite's current group + kristall::favourites.editFavouriteGroup(this->current_location, + popup->fav_group->currentText()); + }); } BrowserTab::~BrowserTab() @@ -877,15 +909,40 @@ void BrowserTab::showFavouritesPopup() // We add it to favourites immediately. kristall::favourites.addUnsorted(this->current_location, this->page_title); - // Show menu, this will block thread + const Favourite fav = kristall::favourites.getFavourite(this->current_location); + 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 - ); + + // Prepare menu: + + popup->is_ready = false; + { + // Setup the group combobox + popup->fav_group->setCurrentIndex(-1); + popup->fav_group->clear(); + QStringList groups = kristall::favourites.groups(); + for (int i = 0; i < groups.length(); ++i) + { + popup->fav_group->addItem(groups[i]); + + // Set combobox index to current group + if (groups[i] == kristall::favourites.groupForFavourite(fav.destination)) + { + popup->fav_group->setCurrentIndex(i); + } + } + } + popup->fav_title->setText(fav.title.isEmpty() + ? fav.destination.toString(QUrl::FullyEncoded) + : fav.title); popup->setFocus(Qt::PopupFocusReason); popup->fav_title->setFocus(Qt::PopupFocusReason); popup->fav_title->selectAll(); + + popup->is_ready = true; + + // Show menu, this will block thread this->ui->fav_button->showMenu(); // Update the favourites entry with what user inputted into menu diff --git a/src/favouritecollection.cpp b/src/favouritecollection.cpp index 253ec15..29b79b7 100644 --- a/src/favouritecollection.cpp +++ b/src/favouritecollection.cpp @@ -202,6 +202,36 @@ void FavouriteCollection::editFavouriteDest(const QModelIndex &index, const QUrl this->getMutableFavourite(index)->destination = url; } +bool FavouriteCollection::editFavouriteGroup(const QUrl &u, const QString &group_name) +{ + // Find and erase favourite node + QUrl url = IoUtil::uniformUrl(u); + for (auto const & group : this->root.children) + { + size_t index = 0; + for (auto it = group->children.begin(); it != group->children.end(); it++, index++) + { + auto& fav = it->get()->as<FavouriteNode>(); + if(IoUtil::uniformUrl(fav.favourite.destination) == url) + { + Favourite f = Favourite { + fav.favourite.title, + fav.favourite.destination + }; + + beginRemoveRows(this->index(fav.parent->index, 0), index, index + 1); + group->children.erase(it); + endRemoveRows(); + this->relayout(); + + this->addFavourite(group_name, f); + return true; + } + } + } + return false; +} + Favourite FavouriteCollection::getFavourite(const QUrl &u) const { QUrl url = IoUtil::uniformUrl(u); @@ -249,6 +279,20 @@ Favourite * FavouriteCollection::getMutableFavourite(const QModelIndex &index) } } +QString FavouriteCollection::groupForFavourite(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->parent->as<GroupNode>().title; + } + } + return QString { }; +} + QStringList FavouriteCollection::groups() const { QStringList result; diff --git a/src/favouritecollection.hpp b/src/favouritecollection.hpp index 359f173..f33b90b 100644 --- a/src/favouritecollection.hpp +++ b/src/favouritecollection.hpp @@ -82,12 +82,16 @@ public: void editFavouriteDest(const QModelIndex & index, const QUrl & url); + bool editFavouriteGroup(const QUrl &url, const QString &group_name); + Favourite getFavourite(const QUrl &url) const; Favourite getFavourite(QModelIndex const & index) const; Favourite * getMutableFavourite(QModelIndex const & index); + QString groupForFavourite(QUrl const & url) const; + QStringList groups() const; //! Returns the group name of the index. diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f46ed75..d93cac8 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -189,6 +189,21 @@ void MainWindow::setUiDensity(UIDensity density, bool previewing) } } +QString MainWindow::newGroupDialog() +{ + QInputDialog dialog { this }; + + dialog.setInputMode(QInputDialog::TextInput); + dialog.setLabelText(tr("Enter name of the new group:")); + + if(dialog.exec() != QDialog::Accepted) + return QString { }; + + kristall::favourites.addGroup(dialog.textValue()); + + return dialog.textValue(); +} + void MainWindow::mousePressEvent(QMouseEvent *event) { QMainWindow::mousePressEvent(event); @@ -565,15 +580,7 @@ void MainWindow::on_favourites_view_customContextMenuRequested(const QPoint pos) QMenu menu; connect(menu.addAction("Create new group..."), &QAction::triggered, [this]() { - QInputDialog dialog { this }; - - dialog.setInputMode(QInputDialog::TextInput); - dialog.setLabelText(tr("Enter name of the new group:")); - - if(dialog.exec() != QDialog::Accepted) - return; - - kristall::favourites.addGroup(dialog.textValue()); + this->newGroupDialog(); }); menu.exec(this->ui->favourites_view->mapToGlobal(pos)); diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp index ae86588..6a7164e 100644 --- a/src/mainwindow.hpp +++ b/src/mainwindow.hpp @@ -41,6 +41,8 @@ public: void setUiDensity(UIDensity density, bool previewing); + QString newGroupDialog(); + void mousePressEvent(QMouseEvent *event) override; private slots: diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 53fe0e7..bc503ca 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -103,7 +103,7 @@ <normaloff>.</normaloff>.</iconset> </property> <property name="windowTitle"> - <string>Bookmarks</string> + <string>Favourites</string> </property> <property name="_shortcut" stdset="0"> <string>Ctrl+B</string> diff --git a/src/widgets/favouritepopup.cpp b/src/widgets/favouritepopup.cpp index b7e11fa..2039115 100644 --- a/src/widgets/favouritepopup.cpp +++ b/src/widgets/favouritepopup.cpp @@ -2,6 +2,7 @@ #include <QToolButton> #include <QVBoxLayout> +#include <QHBoxLayout> #include <QGridLayout> #include <QLabel> #include <QKeyEvent> @@ -9,6 +10,8 @@ FavouritePopup::FavouritePopup(QToolButton *button, QWidget *parent) : QMenu(parent), b(button) { + this->is_ready = false; + auto parent_layout = new QVBoxLayout(); parent_layout->setContentsMargins(8, 8, 8, 8); @@ -20,6 +23,24 @@ FavouritePopup::FavouritePopup(QToolButton *button, QWidget *parent) layout->addWidget(title_lab, 0, 0); layout->addWidget(this->fav_title, 0, 1); + // Group + auto group_lab = new QLabel("Group:"); + layout->addWidget(group_lab); + { + this->fav_group = new QComboBox(); + + auto new_group = new QToolButton(); + new_group->setIcon(QIcon::fromTheme("document-new")); + connect(new_group, &QPushButton::clicked, this, [this]() { + emit this->newGroupClicked(); + }); + + auto group_lay = new QHBoxLayout(); + group_lay->addWidget(this->fav_group); + group_lay->addWidget(new_group); + layout->addLayout(group_lay, 1, 1); + } + // Unfavourite auto unfav_btn = new QPushButton("Unfavourite"); layout->addWidget(unfav_btn); @@ -38,7 +59,7 @@ FavouritePopup::FavouritePopup(QToolButton *button, QWidget *parent) parent_layout->addLayout(layout); this->setLayout(parent_layout); - this->setMinimumWidth(250); + this->setMinimumWidth(350); } void FavouritePopup::confirmPressed() diff --git a/src/widgets/favouritepopup.hpp b/src/widgets/favouritepopup.hpp index a07d2dd..524bd00 100644 --- a/src/widgets/favouritepopup.hpp +++ b/src/widgets/favouritepopup.hpp @@ -4,6 +4,7 @@ #include <QMenu> #include <QPushButton> #include <QLineEdit> +#include <QComboBox> class QToolButton; @@ -22,6 +23,8 @@ signals: void unfavourited(); + void newGroupClicked(); + private: void confirmPressed(); @@ -30,8 +33,11 @@ private: public: QLineEdit *fav_title; + QComboBox *fav_group; QPushButton *confirm_btn; + bool is_ready; + }; #endif |
