Create the Sorter class
This commit is contained in:
parent
e8e212df9c
commit
e96f821e28
|
@ -3,6 +3,8 @@
|
|||
INCLUDEPATH += $$PWD
|
||||
|
||||
HEADERS += $$PWD/qqmlsortfilterproxymodel.h \
|
||||
$$PWD/filter.h
|
||||
$$PWD/filter.h \
|
||||
$$PWD/sorter.h
|
||||
SOURCES += $$PWD/qqmlsortfilterproxymodel.cpp \
|
||||
$$PWD/filter.cpp
|
||||
$$PWD/filter.cpp \
|
||||
$$PWD/sorter.cpp
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
#include "sorter.h"
|
||||
#include <QtQml>
|
||||
|
||||
namespace qqsfpm {
|
||||
|
||||
Sorter::Sorter(QObject *parent) : QObject(parent)
|
||||
{
|
||||
connect(this, &Sorter::sorterChanged, this, &Sorter::onSorterChanged);
|
||||
}
|
||||
|
||||
Sorter::~Sorter() = default;
|
||||
|
||||
bool Sorter::enabled() const
|
||||
{
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
void Sorter::setEnabled(bool enabled)
|
||||
{
|
||||
if (m_enabled == enabled)
|
||||
return;
|
||||
|
||||
m_enabled = enabled;
|
||||
emit enabledChanged();
|
||||
emit sorterChanged();
|
||||
}
|
||||
|
||||
bool Sorter::ascendingOrder() const
|
||||
{
|
||||
return m_ascendingOrder;
|
||||
}
|
||||
|
||||
void Sorter::setAscendingOrder(bool ascendingOrder)
|
||||
{
|
||||
if (m_ascendingOrder == ascendingOrder)
|
||||
return;
|
||||
|
||||
m_ascendingOrder = ascendingOrder;
|
||||
emit ascendingOrderChanged();
|
||||
emit 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;
|
||||
}
|
||||
|
||||
QQmlSortFilterProxyModel* Sorter::proxyModel() const
|
||||
{
|
||||
return m_proxyModel;
|
||||
}
|
||||
|
||||
int Sorter::compare(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const
|
||||
{
|
||||
if (lessThan(sourceLeft, sourceRight))
|
||||
return -1;
|
||||
if (lessThan(sourceRight, sourceLeft))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Sorter::lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const
|
||||
{
|
||||
Q_UNUSED(sourceLeft)
|
||||
Q_UNUSED(sourceRight)
|
||||
return false;
|
||||
}
|
||||
|
||||
void Sorter::proxyModelCompleted()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Sorter::onSorterChanged()
|
||||
{
|
||||
if (m_enabled)
|
||||
emit invalidate();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
#ifndef SORTER_H
|
||||
#define SORTER_H
|
||||
|
||||
#include <QObject>
|
||||
#include "qqmlsortfilterproxymodel.h"
|
||||
|
||||
namespace qqsfpm {
|
||||
|
||||
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)
|
||||
friend class QQmlSortFilterProxyModel;
|
||||
|
||||
public:
|
||||
Sorter(QObject* parent = nullptr);
|
||||
virtual ~Sorter() = 0;
|
||||
|
||||
bool enabled() const;
|
||||
void setEnabled(bool enabled);
|
||||
|
||||
bool ascendingOrder() const;
|
||||
void setAscendingOrder(bool ascendingOrder);
|
||||
|
||||
int compareRows(const QModelIndex& source_left, const QModelIndex& source_right) const;
|
||||
|
||||
signals:
|
||||
void enabledChanged();
|
||||
void ascendingOrderChanged();
|
||||
|
||||
void sorterChanged();
|
||||
void invalidate();
|
||||
|
||||
protected:
|
||||
QQmlSortFilterProxyModel* proxyModel() const;
|
||||
|
||||
virtual int compare(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const;
|
||||
virtual bool lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const;
|
||||
virtual void proxyModelCompleted();
|
||||
|
||||
private slots:
|
||||
void onSorterChanged();
|
||||
|
||||
private:
|
||||
bool m_enabled = true;
|
||||
bool m_ascendingOrder = true;
|
||||
QQmlSortFilterProxyModel* m_proxyModel = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // SORTER_H
|
Loading…
Reference in New Issue