Use case-insensitive comparison in Qt wxComboBox and wxChoice

Define LexicalSortProxyModel to use the same sort order in wxQt as in
the other ports.
This commit is contained in:
Richard Smith
2019-01-16 11:03:52 +00:00
committed by Vadim Zeitlin
parent 000f8ef422
commit b27971c501
3 changed files with 45 additions and 0 deletions

View File

@@ -71,6 +71,8 @@ protected:
virtual void DoClear(); virtual void DoClear();
virtual void DoDeleteOneItem(unsigned int pos); virtual void DoDeleteOneItem(unsigned int pos);
virtual void InitialiseSort(QComboBox *combo);
QComboBox *m_qtComboBox; QComboBox *m_qtComboBox;
private: private:

View File

@@ -10,11 +10,39 @@
#include "wx/choice.h" #include "wx/choice.h"
#include "wx/qt/private/winevent.h" #include "wx/qt/private/winevent.h"
#include <QtWidgets/QComboBox> #include <QtWidgets/QComboBox>
#include <QtCore/QSortFilterProxyModel>
namespace namespace
{ {
class LexicalSortProxyModel : public QSortFilterProxyModel
{
public:
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const wxOVERRIDE
{
const QVariant leftData = sourceModel()->data( left );
const QVariant rightData = sourceModel()->data( right );
if ( leftData.type() != QVariant::String )
return false;
int insensitiveResult = QString::compare(
leftData.value<QString>(),
rightData.value<QString>(),
Qt::CaseInsensitive );
if ( insensitiveResult == 0 )
{
return QString::compare( leftData.value<QString>(),
rightData.value<QString>() ) < 0;
}
return insensitiveResult < 0;
}
};
class wxQtChoice : public wxQtEventSignalHandler< QComboBox, wxChoice > class wxQtChoice : public wxQtEventSignalHandler< QComboBox, wxChoice >
{ {
public: public:
@@ -47,6 +75,14 @@ wxChoice::wxChoice() :
{ {
} }
void wxChoice::InitialiseSort(QComboBox *combo)
{
QSortFilterProxyModel *proxyModel = new LexicalSortProxyModel();
proxyModel->setSourceModel(combo->model());
combo->model()->setParent(proxyModel);
combo->setModel(proxyModel);
}
wxChoice::wxChoice( wxWindow *parent, wxWindowID id, wxChoice::wxChoice( wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxPoint& pos,
@@ -95,6 +131,8 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id,
{ {
m_qtComboBox = new wxQtChoice( parent, this ); m_qtComboBox = new wxQtChoice( parent, this );
InitialiseSort(m_qtComboBox);
while ( n-- > 0 ) while ( n-- > 0 )
m_qtComboBox->addItem( wxQtConvertString( *choices++ )); m_qtComboBox->addItem( wxQtConvertString( *choices++ ));

View File

@@ -128,6 +128,8 @@ bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
const wxString& name ) const wxString& name )
{ {
m_qtComboBox = new wxQtComboBox( parent, this ); m_qtComboBox = new wxQtComboBox( parent, this );
InitialiseSort(m_qtComboBox);
while ( n-- > 0 ) while ( n-- > 0 )
m_qtComboBox->addItem( wxQtConvertString( *choices++ )); m_qtComboBox->addItem( wxQtConvertString( *choices++ ));
m_qtComboBox->setEditText( wxQtConvertString( value )); m_qtComboBox->setEditText( wxQtConvertString( value ));
@@ -140,7 +142,10 @@ void wxComboBox::SetValue(const wxString& value)
if ( HasFlag(wxCB_READONLY) ) if ( HasFlag(wxCB_READONLY) )
SetStringSelection(value); SetStringSelection(value);
else else
{
wxTextEntry::SetValue(value); wxTextEntry::SetValue(value);
m_qtComboBox->setEditText( wxQtConvertString(value) );
}
} }
wxString wxComboBox::DoGetValue() const wxString wxComboBox::DoGetValue() const