From d9080cd791aa3501c1290be301c29996d44f210a Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Thu, 7 Feb 2019 09:08:08 +0000 Subject: [PATCH] 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. --- src/qt/treectrl.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/src/qt/treectrl.cpp b/src/qt/treectrl.cpp index 4e12939906..6c792050a0 100644 --- a/src/qt/treectrl.cpp +++ b/src/qt/treectrl.cpp @@ -15,6 +15,34 @@ namespace { + struct TreeItemDataQt + { + TreeItemDataQt() : data(NULL) + { + } + TreeItemDataQt(wxTreeItemData* data) : data(data) + { + if ( !registered ) + { + qRegisterMetaTypeStreamOperators("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(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(); - return static_cast(value); + TreeItemDataQt value = itemData.value(); + 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(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(data))); + TreeItemDataQt treeItemData(data); + + newItem->setData(0, Qt::UserRole, QVariant::fromValue(treeItemData)); if (pos == static_cast(-1)) {