refactor: Add container interfaces for filters, sorters and roles

This commit is contained in:
Grecko 2018-04-25 19:28:38 +02:00
parent d844e7dcfd
commit febeade02b
8 changed files with 388 additions and 206 deletions

View File

@ -1,8 +1,73 @@
#include "filter.h"
#include "qqmlsortfilterproxymodel.h"
#include <QtQml>
namespace qqsfpm {
FilterContainer::~FilterContainer()
{
}
QList<Filter*> FilterContainer::filters() const
{
return m_filters;
}
void FilterContainer::appendFilter(Filter* filter)
{
m_filters.append(filter);
onFilterAppended(filter);
}
void FilterContainer::removeFilter(Filter* filter)
{
m_filters.removeOne(filter);
onFilterRemoved(filter);
}
void FilterContainer::clearFilters()
{
m_filters.clear();
onFiltersCleared();
}
QQmlListProperty<Filter> FilterContainer::filtersListProperty()
{
return QQmlListProperty<Filter>(reinterpret_cast<QObject*>(this), &m_filters,
&FilterContainer::append_filter,
&FilterContainer::count_filter,
&FilterContainer::at_filter,
&FilterContainer::clear_filters);
}
void FilterContainer::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
{
if (!filter)
return;
FilterContainer* that = reinterpret_cast<FilterContainer*>(list->object);
that->appendFilter(filter);
}
int FilterContainer::count_filter(QQmlListProperty<Filter>* list)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
}
Filter* FilterContainer::at_filter(QQmlListProperty<Filter>* list, int index)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
}
void FilterContainer::clear_filters(QQmlListProperty<Filter> *list)
{
FilterContainer* that = reinterpret_cast<FilterContainer*>(list->object);
that->clearFilters();
}
/*!
\qmltype Filter
\inqmlmodule SortFilterProxyModel
@ -622,49 +687,27 @@ void ExpressionFilter::updateExpression()
m_expression->evaluate();
}
QQmlListProperty<Filter> FilterContainer::filters()
{
return QQmlListProperty<Filter>(this, &m_filters,
&FilterContainer::append_filter,
&FilterContainer::count_filter,
&FilterContainer::at_filter,
&FilterContainer::clear_filters);
}
void FilterContainer::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
void FilterContainerFilter::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
for (Filter* filter : m_filters)
filter->proxyModelCompleted(proxyModel);
}
void FilterContainer::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
void FilterContainerFilter::onFilterAppended(Filter* filter)
{
if (!filter)
return;
FilterContainer* that = static_cast<FilterContainer*>(list->object);
that->m_filters.append(filter);
connect(filter, &Filter::invalidated, that, &FilterContainer::invalidate);
that->invalidate();
connect(filter, &Filter::invalidated, this, &FilterContainerFilter::invalidate);
invalidate();
}
int FilterContainer::count_filter(QQmlListProperty<Filter>* list)
void FilterContainerFilter::onFilterRemoved(Filter* filter)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
Q_UNUSED(filter)
invalidate();
}
Filter* FilterContainer::at_filter(QQmlListProperty<Filter>* list, int index)
void qqsfpm::FilterContainerFilter::onFiltersCleared()
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
}
void FilterContainer::clear_filters(QQmlListProperty<Filter> *list)
{
FilterContainer* that = static_cast<FilterContainer*>(list->object);
that->m_filters.clear();
that->invalidate();
invalidate();
}
/*!

View File

@ -3,7 +3,41 @@
#include <QObject>
#include <QQmlExpression>
#include "qqmlsortfilterproxymodel.h"
#include <QQmlListProperty>
#include <qqml.h>
namespace qqsfpm {
class Filter;
class QQmlSortFilterProxyModel;
class FilterContainer {
public:
virtual ~FilterContainer();
QList<Filter*> filters() const;
void appendFilter(Filter* filter);
void removeFilter(Filter* filter);
void clearFilters();
QQmlListProperty<Filter> filtersListProperty();
protected:
QList<Filter*> m_filters;
private:
virtual void onFilterAppended(Filter* filter) = 0;
virtual void onFilterRemoved(Filter* filter) = 0;
virtual void onFiltersCleared() = 0;
static void append_filter(QQmlListProperty<Filter>* list, Filter* filter);
static int count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, int index);
static void clear_filters(QQmlListProperty<Filter>* list);
};
}
#define FilterContainer_iid "fr.grecko.SortFilterProxyModel.FilterContainer"
Q_DECLARE_INTERFACE(qqsfpm::FilterContainer, FilterContainer_iid)
namespace qqsfpm {
@ -215,43 +249,41 @@ private:
QQmlContext* m_context = nullptr;
};
class FilterContainer : public Filter {
class FilterContainerFilter : public Filter, public FilterContainer {
Q_OBJECT
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filters)
Q_INTERFACES(qqsfpm::FilterContainer)
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filtersListProperty NOTIFY filtersChanged)
Q_CLASSINFO("DefaultProperty", "filters")
public:
using Filter::Filter;
QQmlListProperty<Filter> filters();
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
protected:
QList<Filter*> m_filters;
Q_SIGNALS:
void filtersChanged();
private:
static void append_filter(QQmlListProperty<Filter>* list, Filter* filter);
static int count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, int index);
static void clear_filters(QQmlListProperty<Filter>* list);
void onFilterAppended(Filter* filter) override;
void onFilterRemoved(Filter* filter) override;
void onFiltersCleared() override;
};
class AnyOfFilter : public FilterContainer {
class AnyOfFilter : public FilterContainerFilter {
Q_OBJECT
public:
using FilterContainer::FilterContainer;
using FilterContainerFilter::FilterContainerFilter;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;
};
class AllOfFilter : public FilterContainer {
class AllOfFilter : public FilterContainerFilter {
Q_OBJECT
public:
using FilterContainer::FilterContainer;
using FilterContainerFilter::FilterContainerFilter;
protected:
bool filterRow(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) const override;

View File

@ -6,9 +6,74 @@
#include <QDebug>
#include <QQmlInfo>
#include "filter.h"
#include "qqmlsortfilterproxymodel.h"
namespace qqsfpm {
ProxyRoleContainer::~ProxyRoleContainer()
{
}
QList<ProxyRole*> ProxyRoleContainer::proxyRoles() const
{
return m_proxyRoles;
}
void ProxyRoleContainer::appendProxyRole(ProxyRole* proxyRole)
{
m_proxyRoles.append(proxyRole);
onProxyRoleAppended(proxyRole);
}
void ProxyRoleContainer::removeProxyRole(ProxyRole* proxyRole)
{
m_proxyRoles.removeOne(proxyRole);
onProxyRoleRemoved(proxyRole);
}
void ProxyRoleContainer::clearProxyRoles()
{
m_proxyRoles.clear();
onProxyRolesCleared();
}
QQmlListProperty<ProxyRole> ProxyRoleContainer::proxyRolesListProperty()
{
return QQmlListProperty<ProxyRole>(reinterpret_cast<QObject*>(this), &m_proxyRoles,
&ProxyRoleContainer::append_proxyRole,
&ProxyRoleContainer::count_proxyRole,
&ProxyRoleContainer::at_proxyRole,
&ProxyRoleContainer::clear_proxyRoles);
}
void ProxyRoleContainer::append_proxyRole(QQmlListProperty<ProxyRole>* list, ProxyRole* proxyRole)
{
if (!proxyRole)
return;
ProxyRoleContainer* that = reinterpret_cast<ProxyRoleContainer*>(list->object);
that->appendProxyRole(proxyRole);
}
int ProxyRoleContainer::count_proxyRole(QQmlListProperty<ProxyRole>* list)
{
QList<ProxyRole*>* ProxyRoles = static_cast<QList<ProxyRole*>*>(list->data);
return ProxyRoles->count();
}
ProxyRole* ProxyRoleContainer::at_proxyRole(QQmlListProperty<ProxyRole>* list, int index)
{
QList<ProxyRole*>* ProxyRoles = static_cast<QList<ProxyRole*>*>(list->data);
return ProxyRoles->at(index);
}
void ProxyRoleContainer::clear_proxyRoles(QQmlListProperty<ProxyRole> *list)
{
ProxyRoleContainer* that = reinterpret_cast<ProxyRoleContainer*>(list->object);
that->clearProxyRoles();
}
/*!
\qmltype ProxyRole
\inqmlmodule SortFilterProxyModel
@ -242,14 +307,6 @@ void SwitchRole::setDefaultValue(const QVariant& defaultValue)
\sa Filter
*/
QQmlListProperty<Filter> SwitchRole::filters()
{
return QQmlListProperty<Filter>(this, &m_filters,
&SwitchRole::append_filter,
&SwitchRole::count_filter,
&SwitchRole::at_filter,
&SwitchRole::clear_filters);
}
void SwitchRole::proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel)
{
@ -286,36 +343,23 @@ QVariant SwitchRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterPr
return m_defaultValue;
}
void SwitchRole::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
void SwitchRole::onFilterAppended(Filter *filter)
{
if (!filter)
return;
SwitchRole* that = static_cast<SwitchRole*>(list->object);
that->m_filters.append(filter);
connect(filter, &Filter::invalidated, that, &SwitchRole::invalidate);
connect(filter, &Filter::invalidated, this, &SwitchRole::invalidate);
auto attached = static_cast<SwitchRoleAttached*>(qmlAttachedPropertiesObject<SwitchRole>(filter, true));
connect(attached, &SwitchRoleAttached::valueChanged, that, &SwitchRole::invalidate);
that->invalidate();
connect(attached, &SwitchRoleAttached::valueChanged, this, &SwitchRole::invalidate);
invalidate();
}
int SwitchRole::count_filter(QQmlListProperty<Filter>* list)
void SwitchRole::onFilterRemoved(Filter *filter)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
Q_UNUSED(filter)
invalidate();
}
Filter* SwitchRole::at_filter(QQmlListProperty<Filter>* list, int index)
void SwitchRole::onFiltersCleared()
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
}
void SwitchRole::clear_filters(QQmlListProperty<Filter> *list)
{
SwitchRole* that = static_cast<SwitchRole*>(list->object);
that->m_filters.clear();
that->invalidate();
invalidate();
}
/*!

View File

@ -6,7 +6,40 @@
#include <QQmlScriptString>
#include <QQmlExpression>
#include <qqml.h>
#include "qqmlsortfilterproxymodel.h"
#include "filter.h"
namespace qqsfpm {
class ProxyRole;
class QQmlSortFilterProxyModel;
class ProxyRoleContainer {
public:
virtual ~ProxyRoleContainer();
QList<ProxyRole*> proxyRoles() const;
void appendProxyRole(ProxyRole* proxyRole);
void removeProxyRole(ProxyRole* proxyRole);
void clearProxyRoles();
QQmlListProperty<ProxyRole> proxyRolesListProperty();
protected:
QList<ProxyRole*> m_proxyRoles;
private:
virtual void onProxyRoleAppended(ProxyRole* proxyRole) = 0;
virtual void onProxyRoleRemoved(ProxyRole* proxyRole) = 0;
virtual void onProxyRolesCleared() = 0;
static void append_proxyRole(QQmlListProperty<ProxyRole>* list, ProxyRole* proxyRole);
static int count_proxyRole(QQmlListProperty<ProxyRole>* list);
static ProxyRole* at_proxyRole(QQmlListProperty<ProxyRole>* list, int index);
static void clear_proxyRoles(QQmlListProperty<ProxyRole>* list);
};
}
#define ProxyRoleContainer_iid "fr.grecko.SortFilterProxyModel.ProxyRoleContainer"
Q_DECLARE_INTERFACE(qqsfpm::ProxyRoleContainer, ProxyRoleContainer_iid)
namespace qqsfpm {
@ -82,12 +115,13 @@ private:
QVariant m_value;
};
class SwitchRole : public ProxyRole
class SwitchRole : public ProxyRole, public FilterContainer
{
Q_OBJECT
Q_INTERFACES(qqsfpm::FilterContainer)
Q_PROPERTY(QString defaultRoleName READ defaultRoleName WRITE setDefaultRoleName NOTIFY defaultRoleNameChanged)
Q_PROPERTY(QVariant defaultValue READ defaultValue WRITE setDefaultValue NOTIFY defaultValueChanged)
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filters)
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filtersListProperty)
public:
using ProxyRole::ProxyRole;
@ -98,7 +132,6 @@ public:
QVariant defaultValue() const;
void setDefaultValue(const QVariant& defaultValue);
QQmlListProperty<Filter> filters();
void proxyModelCompleted(const QQmlSortFilterProxyModel& proxyModel) override;
static SwitchRoleAttached* qmlAttachedProperties(QObject* object);
@ -110,14 +143,12 @@ Q_SIGNALS:
private:
QVariant data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) override;
static void append_filter(QQmlListProperty<Filter>* list, Filter* filter);
static int count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, int index);
static void clear_filters(QQmlListProperty<Filter>* list);
void onFilterAppended(Filter *filter) override;
void onFilterRemoved(Filter *filter) override;
void onFiltersCleared() override;
QString m_defaultRoleName;
QVariant m_defaultValue;
QList<Filter*> m_filters;
};
class ExpressionRole : public ProxyRole

View File

@ -163,14 +163,6 @@ void QQmlSortFilterProxyModel::setAscendingSortOrder(bool ascendingSortOrder)
\sa Filter
*/
QQmlListProperty<Filter> QQmlSortFilterProxyModel::filters()
{
return QQmlListProperty<Filter>(this, &m_filters,
&QQmlSortFilterProxyModel::append_filter,
&QQmlSortFilterProxyModel::count_filter,
&QQmlSortFilterProxyModel::at_filter,
&QQmlSortFilterProxyModel::clear_filters);
}
/*!
\qmlproperty list<Sorter> SortFilterProxyModel::sorters
@ -179,14 +171,6 @@ QQmlListProperty<Filter> QQmlSortFilterProxyModel::filters()
\sa Sorter
*/
QQmlListProperty<Sorter> QQmlSortFilterProxyModel::sorters()
{
return QQmlListProperty<Sorter>(this, &m_sorters,
&QQmlSortFilterProxyModel::append_sorter,
&QQmlSortFilterProxyModel::count_sorter,
&QQmlSortFilterProxyModel::at_sorter,
&QQmlSortFilterProxyModel::clear_sorters);
}
/*!
\qmlproperty list<ProxyRole> SortFilterProxyModel::proxyRoles
@ -195,14 +179,6 @@ QQmlListProperty<Sorter> QQmlSortFilterProxyModel::sorters()
\sa ProxyRole
*/
QQmlListProperty<ProxyRole> QQmlSortFilterProxyModel::proxyRoles()
{
return QQmlListProperty<ProxyRole>(this, &m_proxyRoles,
&QQmlSortFilterProxyModel::append_proxyRole,
&QQmlSortFilterProxyModel::count_proxyRole,
&QQmlSortFilterProxyModel::at_proxyRole,
&QQmlSortFilterProxyModel::clear_proxyRoles);
}
void QQmlSortFilterProxyModel::classBegin()
{
@ -470,98 +446,60 @@ QVariantMap QQmlSortFilterProxyModel::modelDataMap(const QModelIndex& modelIndex
return map;
}
void QQmlSortFilterProxyModel::append_filter(QQmlListProperty<Filter>* list, Filter* filter)
void QQmlSortFilterProxyModel::onFilterAppended(Filter* filter)
{
if (!filter)
return;
QQmlSortFilterProxyModel* that = static_cast<QQmlSortFilterProxyModel*>(list->object);
that->m_filters.append(filter);
connect(filter, &Filter::invalidated, that, &QQmlSortFilterProxyModel::invalidateFilter);
that->invalidateFilter();
connect(filter, &Filter::invalidated, this, &QQmlSortFilterProxyModel::invalidateFilter);
this->invalidateFilter();
}
int QQmlSortFilterProxyModel::count_filter(QQmlListProperty<Filter>* list)
void QQmlSortFilterProxyModel::onFilterRemoved(Filter* filter)
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->count();
Q_UNUSED(filter)
invalidateFilter();
}
Filter* QQmlSortFilterProxyModel::at_filter(QQmlListProperty<Filter>* list, int index)
void QQmlSortFilterProxyModel::onFiltersCleared()
{
QList<Filter*>* filters = static_cast<QList<Filter*>*>(list->data);
return filters->at(index);
invalidateFilter();
}
void QQmlSortFilterProxyModel::clear_filters(QQmlListProperty<Filter> *list)
void QQmlSortFilterProxyModel::onSorterAppended(Sorter* sorter)
{
QQmlSortFilterProxyModel* that = static_cast<QQmlSortFilterProxyModel*>(list->object);
that->m_filters.clear();
that->invalidateFilter();
connect(sorter, &Sorter::invalidated, this, &QQmlSortFilterProxyModel::invalidate);
invalidate();
}
void QQmlSortFilterProxyModel::append_sorter(QQmlListProperty<Sorter>* list, Sorter* sorter)
void QQmlSortFilterProxyModel::onSorterRemoved(Sorter* sorter)
{
if (!sorter)
return;
auto that = static_cast<QQmlSortFilterProxyModel*>(list->object);
that->m_sorters.append(sorter);
connect(sorter, &Sorter::invalidated, that, &QQmlSortFilterProxyModel::invalidate);
that->invalidate();
Q_UNUSED(sorter)
invalidate();
}
int QQmlSortFilterProxyModel::count_sorter(QQmlListProperty<Sorter>* list)
void QQmlSortFilterProxyModel::onSortersCleared()
{
auto sorters = static_cast<QList<Sorter*>*>(list->data);
return sorters->count();
invalidate();
}
Sorter* QQmlSortFilterProxyModel::at_sorter(QQmlListProperty<Sorter>* list, int index)
void QQmlSortFilterProxyModel::onProxyRoleAppended(ProxyRole *proxyRole)
{
auto sorters = static_cast<QList<Sorter*>*>(list->data);
return sorters->at(index);
beginResetModel();
connect(proxyRole, &ProxyRole::invalidated, this, &QQmlSortFilterProxyModel::emitProxyRolesChanged);
connect(proxyRole, &ProxyRole::nameAboutToBeChanged, this, &QQmlSortFilterProxyModel::beginResetModel);
connect(proxyRole, &ProxyRole::nameChanged, this, &QQmlSortFilterProxyModel::endResetModel);
endResetModel();
}
void QQmlSortFilterProxyModel::clear_sorters(QQmlListProperty<Sorter>* list)
void QQmlSortFilterProxyModel::onProxyRoleRemoved(ProxyRole *proxyRole)
{
auto that = static_cast<QQmlSortFilterProxyModel*>(list->object);
that->m_sorters.clear();
that->invalidate();
Q_UNUSED(proxyRole)
beginResetModel();
endResetModel();
}
void QQmlSortFilterProxyModel::append_proxyRole(QQmlListProperty<ProxyRole>* list, ProxyRole* proxyRole)
void QQmlSortFilterProxyModel::onProxyRolesCleared()
{
if (!proxyRole)
return;
auto that = static_cast<QQmlSortFilterProxyModel*>(list->object);
connect(proxyRole, &ProxyRole::invalidated, that, &QQmlSortFilterProxyModel::emitProxyRolesChanged);
connect(proxyRole, &ProxyRole::nameAboutToBeChanged, that, &QQmlSortFilterProxyModel::beginResetModel);
connect(proxyRole, &ProxyRole::nameChanged, that, &QQmlSortFilterProxyModel::endResetModel);
that->beginResetModel();
that->m_proxyRoles.append(proxyRole);
that->endResetModel();
}
int QQmlSortFilterProxyModel::count_proxyRole(QQmlListProperty<ProxyRole>* list)
{
auto proxyRoles = static_cast<QList<ProxyRole*>*>(list->data);
return proxyRoles->count();
}
ProxyRole* QQmlSortFilterProxyModel::at_proxyRole(QQmlListProperty<ProxyRole>* list, int index)
{
auto proxyRoles = static_cast<QList<ProxyRole*>*>(list->data);
return proxyRoles->at(index);
}
void QQmlSortFilterProxyModel::clear_proxyRoles(QQmlListProperty<ProxyRole>* list)
{
auto that = static_cast<QQmlSortFilterProxyModel*>(list->object);
that->beginResetModel();
that->m_proxyRoles.clear();
that->endResetModel();
beginResetModel();
endResetModel();
}
void registerQQmlSortFilterProxyModelTypes() {

View File

@ -3,18 +3,23 @@
#include <QSortFilterProxyModel>
#include <QQmlParserStatus>
#include <QQmlListProperty>
#include "filter.h"
#include "sorter.h"
#include "proxyrole.h"
namespace qqsfpm {
class Filter;
class Sorter;
class ProxyRole;
class QQmlSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus
class QQmlSortFilterProxyModel : public QSortFilterProxyModel,
public QQmlParserStatus,
public FilterContainer,
public SorterContainer,
public ProxyRoleContainer
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_INTERFACES(qqsfpm::FilterContainer)
Q_INTERFACES(qqsfpm::SorterContainer)
Q_INTERFACES(qqsfpm::ProxyRoleContainer)
Q_PROPERTY(int count READ count NOTIFY countChanged)
@ -26,9 +31,9 @@ class QQmlSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParser
Q_PROPERTY(QString sortRoleName READ sortRoleName WRITE setSortRoleName NOTIFY sortRoleNameChanged)
Q_PROPERTY(bool ascendingSortOrder READ ascendingSortOrder WRITE setAscendingSortOrder NOTIFY ascendingSortOrderChanged)
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filters)
Q_PROPERTY(QQmlListProperty<qqsfpm::Sorter> sorters READ sorters)
Q_PROPERTY(QQmlListProperty<qqsfpm::ProxyRole> proxyRoles READ proxyRoles)
Q_PROPERTY(QQmlListProperty<qqsfpm::Filter> filters READ filtersListProperty)
Q_PROPERTY(QQmlListProperty<qqsfpm::Sorter> sorters READ sortersListProperty)
Q_PROPERTY(QQmlListProperty<qqsfpm::ProxyRole> proxyRoles READ proxyRolesListProperty)
public:
enum PatternSyntax {
@ -62,10 +67,6 @@ public:
bool ascendingSortOrder() const;
void setAscendingSortOrder(bool ascendingSortOrder);
QQmlListProperty<Filter> filters();
QQmlListProperty<Sorter> sorters();
QQmlListProperty<ProxyRole> proxyRoles();
void classBegin() override;
void componentComplete() override;
@ -119,28 +120,22 @@ private Q_SLOTS:
private:
QVariantMap modelDataMap(const QModelIndex& modelIndex) const;
static void append_filter(QQmlListProperty<Filter>* list, Filter* filter);
static int count_filter(QQmlListProperty<Filter>* list);
static Filter* at_filter(QQmlListProperty<Filter>* list, int index);
static void clear_filters(QQmlListProperty<Filter>* list);
void onFilterAppended(Filter* filter) override;
void onFilterRemoved(Filter* filter) override;
void onFiltersCleared() override;
static void append_sorter(QQmlListProperty<Sorter>* list, Sorter* sorter);
static int count_sorter(QQmlListProperty<Sorter>* list);
static Sorter* at_sorter(QQmlListProperty<Sorter>* list, int index);
static void clear_sorters(QQmlListProperty<Sorter>* list);
void onSorterAppended(Sorter* sorter) override;
void onSorterRemoved(Sorter* sorter) override;
void onSortersCleared() override;
static void append_proxyRole(QQmlListProperty<ProxyRole>* list, ProxyRole* proxyRole);
static int count_proxyRole(QQmlListProperty<ProxyRole>* list);
static ProxyRole* at_proxyRole(QQmlListProperty<ProxyRole>* list, int index);
static void clear_proxyRoles(QQmlListProperty<ProxyRole>* list);
void onProxyRoleAppended(ProxyRole *proxyRole) override;
void onProxyRoleRemoved(ProxyRole *proxyRole) override;
void onProxyRolesCleared() override;
QString m_filterRoleName;
QVariant m_filterValue;
QString m_sortRoleName;
bool m_ascendingSortOrder = true;
QList<Filter*> m_filters;
QList<Sorter*> m_sorters;
QList<ProxyRole*> m_proxyRoles;
bool m_completed = false;
QHash<int, QByteArray> m_roleNames;
QHash<int, ProxyRole*> m_proxyRoleMap;

View File

@ -1,8 +1,74 @@
#include "sorter.h"
#include "qqmlsortfilterproxymodel.h"
#include <QtQml>
namespace qqsfpm {
SorterContainer::~SorterContainer()
{
}
QList<Sorter*> SorterContainer::sorters() const
{
return m_sorters;
}
void SorterContainer::appendSorter(Sorter* sorter)
{
m_sorters.append(sorter);
onSorterAppended(sorter);
}
void SorterContainer::removeSorter(Sorter *sorter)
{
m_sorters.removeOne(sorter);
onSorterRemoved(sorter);
}
void SorterContainer::clearSorters()
{
m_sorters.clear();
onSortersCleared();
}
QQmlListProperty<Sorter> SorterContainer::sortersListProperty()
{
return QQmlListProperty<Sorter>(reinterpret_cast<QObject*>(this), &m_sorters,
&SorterContainer::append_sorter,
&SorterContainer::count_sorter,
&SorterContainer::at_sorter,
&SorterContainer::clear_sorters);
}
void SorterContainer::append_sorter(QQmlListProperty<Sorter>* list, Sorter* sorter)
{
if (!sorter)
return;
SorterContainer* that = reinterpret_cast<SorterContainer*>(list->object);
that->appendSorter(sorter);
}
int SorterContainer::count_sorter(QQmlListProperty<Sorter>* list)
{
QList<Sorter*>* sorters = static_cast<QList<Sorter*>*>(list->data);
return sorters->count();
}
Sorter* SorterContainer::at_sorter(QQmlListProperty<Sorter>* list, int index)
{
QList<Sorter*>* sorters = static_cast<QList<Sorter*>*>(list->data);
return sorters->at(index);
}
void SorterContainer::clear_sorters(QQmlListProperty<Sorter> *list)
{
SorterContainer* that = reinterpret_cast<SorterContainer*>(list->object);
that->clearSorters();
}
/*!
\qmltype Sorter
\inqmlmodule SortFilterProxyModel

View File

@ -2,9 +2,42 @@
#define SORTER_H
#include <QObject>
#include <QQmlListProperty>
#include <QQmlExpression>
#include "qqmlsortfilterproxymodel.h"
#include "QCollator"
#include <QCollator>
namespace qqsfpm {
class Sorter;
class QQmlSortFilterProxyModel;
class SorterContainer {
public:
virtual ~SorterContainer();
QList<Sorter*> sorters() const;
void appendSorter(Sorter* sorter);
void removeSorter(Sorter* sorter);
void clearSorters();
QQmlListProperty<Sorter> sortersListProperty();
protected:
QList<Sorter*> m_sorters;
private:
virtual void onSorterAppended(Sorter* sorter) = 0;
virtual void onSorterRemoved(Sorter* sorter) = 0;
virtual void onSortersCleared() = 0;
static void append_sorter(QQmlListProperty<Sorter>* list, Sorter* sorter);
static int count_sorter(QQmlListProperty<Sorter>* list);
static Sorter* at_sorter(QQmlListProperty<Sorter>* list, int index);
static void clear_sorters(QQmlListProperty<Sorter>* list);
};
}
#define SorterContainer_iid "fr.grecko.SortFilterProxyModel.SorterContainer"
Q_DECLARE_INTERFACE(qqsfpm::SorterContainer, SorterContainer_iid)
namespace qqsfpm {