Browse Source

Modify SortFilterProxyModel to be compliant with Coding Standards

cl-refactor
yann300 10 years ago
parent
commit
21cef78720
  1. 90
      mix/sortfilterproxymodel.cpp
  2. 35
      mix/sortfilterproxymodel.h

90
mix/sortfilterproxymodel.cpp

@ -44,7 +44,7 @@
using namespace dev::mix; using namespace dev::mix;
SortFilterProxyModel::SortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) SortFilterProxyModel::SortFilterProxyModel(QObject* _parent) : QSortFilterProxyModel(_parent)
{ {
connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(countChanged())); connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SIGNAL(countChanged()));
connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(countChanged())); connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SIGNAL(countChanged()));
@ -55,14 +55,14 @@ int SortFilterProxyModel::count() const
return rowCount(); return rowCount();
} }
QObject *SortFilterProxyModel::source() const QObject* SortFilterProxyModel::source() const
{ {
return sourceModel(); return sourceModel();
} }
void SortFilterProxyModel::setSource(QObject *source) void SortFilterProxyModel::setSource(QObject* _source)
{ {
setSourceModel(qobject_cast<QAbstractItemModel *>(source)); setSourceModel(qobject_cast<QAbstractItemModel *>(_source));
} }
QByteArray SortFilterProxyModel::sortRole() const QByteArray SortFilterProxyModel::sortRole() const
@ -70,34 +70,24 @@ QByteArray SortFilterProxyModel::sortRole() const
return roleNames().value(QSortFilterProxyModel::sortRole()); return roleNames().value(QSortFilterProxyModel::sortRole());
} }
void SortFilterProxyModel::setSortRole(const QByteArray &role) void SortFilterProxyModel::setSortRole(QByteArray const& _role)
{ {
QSortFilterProxyModel::setSortRole(roleKey(role)); QSortFilterProxyModel::setSortRole(roleKey(_role));
} }
void SortFilterProxyModel::setSortOrder(Qt::SortOrder order) void SortFilterProxyModel::setSortOrder(Qt::SortOrder _order)
{ {
QSortFilterProxyModel::sort(0, order); QSortFilterProxyModel::sort(0, _order);
} }
/*QByteArray SortFilterProxyModel::filterRole() const
{
return roleNames().value(QSortFilterProxyModel::filterRole());
}*/
/*void SortFilterProxyModel::setFilterRole(const QByteArray &role)
{
QSortFilterProxyModel::setFilterRole(roleKey(role));
}*/
QString SortFilterProxyModel::filterString() const QString SortFilterProxyModel::filterString() const
{ {
return filterRegExp().pattern(); return filterRegExp().pattern();
} }
void SortFilterProxyModel::setFilterString(const QString &filter) void SortFilterProxyModel::setFilterString(QString const& _filter)
{ {
setFilterRegExp(QRegExp(filter, filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(filterSyntax()))); setFilterRegExp(QRegExp(_filter, filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(filterSyntax())));
} }
SortFilterProxyModel::FilterSyntax SortFilterProxyModel::filterSyntax() const SortFilterProxyModel::FilterSyntax SortFilterProxyModel::filterSyntax() const
@ -105,33 +95,33 @@ SortFilterProxyModel::FilterSyntax SortFilterProxyModel::filterSyntax() const
return static_cast<FilterSyntax>(filterRegExp().patternSyntax()); return static_cast<FilterSyntax>(filterRegExp().patternSyntax());
} }
void SortFilterProxyModel::setFilterSyntax(SortFilterProxyModel::FilterSyntax syntax) void SortFilterProxyModel::setFilterSyntax(SortFilterProxyModel::FilterSyntax _syntax)
{ {
setFilterRegExp(QRegExp(filterString(), filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(syntax))); setFilterRegExp(QRegExp(filterString(), filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(_syntax)));
} }
QJSValue SortFilterProxyModel::get(int idx) const QJSValue SortFilterProxyModel::get(int _idx) const
{ {
QJSEngine *engine = qmlEngine(this); QJSEngine *engine = qmlEngine(this);
QJSValue value = engine->newObject(); QJSValue value = engine->newObject();
if (idx >= 0 && idx < count()) { if (_idx >= 0 && _idx < count()) {
QHash<int, QByteArray> roles = roleNames(); QHash<int, QByteArray> roles = roleNames();
QHashIterator<int, QByteArray> it(roles); QHashIterator<int, QByteArray> it(roles);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
value.setProperty(QString::fromUtf8(it.value()), data(index(idx, 0), it.key()).toString()); value.setProperty(QString::fromUtf8(it.value()), data(index(_idx, 0), it.key()).toString());
} }
} }
return value; return value;
} }
int SortFilterProxyModel::roleKey(const QByteArray &role) const int SortFilterProxyModel::roleKey(QByteArray const& _role) const
{ {
QHash<int, QByteArray> roles = roleNames(); QHash<int, QByteArray> roles = roleNames();
QHashIterator<int, QByteArray> it(roles); QHashIterator<int, QByteArray> it(roles);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
if (it.value() == role) if (it.value() == _role)
return it.key(); return it.key();
} }
return -1; return -1;
@ -139,62 +129,26 @@ int SortFilterProxyModel::roleKey(const QByteArray &role) const
QHash<int, QByteArray> SortFilterProxyModel::roleNames() const QHash<int, QByteArray> SortFilterProxyModel::roleNames() const
{ {
if (QAbstractItemModel *source = sourceModel()) if (QAbstractItemModel* source = sourceModel())
return source->roleNames(); return source->roleNames();
return QHash<int, QByteArray>(); return QHash<int, QByteArray>();
} }
bool SortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const bool SortFilterProxyModel::filterAcceptsRow(int _sourceRow, QModelIndex const& _sourceParent) const
{ {
/*QRegExp rx = filterRegExp();
if (rx.isEmpty())
return true;
QAbstractItemModel *model = sourceModel();
if (filterRole().isEmpty()) {
QHash<int, QByteArray> roles = roleNames();
QHashIterator<int, QByteArray> it(roles);
while (it.hasNext()) {
it.next();
QModelIndex sourceIndex = model->index(sourceRow, 0, sourceParent);
QString key = model->data(sourceIndex, it.key()).toString();
if (key.contains(rx))data
return true;
}
return false;
}*/
QRegExp rx = filterRegExp(); QRegExp rx = filterRegExp();
QAbstractItemModel *model = sourceModel(); QAbstractItemModel *model = sourceModel();
QModelIndex sourceIndex = model->index(sourceRow, 0, sourceParent); QModelIndex sourceIndex = model->index(_sourceRow, 0, _sourceParent);
if (!sourceIndex.isValid()) if (!sourceIndex.isValid())
return true; return true;
QString keyType = model->data(sourceIndex, roleKey(type.toUtf8())).toString(); QString keyType = model->data(sourceIndex, roleKey(type.toUtf8())).toString();
QString keyContent = model->data(sourceIndex, roleKey(content.toUtf8())).toString(); QString keyContent = model->data(sourceIndex, roleKey(content.toUtf8())).toString();
return keyType.contains(m_filterType) && keyContent.contains(m_filterContent); return keyType.contains(m_filterType) && keyContent.contains(m_filterContent);
/*
for (auto filter: filterRoles())
{
QString key = model->data(sourceIndex, roleKey(filter.toUtf8())).toString();
if (!key.contains(rx))
return false;
}
return true;
QModelIndex sourceIndex = model->index(sourceRow, 0, sourceParent);
if (!sourceIndex.isValid())
return true;
for (auto role: filterR)
{
QString key = model->data(sourceIndex, roleKey(role)).toString();
if (key.contains(rx))
return true;
}
return false;*/
} }
void SortFilterProxyModel::setFilterType(const QString &_type) void SortFilterProxyModel::setFilterType(QString const& _type)
{ {
m_filterType = QRegExp(_type, filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(filterSyntax())); m_filterType = QRegExp(_type, filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(filterSyntax()));
setFilterRegExp(_type); setFilterRegExp(_type);
@ -205,7 +159,7 @@ QString SortFilterProxyModel::filterType() const
return m_filterType.pattern(); return m_filterType.pattern();
} }
void SortFilterProxyModel::setFilterContent(const QString &_content) void SortFilterProxyModel::setFilterContent(QString const& _content)
{ {
m_filterContent = QRegExp(_content, filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(filterSyntax())); m_filterContent = QRegExp(_content, filterCaseSensitivity(), static_cast<QRegExp::PatternSyntax>(filterSyntax()));
setFilterRegExp(_content); setFilterRegExp(_content);

35
mix/sortfilterproxymodel.h

@ -38,8 +38,7 @@
** **
****************************************************************************/ ****************************************************************************/
#ifndef SORTFILTERPROXYMODEL_H #pragma once
#define SORTFILTERPROXYMODEL_H
#include <QtCore/qsortfilterproxymodel.h> #include <QtCore/qsortfilterproxymodel.h>
#include <QtQml/qjsvalue.h> #include <QtQml/qjsvalue.h>
@ -49,11 +48,11 @@ namespace dev
namespace mix namespace mix
{ {
class SortFilterProxyModel : public QSortFilterProxyModel class SortFilterProxyModel: public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged) Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_PROPERTY(QObject *source READ source WRITE setSource) Q_PROPERTY(QObject* source READ source WRITE setSource)
Q_PROPERTY(QByteArray sortRole READ sortRole WRITE setSortRole) Q_PROPERTY(QByteArray sortRole READ sortRole WRITE setSortRole)
Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder) Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder)
@ -66,26 +65,23 @@ class SortFilterProxyModel : public QSortFilterProxyModel
Q_ENUMS(FilterSyntax) Q_ENUMS(FilterSyntax)
public: public:
explicit SortFilterProxyModel(QObject *parent = 0); explicit SortFilterProxyModel(QObject* _parent = 0);
QObject *source() const; QObject* source() const;
void setSource(QObject *source); void setSource(QObject* _source);
QByteArray sortRole() const; QByteArray sortRole() const;
void setSortRole(const QByteArray &role); void setSortRole(QByteArray const& _role);
void setSortOrder(Qt::SortOrder order); void setSortOrder(Qt::SortOrder _order);
QString filterContent() const; QString filterContent() const;
void setFilterContent(const QString &_content); void setFilterContent(QString const& _content);
QString filterType() const; QString filterType() const;
void setFilterType(const QString &_type); void setFilterType(QString const& _type);
/*QStringList filterRoles() const;
void setFilterRoles(const QStringList &roles);
*/
QString filterString() const; QString filterString() const;
void setFilterString(const QString &filter); void setFilterString(QString const& _filter);
enum FilterSyntax { enum FilterSyntax {
RegExp, RegExp,
@ -94,18 +90,18 @@ public:
}; };
FilterSyntax filterSyntax() const; FilterSyntax filterSyntax() const;
void setFilterSyntax(FilterSyntax syntax); void setFilterSyntax(FilterSyntax _syntax);
int count() const; int count() const;
Q_INVOKABLE QJSValue get(int index) const; Q_INVOKABLE QJSValue get(int _index) const;
signals: signals:
void countChanged(); void countChanged();
protected: protected:
int roleKey(const QByteArray &role) const; int roleKey(QByteArray const& _role) const;
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; bool filterAcceptsRow(int _sourceRow, QModelIndex const& _sourceParent) const;
private: private:
QRegExp m_filterType; QRegExp m_filterType;
@ -116,4 +112,3 @@ private:
} }
} }
#endif // SORTFILTERPROXYMODEL_H

Loading…
Cancel
Save