diff --git a/src/utils/model_template/mauilist.cpp b/src/utils/model_template/mauilist.cpp index 22c818c..5366a13 100644 --- a/src/utils/model_template/mauilist.cpp +++ b/src/utils/model_template/mauilist.cpp @@ -1,51 +1,59 @@ /* * * Copyright (C) 2019 camilo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "mauilist.h" #include "mauimodel.h" MauiList::MauiList(QObject *parent) : QObject(parent), m_model(nullptr) {} int MauiList::mappedIndex(const int &index) const { if(this->m_model) - return this->m_model->mapToSource(this->m_model->index(index, 0)).row(); + return this->m_model->mappedToSource(index); + + return -1; +} + +int MauiList::mappedIndexFromSource(const int &index) const +{ + if(this->m_model) + return this->m_model->mappedFromSource(index); return -1; } bool MauiList::exists(const FMH::MODEL_KEY& key, const QString& value) const { return this->indexOf(key, value) >= 0; } int MauiList::indexOf(const FMH::MODEL_KEY& key, const QString& value) const { const auto items = this->items(); const auto it = std::find_if(items.constBegin(), items.constEnd(), [&](const FMH::MODEL &item) -> bool { - return item[key] == value; - + return item[key] == value; + }); if(it != items.constEnd()) - return std::distance(items.constBegin(), it); + return this->mappedIndexFromSource(std::distance(items.constBegin(), it)); else return -1; } diff --git a/src/utils/model_template/mauilist.h b/src/utils/model_template/mauilist.h index 2d52b1b..97ce0c9 100644 --- a/src/utils/model_template/mauilist.h +++ b/src/utils/model_template/mauilist.h @@ -1,79 +1,80 @@ /* * * Copyright (C) 2019 camilo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef MAUILIST_H #define MAUILIST_H #include "fmh.h" #ifndef STATIC_MAUIKIT #include "mauikit_export.h" #endif #include /** * @todo write docs */ #include class MauiModel; #ifdef STATIC_MAUIKIT class MauiList : public QObject, public QQmlParserStatus #else class MAUIKIT_EXPORT MauiList : public QObject, public QQmlParserStatus #endif { Q_INTERFACES(QQmlParserStatus) Q_OBJECT Q_PROPERTY(int count READ getCount NOTIFY countChanged) public: /** * Default constructor */ explicit MauiList(QObject *parent = nullptr); virtual FMH::MODEL_LIST items() const = 0; virtual void classBegin() override {} virtual void componentComplete() override {} int getCount() const {return items().size(); } const MauiModel *m_model; //becarefull this is owned by qml engine, this is only supossed to be a viewer public slots: int mappedIndex(const int &index) const; - + int mappedIndexFromSource(const int &index) const; + protected: bool exists(const FMH::MODEL_KEY &key, const QString &value) const; int indexOf(const FMH::MODEL_KEY &key, const QString &value) const; signals: void preItemAppended(); void postItemAppended(); void preItemAppendedAt(int index); void preItemRemoved(int index); void postItemRemoved(); void updateModel(int index, QVector roles); void preListChanged(); void postListChanged(); void countChanged(); }; #endif // MAUILIST_H diff --git a/src/utils/model_template/mauimodel.cpp b/src/utils/model_template/mauimodel.cpp index 9442803..d0f960d 100644 --- a/src/utils/model_template/mauimodel.cpp +++ b/src/utils/model_template/mauimodel.cpp @@ -1,262 +1,272 @@ /* * * Copyright (C) 2019 camilo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "mauimodel.h" #include "mauilist.h" MauiModel::MauiModel(QObject *parent) : QSortFilterProxyModel(parent), m_model(new PrivateAbstractListModel(this)) { this->setSourceModel(this->m_model); this->setDynamicSortFilter(true); } void MauiModel::setFilterString(const QString& string) { this->setFilterCaseSensitivity(Qt::CaseInsensitive); this->setFilterFixedString(string); // this->setFilterRegExp(QRegExp(string, Qt::CaseInsensitive)); } void MauiModel::setSortOrder(const int &sortOrder) { this->sort(0, static_cast(sortOrder)); } QVariantMap MauiModel::get(const int& index) { QVariantMap res; if(index >= this->rowCount() || index < 0) return res; for(const auto &role : this->roleNames()) res.insert(role, this->index(index, 0).data(FMH::MODEL_NAME_KEY[role]).toString()); return res; } QVariantList MauiModel::getAll() { QVariantList res; for(auto i = 0; i < this->rowCount(); i++) res << this->get(i); return res; } void MauiModel::setFilter(const QString& filter) { if(this->m_filter == filter) return; this->m_filter = filter; emit this->filterChanged(this->m_filter); this->setFilterFixedString(this->m_filter); } const QString MauiModel::getFilter() const { return this->m_filter; } void MauiModel::setSortOrder(const Qt::SortOrder& sortOrder) { if(this->m_sortOrder == sortOrder) return; this->m_sortOrder = sortOrder; emit this->sortOrderChanged(this->m_sortOrder); this->sort(0, this->m_sortOrder); } Qt::SortOrder MauiModel::getSortOrder() const { return this->m_sortOrder; } void MauiModel::setSort(const QString& sort) { if(this->m_sort == sort) return; this->m_sort = sort; emit this->sortChanged(this->m_sort); this->setSortRole([sort, roles = this->roleNames()]() -> int { for(const auto key : roles.keys()) { if(roles[key] == sort) { qDebug()<< "FOUND ROLE KEY "<< key << roles[key] << sort; return key; } } return -1; }()); this->sort(0, this->m_sortOrder); } QString MauiModel::getSort() const { - return this->m_sort; + return this->m_sort; +} + +int MauiModel::mappedFromSource(const int &index) const +{ + return this->mapFromSource(this->m_model->index(index, 0)).row(); +} + +int MauiModel::mappedToSource(const int &index) const +{ + return this->mapToSource(this->index(index, 0)).row(); } bool MauiModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const { if(this->filterRole() != Qt::DisplayRole) { QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); const auto data = this->sourceModel()->data(index, this->filterRole()).toString(); return data.contains(this->filterRegExp()); } for(const auto role : this->sourceModel()->roleNames()) { QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); const auto data = this->sourceModel()->data(index, FMH::MODEL_NAME_KEY[role]).toString(); if(data.contains(this->filterRegExp())) return true; else continue; } return false; } MauiList *MauiModel::getList() const { return this->m_model->getList(); } MauiList * MauiModel::PrivateAbstractListModel::getList() const { return this->list; } void MauiModel::PrivateAbstractListModel::setList(MauiList* value) { beginResetModel(); if(this->list) this->list->disconnect(this); this->list = value; if(this->list) { connect(this->list, &MauiList::preItemAppendedAt, this, [=](int index) { beginInsertRows(QModelIndex(), index, index); }); connect(this->list, &MauiList::preItemAppended, this, [=]() { const int index = this->list->items().size(); beginInsertRows(QModelIndex(), index, index); }); connect(this->list, &MauiList::postItemAppended, this, [=]() { emit this->list->countChanged(); endInsertRows(); }); connect(this->list, &MauiList::preItemRemoved, this, [=](int index) { beginRemoveRows(QModelIndex(), index, index); }); connect(this->list, &MauiList::postItemRemoved, this, [=]() { emit this->list->countChanged(); endRemoveRows(); }); connect(this->list, &MauiList::updateModel, this, [=](int index, QVector roles) { emit this->dataChanged(this->index(index), this->index(index), roles); }); connect(this->list, &MauiList::preListChanged, this, [=]() { beginResetModel(); }); connect(this->list, &MauiList::postListChanged, this, [=]() { emit this->list->countChanged(); endResetModel(); }); } endResetModel(); } void MauiModel::setList(MauiList *value) { this->m_model->setList(value); this->getList()->m_model = this; } MauiModel::PrivateAbstractListModel::PrivateAbstractListModel(MauiModel *model) : QAbstractListModel(model), list(nullptr), m_model(model) {} int MauiModel::PrivateAbstractListModel::rowCount(const QModelIndex &parent) const { if (parent.isValid() || !list) return 0; return list->items().size(); } QVariant MauiModel::PrivateAbstractListModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || !list) return QVariant(); return list->items().at(index.row())[static_cast(role)]; } bool MauiModel::PrivateAbstractListModel::setData(const QModelIndex &index, const QVariant &value, int role) { Q_UNUSED(index); Q_UNUSED(value); Q_UNUSED(role); return false; } Qt::ItemFlags MauiModel::PrivateAbstractListModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; return Qt::ItemIsEditable; // FIXME: Implement me! } QHash MauiModel::PrivateAbstractListModel::roleNames() const { QHash names; for(const auto &key : FMH::MODEL_NAME.keys()) names[key] = QString(FMH::MODEL_NAME[key]).toUtf8(); return names; } diff --git a/src/utils/model_template/mauimodel.h b/src/utils/model_template/mauimodel.h index 53fb499..b398ba2 100644 --- a/src/utils/model_template/mauimodel.h +++ b/src/utils/model_template/mauimodel.h @@ -1,108 +1,111 @@ /* * * Copyright (C) 2019 camilo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef MAUIMODEL_H #define MAUIMODEL_H #include #include #include #include class MauiList; #ifndef STATIC_MAUIKIT #include "mauikit_export.h" #endif #ifdef STATIC_MAUIKIT class MauiModel : public QSortFilterProxyModel #else class MAUIKIT_EXPORT MauiModel : public QSortFilterProxyModel #endif { Q_OBJECT Q_PROPERTY(MauiList *list READ getList WRITE setList) Q_PROPERTY(QString filter READ getFilter WRITE setFilter NOTIFY filterChanged) Q_PROPERTY(Qt::SortOrder sortOrder READ getSortOrder WRITE setSortOrder NOTIFY sortOrderChanged) Q_PROPERTY(QString sort READ getSort WRITE setSort NOTIFY sortChanged) public: MauiModel(QObject *parent = nullptr); MauiList* getList() const; void setList(MauiList *value); protected: bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; private: class PrivateAbstractListModel; PrivateAbstractListModel *m_model; QString m_filter; Qt::SortOrder m_sortOrder; QString m_sort; public slots: void setFilterString(const QString &string); //deprecrated void setSortOrder(const int &sortOrder); //deprecrated QVariantMap get(const int &index); QVariantList getAll(); void setFilter(const QString &filter); const QString getFilter() const; void setSortOrder(const Qt::SortOrder &sortOrder); Qt::SortOrder getSortOrder() const; void setSort(const QString &sort); QString getSort() const; + + int mappedFromSource(const int &index) const; + int mappedToSource(const int &index) const; signals: void listChanged(); void filterChanged(QString filter); void sortOrderChanged(Qt::SortOrder sortOrder); void sortChanged(QString sort); }; class MauiModel::PrivateAbstractListModel : public QAbstractListModel { Q_OBJECT public: PrivateAbstractListModel(MauiModel *model); int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; // Editable: bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; Qt::ItemFlags flags(const QModelIndex& index) const override; virtual QHash roleNames() const override; MauiList* getList() const; void setList(MauiList *value); private: MauiList *list; MauiModel *m_model; }; #endif // MAUIMODEL_H