-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable natural sort in the nations view
Closes #2050.
- Loading branch information
Showing
5 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* SPDX-License-Identifier: GPLv3-or-later | ||
* SPDX-FileCopyrightText: Louis Moureaux <[email protected]> | ||
*/ | ||
|
||
#include "collated_sort_proxy.h" | ||
|
||
namespace freeciv { | ||
|
||
/** | ||
* \class collated_sort_filter_proxy_model | ||
* \brief A sort and filter proxy model supporting string collation. | ||
* | ||
* This model can be used when strings in a model should use a "natural" sort | ||
* order. | ||
*/ | ||
|
||
/** | ||
* \brief Constructor. | ||
*/ | ||
collated_sort_filter_proxy_model::collated_sort_filter_proxy_model( | ||
QObject *parent) | ||
: QSortFilterProxyModel(parent) | ||
{ | ||
} | ||
|
||
/** | ||
* \brief Changes the collator currently in use. | ||
*/ | ||
void collated_sort_filter_proxy_model::set_collator(const QCollator &coll) | ||
{ | ||
m_collator = coll; | ||
invalidate(); | ||
} | ||
|
||
/** | ||
* \brief Reimplemented protected function. | ||
* | ||
* This overrides @c QSortFilterProxyModel::lessThan to use the collator when | ||
* comparing strings. | ||
*/ | ||
bool collated_sort_filter_proxy_model::lessThan( | ||
const QModelIndex &source_left, const QModelIndex &source_right) const | ||
{ | ||
// Copied from QSortFilterProxyModel | ||
QVariant l = (source_left.model() | ||
? source_left.model()->data(source_left, sortRole()) | ||
: QVariant()); | ||
QVariant r = (source_right.model() | ||
? source_right.model()->data(source_right, sortRole()) | ||
: QVariant()); | ||
|
||
if (l.type() == QVariant::String && r.type() == QVariant::String) { | ||
return m_collator(l.toString(), r.toString()); | ||
} | ||
|
||
return QSortFilterProxyModel::lessThan(source_left, source_right); | ||
} | ||
|
||
} // namespace freeciv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* SPDX-License-Identifier: GPLv3-or-later | ||
* SPDX-FileCopyrightText: Louis Moureaux <[email protected]> | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QCollator> | ||
#include <QSortFilterProxyModel> | ||
|
||
namespace freeciv { | ||
|
||
class collated_sort_filter_proxy_model : public QSortFilterProxyModel { | ||
QCollator m_collator; | ||
|
||
public: | ||
collated_sort_filter_proxy_model(QObject *parent = nullptr); | ||
|
||
/// Retrieves the string collator currently in use. | ||
QCollator collator() const { return m_collator; } | ||
void set_collator(const QCollator &coll); | ||
|
||
protected: | ||
bool lessThan(const QModelIndex &source_left, | ||
const QModelIndex &source_right) const override; | ||
}; | ||
|
||
} // namespace freeciv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters