From 925203b4a5a5f47b08495ef162e95b53a858ac44 Mon Sep 17 00:00:00 2001 From: vsonnier Date: Sat, 4 Mar 2017 11:22:29 +0100 Subject: [PATCH] Bookmarks (cont...) : more understanding, more comments, more fixes. NOW memory management should be clean... --- src/forms/Bookmark/BookmarkView.cpp | 56 +++++++++++++++++++---------- src/forms/Bookmark/BookmarkView.h | 23 ++++++++++-- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/forms/Bookmark/BookmarkView.cpp b/src/forms/Bookmark/BookmarkView.cpp index b6e84f7..bbf5e05 100644 --- a/src/forms/Bookmark/BookmarkView.cpp +++ b/src/forms/Bookmark/BookmarkView.cpp @@ -247,8 +247,15 @@ bool BookmarkView::isKeywordMatch(std::wstring search_str, std::vectorGetSelection()); - + //capture the previously selected item info BY COPY (because the original will be destroyed together with the destroyed tree items) to restore it again after + //having rebuilding the whole tree. + TreeViewItem* prevSel = itemToTVI(m_treeView->GetSelection()); + TreeViewItem* prevSelCopy = nullptr; + + if (prevSel != NULL) { + prevSelCopy = new TreeViewItem(*prevSel); + } + BookmarkNames groupNames; wxGetApp().getBookmarkMgr().getGroups(groupNames); @@ -272,7 +279,7 @@ wxTreeItemId BookmarkView::refreshBookmarks() { wxTreeItemId group_itm = m_treeView->AppendItem(bookmarkBranch, gn_i); SetTreeItemData(group_itm, tvi); groups[gn_i] = group_itm; - if (prevSel != nullptr && prevSel->type == TreeViewItem::TREEVIEW_ITEM_TYPE_GROUP && gn_i == prevSel->groupName) { + if (prevSelCopy != nullptr && prevSelCopy->type == TreeViewItem::TREEVIEW_ITEM_TYPE_GROUP && gn_i == prevSelCopy->groupName) { bmSelFound = group_itm; } } @@ -315,7 +322,7 @@ wxTreeItemId BookmarkView::refreshBookmarks() { wxTreeItemId itm = m_treeView->AppendItem(groupItem, labelVal); SetTreeItemData(itm, tvi); - if (prevSel != nullptr && prevSel->type == TreeViewItem::TREEVIEW_ITEM_TYPE_BOOKMARK && prevSel->bookmarkEnt == bmEnt && groupExpanded) { + if (prevSelCopy != nullptr && prevSelCopy->type == TreeViewItem::TREEVIEW_ITEM_TYPE_BOOKMARK && prevSelCopy->bookmarkEnt == bmEnt && groupExpanded) { bmSelFound = itm; } if (nextEnt == bmEnt) { @@ -328,6 +335,9 @@ wxTreeItemId BookmarkView::refreshBookmarks() { m_treeView->Expand(groupItem); } } + + delete prevSelCopy; + return bmSelFound; } @@ -338,7 +348,14 @@ void BookmarkView::doUpdateActiveList() { // DemodulatorInstance *activeDemodulator = wxGetApp().getDemodMgr().getActiveDemodulator(); DemodulatorInstance *lastActiveDemodulator = wxGetApp().getDemodMgr().getLastActiveDemodulator(); - TreeViewItem *prevSel = itemToTVI(m_treeView->GetSelection()); + //capture the previously selected item info BY COPY (because the original will be destroyed together with the destroyed tree items) to restore it again after + //having rebuilding the whole tree. + TreeViewItem* prevSel = itemToTVI(m_treeView->GetSelection()); + TreeViewItem* prevSelCopy = nullptr; + + if (prevSel != NULL) { + prevSelCopy = new TreeViewItem(*prevSel); + } // Actives m_treeView->DeleteChildren(activeBranch); @@ -405,12 +422,11 @@ void BookmarkView::doUpdateActiveList() { if (nextRange == re_i) { selItem = itm; nextRange = nullptr; - } else if (!selItem && rangeExpandState && prevSel && prevSel->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RANGE && prevSel->rangeEnt == re_i) { + } else if (!selItem && rangeExpandState && prevSelCopy && prevSelCopy->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RANGE && prevSelCopy->rangeEnt == re_i) { selItem = itm; } } - - + bool recentExpandState = searchState || expandState["recent"]; // Recents @@ -453,7 +469,7 @@ void BookmarkView::doUpdateActiveList() { if (nextEnt == bmr_i) { selItem = itm; nextEnt = nullptr; - } else if (!selItem && recentExpandState && prevSel && prevSel->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RECENT && prevSel->bookmarkEnt == bmr_i) { + } else if (!selItem && recentExpandState && prevSelCopy && prevSelCopy->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RECENT && prevSelCopy->bookmarkEnt == bmr_i) { selItem = itm; } } @@ -474,9 +490,12 @@ void BookmarkView::doUpdateActiveList() { m_treeView->Collapse(rangeBranch); } + //select the item having the same meaning as the previously selected item if (selItem != nullptr) { m_treeView->SelectItem(selItem); } + + delete prevSelCopy; } @@ -561,11 +580,11 @@ void BookmarkView::onTreeActivate( wxTreeEvent& event ) { nextDemod = tvi->demod; } } else if (tvi->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RECENT) { - - activateBookmark(tvi->bookmarkEnt); + nextEnt = tvi->bookmarkEnt; wxGetApp().getBookmarkMgr().removeRecent(tvi->bookmarkEnt); - wxGetApp().getBookmarkMgr().updateActiveList(); + + activateBookmark(tvi->bookmarkEnt); } else if (tvi->type == TreeViewItem::TREEVIEW_ITEM_TYPE_BOOKMARK) { activateBookmark(tvi->bookmarkEnt); } else if (tvi->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RANGE) { @@ -889,6 +908,7 @@ void BookmarkView::activateBookmark(BookmarkEntryPtr bmEnt) { newDemod->setActive(true); wxGetApp().bindDemodulator(newDemod); + //order immediate refresh of the whole tree. doUpdateActiveList(); } @@ -1205,11 +1225,11 @@ void BookmarkView::onActivateRecent( wxCommandEvent& /* event */ ) { TreeViewItem *curSel = itemToTVI(m_treeView->GetSelection()); if (curSel && curSel->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RECENT) { - - activateBookmark(curSel->bookmarkEnt); + BookmarkEntryPtr bookmarkEntToActivate = curSel->bookmarkEnt; m_treeView->Delete(m_treeView->GetSelection()); - wxGetApp().getBookmarkMgr().removeRecent(curSel->bookmarkEnt); - wxGetApp().getBookmarkMgr().updateActiveList(); + + wxGetApp().getBookmarkMgr().removeRecent(bookmarkEntToActivate); + activateBookmark(bookmarkEntToActivate); } } @@ -1222,9 +1242,9 @@ void BookmarkView::onRemoveRecent ( wxCommandEvent& /* event */ ) { TreeViewItem *curSel = itemToTVI(m_treeView->GetSelection()); if (curSel && curSel->type == TreeViewItem::TREEVIEW_ITEM_TYPE_RECENT) { - + BookmarkEntryPtr bookmarkEntToRemove = curSel->bookmarkEnt; m_treeView->Delete(m_treeView->GetSelection()); - wxGetApp().getBookmarkMgr().removeRecent(curSel->bookmarkEnt); + wxGetApp().getBookmarkMgr().removeRecent(bookmarkEntToRemove); wxGetApp().getBookmarkMgr().updateActiveList(); } } diff --git a/src/forms/Bookmark/BookmarkView.h b/src/forms/Bookmark/BookmarkView.h index dcecc0a..04d8e2b 100644 --- a/src/forms/Bookmark/BookmarkView.h +++ b/src/forms/Bookmark/BookmarkView.h @@ -25,6 +25,14 @@ public: bookmarkEnt = nullptr; rangeEnt = nullptr; }; + // copy constructor + TreeViewItem(const TreeViewItem& src) { + demod = src.demod; + bookmarkEnt = src.bookmarkEnt; + rangeEnt = src.rangeEnt; + type = src.type; + groupName = src.groupName; + }; virtual ~TreeViewItem() { // @@ -53,11 +61,17 @@ public: virtual ~BookmarkView(); + //order an asynchronous refresh/rebuild of the whole tree, + //will take effect at the next onUpdateTimer() occurence. void updateActiveList(); - void updateBookmarks(); - bool isKeywordMatch(std::wstring str, std::vector &keywords); - void updateBookmarks(std::string group); + //order asynchronous updates of the bookmarks, + //will take effect at the next onUpdateTimer() occurence. + void updateBookmarks(); + void updateBookmarks(std::string group); + + bool isKeywordMatch(std::wstring str, std::vector &keywords); + wxTreeItemId refreshBookmarks(); void updateTheme(); void onMenuItem(wxCommandEvent& event); @@ -73,7 +87,9 @@ protected: void activeSelection(DemodulatorInstance *dsel); void bookmarkSelection(BookmarkEntryPtr bmSel); void rangeSelection(BookmarkRangeEntryPtr re); + void activateBookmark(BookmarkEntryPtr bmEnt); + void activateRange(BookmarkRangeEntryPtr rangeEnt); void recentSelection(BookmarkEntryPtr bmSel); void groupSelection(std::string groupName); @@ -87,6 +103,7 @@ protected: void onUpdateTimer( wxTimerEvent& event ); + //refresh / rebuild the whole tree item immediatly void doUpdateActiveList(); void onTreeBeginLabelEdit( wxTreeEvent& event );