From 57348b4a5914e217446dafcaac5db0906a4e95b3 Mon Sep 17 00:00:00 2001 From: Graham Dawes Date: Wed, 6 Feb 2019 09:20:13 +0000 Subject: [PATCH] Add EditControl support for wxTreeCtrl under wxQT --- include/wx/qt/private/treeitemfactory.h | 8 + include/wx/qt/treectrl.h | 4 +- src/qt/treectrl.cpp | 205 +++++++++++++----------- 3 files changed, 120 insertions(+), 97 deletions(-) diff --git a/include/wx/qt/private/treeitemfactory.h b/include/wx/qt/private/treeitemfactory.h index c2c91c34fa..886af1e0a5 100644 --- a/include/wx/qt/private/treeitemfactory.h +++ b/include/wx/qt/private/treeitemfactory.h @@ -2,6 +2,8 @@ #define _WX_TREEITEM_FACTORY_H_ #include +#include +#include #include "wx/recguard.h" #include "wx/textctrl.h" @@ -71,6 +73,12 @@ public: { } + void AttachTo(QTreeWidget *tree) + { + QItemDelegate *qItemDelegate = static_cast(tree->itemDelegate()); + qItemDelegate->setItemEditorFactory(this); + } + QWidget* createEditor(int WXUNUSED(userType), QWidget* parent) const wxOVERRIDE { m_textCtrl = new wxQtListTextCtrl(m_parent, parent); diff --git a/include/wx/qt/treectrl.h b/include/wx/qt/treectrl.h index e256e768af..47c3b1db5b 100644 --- a/include/wx/qt/treectrl.h +++ b/include/wx/qt/treectrl.h @@ -8,7 +8,7 @@ #ifndef _WX_QT_TREECTRL_H_ #define _WX_QT_TREECTRL_H_ -class QTreeWidget; +class wxQTreeWidget; class WXDLLIMPEXP_CORE wxTreeCtrl : public wxTreeCtrlBase { @@ -140,7 +140,7 @@ private: void SendDeleteEvent(const wxTreeItemId &item); wxTreeItemId GetNext(const wxTreeItemId &item) const; - QTreeWidget *m_qtTreeWidget; + wxQTreeWidget *m_qtTreeWidget; wxDECLARE_DYNAMIC_CLASS(wxTreeCtrl); }; diff --git a/src/qt/treectrl.cpp b/src/qt/treectrl.cpp index 4e12939906..3db395ac13 100644 --- a/src/qt/treectrl.cpp +++ b/src/qt/treectrl.cpp @@ -8,9 +8,11 @@ // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #include "wx/treectrl.h" -#include "wx/qt/private/winevent.h" #include "wx/imaglist.h" +#include "wx/qt/private/winevent.h" +#include "wx/qt/private/treeitemfactory.h" + #include namespace @@ -25,102 +27,12 @@ namespace return wxTreeItemId(item); } - class wxQTreeWidget : public wxQtEventSignalHandler - { - public: - wxQTreeWidget(wxWindow *parent, wxTreeCtrl *handler) : - wxQtEventSignalHandler(parent, handler) - { - connect(this, &QTreeWidget::currentItemChanged, this, &wxQTreeWidget::OnCurrentItemChanged); - connect(this, &QTreeWidget::itemActivated, this, &wxQTreeWidget::OnItemActivated); - connect(this, &QTreeWidget::itemClicked, this, &wxQTreeWidget::OnItemClicked); - connect(this, &QTreeWidget::itemCollapsed, this, &wxQTreeWidget::OnItemCollapsed); - connect(this, &QTreeWidget::itemExpanded, this, &wxQTreeWidget::OnItemExpanded); - } - - private: - void OnCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) - { - wxTreeEvent changingEvent(wxEVT_TREE_SEL_CHANGING, GetHandler(), wxQtConvertTreeItem(current)); - EmitEvent(changingEvent); - - if ( !changingEvent.IsAllowed() ) - { - blockSignals(true); - setCurrentItem(previous); - blockSignals(false); - return; - } - - wxTreeEvent changedEvent(wxEVT_TREE_SEL_CHANGED, GetHandler(), wxQtConvertTreeItem(current)); - EmitEvent(changedEvent); - } - - void OnItemActivated(QTreeWidgetItem *item, int WXUNUSED(column)) - { - wxTreeEvent event(wxEVT_TREE_ITEM_ACTIVATED, GetHandler(), wxQtConvertTreeItem(item)); - EmitEvent(event); - } - - void OnItemClicked(QTreeWidgetItem *item) - { - wxMouseState mouseState = wxGetMouseState(); - - wxEventType eventType; - if (mouseState.RightIsDown()) - eventType = wxEVT_TREE_ITEM_RIGHT_CLICK; - else if (mouseState.MiddleIsDown()) - eventType = wxEVT_TREE_ITEM_MIDDLE_CLICK; - else - return; - - wxTreeEvent event(eventType, GetHandler(), wxQtConvertTreeItem(item)); - EmitEvent(event); - } - - void OnItemCollapsed(QTreeWidgetItem *item) - { - wxTreeEvent collapsingEvent(wxEVT_TREE_ITEM_COLLAPSING, GetHandler(), wxQtConvertTreeItem(item)); - EmitEvent(collapsingEvent); - - if (!collapsingEvent.IsAllowed()) - { - blockSignals(true); - item->setExpanded(true); - blockSignals(false); - return; - } - - wxTreeEvent collapsedEvent(wxEVT_TREE_ITEM_COLLAPSED, GetHandler(), wxQtConvertTreeItem(item)); - EmitEvent(collapsedEvent); - } - - void OnItemExpanded(QTreeWidgetItem *item) - { - wxTreeEvent expandingEvent(wxEVT_TREE_ITEM_EXPANDING, GetHandler(), wxQtConvertTreeItem(item)); - EmitEvent(expandingEvent); - - if (!expandingEvent.IsAllowed()) - { - blockSignals(true); - item->setExpanded(false); - blockSignals(false); - return; - } - - wxTreeEvent expandedEvent(wxEVT_TREE_ITEM_EXPANDED, GetHandler(), wxQtConvertTreeItem(item)); - EmitEvent(expandedEvent); - - } - }; - - size_t CountChildren(QTreeWidgetItem *item) { const int currentCount = item->childCount(); size_t totalCount = currentCount; - for(int i = 0; i < currentCount; ++i) + for (int i = 0; i < currentCount; ++i) { totalCount += CountChildren(item->child(i)); } @@ -141,9 +53,107 @@ namespace wxFAIL_MSG("Unspported tree icon state"); return QIcon::Normal; } - } +class wxQTreeWidget : public wxQtEventSignalHandler +{ +public: + wxQTreeWidget(wxWindow *parent, wxTreeCtrl *handler) : + wxQtEventSignalHandler(parent, handler), + m_editorFactory(handler) + { + connect(this, &QTreeWidget::currentItemChanged, this, &wxQTreeWidget::OnCurrentItemChanged); + connect(this, &QTreeWidget::itemActivated, this, &wxQTreeWidget::OnItemActivated); + connect(this, &QTreeWidget::itemClicked, this, &wxQTreeWidget::OnItemClicked); + connect(this, &QTreeWidget::itemCollapsed, this, &wxQTreeWidget::OnItemCollapsed); + connect(this, &QTreeWidget::itemExpanded, this, &wxQTreeWidget::OnItemExpanded); + + m_editorFactory.AttachTo(this); + } + + wxTextCtrl *GetEditControl() + { + return m_editorFactory.GetEditControl(); + } + +private: + void OnCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) + { + wxTreeEvent changingEvent(wxEVT_TREE_SEL_CHANGING, GetHandler(), wxQtConvertTreeItem(current)); + EmitEvent(changingEvent); + + if ( !changingEvent.IsAllowed() ) + { + blockSignals(true); + setCurrentItem(previous); + blockSignals(false); + return; + } + + wxTreeEvent changedEvent(wxEVT_TREE_SEL_CHANGED, GetHandler(), wxQtConvertTreeItem(current)); + EmitEvent(changedEvent); + } + + void OnItemActivated(QTreeWidgetItem *item, int WXUNUSED(column)) + { + wxTreeEvent event(wxEVT_TREE_ITEM_ACTIVATED, GetHandler(), wxQtConvertTreeItem(item)); + EmitEvent(event); + } + + void OnItemClicked(QTreeWidgetItem *item) + { + wxMouseState mouseState = wxGetMouseState(); + + wxEventType eventType; + if ( mouseState.RightIsDown() ) + eventType = wxEVT_TREE_ITEM_RIGHT_CLICK; + else if ( mouseState.MiddleIsDown() ) + eventType = wxEVT_TREE_ITEM_MIDDLE_CLICK; + else + return; + + wxTreeEvent event(eventType, GetHandler(), wxQtConvertTreeItem(item)); + EmitEvent(event); + } + + void OnItemCollapsed(QTreeWidgetItem *item) + { + wxTreeEvent collapsingEvent(wxEVT_TREE_ITEM_COLLAPSING, GetHandler(), wxQtConvertTreeItem(item)); + EmitEvent(collapsingEvent); + + if ( !collapsingEvent.IsAllowed() ) + { + blockSignals(true); + item->setExpanded(true); + blockSignals(false); + return; + } + + wxTreeEvent collapsedEvent(wxEVT_TREE_ITEM_COLLAPSED, GetHandler(), wxQtConvertTreeItem(item)); + EmitEvent(collapsedEvent); + } + + void OnItemExpanded(QTreeWidgetItem *item) + { + wxTreeEvent expandingEvent(wxEVT_TREE_ITEM_EXPANDING, GetHandler(), wxQtConvertTreeItem(item)); + EmitEvent(expandingEvent); + + if ( !expandingEvent.IsAllowed() ) + { + blockSignals(true); + item->setExpanded(false); + blockSignals(false); + return; + } + + wxTreeEvent expandedEvent(wxEVT_TREE_ITEM_EXPANDED, GetHandler(), wxQtConvertTreeItem(item)); + EmitEvent(expandedEvent); + + } + + wxQtTreeItemEditorFactory m_editorFactory; +}; + wxTreeCtrl::wxTreeCtrl() : m_qtTreeWidget(NULL) { @@ -772,17 +782,22 @@ void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item, wxClassInfo* textCtrlClass) { wxCHECK_MSG(item.IsOk(), NULL, "invalid tree item"); - return NULL; + + QTreeWidgetItem *qTreeItem = wxQtConvertTreeItem(item); + m_qtTreeWidget->openPersistentEditor(qTreeItem); + return m_qtTreeWidget->GetEditControl(); } wxTextCtrl *wxTreeCtrl::GetEditControl() const { - return NULL; + return m_qtTreeWidget->GetEditControl(); } void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) { wxCHECK_RET(item.IsOk(), "invalid tree item"); + QTreeWidgetItem *qTreeItem = wxQtConvertTreeItem(item); + m_qtTreeWidget->closePersistentEditor(qTreeItem); } void wxTreeCtrl::SortChildren(const wxTreeItemId& item)