refactor: ascendingSortOrder property becomes sortOrder

Changed type from a bool to the Qt::SortOrder enum.

It's still possible to use the ascendingSortOrder bool property (for
now).
This commit is contained in:
Pierre-Yves Siret 2017-09-02 16:07:22 +02:00
parent 1c0731a38c
commit 985c5b0e55
2 changed files with 22 additions and 8 deletions

View File

@ -26,23 +26,33 @@ void Sorter::setEnabled(bool enabled)
bool Sorter::ascendingOrder() const
{
return m_ascendingOrder;
return sortOrder() == Qt::AscendingOrder;
}
void Sorter::setAscendingOrder(bool ascendingOrder)
{
if (m_ascendingOrder == ascendingOrder)
setSortOrder(ascendingOrder ? Qt::AscendingOrder : Qt::DescendingOrder);
}
Qt::SortOrder Sorter::sortOrder() const
{
return m_sortOrder;
}
void Sorter::setSortOrder(Qt::SortOrder sortOrder)
{
if (m_sortOrder == sortOrder)
return;
m_ascendingOrder = ascendingOrder;
Q_EMIT ascendingOrderChanged();
m_sortOrder = sortOrder;
Q_EMIT sortOrderChanged();
sorterChanged();
}
int Sorter::compareRows(const QModelIndex &source_left, const QModelIndex &source_right) const
{
int comparison = compare(source_left, source_right);
return m_ascendingOrder ? comparison : -comparison;
return (m_sortOrder == Qt::AscendingOrder) ? comparison : -comparison;
}
QQmlSortFilterProxyModel* Sorter::proxyModel() const

View File

@ -11,7 +11,8 @@ class Sorter : public QObject
{
Q_OBJECT
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
Q_PROPERTY(bool ascendingOrder READ ascendingOrder WRITE setAscendingOrder NOTIFY ascendingOrderChanged)
Q_PROPERTY(bool ascendingOrder READ ascendingOrder WRITE setAscendingOrder NOTIFY sortOrderChanged)
Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
friend class QQmlSortFilterProxyModel;
public:
@ -24,11 +25,14 @@ public:
bool ascendingOrder() const;
void setAscendingOrder(bool ascendingOrder);
Qt::SortOrder sortOrder() const;
void setSortOrder(Qt::SortOrder sortOrder);
int compareRows(const QModelIndex& source_left, const QModelIndex& source_right) const;
Q_SIGNALS:
void enabledChanged();
void ascendingOrderChanged();
void sortOrderChanged();
void invalidate();
@ -42,7 +46,7 @@ protected:
private:
bool m_enabled = true;
bool m_ascendingOrder = true;
Qt::SortOrder m_sortOrder = Qt::AscendingOrder;
QQmlSortFilterProxyModel* m_proxyModel = nullptr;
};