From 58df1ee13f734e6f494b031da737510053bbc58f Mon Sep 17 00:00:00 2001 From: Liam Treacy Date: Mon, 21 Jan 2019 11:00:25 +0000 Subject: [PATCH 1/7] Added wxOVERRIDE specifiers to qt wxListBox methods --- include/wx/qt/listbox.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/wx/qt/listbox.h b/include/wx/qt/listbox.h index e0086cda58..0ffff6e51c 100644 --- a/include/wx/qt/listbox.h +++ b/include/wx/qt/listbox.h @@ -49,8 +49,8 @@ public: const wxValidator& validator = wxDefaultValidator, const wxString& name = wxListBoxNameStr); - virtual bool IsSelected(int n) const; - virtual int GetSelections(wxArrayInt& aSelections) const; + virtual bool IsSelected(int n) const wxOVERRIDE; + virtual int GetSelections(wxArrayInt& aSelections) const wxOVERRIDE; virtual unsigned int GetCount() const; virtual wxString GetString(unsigned int n) const; @@ -63,9 +63,9 @@ public: void QtSendEvent(wxEventType evtType, const QModelIndex &index, bool selected); 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, unsigned int pos, From eb6b660d2701254944af997da69801e867a306f3 Mon Sep 17 00:00:00 2001 From: Liam Treacy Date: Mon, 21 Jan 2019 14:04:47 +0000 Subject: [PATCH 2/7] Added setStyle method to wxListBox to allow for the proper setting of sorting and selection style of the QListWidget --- include/wx/qt/listbox.h | 1 + src/qt/listbox.cpp | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/wx/qt/listbox.h b/include/wx/qt/listbox.h index 0ffff6e51c..4b9c9c5ac1 100644 --- a/include/wx/qt/listbox.h +++ b/include/wx/qt/listbox.h @@ -90,6 +90,7 @@ protected: private: virtual void Init(); //common construction + void setStyle(long style); void UnSelectAll(); wxDECLARE_DYNAMIC_CLASS(wxListBox); diff --git a/src/qt/listbox.cpp b/src/qt/listbox.cpp index ffd6f84aa8..98595d3d7b 100644 --- a/src/qt/listbox.cpp +++ b/src/qt/listbox.cpp @@ -90,10 +90,7 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, QListWidgetItem* item; m_qtWindow = m_qtListWidget = new wxQtListWidget( parent, this ); - if ( style == wxLB_SORT ) - { - m_qtListWidget->setSortingEnabled(true); - } + setStyle(style); while ( n-- > 0 ) { @@ -120,6 +117,8 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, Init(); m_qtWindow = m_qtListWidget = new wxQtListWidget( parent, this ); + setStyle(style); + QStringList items; for (size_t i = 0; i < choices.size(); ++i) @@ -130,6 +129,27 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, return wxListBoxBase::Create( parent, id, pos, size, style, validator, name ); } +void wxListBox::setStyle(long style) +{ + if ( style & wxLB_SORT ) + { + m_qtListWidget->setSortingEnabled(true); + } + + 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() { #if wxUSE_CHECKLISTBOX From ffcda97d04fdcdacac57b05938e52df59e69c076 Mon Sep 17 00:00:00 2001 From: Liam Treacy Date: Mon, 21 Jan 2019 15:48:12 +0000 Subject: [PATCH 3/7] wxListBox GetSelection and DoSetSelection now use the proper QListWidget interface for specifying which index is selected. --- src/qt/listbox.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/qt/listbox.cpp b/src/qt/listbox.cpp index 98595d3d7b..bdac7368a8 100644 --- a/src/qt/listbox.cpp +++ b/src/qt/listbox.cpp @@ -202,7 +202,20 @@ void wxListBox::SetString(unsigned int n, const wxString& s) int wxListBox::GetSelection() const { - return m_qtListWidget->currentIndex().row(); + if ( m_qtListWidget->selectedItems().empty() ) + { + return wxNOT_FOUND; + } + + for ( unsigned int i = 0; i < GetCount(); ++i) + { + if( m_qtListWidget->item(i) == m_qtListWidget->selectedItems().first() ) + { + return i; + } + } + + return wxNOT_FOUND; } void wxListBox::DoSetFirstItem(int WXUNUSED(n)) @@ -217,7 +230,7 @@ void wxListBox::DoSetSelection(int n, bool select) 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, From ab3c460a9350207bb5d6b0ad353452b5163f4900 Mon Sep 17 00:00:00 2001 From: Liam Treacy Date: Mon, 21 Jan 2019 16:00:03 +0000 Subject: [PATCH 4/7] Updated the GetSelections method to use QListWidget method to retrieve selected items rather than iterating through the list. Also updated the UnSelectAll method to be more efficient --- src/qt/listbox.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/qt/listbox.cpp b/src/qt/listbox.cpp index bdac7368a8..6aa053b2fb 100644 --- a/src/qt/listbox.cpp +++ b/src/qt/listbox.cpp @@ -167,10 +167,9 @@ int wxListBox::GetSelections(wxArrayInt& aSelections) const { aSelections.clear(); - for ( unsigned int i = 0; i < GetCount(); ++i) + Q_FOREACH(QListWidgetItem* l, m_qtListWidget->selectedItems()) { - if ( IsSelected(i) ) - aSelections.push_back(i); + aSelections.push_back( m_qtListWidget->row(l) ); } return aSelections.size(); @@ -299,9 +298,8 @@ QScrollArea *wxListBox::QtGetScrollBarsContainer() const void wxListBox::UnSelectAll() { - for ( unsigned int i = 0; i < GetCount(); ++i ) + Q_FOREACH(QListWidgetItem* l, m_qtListWidget->selectedItems()) { - if ( IsSelected(i) ) - DoSetSelection(i, false); + m_qtListWidget->setItemSelected( l, false ); } } From d8d7f673b264795a4a7a9dee85745c5871079939 Mon Sep 17 00:00:00 2001 From: Liam Treacy Date: Mon, 21 Jan 2019 16:02:03 +0000 Subject: [PATCH 5/7] Added comment explaining style mutual exclusive flags --- src/qt/listbox.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/qt/listbox.cpp b/src/qt/listbox.cpp index 6aa053b2fb..de3a45cc2f 100644 --- a/src/qt/listbox.cpp +++ b/src/qt/listbox.cpp @@ -136,6 +136,7 @@ void wxListBox::setStyle(long style) m_qtListWidget->setSortingEnabled(true); } + // The following styles are mutually exclusive if ( style & wxLB_SINGLE ) { m_qtListWidget->setSelectionMode(QAbstractItemView::SingleSelection); From a2356fbe4a655490db4724283929f7d9a4637483 Mon Sep 17 00:00:00 2001 From: Liam Treacy Date: Mon, 21 Jan 2019 16:16:17 +0000 Subject: [PATCH 6/7] Made the wxListBox::GetSelection method more efficient --- src/qt/listbox.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/qt/listbox.cpp b/src/qt/listbox.cpp index de3a45cc2f..51a1457c49 100644 --- a/src/qt/listbox.cpp +++ b/src/qt/listbox.cpp @@ -207,15 +207,10 @@ int wxListBox::GetSelection() const return wxNOT_FOUND; } - for ( unsigned int i = 0; i < GetCount(); ++i) - { - if( m_qtListWidget->item(i) == m_qtListWidget->selectedItems().first() ) - { - return i; - } - } - return wxNOT_FOUND; + QListWidgetItem* item = m_qtListWidget->selectedItems().first(); + + return m_qtListWidget->row(item); } void wxListBox::DoSetFirstItem(int WXUNUSED(n)) From c8f427181e548be8e3f1dee939c2152700c23f33 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 21 Jan 2019 23:43:03 +0100 Subject: [PATCH 7/7] Reuse code between wxListBox::Create() overloads Add DoCreate() helper to actually create and initialize QListWidget. --- include/wx/qt/listbox.h | 4 +++- src/qt/listbox.cpp | 18 +++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/wx/qt/listbox.h b/include/wx/qt/listbox.h index 4b9c9c5ac1..19126336a3 100644 --- a/include/wx/qt/listbox.h +++ b/include/wx/qt/listbox.h @@ -90,7 +90,9 @@ protected: private: virtual void Init(); //common construction - void setStyle(long style); + // Common part of both Create() overloads. + void DoCreate(wxWindow* parent, long style); + void UnSelectAll(); wxDECLARE_DYNAMIC_CLASS(wxListBox); diff --git a/src/qt/listbox.cpp b/src/qt/listbox.cpp index 51a1457c49..504b98d689 100644 --- a/src/qt/listbox.cpp +++ b/src/qt/listbox.cpp @@ -86,11 +86,9 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, const wxValidator& validator, const wxString& name) { - Init(); - QListWidgetItem* item; - m_qtWindow = m_qtListWidget = new wxQtListWidget( parent, this ); + DoCreate(parent, style); - setStyle(style); + QListWidgetItem* item; while ( n-- > 0 ) { @@ -114,10 +112,7 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, const wxValidator& validator, const wxString& name) { - Init(); - m_qtWindow = m_qtListWidget = new wxQtListWidget( parent, this ); - - setStyle(style); + DoCreate(parent, style); QStringList items; @@ -129,8 +124,13 @@ bool wxListBox::Create(wxWindow *parent, wxWindowID id, return wxListBoxBase::Create( parent, id, pos, size, style, validator, name ); } -void wxListBox::setStyle(long style) +void wxListBox::DoCreate(wxWindow* parent, long style) { + Init(); + + m_qtWindow = + m_qtListWidget = new wxQtListWidget( parent, this ); + if ( style & wxLB_SORT ) { m_qtListWidget->setSortingEnabled(true);