From 43e3921fa33a932e821d2cbe48b29185f2894b4b Mon Sep 17 00:00:00 2001 From: James Yuzawa Date: Sat, 9 Nov 2024 09:22:14 -0500 Subject: [PATCH 1/2] Support tag renaming --- src/qtgui/bookmarks.cpp | 2 ++ src/qtgui/bookmarkstaglist.cpp | 39 +++++++++++++++++----------------- src/qtgui/bookmarkstaglist.h | 2 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/qtgui/bookmarks.cpp b/src/qtgui/bookmarks.cpp index d4593e5ac..f884e512d 100644 --- a/src/qtgui/bookmarks.cpp +++ b/src/qtgui/bookmarks.cpp @@ -140,6 +140,7 @@ bool Bookmarks::load() std::stable_sort(m_BookmarkList.begin(),m_BookmarkList.end()); emit BookmarksChanged(); + emit TagListChanged(); return true; } return false; @@ -201,6 +202,7 @@ bool Bookmarks::save() } emit BookmarksChanged(); + emit TagListChanged(); file.close(); return true; } diff --git a/src/qtgui/bookmarkstaglist.cpp b/src/qtgui/bookmarkstaglist.cpp index 791f48d93..93caa4a9e 100644 --- a/src/qtgui/bookmarkstaglist.cpp +++ b/src/qtgui/bookmarkstaglist.cpp @@ -23,6 +23,7 @@ #include "bookmarkstaglist.h" #include "bookmarks.h" #include +#include #include #include #include @@ -182,19 +183,6 @@ void BookmarksTagList::ShowContextMenu(const QPoint& pos) { QMenu* menu=new QMenu(this); - // Rename currently does not work. - // The problem is that after the tag name is changed in GUI - // you can not find the right TagInfo because you dont know - // the old tag name. - #if 0 - // MenuItem "Rename" - { - QAction* actionRename = new QAction("Rename", this); - menu->addAction(actionRename); - connect(actionRename, SIGNAL(triggered()), this, SLOT(RenameSelectedTag())); - } - #endif - // MenuItem "Create new Tag" { QAction* actionNewTag = new QAction("Create new Tag", this); @@ -202,6 +190,13 @@ void BookmarksTagList::ShowContextMenu(const QPoint& pos) connect(actionNewTag, SIGNAL(triggered()), this, SLOT(AddNewTag())); } + // MenuItem "Rename Tag" + { + QAction* actionRename = new QAction("Rename Tag", this); + menu->addAction(actionRename); + connect(actionRename, SIGNAL(triggered()), this, SLOT(RenameSelectedTag())); + } + // Menu "Delete Tag" { QAction* actionDeleteTag = new QAction("Delete Tag", this); @@ -226,7 +221,6 @@ void BookmarksTagList::ShowContextMenu(const QPoint& pos) menu->popup(viewport()->mapToGlobal(pos)); } -#if 0 bool BookmarksTagList::RenameSelectedTag() { QModelIndexList selected = selectionModel()->selectedRows(); @@ -236,14 +230,21 @@ bool BookmarksTagList::RenameSelectedTag() return true; } - int iRow = selected.first().row(); - QTableWidgetItem* pItem = item(iRow,1);bUpdating - editItem(pItem); - //Bookmarks::Get().save(); + int row = selected.first().row(); + QString oldName = item(row, 1)->text(); + TagInfo::sptr info = Bookmarks::Get().findOrAddTag(oldName); + bool ok; + QString newName = QInputDialog::getText(this, "Rename Tag", "Tag Name:", + QLineEdit::Normal, oldName, &ok); + if (ok && newName != oldName) + { + info->name = newName; + updateTags(); + Bookmarks::Get().save(); + } return true; } -#endif void BookmarksTagList::AddNewTag() { diff --git a/src/qtgui/bookmarkstaglist.h b/src/qtgui/bookmarkstaglist.h index 66caa9a48..b85ac3fff 100644 --- a/src/qtgui/bookmarkstaglist.h +++ b/src/qtgui/bookmarkstaglist.h @@ -48,7 +48,7 @@ public slots: void changeColor(int row, int column); void toggleCheckedState(int row, int column); void ShowContextMenu(const QPoint& pos); - //bool RenameSelectedTag(); + bool RenameSelectedTag(); void AddNewTag(); void AddTag(QString name, Qt::CheckState checkstate = Qt::Checked, QColor color = TagInfo::DefaultColor); void DeleteSelectedTag(); From 059a84c0765f1ee8c26411298b6ba1f30bb1c8b1 Mon Sep 17 00:00:00 2001 From: James Yuzawa Date: Sun, 10 Nov 2024 18:28:18 -0500 Subject: [PATCH 2/2] handle edit/delete of untagged and duplicates better --- src/qtgui/bookmarks.cpp | 30 +++++++++++++++++++++++ src/qtgui/bookmarks.h | 1 + src/qtgui/bookmarkstaglist.cpp | 45 ++++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/qtgui/bookmarks.cpp b/src/qtgui/bookmarks.cpp index f884e512d..f540dda36 100644 --- a/src/qtgui/bookmarks.cpp +++ b/src/qtgui/bookmarks.cpp @@ -251,6 +251,36 @@ TagInfo::sptr Bookmarks::findOrAddTag(QString tagName) return m_TagList.last(); } +bool Bookmarks::renameTag(QString oldTagName, QString tagName) +{ + tagName = tagName.trimmed(); + + // Do not edit "Untagged" tag. + if (oldTagName.compare(TagInfo::strUntagged) == 0) + { + return false; + } + + // Ensure new tag does not already exist + int idx = getTagIndex(tagName); + if (idx != -1) + { + return false; + } + + for (int i = 0; i < m_TagList.size(); i++) + { + TagInfo::sptr info = m_TagList[i]; + if (oldTagName.compare(info->name) == 0) + { + info->name = tagName; + save(); + return true; + } + } + return false; +} + bool Bookmarks::removeTag(QString tagName) { tagName = tagName.trimmed(); diff --git a/src/qtgui/bookmarks.h b/src/qtgui/bookmarks.h index 8e3fee6ea..b7d8e7411 100644 --- a/src/qtgui/bookmarks.h +++ b/src/qtgui/bookmarks.h @@ -115,6 +115,7 @@ class Bookmarks : public QObject QList getTagList() { return QList(m_TagList); } TagInfo::sptr findOrAddTag(QString tagName); + bool renameTag(QString oldTagName, QString tagName); int getTagIndex(QString tagName); bool removeTag(QString tagName); bool setTagChecked(QString tagName, bool bChecked); diff --git a/src/qtgui/bookmarkstaglist.cpp b/src/qtgui/bookmarkstaglist.cpp index 93caa4a9e..f4f6c37f3 100644 --- a/src/qtgui/bookmarkstaglist.cpp +++ b/src/qtgui/bookmarkstaglist.cpp @@ -24,6 +24,7 @@ #include "bookmarks.h" #include #include +#include #include #include #include @@ -190,18 +191,25 @@ void BookmarksTagList::ShowContextMenu(const QPoint& pos) connect(actionNewTag, SIGNAL(triggered()), this, SLOT(AddNewTag())); } - // MenuItem "Rename Tag" - { - QAction* actionRename = new QAction("Rename Tag", this); - menu->addAction(actionRename); - connect(actionRename, SIGNAL(triggered()), this, SLOT(RenameSelectedTag())); - } + QTableWidgetItem* theItem = itemAt(pos); + if (theItem) { + QString tagName = item(theItem->row(), 1)->text(); + if (tagName.compare(TagInfo::strUntagged) != 0) + { + // MenuItem "Rename Tag" + { + QAction* actionRename = new QAction("Rename Tag", this); + menu->addAction(actionRename); + connect(actionRename, SIGNAL(triggered()), this, SLOT(RenameSelectedTag())); + } - // Menu "Delete Tag" - { - QAction* actionDeleteTag = new QAction("Delete Tag", this); - menu->addAction(actionDeleteTag); - connect(actionDeleteTag, SIGNAL(triggered()), this, SLOT(DeleteSelectedTag())); + // Menu "Delete Tag" + { + QAction* actionDeleteTag = new QAction("Delete Tag", this); + menu->addAction(actionDeleteTag); + connect(actionDeleteTag, SIGNAL(triggered()), this, SLOT(DeleteSelectedTag())); + } + } } // Menu "Select All" @@ -232,16 +240,21 @@ bool BookmarksTagList::RenameSelectedTag() int row = selected.first().row(); QString oldName = item(row, 1)->text(); - TagInfo::sptr info = Bookmarks::Get().findOrAddTag(oldName); bool ok; QString newName = QInputDialog::getText(this, "Rename Tag", "Tag Name:", QLineEdit::Normal, oldName, &ok); - if (ok && newName != oldName) + if (ok) { - info->name = newName; - updateTags(); - Bookmarks::Get().save(); + if (Bookmarks::Get().renameTag(oldName, newName)) + { + updateTags(); + } + else + { + QMessageBox::warning(this, "Rename Tag", QString("Tag %1 already exists").arg(newName), + QMessageBox::Ok, QMessageBox::Ok); + } } return true; }