/* * Copyright (C) 2014 Christian Mollekopf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #pragma once #include #include #include #include #include #include "query.h" #include "log.h" #include "resultprovider.h" #include "threadboundary.h" template class ModelResult : public QAbstractItemModel { public: enum Roles { DomainObjectRole = Qt::UserRole + 1, ChildrenFetchedRole, DomainObjectBaseRole }; ModelResult(const Sink::Query &query, const QList &propertyColumns, const Sink::Log::Context &); ~ModelResult(); void setEmitter(const typename Sink::ResultEmitter::Ptr &); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QModelIndex parent(const QModelIndex &index) const; bool hasChildren(const QModelIndex &parent = QModelIndex()) const; bool canFetchMore(const QModelIndex &parent) const; void fetchMore(const QModelIndex &parent); void setFetcher(const std::function &fetcher); private: void add(const Ptr &value); void modify(const Ptr &value); void remove(const Ptr &value); bool childrenFetched(const QModelIndex &) const; qint64 parentId(const Ptr &value); QModelIndex createIndexFromId(const qint64 &id) const; void fetchEntities(const QModelIndex &parent); Sink::Log::Context mLogCtx; // TODO we should be able to directly use T as index, with an appropriate hash function, and thus have a QMap and QList QMap mEntities; QMap /* child entity id*/> mTree; QMap mParents; QSet mEntityChildrenFetched; QSet mEntityChildrenFetchComplete; QSet mEntityAllChildrenFetched; QList mPropertyColumns; Sink::Query mQuery; std::function loadEntities; typename Sink::ResultEmitter::Ptr mEmitter; async::ThreadBoundary threadBoundary; };