Skip to content

Commit

Permalink
bookmarks: Avoid leaking taglist menu and QDialog
Browse files Browse the repository at this point in the history
Avoid leaking taglist menu each time it is shown
Avoid leaking QDialog each time it is shown
  • Loading branch information
vladisslav2011 committed Feb 4, 2025
1 parent dade54e commit 37ef2c0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 65 deletions.
87 changes: 43 additions & 44 deletions src/qtgui/bookmarkstaglist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,48 @@ BookmarksTagList::BookmarksTagList(QWidget *parent, bool bShowUntagged )
this, SLOT(on_cellClicked(int,int)));

// right click menu
popupMenu=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);
popupMenu->addAction(actionRename);
connect(actionRename, SIGNAL(triggered()), this, SLOT(RenameSelectedTag()));
}
#endif

// MenuItem "Create new Tag"
{
QAction* actionNewTag = new QAction("Create new Tag", this);
popupMenu->addAction(actionNewTag);
connect(actionNewTag, SIGNAL(triggered()), this, SLOT(AddNewTag()));
}

// Menu "Delete Tag"
{
QAction* actionDeleteTag = new QAction("Delete Tag", this);
popupMenu->addAction(actionDeleteTag);
connect(actionDeleteTag, SIGNAL(triggered()), this, SLOT(DeleteSelectedTag()));
}

// Menu "Select All"
{
QAction* action = new QAction("Select All", this);
popupMenu->addAction(action);
connect(action, SIGNAL(triggered()), this, SLOT(SelectAll()));
}

// Menu "Deselect All"
{
QAction* action = new QAction("Deselect All", this);
popupMenu->addAction(action);
connect(action, SIGNAL(triggered()), this, SLOT(DeselectAll()));
}
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(ShowContextMenu(const QPoint&)));
Expand Down Expand Up @@ -180,50 +222,7 @@ QStringList BookmarksTagList::getSelectedTags()

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);
menu->addAction(actionNewTag);
connect(actionNewTag, SIGNAL(triggered()), this, SLOT(AddNewTag()));
}

// Menu "Delete Tag"
{
QAction* actionDeleteTag = new QAction("Delete Tag", this);
menu->addAction(actionDeleteTag);
connect(actionDeleteTag, SIGNAL(triggered()), this, SLOT(DeleteSelectedTag()));
}

// Menu "Select All"
{
QAction* action = new QAction("Select All", this);
menu->addAction(action);
connect(action, SIGNAL(triggered()), this, SLOT(SelectAll()));
}

// Menu "Deselect All"
{
QAction* action = new QAction("Deselect All", this);
menu->addAction(action);
connect(action, SIGNAL(triggered()), this, SLOT(DeselectAll()));
}

menu->popup(viewport()->mapToGlobal(pos));
popupMenu->popup(viewport()->mapToGlobal(pos));
}

#if 0
Expand Down
2 changes: 1 addition & 1 deletion src/qtgui/bookmarkstaglist.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BookmarksTagList : public QTableWidget

private:
bool m_bShowUntagged;

QMenu* popupMenu{nullptr};
signals:

public slots:
Expand Down
39 changes: 19 additions & 20 deletions src/qtgui/dockbookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <QMessageBox>

#include "bookmarks.h"
#include "bookmarkstaglist.h"
#include "dockbookmarks.h"
#include "dockrxopt.h"
#include "qtcolorpicker.h"
Expand Down Expand Up @@ -73,6 +72,20 @@ DockBookmarks::DockBookmarks(QWidget *parent) :
connect(ui->tableViewFrequencyList, SIGNAL(customContextMenuRequested(const QPoint&)),
this, SLOT(ShowContextMenu(const QPoint&)));

tagsDialog = new QDialog(this);
tagsDialog->setWindowTitle("Change Bookmark Tags");

dialogTaglist = new BookmarksTagList(tagsDialog, false);

QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), tagsDialog, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), tagsDialog, SLOT(reject()));

QVBoxLayout *mainLayout = new QVBoxLayout(tagsDialog);
mainLayout->addWidget(dialogTaglist);
mainLayout->addWidget(buttonBox);

// Update GUI
Bookmarks::Get().load();
bookmarksTableModel->update();
Expand Down Expand Up @@ -249,27 +262,13 @@ void DockBookmarks::changeBookmarkTags(int row, int /*column*/)
// Create and show the Dialog for a new Bookmark.
// Write the result into variable 'tags'.
{
QDialog dialog(this);
dialog.setWindowTitle("Change Bookmark Tags");

BookmarksTagList* taglist = new BookmarksTagList(&dialog, false);
taglist->updateTags();
taglist->setSelectedTags(bmi.tags);
taglist->DeleteTag(TagInfo::strUntagged);

QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

QVBoxLayout *mainLayout = new QVBoxLayout(&dialog);
mainLayout->addWidget(taglist);
mainLayout->addWidget(buttonBox);

ok = dialog.exec();
dialogTaglist->updateTags();
dialogTaglist->setSelectedTags(bmi.tags);
dialogTaglist->DeleteTag(TagInfo::strUntagged);
ok = tagsDialog->exec();
if (ok)
{
tags = taglist->getSelectedTags();
tags = dialogTaglist->getSelectedTags();

// Change Tags of Bookmark
bmi.tags.clear();
Expand Down
3 changes: 3 additions & 0 deletions src/qtgui/dockbookmarks.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QDockWidget>
#include <QTableWidgetItem>
#include "qtgui/bookmarkstablemodel.h"
#include "qtgui/bookmarkstaglist.h"
#include <QItemDelegate>

namespace Ui {
Expand All @@ -48,6 +49,8 @@ class DockBookmarks : public QDockWidget
private:
Ui::DockBookmarks *ui;
QMenu* contextmenu;
QDialog* tagsDialog;
BookmarksTagList* dialogTaglist;
qint64 m_currentFrequency;
bool m_updating;
BookmarksTableModel *bookmarksTableModel;
Expand Down

0 comments on commit 37ef2c0

Please sign in to comment.