Merge pull request #1145 from vladisslav2011/fix_bookmarks_crash

Switch to using shared pointers for TagInfo
This commit is contained in:
Clayton Smith 2023-04-16 11:37:44 -04:00 committed by GitHub
commit a5dd092bc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 57 deletions

View File

@ -4,6 +4,7 @@
NEW: Restore AM & AM-Sync settings between sessions.
NEW: Set/get audio gain via remote control.
FIXED: Loading of narrow FM tau setting.
FIXED: Crash when adding or removing bookmark tags.
REMOVED: Support for GNU Radio 3.7.

View File

@ -2422,11 +2422,11 @@ void MainWindow::on_actionAddBookmark_triggered()
info.name=name;
info.tags.clear();
if (tags.empty())
info.tags.append(&Bookmarks::Get().findOrAddTag(""));
info.tags.append(Bookmarks::Get().findOrAddTag(""));
for (i = 0; i < tags.size(); ++i)
info.tags.append(&Bookmarks::Get().findOrAddTag(tags[i]));
info.tags.append(Bookmarks::Get().findOrAddTag(tags[i]));
Bookmarks::Get().add(info);
uiDockBookmarks->updateTags();

View File

@ -36,7 +36,7 @@ Bookmarks* Bookmarks::m_pThis = 0;
Bookmarks::Bookmarks()
{
TagInfo tag(TagInfo::strUntagged);
TagInfo::sptr tag = TagInfo::make(TagInfo::strUntagged);
m_TagList.append(tag);
}
@ -96,8 +96,8 @@ bool Bookmarks::load()
QStringList strings = line.split(";");
if(strings.count() == 2)
{
TagInfo &info = findOrAddTag(strings[0]);
info.color = QColor(strings[1].trimmed());
TagInfo::sptr info = findOrAddTag(strings[0]);
info->color = QColor(strings[1].trimmed());
}
else
{
@ -127,7 +127,7 @@ bool Bookmarks::load()
QStringList TagList = strTags.split(",");
for(int iTag=0; iTag<TagList.size(); ++iTag)
{
info.tags.append(&findOrAddTag(TagList[iTag].trimmed()));
info.tags.append(findOrAddTag(TagList[iTag].trimmed()));
}
m_BookmarkList.append(info);
@ -158,21 +158,20 @@ bool Bookmarks::save()
stream << QString("# Tag name").leftJustified(20) + "; " +
QString(" color") << '\n';
QSet<TagInfo*> usedTags;
QMap<QString, TagInfo::sptr> usedTags;
for (int iBookmark = 0; iBookmark < m_BookmarkList.size(); iBookmark++)
{
BookmarkInfo& info = m_BookmarkList[iBookmark];
for(int iTag = 0; iTag < info.tags.size(); ++iTag)
for (QList<TagInfo::sptr>::iterator iTag = info.tags.begin(); iTag < info.tags.end(); ++iTag)
{
TagInfo& tag = *info.tags[iTag];
usedTags.insert(&tag);
usedTags.insert((*iTag)->name, *iTag);
}
}
for (QSet<TagInfo*>::iterator i = usedTags.begin(); i != usedTags.end(); i++)
for (QMap<QString, TagInfo::sptr>::iterator i = usedTags.begin(); i != usedTags.end(); i++)
{
TagInfo& info = **i;
stream << info.name.leftJustified(20) + "; " + info.color.name() << '\n';
TagInfo::sptr info = *i;
stream << info->name.leftJustified(20) + "; " + info->color.name() << '\n';
}
stream << '\n';
@ -192,12 +191,12 @@ bool Bookmarks::save()
QString::number(info.bandwidth).rightJustified(10) + "; ";
for(int iTag = 0; iTag<info.tags.size(); ++iTag)
{
TagInfo& tag = *info.tags[iTag];
if(iTag!=0)
TagInfo::sptr tag = info.tags[iTag];
if (iTag!=0)
{
line.append(",");
}
line.append(tag.name);
line.append(tag->name);
}
stream << line << '\n';
@ -233,7 +232,7 @@ QList<BookmarkInfo> Bookmarks::getBookmarksInRange(qint64 low, qint64 high)
}
TagInfo &Bookmarks::findOrAddTag(QString tagName)
TagInfo::sptr Bookmarks::findOrAddTag(QString tagName)
{
tagName = tagName.trimmed();
@ -245,7 +244,7 @@ TagInfo &Bookmarks::findOrAddTag(QString tagName)
if (idx != -1)
return m_TagList[idx];
TagInfo info(tagName);
TagInfo::sptr info = TagInfo::make(tagName);
m_TagList.append(info);
emit TagListChanged();
return m_TagList.last();
@ -264,17 +263,17 @@ bool Bookmarks::removeTag(QString tagName)
return false;
// Delete Tag from all Bookmarks that use it.
TagInfo* pTagToDelete = &m_TagList[idx];
for(int i=0; i<m_BookmarkList.size(); ++i)
TagInfo::sptr pTagToDelete = m_TagList[idx];
for(int i=0; i < m_BookmarkList.size(); ++i)
{
BookmarkInfo& bmi = m_BookmarkList[i];
for(int t=0; t<bmi.tags.size(); ++t)
{
TagInfo* pTag = bmi.tags[t];
if(pTag == pTagToDelete)
TagInfo::sptr pTag = bmi.tags[t];
if(pTag.get() == pTagToDelete.get())
{
if(bmi.tags.size()>1) bmi.tags.removeAt(t);
else bmi.tags[0] = &findOrAddTag(TagInfo::strUntagged);
else bmi.tags[0] = findOrAddTag(TagInfo::strUntagged);
}
}
}
@ -292,7 +291,7 @@ bool Bookmarks::setTagChecked(QString tagName, bool bChecked)
{
int idx = getTagIndex(tagName);
if (idx == -1) return false;
m_TagList[idx].active = bChecked;
m_TagList[idx]->active = bChecked;
emit BookmarksChanged();
emit TagListChanged();
return true;
@ -303,7 +302,7 @@ int Bookmarks::getTagIndex(QString tagName)
tagName = tagName.trimmed();
for (int i = 0; i < m_TagList.size(); i++)
{
if (m_TagList[i].name == tagName)
if (m_TagList[i]->name == tagName)
return i;
}
@ -314,10 +313,10 @@ const QColor BookmarkInfo::GetColor() const
{
for(int iTag=0; iTag<tags.size(); ++iTag)
{
TagInfo& tag = *tags[iTag];
if(tag.active)
TagInfo::sptr tag = tags[iTag];
if(tag->active)
{
return tag.color;
return tag->color;
}
}
return TagInfo::DefaultColor;
@ -328,8 +327,8 @@ bool BookmarkInfo::IsActive() const
bool bActive = false;
for(int iTag=0; iTag<tags.size(); ++iTag)
{
TagInfo& tag = *tags[iTag];
if(tag.active)
TagInfo::sptr tag = tags[iTag];
if(tag->active)
{
bActive = true;
break;

View File

@ -30,9 +30,11 @@
#include <QList>
#include <QStringList>
#include <QColor>
#include <memory>
struct TagInfo
{
using sptr = std::shared_ptr<TagInfo>;
QString name;
QColor color;
bool active;
@ -46,6 +48,10 @@ struct TagInfo
this->color=DefaultColor;
this->name = name;
}
static sptr make(QString name = "")
{
return std::make_shared<TagInfo>(name);
}
bool operator<(const TagInfo &other) const
{
return name < other.name;
@ -58,7 +64,7 @@ struct BookmarkInfo
QString name;
QString modulation;
qint64 bandwidth;
QList<TagInfo*> tags;
QList<TagInfo::sptr> tags;
BookmarkInfo()
{
@ -107,8 +113,8 @@ public:
//int lowerBound(qint64 low);
//int upperBound(qint64 high);
QList<TagInfo> getTagList() { return QList<TagInfo>(m_TagList); }
TagInfo& findOrAddTag(QString tagName);
QList<TagInfo::sptr> getTagList() { return QList<TagInfo::sptr>(m_TagList); }
TagInfo::sptr findOrAddTag(QString tagName);
int getTagIndex(QString tagName);
bool removeTag(QString tagName);
bool setTagChecked(QString tagName, bool bChecked);
@ -117,10 +123,10 @@ public:
private:
Bookmarks(); // Singleton Constructor is private.
QList<BookmarkInfo> m_BookmarkList;
QList<TagInfo> m_TagList;
QString m_bookmarksFile;
static Bookmarks* m_pThis;
QList<BookmarkInfo> m_BookmarkList;
QList<TagInfo::sptr> m_TagList;
QString m_bookmarksFile;
static Bookmarks* m_pThis;
signals:
void BookmarksChanged(void);

View File

@ -101,8 +101,8 @@ QVariant BookmarksTableModel::data ( const QModelIndex & index, int role ) const
{
strTags.append(",");
}
TagInfo& tag = *info.tags[iTag];
strTags.append(tag.name);
TagInfo::sptr tag = info.tags[iTag];
strTags.append(tag->name);
}
return strTags;
}
@ -154,7 +154,7 @@ bool BookmarksTableModel::setData(const QModelIndex &index, const QVariant &valu
for(int i=0; i<strList.size(); ++i)
{
QString strTag = strList[i].trimmed();
info.tags.append( &Bookmarks::Get().findOrAddTag(strTag) );
info.tags.append( Bookmarks::Get().findOrAddTag(strTag) );
}
emit dataChanged(index, index);
return true;
@ -196,8 +196,8 @@ void BookmarksTableModel::update()
bool bActive = false;
for(int iTag=0; iTag<info.tags.size(); ++iTag)
{
TagInfo& tag = *info.tags[iTag];
if(tag.active)
TagInfo::sptr tag = info.tags[iTag];
if(tag->active)
{
bActive = true;
break;

View File

@ -64,13 +64,13 @@ void BookmarksTagList::on_cellClicked(int row, int column)
void BookmarksTagList::changeColor(int row, int /*column*/)
{
TagInfo &info = Bookmarks::Get().findOrAddTag(item(row, 1)->text());
QColor color = QColorDialog::getColor(info.color, this);
TagInfo::sptr info = Bookmarks::Get().findOrAddTag(item(row, 1)->text());
QColor color = QColorDialog::getColor(info->color, this);
if(!color.isValid())
return;
info.color=color;
info->color=color;
updateTags();
Bookmarks::Get().save();
}
@ -101,13 +101,13 @@ void BookmarksTagList::updateTags()
}
// Get current List of Tags.
QList<TagInfo> newTags = Bookmarks::Get().getTagList();
QList<TagInfo::sptr> newTags = Bookmarks::Get().getTagList();
if(!m_bShowUntagged)
{
for(int i=0; i<newTags.size(); ++i)
{
TagInfo& taginfo = newTags[i];
if(taginfo.name.compare(TagInfo::strUntagged)==0)
TagInfo::sptr taginfo = newTags[i];
if(taginfo->name.compare(TagInfo::strUntagged)==0)
{
newTags.removeAt(i);
break;
@ -121,9 +121,9 @@ void BookmarksTagList::updateTags()
setRowCount(0);
for(int i=0; i<newTags.count(); i++)
{
AddTag(newTags[i].name,
( unchecked.contains(newTags[i].name) ? Qt::Unchecked : Qt::Checked ),
newTags[i].color);
AddTag(newTags[i]->name,
( unchecked.contains(newTags[i]->name) ? Qt::Unchecked : Qt::Checked ),
newTags[i]->color);
}
setSortingEnabled(true);
@ -144,7 +144,7 @@ void BookmarksTagList::setSelectedTagsAsString(const QString& strTags)
setSortingEnabled(true);
}
void BookmarksTagList::setSelectedTags(QList<TagInfo*> tags)
void BookmarksTagList::setSelectedTags(QList<TagInfo::sptr>& tags)
{
int iRows = rowCount();
for(int i=0; i<iRows; ++i)
@ -152,9 +152,9 @@ void BookmarksTagList::setSelectedTags(QList<TagInfo*> tags)
QTableWidgetItem* pItem = item(i,1);
QString name = pItem->text();
bool bChecked = false;
for(QList<TagInfo*>::const_iterator it=tags.begin(), itend=tags.end(); it!=itend; ++it)
for(QList<TagInfo::sptr>::const_iterator it=tags.begin(), itend=tags.end(); it!=itend; ++it)
{
TagInfo* pTag = *it;
TagInfo::sptr pTag = *it;
if(pTag->name == name) bChecked = true;
}
pItem->setCheckState(bChecked ? Qt::Checked : Qt::Unchecked);

View File

@ -34,7 +34,7 @@ public:
explicit BookmarksTagList(QWidget *parent = 0, bool bShowUntagged = true);
QStringList getSelectedTags();
void setSelectedTagsAsString(const QString& strTags);
void setSelectedTags(QList<TagInfo*> tags);
void setSelectedTags(QList<TagInfo::sptr>& tags);
bool m_bUpdating;
private:

View File

@ -267,11 +267,11 @@ void DockBookmarks::changeBookmarkTags(int row, int /*column*/)
bmi.tags.clear();
if (tags.size() == 0)
{
bmi.tags.append(&Bookmarks::Get().findOrAddTag("")); // "Untagged"
bmi.tags.append(Bookmarks::Get().findOrAddTag("")); // "Untagged"
}
for (int i = 0; i < tags.size(); ++i)
{
bmi.tags.append(&Bookmarks::Get().findOrAddTag(tags[i]));
bmi.tags.append(Bookmarks::Get().findOrAddTag(tags[i]));
}
Bookmarks::Get().save();
}