aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Skec <skec@protonmail.ch>2021-01-07 19:28:39 +1100
committerFelix Queißner <felix@ib-queissner.de>2021-01-07 10:05:00 +0100
commit2e0b8d3495d6c46c2d3afb88787fd7ca3ed1558e (patch)
treee0e878b871b81a4e6e3bc24bf39ca135dfe91671 /src
parent19046566a62f9e616a23584ebbe045ddbc132023 (diff)
downloadkristall-2e0b8d3495d6c46c2d3afb88787fd7ca3ed1558e.tar.gz
Added new context menus to favourite group
Diffstat (limited to 'src')
-rw-r--r--src/favouritecollection.cpp50
-rw-r--r--src/favouritecollection.hpp3
-rw-r--r--src/mainwindow.cpp50
3 files changed, 94 insertions, 9 deletions
diff --git a/src/favouritecollection.cpp b/src/favouritecollection.cpp
index 29b79b7..10ea647 100644
--- a/src/favouritecollection.cpp
+++ b/src/favouritecollection.cpp
@@ -154,6 +154,31 @@ bool FavouriteCollection::addGroup(const QString &group_name)
return internalAddGroup(group_name, group);
}
+bool FavouriteCollection::renameGroup(const QString &old_name, const QString &new_name)
+{
+ GroupNode * to_set = nullptr;
+ for (auto const & grp : root.children)
+ {
+ auto * g = static_cast<GroupNode*>(grp.get());
+ if (g->title == old_name)
+ {
+ to_set = g;
+ }
+ if (g->title == new_name)
+ {
+ return false;
+ }
+ }
+
+ if (to_set)
+ {
+ to_set->title = new_name;
+ return true;
+ }
+
+ return false;
+}
+
bool FavouriteCollection::addFavourite(const QString &group_name, const Favourite &fav)
{
GroupNode * group;
@@ -374,6 +399,31 @@ bool FavouriteCollection::deleteGroup(const QString &group_name)
return false;
}
+bool FavouriteCollection::deleteGroupRecursive(const QString &group_name)
+{
+ size_t index = 0;
+ for (auto it = root.children.begin(); it != root.children.end(); ++it, ++index)
+ {
+ auto & group = it->get()->as<GroupNode>();
+ if (group.title == group_name)
+ {
+ // Delete group children
+ auto group_idx = this->index(index, 0);
+ beginRemoveRows(group_idx, 0, group.children.size());
+ group.children.clear();
+ endRemoveRows();
+
+ // Delete the group
+ beginRemoveRows(QModelIndex { }, index, index + 1);
+ root.children.erase(it);
+ endRemoveRows();
+
+ return true;
+ }
+ }
+ return false;
+}
+
QVector<QPair<QString, Favourite const *>> FavouriteCollection::allFavourites() const
{
QVector<QPair<QString, Favourite const *>> identities;
diff --git a/src/favouritecollection.hpp b/src/favouritecollection.hpp
index f33b90b..9c18ec3 100644
--- a/src/favouritecollection.hpp
+++ b/src/favouritecollection.hpp
@@ -75,6 +75,8 @@ public:
bool addGroup(QString const & group);
+ bool renameGroup(QString const & old_name, QString const & new_name);
+
bool addFavourite(QString const & group, Favourite const & fav);
void editFavouriteTitle(const QModelIndex &index, const QString &title);
@@ -101,6 +103,7 @@ public:
bool canDeleteGroup(QString const & group_name);
bool deleteGroup(QString const & group_name);
+ bool deleteGroupRecursive(QString const & group_name);
//! Returns a list of non-mutable references to all contained favourites.
//! Note that the group will change in-order, so all favourites for group a
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index d93cac8..4a09811 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -538,30 +538,30 @@ void MainWindow::on_favourites_view_customContextMenuRequested(const QPoint pos)
menu.addSeparator();
- connect(menu.addAction("Rename"), &QAction::triggered, [this, idx]() {
+ connect(menu.addAction("Relocate"), &QAction::triggered, [this, idx]() {
QInputDialog dialog { this };
dialog.setInputMode(QInputDialog::TextInput);
- dialog.setLabelText(tr("New name of this favourite:"));
- dialog.setTextValue(kristall::favourites.getFavourite(idx).getTitle());
+ dialog.setLabelText(tr("Enter new location of this favourite:"));
+ dialog.setTextValue(kristall::favourites.getFavourite(idx).destination.toString(QUrl::FullyEncoded));
if (dialog.exec() != QDialog::Accepted)
return;
- kristall::favourites.editFavouriteTitle(idx, dialog.textValue());
+ kristall::favourites.editFavouriteDest(idx, QUrl(dialog.textValue()));
});
- connect(menu.addAction("Relocate"), &QAction::triggered, [this, idx]() {
+ connect(menu.addAction("Rename"), &QAction::triggered, [this, idx]() {
QInputDialog dialog { this };
dialog.setInputMode(QInputDialog::TextInput);
- dialog.setLabelText(tr("Enter new location of this favourite:"));
- dialog.setTextValue(kristall::favourites.getFavourite(idx).destination.toString(QUrl::FullyEncoded));
+ dialog.setLabelText(tr("New name of this favourite:"));
+ dialog.setTextValue(kristall::favourites.getFavourite(idx).getTitle());
if (dialog.exec() != QDialog::Accepted)
return;
- kristall::favourites.editFavouriteDest(idx, QUrl(dialog.textValue()));
+ kristall::favourites.editFavouriteTitle(idx, dialog.textValue());
});
menu.addSeparator();
@@ -573,7 +573,39 @@ void MainWindow::on_favourites_view_customContextMenuRequested(const QPoint pos)
menu.exec(this->ui->favourites_view->mapToGlobal(pos));
}
else if(QString group = kristall::favourites.group(idx); not group.isEmpty()) {
- qDebug() << group;
+ QMenu menu;
+
+ connect(menu.addAction("Rename group"), &QAction::triggered, [this, group]() {
+ QInputDialog dialog { this };
+
+ dialog.setInputMode(QInputDialog::TextInput);
+ dialog.setLabelText(tr("New name of this group:"));
+ dialog.setTextValue(group);
+
+ if (dialog.exec() != QDialog::Accepted)
+ return;
+
+ if (!kristall::favourites.renameGroup(group, dialog.textValue()))
+ QMessageBox::information(this, "Kristall", "Rename failed: group name already in use.");
+ });
+
+ menu.addSeparator();
+
+ connect(menu.addAction("Delete group"), &QAction::triggered, [this, idx]() {
+ if (QMessageBox::question(
+ this,
+ "Kristall",
+ "Are you sure you want to delete this Favourite Group?\n"
+ "All favourites in this group will be lost.\n\n"
+ "This action cannot be undone!"
+ ) != QMessageBox::Yes)
+ {
+ return;
+ }
+ kristall::favourites.deleteGroupRecursive(kristall::favourites.group(idx));
+ });
+
+ menu.exec(this->ui->favourites_view->mapToGlobal(pos));
}
}
else {