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:
committed by
Vadim Zeitlin
parent
000f8ef422
commit
b27971c501
@@ -10,11 +10,39 @@
|
||||
|
||||
#include "wx/choice.h"
|
||||
#include "wx/qt/private/winevent.h"
|
||||
|
||||
#include <QtWidgets/QComboBox>
|
||||
#include <QtCore/QSortFilterProxyModel>
|
||||
|
||||
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 >
|
||||
{
|
||||
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,
|
||||
const wxPoint& pos,
|
||||
@@ -95,6 +131,8 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id,
|
||||
{
|
||||
m_qtComboBox = new wxQtChoice( parent, this );
|
||||
|
||||
InitialiseSort(m_qtComboBox);
|
||||
|
||||
while ( n-- > 0 )
|
||||
m_qtComboBox->addItem( wxQtConvertString( *choices++ ));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user