diff --git a/gpt4all-chat/localdocsmodel.cpp b/gpt4all-chat/localdocsmodel.cpp index e12e3773..b2bea0fa 100644 --- a/gpt4all-chat/localdocsmodel.cpp +++ b/gpt4all-chat/localdocsmodel.cpp @@ -75,88 +75,60 @@ QHash LocalDocsModel::roleNames() const return roles; } -void LocalDocsModel::updateInstalled(int folder_id, bool b) +template +void LocalDocsModel::updateField(int folder_id, T value, + const std::function& updater, + const QVector& roles) { for (int i = 0; i < m_collectionList.size(); ++i) { if (m_collectionList.at(i).folder_id != folder_id) continue; - m_collectionList[i].installed = b; - emit collectionItemUpdated(i, m_collectionList[i]); - emit dataChanged(this->index(i), this->index(i), {InstalledRole}); + updater(m_collectionList[i], value); + emit dataChanged(this->index(i), this->index(i), roles); } } +void LocalDocsModel::updateInstalled(int folder_id, bool b) +{ + updateField(folder_id, b, + [](CollectionItem& item, bool val) { item.installed = val; }, {InstalledRole}); +} + void LocalDocsModel::updateIndexing(int folder_id, bool b) { - for (int i = 0; i < m_collectionList.size(); ++i) { - if (m_collectionList.at(i).folder_id != folder_id) - continue; - - m_collectionList[i].indexing = b; - emit collectionItemUpdated(i, m_collectionList[i]); - emit dataChanged(this->index(i), this->index(i), {IndexingRole}); - } + updateField(folder_id, b, + [](CollectionItem& item, bool val) { item.indexing = val; }, {IndexingRole}); } void LocalDocsModel::updateCurrentDocsToIndex(int folder_id, size_t currentDocsToIndex) { - for (int i = 0; i < m_collectionList.size(); ++i) { - if (m_collectionList.at(i).folder_id != folder_id) - continue; - - m_collectionList[i].currentDocsToIndex = currentDocsToIndex; - emit collectionItemUpdated(i, m_collectionList[i]); - emit dataChanged(this->index(i), this->index(i), {CurrentDocsToIndexRole}); - } + updateField(folder_id, currentDocsToIndex, + [](CollectionItem& item, size_t val) { item.currentDocsToIndex = val; }, {CurrentDocsToIndexRole}); } void LocalDocsModel::updateTotalDocsToIndex(int folder_id, size_t totalDocsToIndex) { - for (int i = 0; i < m_collectionList.size(); ++i) { - if (m_collectionList.at(i).folder_id != folder_id) - continue; - - m_collectionList[i].totalDocsToIndex = totalDocsToIndex; - emit collectionItemUpdated(i, m_collectionList[i]); - emit dataChanged(this->index(i), this->index(i), {TotalDocsToIndexRole}); - } + updateField(folder_id, totalDocsToIndex, + [](CollectionItem& item, size_t val) { item.totalDocsToIndex = val; }, {TotalDocsToIndexRole}); } void LocalDocsModel::subtractCurrentBytesToIndex(int folder_id, size_t subtractedBytes) { - for (int i = 0; i < m_collectionList.size(); ++i) { - if (m_collectionList.at(i).folder_id != folder_id) - continue; - - m_collectionList[i].currentBytesToIndex -= subtractedBytes; - emit collectionItemUpdated(i, m_collectionList[i]); - emit dataChanged(this->index(i), this->index(i), {CurrentBytesToIndexRole}); - } + updateField(folder_id, subtractedBytes, + [](CollectionItem& item, size_t val) { item.currentBytesToIndex -= val; }, {CurrentBytesToIndexRole}); } void LocalDocsModel::updateCurrentBytesToIndex(int folder_id, size_t currentBytesToIndex) { - for (int i = 0; i < m_collectionList.size(); ++i) { - if (m_collectionList.at(i).folder_id != folder_id) - continue; - - m_collectionList[i].currentBytesToIndex = currentBytesToIndex; - emit collectionItemUpdated(i, m_collectionList[i]); - emit dataChanged(this->index(i), this->index(i), {CurrentBytesToIndexRole}); - } + updateField(folder_id, currentBytesToIndex, + [](CollectionItem& item, size_t val) { item.currentBytesToIndex = val; }, {CurrentBytesToIndexRole}); } void LocalDocsModel::updateTotalBytesToIndex(int folder_id, size_t totalBytesToIndex) { - for (int i = 0; i < m_collectionList.size(); ++i) { - if (m_collectionList.at(i).folder_id != folder_id) - continue; - - m_collectionList[i].totalBytesToIndex = totalBytesToIndex; - emit collectionItemUpdated(i, m_collectionList[i]); - emit dataChanged(this->index(i), this->index(i), {TotalBytesToIndexRole}); - } + updateField(folder_id, totalBytesToIndex, + [](CollectionItem& item, size_t val) { item.totalBytesToIndex = val; }, {TotalBytesToIndexRole}); } void LocalDocsModel::addCollectionItem(const CollectionItem &item) diff --git a/gpt4all-chat/localdocsmodel.h b/gpt4all-chat/localdocsmodel.h index 41d37bd2..47997143 100644 --- a/gpt4all-chat/localdocsmodel.h +++ b/gpt4all-chat/localdocsmodel.h @@ -42,9 +42,6 @@ public: QVariant data(const QModelIndex &index, int role) const override; QHash roleNames() const override; -Q_SIGNALS: - void collectionItemUpdated(int index, const CollectionItem& item); - public Q_SLOTS: void updateInstalled(int folder_id, bool b); void updateIndexing(int folder_id, bool b); @@ -60,7 +57,10 @@ public Q_SLOTS: void collectionListUpdated(const QList &collectionList); private: - void updateItem(int index, const CollectionItem& item); + template + void updateField(int folder_id, T value, + const std::function& updater, + const QVector& roles); private: QList m_collectionList;