Get initial tree control drag and drop working. Create a new type for tree item data, as it needs to be serialisable for drag and drop, and void* is not.

This commit is contained in:
Jay Nabonne 2019-02-07 09:08:08 +00:00
parent e8d39f3b5c
commit d9080cd791

View File

@ -15,6 +15,34 @@
namespace
{
struct TreeItemDataQt
{
TreeItemDataQt() : data(NULL)
{
}
TreeItemDataQt(wxTreeItemData* data) : data(data)
{
if ( !registered )
{
qRegisterMetaTypeStreamOperators<TreeItemDataQt>("TreeItemDataQt");
registered = true;
}
}
wxTreeItemData *data;
static bool registered;
};
bool TreeItemDataQt::registered = false;
Q_DECLARE_METATYPE(TreeItemDataQt)
QDataStream &operator<<(QDataStream &out, const TreeItemDataQt &myObj)
{
return out;
}
QDataStream &operator>>(QDataStream &in, TreeItemDataQt &myObj)
{
return in;
}
QTreeWidgetItem *wxQtConvertTreeItem(const wxTreeItemId &item)
{
return static_cast<QTreeWidgetItem*>(item.GetID());
@ -36,6 +64,10 @@ namespace
connect(this, &QTreeWidget::itemClicked, this, &wxQTreeWidget::OnItemClicked);
connect(this, &QTreeWidget::itemCollapsed, this, &wxQTreeWidget::OnItemCollapsed);
connect(this, &QTreeWidget::itemExpanded, this, &wxQTreeWidget::OnItemExpanded);
setDragEnabled(true);
viewport()->setAcceptDrops(true);
setDropIndicatorShown(true);
}
private:
@ -112,6 +144,50 @@ namespace
EmitEvent(expandedEvent);
}
virtual void dragEnterEvent(QDragEnterEvent* event) wxOVERRIDE
{
wxEventType command = (event->mouseButtons() & Qt::RightButton)
? wxEVT_TREE_BEGIN_RDRAG
: wxEVT_TREE_BEGIN_DRAG;
QTreeWidgetItem *hitItem = itemAt(event->pos());
wxTreeEvent tree_event(
command,
GetHandler(),
wxQtConvertTreeItem(hitItem)
);
tree_event.SetPoint(wxQtConvertPoint(event->pos()));
// Vetoed unless explicitly accepted.
tree_event.Veto();
EmitEvent(tree_event);
if ( tree_event.IsAllowed() )
{
event->accept();
}
}
virtual void dropEvent(QDropEvent* event) wxOVERRIDE
{
const wxPoint pos = wxQtConvertPoint(event->pos());
QTreeWidgetItem *hitItem = itemAt(event->pos());
wxTreeEvent tree_event(
wxEVT_TREE_END_DRAG,
GetHandler(),
wxQtConvertTreeItem(hitItem)
);
tree_event.SetPoint(wxQtConvertPoint(event->pos()));
EmitEvent(tree_event);
}
};
@ -138,7 +214,6 @@ namespace
return QIcon::Selected;
}
wxFAIL_MSG("Unspported tree icon state");
return QIcon::Normal;
}
@ -229,8 +304,8 @@ wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
QTreeWidgetItem* qTreeItem = wxQtConvertTreeItem(item);
QVariant itemData = qTreeItem->data(0, Qt::UserRole);
void* value = itemData.value<void *>();
return static_cast<wxTreeItemData*>(value);
TreeItemDataQt value = itemData.value<TreeItemDataQt>();
return value.data;
}
wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
@ -239,7 +314,6 @@ wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
QTreeWidgetItem* qTreeItem = wxQtConvertTreeItem(item);
return wxQtConvertColour(qTreeItem->textColor(0));
}
wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
@ -291,7 +365,8 @@ void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
data->SetId(item);
QTreeWidgetItem *qTreeItem = wxQtConvertTreeItem(item);
qTreeItem->setData(0, Qt::UserRole, QVariant::fromValue(static_cast<void*>(data)));
TreeItemDataQt treeItemData(data);
qTreeItem->setData(0, Qt::UserRole, QVariant::fromValue(treeItemData));
}
void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
@ -829,7 +904,9 @@ wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
QTreeWidgetItem *newItem = new QTreeWidgetItem;
newItem->setText(0, wxQtConvertString(text));
newItem->setData(0, Qt::UserRole, QVariant::fromValue(static_cast<void*>(data)));
TreeItemDataQt treeItemData(data);
newItem->setData(0, Qt::UserRole, QVariant::fromValue(treeItemData));
if (pos == static_cast<size_t>(-1))
{