Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor loop to use QHash and ensure SimpleFileTreeModel destructor is called #13

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the view to the model is kind of an anti-pattern, is that really necessary? What is something you tried for ownership?

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
Loading