Skip to content

Commit

Permalink
Errors in pin_changed event handling fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Sep 22, 2023
1 parent b7f9bac commit 9abb832
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 71 deletions.
3 changes: 3 additions & 0 deletions include/hal_core/netlist/gate_library/enums/pin_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ namespace hal
PinDirChange,
PinDelete
};

template<>
std::map<PinEvent, std::string> EnumStrings<PinEvent>::data;
} // namespace hal
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace hal
PinType mPinType;
QString mNetName;

void setPinType(const QString& type);
void setPinDirection(const QString& dir);
public:

PortTreeItem(Type itype, QString pinName, PinDirection dir, PinType ptype, QString netName = QString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,12 @@ namespace hal
return QVariant();
}

void PortTreeItem::setPinType(const QString& type)
{
mPinType = PinType::none;
for (int pt = 1; pt < (int) PinType::sum; ++pt)
{
if (QString::fromStdString(enum_to_string((PinType)pt)) == type)
mPinType = (PinType)pt;
}
}

void PortTreeItem::setPinDirection(const QString& dir)
{
mPinDirection = PinDirection::none;
for (int pd = 1; pd <= (int) PinDirection::internal; ++pd)
{
if (QString::fromStdString(enum_to_string((PinDirection)pd)) == dir)
mPinDirection = (PinDirection)pd;
}
}

void PortTreeItem::setData(QList<QVariant> data)
{
mPinName = data[0].toString();
setPinDirection(data[1].toString());
setPinType(data[2].toString());
mNetName = data[3].toString();
mPinName = data[0].toString();
mPinDirection = enum_from_string<PinDirection>(data[1].toString().toStdString());
mPinType = enum_from_string<PinType>(data[2].toString().toStdString());
mNetName = data[3].toString();
}

void PortTreeItem::setDataAtIndex(int index, QVariant &data)
Expand All @@ -75,10 +55,10 @@ namespace hal
mPinName = data.toString();
break;}
case 1: {
setPinDirection(data.toString());
mPinDirection = enum_from_string<PinDirection>(data.toString().toStdString());
break;}
case 2: {
setPinType(data.toString());
mPinType = enum_from_string<PinType>(data.toString().toStdString());
break;}
case 3: {
mNetName = data.toString();
Expand Down Expand Up @@ -361,7 +341,7 @@ namespace hal
QDataStream dataStream(&encItem, QIODevice::ReadOnly);
dataStream >> type >> id;
auto parentItem = static_cast<PortTreeItem*>(getItemFromIndex(parent));
qDebug() << "type: " << type << ", id" << id << ", row: " << row;
// qDebug() << "type: " << type << ", id" << id << ", row: " << row;

// construct a "drop-matrix" here, but only 4(5) things are NOT allowed (so check for these):
// 1: drop a pin on its OWN parent
Expand Down Expand Up @@ -530,40 +510,50 @@ namespace hal
Q_UNUSED(pgid);
if ((int)m->get_id() != mModuleId) return;

log_info("gui", "Handle pin_changed event {} ID={}", enum_to_string<PinEvent>(pev), pgid);
PortTreeItem* ptiPin = nullptr;
PortTreeItem* ptiGroup = nullptr;
const PinGroup<ModulePin>* pgroup = nullptr;
const ModulePin* pin = nullptr;
int pinRow = -1;


if (pev < PinEvent::PinCreate)
{
// group event
ptiGroup = mIdToGroupItem.value(pgid);
pgroup = m->get_pin_group_by_id(pgid);
if (!pgroup)
if (pev != PinEvent::GroupDelete)
{
log_warning("gui", "Cannot handle event for pin group ID={}, no such group.", pgid);
return;
pgroup = m->get_pin_group_by_id(pgid);
if (!pgroup)
{
log_warning("gui", "Cannot handle event for pin group ID={}, no such group.", pgid);
return;
}
}
}
else
{
// pin event
ptiPin = mIdToPinItem.value(pgid);
pin = m->get_pin_by_id(pgid);
if (!pin)
if (pev != PinEvent::PinDelete)
{
log_warning("gui", "Cannot handle event for pin ID={}, no such pid.", pgid);
return;
pin = m->get_pin_by_id(pgid);
if (!pin)
{
log_warning("gui", "Cannot handle event for pin ID={}, no such pid.", pgid);
return;
}
auto pgPair = pin->get_group();
pgroup = pgPair.first;
pinRow = pgPair.second;
if (pgroup)
ptiGroup = mIdToGroupItem.value(pgroup->get_id());
}
auto pgPair = pin->get_group();
pgroup = pgPair.first;
pinRow = pgPair.second;
if (pgroup)
ptiGroup = mIdToGroupItem.value(pgroup->get_id());
}

QModelIndex dataChangedIndex;

switch (pev)
{
case PinEvent::GroupCreate:
Expand All @@ -585,12 +575,15 @@ namespace hal
}
case PinEvent::GroupRename:
ptiGroup->setName(QString::fromStdString(pgroup->get_name()));
dataChangedIndex = getIndexFromItem(ptiGroup);
break;
case PinEvent::GroupTypeChange:
ptiGroup->setPinType(pgroup->get_type());
dataChangedIndex = getIndexFromItem(ptiGroup);
break;
case PinEvent::GroupDirChange:
ptiGroup->setPinDirection(pgroup->get_direction());
dataChangedIndex = getIndexFromItem(ptiGroup);
break;
case PinEvent::GroupDelete:
removeItem(ptiGroup);
Expand Down Expand Up @@ -639,12 +632,15 @@ namespace hal
}
case PinEvent::PinRename:
ptiPin->setName(QString::fromStdString(pin->get_name()));
dataChangedIndex = getIndexFromItem(ptiPin);
break;
case PinEvent::PinTypeChange:
ptiPin->setPinType(pin->get_type());
dataChangedIndex = getIndexFromItem(ptiPin);
break;
case PinEvent::PinDirChange:
ptiPin->setPinDirection(pin->get_direction());
dataChangedIndex = getIndexFromItem(ptiPin);
break;
case PinEvent::PinDelete:
removeItem(ptiPin);
Expand All @@ -653,6 +649,12 @@ namespace hal
default:
break;
}

if (dataChangedIndex.isValid())
{
QModelIndex inxLastCol = createIndex(dataChangedIndex.row(),columnCount()-1,dataChangedIndex.internalPointer());
Q_EMIT dataChanged(dataChangedIndex,inxLastCol);
}
}

void ModulePinsTreeModel::dndGroupOnGroup(BaseTreeItem *droppedGroup, BaseTreeItem *onDroppedGroup)
Expand Down Expand Up @@ -684,24 +686,13 @@ namespace hal
if(ownRow < row && !bottomEdge) desiredIdx--;
ActionPingroup* act = new ActionPingroup(PinActionType::GroupMove,droppedGroup->id(),"",desiredIdx);
act->setObject(UserActionObject(mModuleId,UserActionObjectType::Module));
bool ok = act->exec();
if(ok){
removeItem(droppedGroup);
insertItem(droppedGroup, mRootItem, desiredIdx);
}
act->exec();
}

void ModulePinsTreeModel::dndPinOnGroup(PortTreeItem *droppedPin, BaseTreeItem *onDroppedGroup)
{
ActionPingroup* act = new ActionPingroup(PinActionType::PinAsignGroup,droppedPin->id(),"",static_cast<PortTreeItem*>(onDroppedGroup)->id());
act->exec();
auto oldParent = droppedPin->getParent();
removeItem(droppedPin);
insertItem(droppedPin, onDroppedGroup, onDroppedGroup->getChildCount());
if(!(oldParent->getChildCount())){
removeItem(static_cast<PortTreeItem*>(oldParent));
delete oldParent;
}
}

