Add EditControl support for wxTreeCtrl under wxQT

This commit is contained in:
Graham Dawes 2019-02-06 09:20:13 +00:00
parent cfe36f7ae7
commit 57348b4a59
3 changed files with 120 additions and 97 deletions

View File

@ -2,6 +2,8 @@
#define _WX_TREEITEM_FACTORY_H_
#include <QtWidgets/QItemEditorFactory>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QItemDelegate>
#include "wx/recguard.h"
#include "wx/textctrl.h"
@ -71,6 +73,12 @@ public:
{
}
void AttachTo(QTreeWidget *tree)
{
QItemDelegate *qItemDelegate = static_cast<QItemDelegate*>(tree->itemDelegate());
qItemDelegate->setItemEditorFactory(this);
}
QWidget* createEditor(int WXUNUSED(userType), QWidget* parent) const wxOVERRIDE
{
m_textCtrl = new wxQtListTextCtrl(m_parent, parent);

View File

@ -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);
};

View File

@ -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 <QtWidgets/QTreeWidget>
namespace
@ -25,102 +27,12 @@ namespace
return wxTreeItemId(item);
}
class wxQTreeWidget : public wxQtEventSignalHandler<QTreeWidget, wxTreeCtrl>
{
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<QTreeWidget, wxTreeCtrl>
{
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)