Merge branch 'qt_listbox_failing_tests'

Fixes for item selection and cleanup.

See https://github.com/wxWidgets/wxWidgets/pull/1159
This commit is contained in:
Vadim Zeitlin
2019-01-21 23:43:56 +01:00
2 changed files with 51 additions and 21 deletions

View File

@@ -49,8 +49,8 @@ public:
const wxValidator& validator = wxDefaultValidator, const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxListBoxNameStr); const wxString& name = wxListBoxNameStr);
virtual bool IsSelected(int n) const; virtual bool IsSelected(int n) const wxOVERRIDE;
virtual int GetSelections(wxArrayInt& aSelections) const; virtual int GetSelections(wxArrayInt& aSelections) const wxOVERRIDE;
virtual unsigned int GetCount() const; virtual unsigned int GetCount() const;
virtual wxString GetString(unsigned int n) const; virtual wxString GetString(unsigned int n) const;
@@ -63,9 +63,9 @@ public:
void QtSendEvent(wxEventType evtType, const QModelIndex &index, bool selected); void QtSendEvent(wxEventType evtType, const QModelIndex &index, bool selected);
protected: protected:
virtual void DoSetFirstItem(int n); virtual void DoSetFirstItem(int n) wxOVERRIDE;
virtual void DoSetSelection(int n, bool select); virtual void DoSetSelection(int n, bool select) wxOVERRIDE;
virtual int DoInsertItems(const wxArrayStringsAdapter & items, virtual int DoInsertItems(const wxArrayStringsAdapter & items,
unsigned int pos, unsigned int pos,
@@ -90,6 +90,9 @@ protected:
private: private:
virtual void Init(); //common construction virtual void Init(); //common construction
// Common part of both Create() overloads.
void DoCreate(wxWindow* parent, long style);
void UnSelectAll(); void UnSelectAll();
wxDECLARE_DYNAMIC_CLASS(wxListBox); wxDECLARE_DYNAMIC_CLASS(wxListBox);

View File

@@ -86,14 +86,9 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id,
const wxValidator& validator, const wxValidator& validator,
const wxString& name) const wxString& name)
{ {
Init(); DoCreate(parent, style);
QListWidgetItem* item;
m_qtWindow = m_qtListWidget = new wxQtListWidget( parent, this );
if ( style == wxLB_SORT ) QListWidgetItem* item;
{
m_qtListWidget->setSortingEnabled(true);
}
while ( n-- > 0 ) while ( n-- > 0 )
{ {
@@ -117,8 +112,7 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id,
const wxValidator& validator, const wxValidator& validator,
const wxString& name) const wxString& name)
{ {
Init(); DoCreate(parent, style);
m_qtWindow = m_qtListWidget = new wxQtListWidget( parent, this );
QStringList items; QStringList items;
@@ -130,6 +124,33 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id,
return wxListBoxBase::Create( parent, id, pos, size, style, validator, name ); return wxListBoxBase::Create( parent, id, pos, size, style, validator, name );
} }
void wxListBox::DoCreate(wxWindow* parent, long style)
{
Init();
m_qtWindow =
m_qtListWidget = new wxQtListWidget( parent, this );
if ( style & wxLB_SORT )
{
m_qtListWidget->setSortingEnabled(true);
}
// The following styles are mutually exclusive
if ( style & wxLB_SINGLE )
{
m_qtListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
}
else if ( style & wxLB_MULTIPLE )
{
m_qtListWidget->setSelectionMode(QAbstractItemView::MultiSelection);
}
else if ( style & wxLB_EXTENDED )
{
wxMISSING_IMPLEMENTATION( wxSTRINGIZE( wxLB_EXTENDED ));
}
}
void wxListBox::Init() void wxListBox::Init()
{ {
#if wxUSE_CHECKLISTBOX #if wxUSE_CHECKLISTBOX
@@ -147,10 +168,9 @@ int wxListBox::GetSelections(wxArrayInt& aSelections) const
{ {
aSelections.clear(); aSelections.clear();
for ( unsigned int i = 0; i < GetCount(); ++i) Q_FOREACH(QListWidgetItem* l, m_qtListWidget->selectedItems())
{ {
if ( IsSelected(i) ) aSelections.push_back( m_qtListWidget->row(l) );
aSelections.push_back(i);
} }
return aSelections.size(); return aSelections.size();
@@ -182,7 +202,15 @@ void wxListBox::SetString(unsigned int n, const wxString& s)
int wxListBox::GetSelection() const int wxListBox::GetSelection() const
{ {
return m_qtListWidget->currentIndex().row(); if ( m_qtListWidget->selectedItems().empty() )
{
return wxNOT_FOUND;
}
QListWidgetItem* item = m_qtListWidget->selectedItems().first();
return m_qtListWidget->row(item);
} }
void wxListBox::DoSetFirstItem(int WXUNUSED(n)) void wxListBox::DoSetFirstItem(int WXUNUSED(n))
@@ -197,7 +225,7 @@ void wxListBox::DoSetSelection(int n, bool select)
return; return;
} }
return m_qtListWidget->setCurrentRow(n, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect ); m_qtListWidget->setItemSelected( m_qtListWidget->item(n), select);
} }
int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
@@ -266,9 +294,8 @@ QScrollArea *wxListBox::QtGetScrollBarsContainer() const
void wxListBox::UnSelectAll() void wxListBox::UnSelectAll()
{ {
for ( unsigned int i = 0; i < GetCount(); ++i ) Q_FOREACH(QListWidgetItem* l, m_qtListWidget->selectedItems())
{ {
if ( IsSelected(i) ) m_qtListWidget->setItemSelected( l, false );
DoSetSelection(i, false);
} }
} }