Skip to content

Commit

Permalink
Refactor loop to use QHash and ensure SimpleFileTreeModel destructor …
Browse files Browse the repository at this point in the history
…is called (#13)

* Use QHash to see if child item was already added
* Pass parent to model so its destructor is called

---------

Co-authored-by: RJ <[email protected]>
  • Loading branch information
Liderate and Liderate authored Oct 3, 2024
1 parent 567d8ac commit 79cdca3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/previewbsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ QWidget* PreviewBsa::genBsaPreview(const QString& fileName, const QSize&)
layout->addWidget(infoLabel);

QTreeView* view = new QTreeView();
SimpleFileTreeModel* model = new SimpleFileTreeModel(m_Files);
SimpleFileTreeModel* model = new SimpleFileTreeModel(m_Files, view);

view->setModel(model);
layout->addWidget(view);
Expand Down
8 changes: 7 additions & 1 deletion src/simplefiletreeitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ SimpleFileTreeItem::~SimpleFileTreeItem()
qDeleteAll(m_childItems);
}

void SimpleFileTreeItem::appendChild(SimpleFileTreeItem* item)
void SimpleFileTreeItem::appendChild(const QString& name, SimpleFileTreeItem* item)
{
m_childItems.append(item);
m_childItemsByName.insert(name, item);
}

SimpleFileTreeItem* SimpleFileTreeItem::child(int row)
Expand All @@ -30,6 +31,11 @@ SimpleFileTreeItem* SimpleFileTreeItem::child(int row)
return m_childItems.at(row);
}

SimpleFileTreeItem* SimpleFileTreeItem::childByName(const QString& name)
{
return m_childItemsByName.value(name);
}

QVector<SimpleFileTreeItem*> SimpleFileTreeItem::children()
{
return m_childItems;
Expand Down
4 changes: 3 additions & 1 deletion src/simplefiletreeitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class SimpleFileTreeItem
SimpleFileTreeItem* parentItem = nullptr);
~SimpleFileTreeItem();

void appendChild(SimpleFileTreeItem* child);
void appendChild(const QString& name, SimpleFileTreeItem* child);

SimpleFileTreeItem* child(int row);
SimpleFileTreeItem* childByName(const QString& name);
QVector<SimpleFileTreeItem*> children();
int childCount() const;
int columnCount() const;
Expand All @@ -23,6 +24,7 @@ class SimpleFileTreeItem

private:
QVector<SimpleFileTreeItem*> m_childItems;
QHash<QString, SimpleFileTreeItem*> m_childItemsByName;
QVector<QVariant> m_itemData;
SimpleFileTreeItem* m_parentItem;
};
Expand Down
14 changes: 3 additions & 11 deletions src/simplefiletreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,18 @@ void SimpleFileTreeModel::setupModelData(const QStringList& lines,
auto currentParent = m_RootItem;

for (int i = 0; i < lineEntries.count(); i++) {
QString currentEntryName = lineEntries[i];
SimpleFileTreeItem* currentEntry = nullptr;
QString currentEntryName = lineEntries[i];

// check if item was already added
if (currentParent->childCount() > 0) {
for (auto child : currentParent->children()) {
if (child->data(0).toString() == currentEntryName) {
currentEntry = child;
break;
}
}
}
SimpleFileTreeItem* currentEntry = currentParent->childByName(currentEntryName);

// add tree item if not found
if (currentEntry == nullptr) {
QVector<QVariant> columnData;
columnData.reserve(m_ColumnCount);
columnData << currentEntryName;
currentEntry = new SimpleFileTreeItem(columnData, currentParent);
currentParent->appendChild(currentEntry);
currentParent->appendChild(currentEntryName, currentEntry);
}

// as we go deeper into the path
Expand Down

0 comments on commit 79cdca3

Please sign in to comment.