Commit 6dec58c4 authored by Claudio Cambra's avatar Claudio Cambra
Browse files

Clean up todosortfilterproxymodel



Signed-off-by: Claudio Cambra's avatarClaudio Cambra <claudio.cambra@kde.org>
parent abdcc06b
Pipeline #248933 passed with stage
in 2 minutes and 39 seconds
......@@ -132,7 +132,7 @@ TreeListView {
id: todoModel
calendar: Kalendar.CalendarManager.calendar
incidenceChanger: Kalendar.CalendarManager.incidenceChanger
filterMap: Kalendar.Filter
filterObject: Kalendar.Filter
showCompleted: root.showCompleted
sortBy: root.sortBy
sortAscending: root.ascendingOrder
......
......@@ -20,7 +20,8 @@ ListView {
Connections {
target: root.sourceModel
function onFilterMapAboutToChange() {
// NOTE: This is Kalendar and TodoSortFilterProxyModel-specific
function onFilterObjectAboutToChange() {
// This is a must to prevent the KDescendantsProxyModel from crashing
for(let i = 0; i < descendantsModel.rowCount(); i++) {
descendantsModel.expandChildren(i);
......
......@@ -8,11 +8,12 @@ TodoSortFilterProxyModel::TodoSortFilterProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
const QString todoMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.todo");
m_todoTreeModel = new Akonadi::IncidenceTreeModel(QStringList() << todoMimeType, this);
m_todoTreeModel.reset(new Akonadi::IncidenceTreeModel(QStringList() << todoMimeType, this));
const auto pref = EventViews::PrefsPtr(new EventViews::Prefs);
m_baseTodoModel = new TodoModel(pref, this);
m_baseTodoModel->setSourceModel(m_todoTreeModel);
setSourceModel(m_baseTodoModel);
m_baseTodoModel.reset(new TodoModel(pref, this));
m_baseTodoModel->setSourceModel(m_todoTreeModel.data());
setSourceModel(m_baseTodoModel.data());
setDynamicSortFilter(true);
setSortCaseSensitivity(Qt::CaseInsensitive);
......@@ -21,7 +22,6 @@ TodoSortFilterProxyModel::TodoSortFilterProxyModel(QObject *parent)
KSharedConfig::Ptr config = KSharedConfig::openConfig();
KConfigGroup rColorsConfig(config, "Resources Colors");
m_colorWatcher = KConfigWatcher::create(config);
QObject::connect(m_colorWatcher.data(), &KConfigWatcher::configChanged, this, &TodoSortFilterProxyModel::loadColors);
loadColors();
......@@ -31,12 +31,6 @@ TodoSortFilterProxyModel::TodoSortFilterProxyModel(QObject *parent)
m_dateRefreshTimer.start();
}
TodoSortFilterProxyModel::~TodoSortFilterProxyModel()
{
delete m_baseTodoModel;
delete m_todoTreeModel;
}
int TodoSortFilterProxyModel::columnCount(const QModelIndex &) const
{
return 1;
......@@ -316,15 +310,15 @@ bool TodoSortFilterProxyModel::filterAcceptsRowCheck(int row, const QModelIndex
const QModelIndex sourceIndex = sourceModel()->index(row, 0, sourceParent);
Q_ASSERT(sourceIndex.isValid());
if (m_filterMap == nullptr) {
if (m_filterObject == nullptr) {
return QSortFilterProxyModel::filterAcceptsRow(row, sourceParent);
}
bool acceptRow = true;
if (m_filterMap->collectionId() > -1) {
if (m_filterObject->collectionId() > -1) {
const auto collectionId = sourceIndex.data(TodoModel::TodoRole).value<Akonadi::Item>().parentCollection().id();
acceptRow = acceptRow && collectionId == m_filterMap->collectionId();
acceptRow = acceptRow && collectionId == m_filterObject->collectionId();
}
switch (m_showCompleted) {
......@@ -338,8 +332,8 @@ bool TodoSortFilterProxyModel::filterAcceptsRowCheck(int row, const QModelIndex
break;
}
if (!m_filterMap->tags().isEmpty()) {
const auto tags = m_filterMap->tags();
if (!m_filterObject->tags().isEmpty()) {
const auto tags = m_filterObject->tags();
bool containsTag = false;
for (const auto &tag : tags) {
const auto todoPtr = sourceIndex.data(TodoModel::TodoPtrRole).value<KCalendarCore::Todo::Ptr>();
......@@ -440,44 +434,50 @@ void TodoSortFilterProxyModel::setShowCompleted(int showCompleted)
sortTodoModel();
}
Filter *TodoSortFilterProxyModel::filterMap() const
Filter *TodoSortFilterProxyModel::filterObject() const
{
return m_filterMap;
return m_filterObject;
}
void TodoSortFilterProxyModel::setFilterMap(Filter *filterMap)
void TodoSortFilterProxyModel::setFilterObject(Filter *filterObject)
{
if (m_filterMap == filterMap) {
if (m_filterObject == filterObject) {
return;
}
if (m_filterMap) {
disconnect(m_filterMap, nullptr, this, nullptr);
if (m_filterObject) {
disconnect(m_filterObject, nullptr, this, nullptr);
}
Q_EMIT filterMapAboutToChange();
Q_EMIT filterObjectAboutToChange();
Q_EMIT layoutAboutToBeChanged();
m_filterMap = filterMap;
Q_EMIT filterMapChanged();
m_filterObject = filterObject;
Q_EMIT filterObjectChanged();
if (!m_filterMap->name().isEmpty()) {
const auto name = m_filterMap->name();
setFilterFixedString(name);
}
invalidateFilter();
connect(m_filterMap, &Filter::nameChanged, this, [this]() {
Q_EMIT filterMapAboutToChange();
setFilterFixedString(m_filterMap->name());
const auto nameFilter = m_filterObject->name();
const auto handleFilterNameChange = [this] {
Q_EMIT filterObjectAboutToChange();
setFilterFixedString(m_filterObject->name());
Q_EMIT layoutChanged();
});
auto handleFilterChange = [this]() {
Q_EMIT filterMapAboutToChange();
Q_EMIT filterObjectChanged();
};
const auto handleFilterObjectChange = [this] {
Q_EMIT filterObjectAboutToChange();
invalidateFilter();
Q_EMIT layoutChanged();
Q_EMIT filterObjectChanged();
};
connect(m_filterMap, &Filter::tagsChanged, this, handleFilterChange);
connect(m_filterMap, &Filter::collectionIdChanged, this, handleFilterChange);
connect(m_filterObject, &Filter::nameChanged, this, handleFilterNameChange);
connect(m_filterObject, &Filter::tagsChanged, this, handleFilterObjectChange);
connect(m_filterObject, &Filter::collectionIdChanged, this, handleFilterObjectChange);
if (!nameFilter.isEmpty()) {
setFilterFixedString(nameFilter);
}
invalidateFilter();
Q_EMIT layoutChanged();
sortTodoModel();
......@@ -489,7 +489,7 @@ void TodoSortFilterProxyModel::sortTodoModel()
QSortFilterProxyModel::sort(m_sortColumn, order);
}
void TodoSortFilterProxyModel::filterTodoName(QString name, int showCompleted)
void TodoSortFilterProxyModel::filterTodoName(const QString &name, const int showCompleted)
{
Q_EMIT layoutAboutToBeChanged();
setFilterFixedString(name);
......
......@@ -22,7 +22,7 @@ class TodoSortFilterProxyModel : public QSortFilterProxyModel
Q_OBJECT
Q_PROPERTY(Akonadi::IncidenceChanger *incidenceChanger READ incidenceChanger WRITE setIncidenceChanger NOTIFY incidenceChangerChanged)
Q_PROPERTY(Akonadi::ETMCalendar::Ptr calendar READ calendar WRITE setCalendar NOTIFY calendarChanged)
Q_PROPERTY(Filter *filterMap READ filterMap WRITE setFilterMap NOTIFY filterMapChanged)
Q_PROPERTY(Filter *filterObject READ filterObject WRITE setFilterObject NOTIFY filterObjectChanged)
Q_PROPERTY(int showCompleted READ showCompleted WRITE setShowCompleted NOTIFY showCompletedChanged)
Q_PROPERTY(int sortBy READ sortBy WRITE setSortBy NOTIFY sortByChanged)
Q_PROPERTY(bool sortAscending READ sortAscending WRITE setSortAscending NOTIFY sortAscendingChanged)
......@@ -85,7 +85,7 @@ public:
Q_ENUM(DueDateDisplayFormat)
explicit TodoSortFilterProxyModel(QObject *parent = nullptr);
~TodoSortFilterProxyModel() override;
~TodoSortFilterProxyModel() = default;
int columnCount(const QModelIndex &parent) const override;
QHash<int, QByteArray> roleNames() const override;
......@@ -95,29 +95,17 @@ public:
bool hasAcceptedChildren(int row, const QModelIndex &sourceParent) const;
Akonadi::ETMCalendar::Ptr calendar();
void setCalendar(Akonadi::ETMCalendar::Ptr &calendar);
Akonadi::IncidenceChanger *incidenceChanger();
void setIncidenceChanger(Akonadi::IncidenceChanger *changer);
int showCompleted() const;
void setShowCompleted(int showCompleted);
Filter *filterMap() const;
void setFilterMap(Filter *filterMap);
Filter *filterObject() const;
int sortBy() const;
void setSortBy(int sortBy);
bool sortAscending() const;
void setSortAscending(bool sortAscending);
bool showCompletedSubtodosInIncomplete() const;
void setShowCompletedSubtodosInIncomplete(bool showCompletedSubtodosInIncomplete);
void sortTodoModel();
Q_INVOKABLE void filterTodoName(QString name, int showCompleted = ShowAll);
Q_SIGNALS:
void calendarChanged();
void filterMapAboutToChange();
void filterMapChanged();
void filterObjectAboutToChange();
void filterObjectChanged();
void showCompletedChanged();
void sortByChanged();
void sortAscendingChanged();
......@@ -125,6 +113,18 @@ Q_SIGNALS:
void showCompletedSubtodosInIncompleteChanged();
void incidenceChangerChanged();
public Q_SLOTS:
void setCalendar(Akonadi::ETMCalendar::Ptr &calendar);
void setIncidenceChanger(Akonadi::IncidenceChanger *changer);
void setFilterObject(Filter *filterObject);
void setShowCompleted(const int showCompleted);
void setSortBy(const int sortBy);
void setSortAscending(const bool sortAscending);
void setShowCompletedSubtodosInIncomplete(const bool showCompletedSubtodosInIncomplete);
void sortTodoModel();
void filterTodoName(const QString &name, const int showCompleted = ShowAll);
protected:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
......@@ -146,14 +146,14 @@ private:
int compareCompletion(const QModelIndex &left, const QModelIndex &right) const;
Akonadi::ETMCalendar::Ptr m_calendar;
Akonadi::IncidenceTreeModel *m_todoTreeModel = nullptr;
TodoModel *m_baseTodoModel = nullptr;
QScopedPointer<Akonadi::IncidenceTreeModel> m_todoTreeModel;
QScopedPointer<TodoModel> m_baseTodoModel;
Akonadi::IncidenceChanger *m_lastSetChanger = nullptr;
QHash<QString, QColor> m_colors;
KConfigWatcher::Ptr m_colorWatcher;
int m_showCompleted = ShowComplete::ShowAll;
int m_showCompletedStore; // For when searches happen
Filter *m_filterMap = nullptr;
Filter *m_filterObject = nullptr;
int m_sortColumn = DueDateColumn;
bool m_sortAscending = false;
bool m_showCompletedSubtodosInIncomplete = true;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment