From bcaca5d374aefbbd4c68534a8a9fe681ca7e66ca Mon Sep 17 00:00:00 2001 From: joern274 Date: Fri, 29 Sep 2023 17:10:36 +0200 Subject: [PATCH] Bugfixes and implementation of dndPinBetweenGroup --- .../module_details_widget/port_tree_model.h | 3 +- .../include/gui/user_action/action_pingroup.h | 10 ++-- .../module_details_widget/port_tree_model.cpp | 51 +++++-------------- .../gui/src/user_action/action_pingroup.cpp | 32 +++++++++--- 4 files changed, 44 insertions(+), 52 deletions(-) diff --git a/plugins/gui/include/gui/selection_details_widget/module_details_widget/port_tree_model.h b/plugins/gui/include/gui/selection_details_widget/module_details_widget/port_tree_model.h index 52604b5dd51..575b4637d93 100644 --- a/plugins/gui/include/gui/selection_details_widget/module_details_widget/port_tree_model.h +++ b/plugins/gui/include/gui/selection_details_widget/module_details_widget/port_tree_model.h @@ -52,7 +52,7 @@ namespace hal public: - PortTreeItem(Type itype, QString pinName, PinDirection dir, PinType ptype, QString netName = QString()); + PortTreeItem(Type itype, u32 id_, QString pinName, PinDirection dir, PinType ptype, QString netName = QString()); PortTreeItem() : mItemType(None), mId(0) {;} QVariant getData(int column) const override; void setData(QList data) override; @@ -61,7 +61,6 @@ namespace hal int getColumnCount() const override; void setItemType(Type tp) { mItemType = tp; } Type itemType() const { return mItemType; } - void setId(u32 id_) { mId = id_; } QString name() const { return mPinName; } void setName(const QString& nam) { mPinName = nam; } void setPinType(PinType ptype) { mPinType = ptype; } diff --git a/plugins/gui/include/gui/user_action/action_pingroup.h b/plugins/gui/include/gui/user_action/action_pingroup.h index d1562ff4ed7..f838cd72a21 100644 --- a/plugins/gui/include/gui/user_action/action_pingroup.h +++ b/plugins/gui/include/gui/user_action/action_pingroup.h @@ -52,6 +52,8 @@ namespace hal int pinRow2Index(const ModulePin* pin, int row); + QString generateGroupName(const Module* mod, const ModulePin* pin); + void dumpPingroups(Module* m = nullptr); /** * @ingroup user_action @@ -154,10 +156,10 @@ namespace hal void readFromXml(QXmlStreamReader& xmlIn) override; void addToHash(QCryptographicHash& cryptoHash) const override; - static ActionPingroup* addPinsToExistingGroup(const Module* m, u32 grpId, QList pinIds, int irow = -1); - static ActionPingroup* addPinToExistingGroup(const Module* m, u32 grpId, u32 pinId, int irow = -1); - static ActionPingroup* addPinsToNewGroup(const Module* m, const QString& name, QList pinIds); - static ActionPingroup* addPinToNewGroup(const Module* m, const QString& name, u32 pinId); + static ActionPingroup* addPinsToExistingGroup(const Module* m, u32 grpId, QList pinIds, int pinRow = -1); + static ActionPingroup* addPinToExistingGroup(const Module* m, u32 grpId, u32 pinId, int pinRow = -1); + static ActionPingroup* addPinsToNewGroup(const Module* m, const QString& name, QList pinIds, int grpRow = -1); + static ActionPingroup* addPinToNewGroup(const Module* m, const QString& name, u32 pinId, int grpRow = -1); static ActionPingroup* removePinsFromGroup(const Module* m, QList pinIds); }; diff --git a/plugins/gui/src/selection_details_widget/module_details_widget/port_tree_model.cpp b/plugins/gui/src/selection_details_widget/module_details_widget/port_tree_model.cpp index b0ceed5effe..a4e40653a3f 100644 --- a/plugins/gui/src/selection_details_widget/module_details_widget/port_tree_model.cpp +++ b/plugins/gui/src/selection_details_widget/module_details_widget/port_tree_model.cpp @@ -19,8 +19,8 @@ namespace hal { - PortTreeItem::PortTreeItem(Type itype, QString pinName, PinDirection dir, PinType ptype, QString netName) - : mItemType(itype), mPinName(pinName), mPinDirection(dir), mPinType(ptype), mNetName(netName) + PortTreeItem::PortTreeItem(Type itype, u32 id_, QString pinName, PinDirection dir, PinType ptype, QString netName) + : mItemType(itype), mId(id_), mPinName(pinName), mPinDirection(dir), mPinType(ptype), mNetName(netName) {;} QVariant PortTreeItem::getData(int index) const @@ -145,6 +145,7 @@ namespace hal data->setData("pintreemodel/item", encodedData); return data; } + bool ModulePinsTreeModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) { Q_UNUSED(action) @@ -154,6 +155,7 @@ namespace hal int id; QByteArray encItem = data->data("pintreemodel/item"); QDataStream dataStream(&encItem, QIODevice::ReadOnly); + qDebug() << "dropMimeData" << encItem << row << column; dataStream >> type >> id; auto droppedItem = (type == "group") ? static_cast(mIdToGroupItem.value(id)) : static_cast(mIdToPinItem.value(id)); @@ -405,17 +407,16 @@ namespace hal continue; auto pinGroupName = QString::fromStdString(pinGroup->get_name()); - PortTreeItem* pinGroupItem = new PortTreeItem(PortTreeItem::Group,pinGroupName, pinGroup->get_direction(), pinGroup->get_type()); - pinGroupItem->setId(pinGroup->get_id()); + PortTreeItem* pinGroupItem = new PortTreeItem(PortTreeItem::Group, pinGroup->get_id(), pinGroupName, pinGroup->get_direction(), pinGroup->get_type()); mIdToGroupItem.insert(pinGroup->get_id(), pinGroupItem); for(ModulePin* pin : pinGroup->get_pins()) { PortTreeItem* pinItem = new PortTreeItem(PortTreeItem::Pin, + pin->get_id(), QString::fromStdString(pin->get_name()), pin->get_direction(), pin->get_type(), QString::fromStdString(pin->get_net()->get_name())); - pinItem->setId(pin->get_id()); pinGroupItem->appendChild(pinItem); mNameToTreeItem.insert(QString::fromStdString(pin->get_name()), pinItem); mIdToPinItem.insert(pin->get_id(), pinItem); @@ -559,9 +560,11 @@ namespace hal case PinEvent::GroupCreate: { ptiGroup = new PortTreeItem(PortTreeItem::Group, + pgroup->get_id(), QString::fromStdString(pgroup->get_name()), pgroup->get_direction(), pgroup->get_type()); + mIdToGroupItem.insert(ptiGroup->id(), ptiGroup); int inx = pinGroupIndex(m,pgroup); insertItem(ptiGroup, mRootItem, inx); break; @@ -600,11 +603,12 @@ namespace hal if (pin->get_net()) netName = QString::fromStdString(pin->get_net()->get_name()); ptiPin = new PortTreeItem(PortTreeItem::Pin, + pin->get_id(), QString::fromStdString(pin->get_name()), pin->get_direction(), pin->get_type(), netName); - + mIdToPinItem.insert(ptiPin->id(), ptiPin); insertItem(ptiPin, ptiGroup, pinRow); break; } @@ -726,39 +730,10 @@ namespace hal auto pinToMove = mModule->get_pin_by_id(droppedPin->id()); if (!pinToMove) return; - QString groupName = QString::fromStdString(pinToMove->get_name()); - QString baseName = groupName; - int cnt = 2; - while (mModule->get_pin_group_by_name(groupName.toStdString())) - // pin group name already exists - groupName = QString("%1_%2").arg(baseName).arg(cnt++); -/* - ActionPingroup* actMovePin = new ActionPingroup(pinToMove->get_id(),0,0,groupName); - actMovePin->setObject(UserActionObject(mModuleId, UserActionObjectType::Module)); - bool ok = actMovePin->exec(); - if (ok && actMovePin->targetGroupId()) - { - ActionPingroup* actMoveGroup = new ActionPingroup(PinAction::MoveGroup,actMovePin->targetGroupId()); - actMoveGroup->setObject(UserActionObject(mModuleId, UserActionObjectType::Module)); - actMoveGroup->setPinOrderNo(row); - actMoveGroup->exec(); - - auto newGroup = mModule->get_pin_by_id(getIdOfItem(droppedPin))->get_group().first; - auto pinGroupName = QString::fromStdString(newGroup->get_name()); - auto pinGroupDirection = QString::fromStdString(enum_to_string((newGroup->get_direction()))); - auto pinGroupType = QString::fromStdString(enum_to_string(newGroup->get_type())); + QString groupName = generateGroupName(mModule,pinToMove); - PortTreeItem* pinGroupItem = new PortTreeItem(pinGroupName, pinGroupDirection, pinGroupType, ""); - pinGroupItem->setAdditionalData(keyType, QVariant::fromValue(itemType::group)); - pinGroupItem->setAdditionalData(keyId, newGroup->get_id()); - - int pos = mRootItem->getChildCount(); // or query current index - - insertItem(pinGroupItem, mRootItem, pos); - removeItem(droppedPin); - insertItem(droppedPin, pinGroupItem, 0); - } - */ + ActionPingroup* act = ActionPingroup::addPinToNewGroup(mModule, groupName, droppedPin->id(),row); + act->exec(); } void ModulePinsTreeModel::insertItem(PortTreeItem* item, BaseTreeItem* parent, int index) diff --git a/plugins/gui/src/user_action/action_pingroup.cpp b/plugins/gui/src/user_action/action_pingroup.cpp index 3eb268f00c9..903102888b9 100644 --- a/plugins/gui/src/user_action/action_pingroup.cpp +++ b/plugins/gui/src/user_action/action_pingroup.cpp @@ -52,6 +52,19 @@ namespace hal return pg.first->get_start_index() - row; } + QString generateGroupName(const Module* mod, const ModulePin* pin) + { + QString baseName = QString::fromStdString(pin->get_name()); + QSet existingGroups; + for (auto g : mod->get_pin_groups()) + existingGroups.insert(QString::fromStdString(g->get_name())); + QString retval = baseName; + int count = 1; + while (existingGroups.contains(retval)) + retval = QString("%1_%2").arg(baseName).arg(++count); + return retval; + } + QString PinActionType::toString(PinActionType::Type tp) { QMetaEnum me = QMetaEnum::fromType(); @@ -412,7 +425,7 @@ namespace hal return UserAction::exec(); } - ActionPingroup* ActionPingroup::addPinsToExistingGroup(const Module *m, u32 grpId, QList pinIds, int irow) + ActionPingroup* ActionPingroup::addPinsToExistingGroup(const Module *m, u32 grpId, QList pinIds, int pinRow) { ActionPingroup* retval = nullptr; for (u32 pinId : pinIds) @@ -421,21 +434,21 @@ namespace hal retval->mPinActions.append(AtomicAction(PinActionType::PinAsignGroup,pinId,"",grpId)); else retval = new ActionPingroup(PinActionType::PinAsignGroup,pinId,"",grpId); - if (irow >= 0) - retval->mPinActions.append(AtomicAction(PinActionType::PinSetindex,pinId,"",irow++)); + if (pinRow >= 0) + retval->mPinActions.append(AtomicAction(PinActionType::PinSetindex,pinId,"",pinRow++)); } retval->setObject(UserActionObject(m->get_id(),UserActionObjectType::Module)); return retval; } - ActionPingroup* ActionPingroup::addPinToExistingGroup(const Module* m, u32 grpId, u32 pinId, int irow) + ActionPingroup* ActionPingroup::addPinToExistingGroup(const Module* m, u32 grpId, u32 pinId, int pinRow) { QList pinIds; pinIds << pinId; - return addPinsToExistingGroup(m,grpId,pinIds,irow); + return addPinsToExistingGroup(m,grpId,pinIds,pinRow); } - ActionPingroup* ActionPingroup::addPinsToNewGroup(const Module* m, const QString& name, QList pinIds) + ActionPingroup* ActionPingroup::addPinsToNewGroup(const Module* m, const QString& name, QList pinIds, int grpRow) { static int vid = -9; ActionPingroup* retval = new ActionPingroup(PinActionType::GroupCreate,vid,name); @@ -450,15 +463,18 @@ namespace hal } for (u32 pinId : pinIds) retval->mPinActions.append(AtomicAction(PinActionType::PinAsignGroup,pinId,"",vid)); + + if (grpRow >= 0) + retval->mPinActions.append(AtomicAction(PinActionType::GroupMove,vid,"",grpRow)); retval->setObject(UserActionObject(m->get_id(),UserActionObjectType::Module)); return retval; } - ActionPingroup* ActionPingroup::addPinToNewGroup(const Module *m, const QString& name, u32 pinId) + ActionPingroup* ActionPingroup::addPinToNewGroup(const Module *m, const QString& name, u32 pinId, int grpRow) { QList pinIds; pinIds << pinId; - return addPinsToNewGroup(m,name,pinIds); + return addPinsToNewGroup(m,name,pinIds, grpRow); } ActionPingroup* ActionPingroup::removePinsFromGroup(const Module* m, QList pinIds)