Skip to content

Commit

Permalink
Feature: drag from tree view into python editor
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Sep 13, 2024
1 parent ed1bf20 commit 62dc03d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 14 deletions.
2 changes: 2 additions & 0 deletions plugins/gui/include/gui/module_model/module_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ namespace hal
*/
void clear() override;

QMimeData* mimeData(const QModelIndexList &indexes) const override;

/**
* Clears current tree item model and repopulates it with new ModuleItems for the netlist elements
* specified in the parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ namespace hal
void numberOfPortsChanged(const int newNumber);

private:
int mModuleId; // perhaps remove?
Module* mModule;
//name is (hopefully) enough to identify
QMap<QString, BaseTreeItem*> mNameToTreeItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,24 @@ namespace hal
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
QString moveType;
QString moveText;
int id;
if (item->isDirectory())
{
id = item->directory()->id();
moveType = "dir";
moveText = QString("gui.View.setCurrentDirectory(%1)").arg(id);
}
else if (item->isContext())
{
id = item->context()->id();
moveType = "view";
moveText = QString("gui.View.getName(%1)").arg(id);
}
else
Q_ASSERT (1==0);
stream << moveType << id << row << (quintptr) parentItem;
retval->setText(moveType);
retval->setText(moveText);
retval->setData("contexttreemodel/item", encodedData);
return retval;

Expand Down
54 changes: 53 additions & 1 deletion plugins/gui/src/module_model/module_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include "gui/gui_globals.h"

#include "gui/selection_details_widget/selection_details_icon_provider.h"
#include "gui/python/py_code_provider.h"

#include "hal_core/netlist/gate.h"
#include "hal_core/netlist/net.h"
#include <QMimeData>

namespace hal
{
Expand Down Expand Up @@ -81,12 +83,62 @@ namespace hal
return QVariant();
}

QMimeData* ModuleModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData* retval = new QMimeData;
// only single row allowed
int row = -1;
for (const QModelIndex& inx : indexes)
{
if (row < 0)
row = inx.row();
else if (row != inx.row())
return retval;
}
if (row < 0)
return retval;

QModelIndex firstIndex = indexes.at(0);
BaseTreeItem* bti = getItemFromIndex(firstIndex);
row = firstIndex.row();
BaseTreeItem* parentItem = bti->getParent();
ModuleItem* item = dynamic_cast<ModuleItem*>(bti);
if (!item)
{
qDebug() << "cannot cast" << indexes.at(0);
return retval;
}
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
QString moveText;
int id = item->id();

switch (item->getType())
{
case ModuleItem::TreeItemType::Module:
moveText = PyCodeProvider::pyCodeModule(id);
break;
case ModuleItem::TreeItemType::Gate:
moveText = PyCodeProvider::pyCodeGate(id);
break;
case ModuleItem::TreeItemType::Net:
moveText = PyCodeProvider::pyCodeNet(id);
break;
}

stream << item->getType() << id << row << (quintptr) parentItem;
retval->setText(moveText);
retval->setData("modulemodel/item", encodedData);
return retval;

}

Qt::ItemFlags ModuleModel::flags(const QModelIndex& index) const
{
if (!index.isValid())
return 0;

return QAbstractItemModel::flags(index);
return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled;
}

ModuleItem* ModuleModel::getItem(const QModelIndex& index) const
Expand Down
2 changes: 2 additions & 0 deletions plugins/gui/src/module_widget/module_tree_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace hal
ModuleTreeView::ModuleTreeView(QWidget *parent)
: QTreeView(parent), mToggleStateExpanded(true)
{
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
}

void ModuleTreeView::expandAllModules()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "gui/basic_tree_model/base_tree_item.h"
#include "gui/gui_globals.h"
#include "gui/input_dialog/input_dialog.h"
#include "gui/python/py_code_provider.h"
#include "gui/user_action/action_pingroup.h"
#include "gui/user_action/action_remove_items_from_object.h"
#include "gui/user_action/user_action_compound.h"
Expand Down Expand Up @@ -140,7 +141,9 @@ namespace hal
QDataStream stream(&encodedData, QIODevice::WriteOnly);
QString type = item->itemType() == PortTreeItem::Pin ? "pin" : "group";
stream << type << item->id();
data->setText(item->getData(sNameColumn).toString());
data->setText(item->itemType() == PortTreeItem::Pin
? PyCodeProvider::pyCodeModulePinById(mModule->get_id(),item->id())
: PyCodeProvider::pyCodeModulePinGroup(mModule->get_id(),item->id()));
data->setData("pintreemodel/item", encodedData);
return data;
}
Expand Down Expand Up @@ -260,7 +263,6 @@ namespace hal
void ModulePinsTreeModel::clear()
{
BaseTreeModel::clear();
mModuleId = -1; // perhaps remove?
mModule = nullptr;
mNameToTreeItem.clear();
mIdToGroupItem.clear();
Expand All @@ -270,7 +272,6 @@ namespace hal
void ModulePinsTreeModel::setModule(Module* m)
{
clear();
mModuleId = m->get_id();
mModule = m;
beginResetModel();

Expand Down Expand Up @@ -354,13 +355,13 @@ namespace hal

Net* ModulePinsTreeModel::getNetFromItem(PortTreeItem* item)
{
if (mModuleId == -1) //no current module = no represented net
if (!mModule) //no current module = no represented net
return nullptr;

if (item->itemType() == PortTreeItem::Group && item->getChildCount() > 1)
return nullptr;

Module* m = gNetlist->get_module_by_id(mModuleId);
Module* m = gNetlist->get_module_by_id(mModule->get_id());
if (!m)
return nullptr;

Expand All @@ -375,7 +376,8 @@ namespace hal

int ModulePinsTreeModel::getRepresentedModuleId()
{
return mModuleId;
if (!mModule) return -1;
return mModule->get_id();
}

void ModulePinsTreeModel::handleModulePortsChanged(Module* m, PinEvent pev, u32 pgid)
Expand All @@ -388,7 +390,7 @@ namespace hal
PinEvent::GroupDelete};
Q_UNUSED(pev);
Q_UNUSED(pgid);
if ((int)m->get_id() != mModuleId) return;
if (m != mModule) return;

// debug pingroups log_info("gui", "Handle pin_changed event {} ID={}", enum_to_string<PinEvent>(pev), pgid);
PortTreeItem* ptiPin = nullptr;
Expand Down Expand Up @@ -565,7 +567,7 @@ namespace hal
auto tgtgroup = mModule->get_pin_group_by_id(static_cast<PortTreeItem*>(onDroppedGroup)->id());

ActionPingroup* act = ActionPingroup::addPinsToExistingGroup(mModule,tgtgroup->get_id(),pins);
act->setObject(UserActionObject(mModuleId,UserActionObjectType::Module));
act->setObject(UserActionObject(mModule->get_id(),UserActionObjectType::Module));
if (act) act->exec();

// too keep the order, ActionAddItemsToObject cannot be executed with all pins, but a ComAction must be created
Expand All @@ -579,14 +581,14 @@ namespace hal
auto desiredIdx = bottomEdge ? row-1 : row;
if(ownRow < row && !bottomEdge) desiredIdx--;
ActionPingroup* act = new ActionPingroup(PinActionType::GroupMoveToRow,droppedGroup->id(),"",desiredIdx);
act->setObject(UserActionObject(mModuleId,UserActionObjectType::Module));
act->setObject(UserActionObject(mModule->get_id(),UserActionObjectType::Module));
act->exec();
}

void ModulePinsTreeModel::dndPinOnGroup(PortTreeItem *droppedPin, BaseTreeItem *onDroppedGroup)
{
ActionPingroup* act = new ActionPingroup(PinActionType::PinAsignToGroup,droppedPin->id(),"",static_cast<PortTreeItem*>(onDroppedGroup)->id());
act->setObject(UserActionObject(mModuleId,UserActionObjectType::Module));
act->setObject(UserActionObject(mModule->get_id(),UserActionObjectType::Module));
act->exec();
}

Expand All @@ -607,7 +609,7 @@ namespace hal
act = ActionPingroup::addPinToExistingGroup(mModule,static_cast<PortTreeItem*>(onDroppedParent)->id(),droppedPin->id(),desiredIdx);
if (!act) return;
}
act->setObject(UserActionObject(mModuleId,UserActionObjectType::Module));
act->setObject(UserActionObject(mModule->get_id(),UserActionObjectType::Module));
act->exec();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace hal
mIsGrouping = isGrouping;

setContextMenuPolicy(Qt::CustomContextMenu);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
connect(this, &QTreeView::customContextMenuRequested, this, &SelectionTreeView::handleCustomContextMenuRequested);

connect(this, &SelectionTreeView::itemDoubleClicked, this, &SelectionTreeView::handleTreeViewItemFocusClicked);
Expand Down

0 comments on commit 62dc03d

Please sign in to comment.