Commit 76055646 authored by Andreas Cord-Landwehr's avatar Andreas Cord-Landwehr
Browse files

Adapt CourseFilterModel to filter by languages

parent ce966196
......@@ -20,6 +20,7 @@
#include "coursefiltermodel.h"
#include "models/coursemodel.h"
#include "../core/language.h"
#include <KLocalizedString>
#include <QSortFilterProxyModel>
#include <QVariant>
......@@ -28,7 +29,6 @@
CourseFilterModel::CourseFilterModel(QObject* parent)
: QSortFilterProxyModel(parent)
, m_courseModel(nullptr)
, m_view(CourseFilterModel::AllResources)
{
}
......@@ -38,22 +38,22 @@ CourseModel * CourseFilterModel::courseModel() const
return m_courseModel;
}
void CourseFilterModel::setView(CourseFilterModel::CourseResourceView view)
Language * CourseFilterModel::language() const
{
if (view == m_view) {
return m_language;
}
void CourseFilterModel::setLanguage(Language *language)
{
if (m_language == language) {
return;
}
m_view = view;
m_language = language;
emit languageChanged();
invalidateFilter();
emit viewChanged();
emit filteredCountChanged();
}
CourseFilterModel::CourseResourceView CourseFilterModel::view() const
{
return m_view;
}
void CourseFilterModel::setCourseModel(CourseModel *courseModel)
{
if (courseModel == m_courseModel) {
......@@ -81,15 +81,12 @@ bool CourseFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourc
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
switch (m_view) {
case CourseFilterModel::AllResources:
if (m_language == nullptr) {
return true;
}
else if (sourceModel()->data(index, CourseModel::LanguageRole).value<Language*>()->id() == m_language->id()) {
return true;
// case CourseFilterModel::OnlyContributorResources: //FIXME this role was removed
// return sourceModel()->data(index, CourseModel::ContributerResourceRole).toBool();
// case CourseFilterModel::OnlyGetHotNewStuffResources:
// return !sourceModel()->data(index, CourseModel::ContributerResourceRole).toBool();
}
Q_UNREACHABLE();
return false;
}
......
/*
* Copyright 2014-2015 Andreas Cord-Landwehr <cordlandwehr@gkde.org>
* Copyright 2014-2019 Andreas Cord-Landwehr <cordlandwehr@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
......@@ -25,40 +25,34 @@
class Course;
class CourseModel;
class Language;
class CourseFilterModel : public QSortFilterProxyModel
{
Q_OBJECT
Q_PROPERTY(CourseModel *courseModel READ courseModel WRITE setCourseModel NOTIFY courseModelChanged)
Q_PROPERTY(CourseResourceView view READ view WRITE setView NOTIFY viewChanged)
Q_PROPERTY(int filteredCount READ filteredCount NOTIFY filteredCountChanged)
Q_PROPERTY(Language *language READ language WRITE setLanguage NOTIFY languageChanged)
public:
Q_ENUMS(CourseResourceView);
enum CourseResourceView {
OnlyGetHotNewStuffResources = 1,
OnlyContributorResources = 2,
AllResources = 3
};
explicit CourseFilterModel(QObject *parent = nullptr);
CourseModel * courseModel() const;
void setCourseModel(CourseModel* courseModel);
int filteredCount() const;
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
void setView(CourseResourceView view);
CourseResourceView view() const;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
void setLanguage(Language *language);
Language * language() const;
Q_INVOKABLE QVariant course(int row) const;
Q_SIGNALS:
void courseModelChanged();
void viewChanged();
void sortOptionChanged();
void filteredCountChanged();
void languageChanged();
private:
CourseModel *m_courseModel;
CourseResourceView m_view;
CourseModel *m_courseModel{ nullptr };
Language *m_language{ nullptr };
};
#endif
......@@ -49,7 +49,6 @@ Item {
}
CourseModel {
id: courseModel
language: g_editorSession.language
}
UnitModel {
id: selectedUnitModel
......@@ -140,16 +139,20 @@ Item {
id: comboCourse
visible: !g_editorSession.skeletonMode
Layout.fillWidth: true
model: courseModel
model: CourseFilterModel {
id: courseFilterModel
courseModel: courseModel
language: g_editorSession.language
}
textRole: "title"
onCurrentIndexChanged: {
if (courseModel.course(currentIndex)) {
g_editorSession.course = courseModel.course(languageSelectionComboBox.currentIndex)
if (courseFilterModel.course(currentIndex)) {
g_editorSession.course = courseFilterModel.course(languageSelectionComboBox.currentIndex)
}
}
onVisibleChanged: {
if (visible && courseModel.course(currentIndex)) {
g_editorSession.course = courseModel.course(languageSelectionComboBox.currentIndex)
if (visible && courseFilterModel.course(currentIndex)) {
g_editorSession.course = courseFilterModel.course(languageSelectionComboBox.currentIndex)
}
}
}
......
Markdown is supported
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