void ModulePinsTreeModel::dndPinBetweenPin(PortTreeItem *droppedPin, BaseTreeItem *onDroppedParent, int row)
Expand All @@ -723,13 +714,6 @@ namespace hal
}
act->setObject(UserActionObject(mModuleId,UserActionObjectType::Module));
act->exec();
auto oldParent = droppedPin->getParent();
removeItem(droppedPin);
insertItem(droppedPin, onDroppedParent, desiredIdx);
if(!(oldParent->getChildCount())){
removeItem(static_cast<PortTreeItem*>(oldParent));
delete oldParent;
}
}

void ModulePinsTreeModel::dndPinBetweenGroup(PortTreeItem *droppedPin, int row)
Expand Down
20 changes: 19 additions & 1 deletion src/netlist/gate_library/enums/pin_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ namespace hal
{PinType::select, "select"},
{PinType::carry, "carry"},
{PinType::sum, "sum"}};
}

template<>
std::map<PinEvent, std::string> EnumStrings<PinEvent>::data = {
{PinEvent::unknown, "unknown"},
{PinEvent::GroupCreate, "GroupCreate"},
{PinEvent::GroupReorder, "GroupReorder"},
{PinEvent::GroupRename, "GroupRename"},
{PinEvent::GroupTypeChange, "GroupTypeChange"},
{PinEvent::GroupDirChange, "GroupDirChange"},
{PinEvent::GroupDelete, "GroupDelete"},
{PinEvent::PinCreate, "PinCreate"},
{PinEvent::PinReorder, "PinReorder"},
{PinEvent::PinAssignToGroup, "PinAssignToGroup"},
{PinEvent::PinRename, "PinRename"},
{PinEvent::PinTypeChange, "PinTypeChange"},
{PinEvent::PinDirChange, "PinDirChange"},
{PinEvent::PinDelete, "PinDelete"}
};
}
18 changes: 9 additions & 9 deletions src/netlist/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@ namespace hal

bool Module::delete_pin_group(PinGroup<ModulePin>* pin_group)
{
std::vector<u32> distribute_events;
if (pin_group == nullptr)
{
log_warning("module", "could not delete pin group from module '{}' with ID {}: pin group is a 'nullptr'", m_name, m_id);
Expand All @@ -1286,16 +1287,16 @@ namespace hal
return false;
}

bool removed_pins = false;

std::vector<ModulePin*> pins_copy = pin_group->get_pins();
for (ModulePin* pin : pins_copy)
{
removed_pins = true;
if (auto res = create_pin_group(pin->get_name(), {pin}, pin->get_direction(), pin->get_type(), true, 0, false); res.is_error())
auto res = create_pin_group(pin->get_name(), {pin}, pin->get_direction(), pin->get_type(), true, 0, false);
if (res.is_error())
{
return false;
}
distribute_events.push_back(pinevent_associated_data(PinEvent::GroupCreate,res.get()->get_id()));
distribute_events.push_back(pinevent_associated_data(PinEvent::PinAssignToGroup,pin->get_id()));
}

u32 pin_group_id_to_delete = pin_group->get_id();
Expand All @@ -1305,10 +1306,9 @@ namespace hal
return false;
}

if (removed_pins)
{
m_event_handler->notify(ModuleEvent::event::pin_changed, this, pinevent_associated_data(PinEvent::GroupDelete,pin_group_id_to_delete));
}
for (u32 dist_ev : distribute_events)
m_event_handler->notify(ModuleEvent::event::pin_changed, this, dist_ev);
m_event_handler->notify(ModuleEvent::event::pin_changed, this, pinevent_associated_data(PinEvent::GroupDelete,pin_group_id_to_delete));
return true;
}

Expand Down Expand Up @@ -1399,7 +1399,7 @@ namespace hal

if (delete_empty_groups && pg->empty())
{
pin_group_id_to_delete = pin_group->get_id();
pin_group_id_to_delete = pg->get_id();
if (!delete_pin_group_internal(pg))
{
log_warning("module",
Expand Down

0 comments on commit 9abb832

Please sign in to comment.