diff --git a/YACReaderLibrary/db/folder_model.cpp b/YACReaderLibrary/db/folder_model.cpp index 0fd59c4d..f531dc31 100644 --- a/YACReaderLibrary/db/folder_model.cpp +++ b/YACReaderLibrary/db/folder_model.cpp @@ -108,6 +108,12 @@ void FolderModel::reload() setupModelData(_databasePath); } +void FolderModel::reload(const QModelIndex &index) +{ + // TODO: reload just the content under index for better efficiency + setupModelData(_databasePath); +} + QVariant FolderModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) @@ -155,10 +161,9 @@ QVariant FolderModel::data(const QModelIndex &index, int role) const return item->data(FolderModel::Finished); if (role == FolderModel::MangaRole) - return item->data(FolderModel::Manga); + return item->data(FolderModel::Manga) - if (role == FolderModel::IdRole) - return item->id; + if (role == FolderModel::IdRole) return item->id; if (role == FolderModel::CoverPathRole) return getCoverUrlPathForComicHash(item->data(FirstChildHash).toString()); @@ -210,6 +215,39 @@ QModelIndex FolderModel::index(int row, int column, const QModelIndex &parent) return QModelIndex(); } +void iterate(const QModelIndex &index, + const QAbstractItemModel *model, + const std::function &iteration) +{ + if (index.isValid()) { + auto continueIterating = iteration(index); + if (!continueIterating) { + return; + } + } + if ((index.flags() & Qt::ItemNeverHasChildren) || !model->hasChildren(index)) + return; + auto rows = model->rowCount(index); + for (int i = 0; i < rows; ++i) + iterate(model->index(i, 0, index), model, iteration); +} + +QModelIndex FolderModel::index(qulonglong folderId) const +{ + QModelIndex index; + iterate(QModelIndex(), this, [&](const QModelIndex &idx) { + auto item = static_cast(idx.internalPointer()); + if (item->id == folderId) { + index = idx; + return false; + } + + return true; + }); + + return index; +} + QModelIndex FolderModel::parent(const QModelIndex &index) const { if (!index.isValid()) diff --git a/YACReaderLibrary/db/folder_model.h b/YACReaderLibrary/db/folder_model.h index 8f0093ec..cae4de9e 100644 --- a/YACReaderLibrary/db/folder_model.h +++ b/YACReaderLibrary/db/folder_model.h @@ -54,6 +54,7 @@ public: int role = Qt::DisplayRole) const override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex index(qulonglong folderId) const; QModelIndex parent(const QModelIndex &index) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; @@ -61,6 +62,7 @@ public: // Convenience methods void reload(); + void reload(const QModelIndex &index); void setupModelData(QString path); QString getDatabase(); QString getFolderPath(const QModelIndex &folder); diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 41683da1..32fe2c82 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -1547,10 +1547,14 @@ void LibraryWindow::reloadCurrentFolderComicsContent() void LibraryWindow::reloadAfterCopyMove(const QModelIndex &mi) { if (getCurrentFolderIndex() == mi) { - navigationController->loadFolderInfo(mi); - } + auto item = static_cast(mi.internalPointer()); + auto id = item->id; + foldersModel->reload(mi); + auto newMi = foldersModel->index(id); - foldersModel->reload(); + foldersView->setCurrentIndex(foldersModelProxy->mapFromSource(newMi)); + navigationController->loadFolderInfo(newMi); + } enableNeededActions(); } @@ -2076,6 +2080,7 @@ void LibraryWindow::create(QString source, QString dest, QString name) void LibraryWindow::reloadCurrentLibrary() { + qDebug() << "reloadCurrentLibrary"; loadLibrary(selectedLibrary->currentText()); }