From 457cc9de78679da5040e255da4c21e02854c42c0 Mon Sep 17 00:00:00 2001 From: 0penBrain <48731257+0penBrain@users.noreply.github.com> Date: Sat, 7 Oct 2023 00:22:15 +0200 Subject: [PATCH 1/2] Add feature to update existing bookmark --- src/applications/gqrx/mainwindow.cpp | 25 +++++++++++++++++++++ src/applications/gqrx/mainwindow.h | 1 + src/qtgui/bookmarks.cpp | 11 ++++++++++ src/qtgui/bookmarks.h | 10 +++++++++ src/qtgui/bookmarkstablemodel.cpp | 5 +++++ src/qtgui/bookmarkstablemodel.h | 1 + src/qtgui/dockbookmarks.cpp | 33 +++++++++++++++++++++++----- src/qtgui/dockbookmarks.h | 3 +++ 8 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/applications/gqrx/mainwindow.cpp b/src/applications/gqrx/mainwindow.cpp index 784564158..bc4ee0d4e 100644 --- a/src/applications/gqrx/mainwindow.cpp +++ b/src/applications/gqrx/mainwindow.cpp @@ -311,6 +311,7 @@ MainWindow::MainWindow(const QString& cfgfile, bool edit_conf, QWidget *parent) // Bookmarks connect(uiDockBookmarks, SIGNAL(newBookmarkActivated(qint64, QString, int)), this, SLOT(onBookmarkActivated(qint64, QString, int))); + connect(uiDockBookmarks->actionUpdateBookmark, SIGNAL(triggered()), this, SLOT(on_actionUpdateBookmark_triggered())); connect(uiDockBookmarks->actionAddBookmark, SIGNAL(triggered()), this, SLOT(on_actionAddBookmark_triggered())); connect(&Bookmarks::Get(), SIGNAL(BookmarksChanged()), ui->plotter, SLOT(updateOverlay())); @@ -2450,6 +2451,30 @@ void MainWindow::on_actionAboutQt_triggered() QMessageBox::aboutQt(this, tr("About Qt")); } +void MainWindow::on_actionUpdateBookmark_triggered() +{ + auto* action = qobject_cast(sender()); + if (!action) + { + return; + } + bool ok = false; + auto bookmarkIndex = action->data().toInt(&ok); + if (!ok) + { + return; + } + BookmarkInfo info; + info.frequency = ui->freqCtrl->getFrequency(); + info.bandwidth = ui->plotter->getFilterBw(); + info.modulation = uiDockRxOpt->currentDemodAsString(); + auto newIndex = Bookmarks::Get().update(bookmarkIndex, info); + if (newIndex >= 0) + { + uiDockBookmarks->selectBookmark(newIndex); + } +} + void MainWindow::on_actionAddBookmark_triggered() { bool ok=false; diff --git a/src/applications/gqrx/mainwindow.h b/src/applications/gqrx/mainwindow.h index 5151bb2c1..be4b407b8 100644 --- a/src/applications/gqrx/mainwindow.h +++ b/src/applications/gqrx/mainwindow.h @@ -242,6 +242,7 @@ private slots: void on_actionKbdShortcuts_triggered(); void on_actionAbout_triggered(); void on_actionAboutQt_triggered(); + void on_actionUpdateBookmark_triggered(); void on_actionAddBookmark_triggered(); void on_actionDX_Cluster_triggered(); diff --git a/src/qtgui/bookmarks.cpp b/src/qtgui/bookmarks.cpp index d4593e5ac..766b89a59 100644 --- a/src/qtgui/bookmarks.cpp +++ b/src/qtgui/bookmarks.cpp @@ -56,6 +56,17 @@ void Bookmarks::setConfigDir(const QString& cfg_dir) std::cout << "BookmarksFile is " << m_bookmarksFile.toStdString() << std::endl; } +int Bookmarks::update(int index, BookmarkInfo& info) +{ + m_BookmarkList[index].frequency = info.frequency; + m_BookmarkList[index].bandwidth = info.bandwidth; + m_BookmarkList[index].modulation = info.modulation; + auto newInfo = m_BookmarkList[index]; + std::stable_sort(m_BookmarkList.begin(),m_BookmarkList.end()); + save(); + return m_BookmarkList.indexOf(newInfo); +} + void Bookmarks::add(BookmarkInfo &info) { m_BookmarkList.append(info); diff --git a/src/qtgui/bookmarks.h b/src/qtgui/bookmarks.h index 8e3fee6ea..c8f0ec028 100644 --- a/src/qtgui/bookmarks.h +++ b/src/qtgui/bookmarks.h @@ -84,6 +84,15 @@ struct BookmarkInfo { return frequency < other.frequency; } + + bool operator==(const BookmarkInfo &other) const + { + return frequency == other.frequency + and name == other.name + and modulation == other.modulation + and bandwidth == other.bandwidth + and tags == other.tags; + } /* void setTags(QString tagString); QString getTagString(); @@ -104,6 +113,7 @@ class Bookmarks : public QObject static Bookmarks& Get(); void add(BookmarkInfo& info); + int update(int index, BookmarkInfo& info); void remove(int index); bool load(); bool save(); diff --git a/src/qtgui/bookmarkstablemodel.cpp b/src/qtgui/bookmarkstablemodel.cpp index 4fc5cc239..bd12384c6 100644 --- a/src/qtgui/bookmarkstablemodel.cpp +++ b/src/qtgui/bookmarkstablemodel.cpp @@ -223,3 +223,8 @@ int BookmarksTableModel::GetBookmarksIndexForRow(int iRow) { return m_mapRowToBookmarksIndex[iRow]; } + +int BookmarksTableModel::GetRowForBookmarksIndex(int bookmarkIndex) +{ + return m_mapRowToBookmarksIndex.key(bookmarkIndex, -1); +} diff --git a/src/qtgui/bookmarkstablemodel.h b/src/qtgui/bookmarkstablemodel.h index f8b2e6274..d811371e5 100644 --- a/src/qtgui/bookmarkstablemodel.h +++ b/src/qtgui/bookmarkstablemodel.h @@ -54,6 +54,7 @@ class BookmarksTableModel : public QAbstractTableModel BookmarkInfo* getBookmarkAtRow(int row); int GetBookmarksIndexForRow(int iRow); + int GetRowForBookmarksIndex(int bookmarkIndex); private: QList m_Bookmarks; diff --git a/src/qtgui/dockbookmarks.cpp b/src/qtgui/dockbookmarks.cpp index 5511d6968..821e8c19a 100644 --- a/src/qtgui/dockbookmarks.cpp +++ b/src/qtgui/dockbookmarks.cpp @@ -58,16 +58,19 @@ DockBookmarks::DockBookmarks(QWidget *parent) : // Bookmarks Context menu contextmenu = new QMenu(this); + + //MenuItem Update + { + actionUpdateBookmark = new QAction("Update bookmark", this); + } // MenuItem Delete { - QAction* action = new QAction("Delete Bookmark", this); - contextmenu->addAction(action); - connect(action, SIGNAL(triggered()), this, SLOT(DeleteSelectedBookmark())); + actionDeleteBookmark = new QAction("Delete bookmark", this); + connect(actionDeleteBookmark, SIGNAL(triggered()), this, SLOT(DeleteSelectedBookmark())); } // MenuItem Add { - actionAddBookmark = new QAction("Add Bookmark", this); - contextmenu->addAction(actionAddBookmark); + actionAddBookmark = new QAction("Add new bookmark", this); } ui->tableViewFrequencyList->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->tableViewFrequencyList, SIGNAL(customContextMenuRequested(const QPoint&)), @@ -189,6 +192,17 @@ bool DockBookmarks::DeleteSelectedBookmark() void DockBookmarks::ShowContextMenu(const QPoint& pos) { + contextmenu->clear(); + auto bookmark = ui->tableViewFrequencyList->indexAt(pos); + if (bookmark != QModelIndex()) + { + auto bookmarkIndex = bookmarksTableModel->GetBookmarksIndexForRow(bookmark.row()); + actionUpdateBookmark->setData(QVariant::fromValue(bookmarkIndex)); + contextmenu->addAction(actionUpdateBookmark); + contextmenu->addAction(actionDeleteBookmark); + contextmenu->addSeparator(); + } + contextmenu->addAction(actionAddBookmark); contextmenu->popup(ui->tableViewFrequencyList->viewport()->mapToGlobal(pos)); } @@ -230,6 +244,15 @@ void ComboBoxDelegateModulation::setModelData(QWidget *editor, QAbstractItemMode model->setData(index, comboBox->currentText(), Qt::EditRole); } +void DockBookmarks::selectBookmark(int bookmarkIndex) +{ + auto row = bookmarksTableModel->GetRowForBookmarksIndex(bookmarkIndex); + if (row >= 0) + { + ui->tableViewFrequencyList->selectRow(row);; + } +} + void DockBookmarks::changeBookmarkTags(int row, int /*column*/) { bool ok = false; diff --git a/src/qtgui/dockbookmarks.h b/src/qtgui/dockbookmarks.h index baf3818b0..12c0bb46f 100644 --- a/src/qtgui/dockbookmarks.h +++ b/src/qtgui/dockbookmarks.h @@ -60,11 +60,14 @@ class DockBookmarks : public QDockWidget // ui->tableViewFrequencyList // ui->tableWidgetTagList + QAction* actionUpdateBookmark; + QAction* actionDeleteBookmark; QAction* actionAddBookmark; void updateTags(); void updateBookmarks(); void changeBookmarkTags(int row, int /*column*/); + void selectBookmark(int bookmarkIndex); signals: void newBookmarkActivated(qint64, QString, int); From c54b7479cd77a84e38237794085f04b4d2861823 Mon Sep 17 00:00:00 2001 From: 0penBrain <48731257+0penBrain@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:26:13 +0200 Subject: [PATCH 2/2] Refactor DockBookmarks context menu memory allocation --- src/qtgui/dockbookmarks.cpp | 5 ++--- src/qtgui/dockbookmarks.h | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/qtgui/dockbookmarks.cpp b/src/qtgui/dockbookmarks.cpp index 821e8c19a..464dfb796 100644 --- a/src/qtgui/dockbookmarks.cpp +++ b/src/qtgui/dockbookmarks.cpp @@ -56,8 +56,7 @@ DockBookmarks::DockBookmarks(QWidget *parent) : ComboBoxDelegateModulation* delegateModulation = new ComboBoxDelegateModulation(this); ui->tableViewFrequencyList->setItemDelegateForColumn(2, delegateModulation); - // Bookmarks Context menu - contextmenu = new QMenu(this); + // Actions for context menu //MenuItem Update { @@ -192,7 +191,7 @@ bool DockBookmarks::DeleteSelectedBookmark() void DockBookmarks::ShowContextMenu(const QPoint& pos) { - contextmenu->clear(); + auto contextmenu = new QMenu(this); auto bookmark = ui->tableViewFrequencyList->indexAt(pos); if (bookmark != QModelIndex()) { diff --git a/src/qtgui/dockbookmarks.h b/src/qtgui/dockbookmarks.h index 12c0bb46f..b8b17b3ae 100644 --- a/src/qtgui/dockbookmarks.h +++ b/src/qtgui/dockbookmarks.h @@ -47,7 +47,6 @@ class DockBookmarks : public QDockWidget private: Ui::DockBookmarks *ui; - QMenu* contextmenu; qint64 m_currentFrequency; bool m_updating; BookmarksTableModel *bookmarksTableModel;