Generate events when selecting tree items programmatically

In wx API, calling wxTreeCtrl::SelectItem() is supposed to generate an
event.

Also use Qt selection model to support single-selection mode.

Closes https://github.com/wxWidgets/wxWidgets/pull/1453
This commit is contained in:
Graham Dawes 2019-07-30 16:20:00 +01:00 committed by Vadim Zeitlin
parent c8eef72417
commit 39cb337008

View File

@ -244,6 +244,12 @@ public:
return m_placeHolderImage;
}
void select(QTreeWidgetItem* item, QItemSelectionModel::SelectionFlag selectionFlag)
{
const QModelIndex &index = indexFromItem(item);
selectionModel()->select(index, selectionFlag);
}
protected:
virtual void drawRow(
QPainter *painter,
@ -1164,19 +1170,13 @@ void wxTreeCtrl::Toggle(const wxTreeItemId& item)
void wxTreeCtrl::Unselect()
{
QTreeWidgetItem *current = m_qtTreeWidget->currentItem();
if ( current != NULL )
current->setSelected(false);
m_qtTreeWidget->select(current, QItemSelectionModel::Deselect);
}
void wxTreeCtrl::UnselectAll()
{
QList<QTreeWidgetItem *> selections = m_qtTreeWidget->selectedItems();
const size_t selectedCount = selections.size();
for ( size_t i = 0; i < selectedCount; ++i)
{
selections[i]->setSelected(false);
}
m_qtTreeWidget->selectionModel()->clearSelection();
}
void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
@ -1189,7 +1189,15 @@ void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
}
QTreeWidgetItem *qTreeItem = wxQtConvertTreeItem(item);
qTreeItem->setSelected(select);
if ( qTreeItem )
{
m_qtTreeWidget->select(qTreeItem, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
if ( select && m_qtTreeWidget->selectionMode() == QTreeWidget::SingleSelection )
{
m_qtTreeWidget->setCurrentItem(qTreeItem);
}
}
}
void wxTreeCtrl::SelectChildren(const wxTreeItemId& parent)
@ -1201,7 +1209,7 @@ void wxTreeCtrl::SelectChildren(const wxTreeItemId& parent)
for ( int i = 0; i < childCount; ++i )
{
qTreeItem->child(i)->setSelected(true);
m_qtTreeWidget->select(qTreeItem->child(i), QItemSelectionModel::Select);
}
}