Commit ba67d4ec authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Sort sources page by backend name

parent 7879a5b5
......@@ -67,6 +67,7 @@ void DiscoverDeclarativePlugin::registerTypes(const char* /*uri*/)
qmlRegisterUncreatableType<AbstractResource>("org.kde.discover", 2, 0, "AbstractResource", QStringLiteral("should come from the ResourcesModel"));
qmlRegisterUncreatableType<AbstractSourcesBackend>("org.kde.discover", 2, 0, "AbstractSourcesBackend", QStringLiteral("should come from the SourcesModel"));
qmlRegisterUncreatableType<Transaction>("org.kde.discover", 2, 0, "Transaction", QStringLiteral("should come from the backends"));
qmlRegisterUncreatableType<SourcesModel>("org.kde.discover", 2, 0, "SourcesModelClass", QStringLiteral("should come from the backends"));
qmlRegisterType<Rating>();
qmlRegisterType<AbstractResourcesBackend>();
qmlRegisterType<AbstractReviewsBackend>();
......
......@@ -68,6 +68,17 @@
#include <resources/StoredResultsStream.h>
#include <utils.h>
class OurSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus
{
Q_OBJECT
public:
void classBegin() override {}
void componentComplete() override {
if (dynamicSortFilter())
sort(0);
}
};
DiscoverMainWindow::DiscoverMainWindow(CompactMode mode)
: QObject()
, m_collection(this)
......@@ -84,7 +95,7 @@ DiscoverMainWindow::DiscoverMainWindow(CompactMode mode)
qmlRegisterType<PaginateModel>("org.kde.discover.app", 1, 0, "PaginateModel");
qmlRegisterType<KConcatenateRowsProxyModel>("org.kde.discover.app", 1, 0, "KConcatenateRowsProxyModel");
qmlRegisterType<FeaturedModel>("org.kde.discover.app", 1, 0, "FeaturedModel");
qmlRegisterType<QSortFilterProxyModel>("org.kde.discover.app", 1, 0, "QSortFilterProxyModel");
qmlRegisterType<OurSortFilterProxyModel>("org.kde.discover.app", 1, 0, "QSortFilterProxyModel");
qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/qml/DiscoverSystemPalette.qml")), "org.kde.discover.app", 1, 0, "DiscoverSystemPalette");
qmlRegisterType<QQuickView>();
......@@ -407,3 +418,5 @@ void DiscoverMainWindow::showPassiveNotification(const QString& msg)
QMetaObject::invokeMethod(rootObject(), "showPassiveNotification", Qt::QueuedConnection, Q_ARG(QVariant, msg), Q_ARG(QVariant, {}), Q_ARG(QVariant, {}), Q_ARG(QVariant, {}));
});
}
#include "DiscoverMainWindow.moc"
......@@ -46,8 +46,9 @@ DiscoverPage {
mainItem: ListView {
id: sourcesView
model: QSortFilterProxyModel{
model: QSortFilterProxyModel {
filterRegExp: new RegExp(page.search, 'i')
sortRole: SourcesModelClass.SourceNameRole
sourceModel: SourcesModel
}
currentIndex: -1
......
......@@ -27,12 +27,14 @@
Q_GLOBAL_STATIC(SourcesModel, s_sources)
const auto DisplayName = "DisplayName";
class SourceBackendModel : public QAbstractListModel
{
Q_OBJECT
public:
SourceBackendModel(AbstractResourcesBackend* backend, QObject* parent)
: QAbstractListModel(parent), m_backend(backend)
SourceBackendModel(AbstractResourcesBackend* backend)
: QAbstractListModel(backend), m_backend(backend)
{}
QVariant data(const QModelIndex & index, int role) const override {
......@@ -82,25 +84,44 @@ static bool ensureModel(const QList<QByteArray> &roles)
void SourcesModel::addSourcesBackend(AbstractSourcesBackend* sources)
{
auto b = addBackend(qobject_cast<AbstractResourcesBackend*>(sources->parent()));
auto backend = qobject_cast<AbstractResourcesBackend*>(sources->parent());
auto b = addBackend(backend);
if (!b)
return;
b->m_sources = sources;
Q_ASSERT(ensureModel(sources->sources()->roleNames().values()));
addSourceModel(sources->sources());
auto m = sources->sources();
m->setProperty(DisplayName, backend->displayName());
addSourceModel(m);
}
SourceBackendModel* SourcesModel::addBackend(AbstractResourcesBackend* backend)
{
Q_ASSERT(backend);
if (backend->dynamicPropertyNames().contains("InSourcesModel"))
const auto inSourcesModel = "InSourcesModel";
if (backend->dynamicPropertyNames().contains(inSourcesModel))
return nullptr;
backend->setProperty("InSourcesModel", 1);
backend->setProperty(inSourcesModel, 1);
auto b = new SourceBackendModel(backend, this);
auto b = new SourceBackendModel(backend);
b->setProperty(DisplayName, backend->displayName());
addSourceModel(b);
return b;
}
QVariant SourcesModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) return {};
else if (role == SourceNameRole) {
const auto sidx = mapToSource(index);
const auto model = sidx.model();
auto ret = model->property(DisplayName);
return ret;
} else {
return KConcatenateRowsProxyModel::data(index, role);
}
}
#include "SourcesModel.moc"
......@@ -37,12 +37,16 @@ class DISCOVERCOMMON_EXPORT SourcesModel : public KConcatenateRowsProxyModel
public:
enum Roles {
SourcesBackend = Qt::UserRole+1,
SourceNameRole,
ResourcesBackend
};
Q_ENUM(Roles)
explicit SourcesModel(QObject* parent = nullptr);
~SourcesModel() override;
static SourcesModel* global();
QVariant data(const QModelIndex & index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
SourceBackendModel* addBackend(AbstractResourcesBackend* backend);
......
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