Skip to content

Commit

Permalink
Migrate ModuleModel to BaseTreeModel and ModuleItem to BaseTreeItem
Browse files Browse the repository at this point in the history
  • Loading branch information
Julia authored and Julia committed Nov 8, 2023
1 parent 930a328 commit 2fae36c
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 146 deletions.
22 changes: 0 additions & 22 deletions plugins/gui/include/gui/module_model/module_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,6 @@ namespace hal
*/
ModuleItem(const u32 id, const TreeItemType type = TreeItemType::Module);

/**
* Appends a child ModuleItem to this ModuleItem.
*
* @param row - The index of the childs of this ModuleItem the new child should be moved to
* @param child - The new child to be inserted
*/
void insertChild(int row, ModuleItem* child);

/**
* Removes a child ModuleItem from this ModuleItem.
*
* @param child - The child to remove
*/
void removeChild(ModuleItem* child);

/**
* Inserts a child ModuleItem at the end of the children list of this ModuleItem.
*
* @param child - The child to be appended
*/
void appendChild(ModuleItem* child);

/**
* Given a set of ModuleItems (in a map [id]->[ModuleItem]) this function adds each ModuleItem of this set as
* a new children if its underlying module is a submodule (child) of the underlying module of this ModuleItem.
Expand Down
36 changes: 0 additions & 36 deletions plugins/gui/include/gui/module_model/module_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,6 @@ namespace hal
explicit ModuleModel(QObject* parent = nullptr);

// === Pure Virtual ===
/**
* Returns the index of the item in the model specified by the given row, column and parent index.
*
* @param row - The row of the item
* @param column - The column of the item
* @param parent - the index of the parent of the item
* @returns the index at the specified position
*/
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;

/**
* Returns the parent of the model item with the given index. If the item has no parent (i.e. index is
* invalid or module is the top module), and invalid QModelIndex is returned.
*
* @param index - The index to find the parent for
* @returns the model index of the parent
*/
QModelIndex parent(const QModelIndex& index) const override;

/**
* Returns the number of rows under the given parent (i.e. the number of children of the parent).
*
* @param parent - The model index of the parent
* @returns the number of rows under the given parent
*/
int rowCount(const QModelIndex& parent = QModelIndex()) const override;

/**
* Returns the number of columns for the children of the given parent. Since the module model only contains
* one column this function returns always 1.
*
* @param parent - The model index of the parent
* @returns the number of columns for the children of the given parent. Always 1.
*/
int columnCount(const QModelIndex& parent = QModelIndex()) const override;

/**
* Returns the data stored under the given role for the item referred to by the index.
Expand Down Expand Up @@ -239,7 +204,6 @@ namespace hal
bool isModifying();

private:
ModuleItem* mTopModuleItem;

QMap<u32, ModuleItem*> mModuleMap;
QMap<u32, ModuleItem*> mGateMap;
Expand Down
15 changes: 0 additions & 15 deletions plugins/gui/src/module_model/module_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@ ModuleItem::ModuleItem(const u32 id, const TreeItemType type) :
}
}

void ModuleItem::insertChild(int row, ModuleItem* child)
{
mChildItems.insert(row, child);
}

void ModuleItem::removeChild(ModuleItem* child)
{
mChildItems.removeOne(child);
}

void ModuleItem::appendChild(ModuleItem* child)
{
mChildItems.append(child);
}

void ModuleItem::appendExistingChildIfAny(const QMap<u32,ModuleItem*>& moduleMap)
{
if(mType != TreeItemType::Module) // only module can have children
Expand Down
76 changes: 3 additions & 73 deletions plugins/gui/src/module_model/module_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,12 @@

namespace hal
{
ModuleModel::ModuleModel(QObject* parent) : BaseTreeModel(parent), mTopModuleItem(nullptr)
ModuleModel::ModuleModel(QObject* parent) : BaseTreeModel(parent)
{
// use root item to store header information
setHeaderLabels(QStringList() << "Name" << "ID" << "Type");
}

QModelIndex ModuleModel::index(int row, int column, const QModelIndex& parent) const
{
// BEHAVIOR FOR ILLEGAL INDICES IS UNDEFINED
// SEE QT DOCUMENTATION
if (!hasIndex(row, column, parent))
return QModelIndex();

if (!parent.isValid())
{
if (row == 0 && column >= 0 && column < 3 && mTopModuleItem)
return createIndex(0, column, mTopModuleItem);
else
return QModelIndex();
}

if (column < 0 || column >= 3 || parent.column() < 0 || parent.column() >= 3)
return QModelIndex();

ModuleItem* parent_item = getItem(parent);

ModuleItem* child_item = static_cast<ModuleItem*>(parent_item)->child(row);
assert(child_item);

return createIndex(row, column, child_item);
}

QModelIndex ModuleModel::parent(const QModelIndex& index) const
{
if (!index.isValid())
return QModelIndex();

ModuleItem* item = getItem(index);

if (item == mTopModuleItem)
return QModelIndex();

ModuleItem* parent_item = static_cast<ModuleItem*>(item->getParent());
return createIndex(parent_item->row(), 0, parent_item);

}

int ModuleModel::rowCount(const QModelIndex& parent) const
{
if (!parent.isValid()) // ??
return 1;

//if (parent.column() != 0)
// return 0;

ModuleItem* parent_item = getItem(parent);

return parent_item->childCount();
}

int ModuleModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent)

return 3;
}

QVariant ModuleModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
Expand Down Expand Up @@ -143,7 +82,7 @@ namespace hal
QVector<int> row_numbers;
const ModuleItem* current_item = item;

while (current_item != mTopModuleItem)
while (current_item != mRootItem->getChild(0))
{
row_numbers.append(current_item->row());
current_item = static_cast<const ModuleItem*>(current_item->constParent());
Expand All @@ -163,7 +102,6 @@ namespace hal
mModuleMap.insert(1, item);

beginInsertRows(index(0, 0, QModelIndex()), 0, 0);
mTopModuleItem = item;
mRootItem->appendChild(item);
endInsertRows();

Expand All @@ -187,15 +125,7 @@ namespace hal
{
beginResetModel();

mTopModuleItem = nullptr;

for (ModuleItem* m : mModuleMap)
delete m;
for (ModuleItem* g : mGateMap)
delete g;
for (ModuleItem* n : mNetMap)
delete n;

BaseTreeModel::clear();
mModuleMap.clear();
mGateMap.clear();
mNetMap.clear();
Expand Down

0 comments on commit 2fae36c

Please sign in to comment.