Коммит 297cea9e создал по автору OMP Education's avatar OMP Education Зафиксировано автором k.sagalakova
Просмотр файлов

[View] Add preload all pages.



Co-Authored-By: default avatarAnton Zernin <a.zernin@omp.ru>
владелец ea6ba86e
// SPDX-FileCopyrightText: 2022 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef BASEDOCUMENT_H
......@@ -41,6 +41,7 @@ public:
virtual QString path() const;
virtual QSizeF pageSize(int pageNumber) const = 0;
virtual int count() const = 0;
virtual void loadAllPages() = 0;
virtual QSharedPointer<BasePage> loadPage(int pageIndex) = 0;
virtual void startLoadBookmarks() const = 0;
virtual QVector<BaseBookmark *> bookmarks() const = 0;
......
// SPDX-FileCopyrightText: 2022 - 2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#include "interface/basedocument.h"
......@@ -68,6 +68,12 @@ void PagesWithNotesModel::setNewData(BaseDocument *provider)
endResetModel();
}
void PagesWithNotesModel::preLoadAllNotes()
{
if (m_documentProvider)
m_documentProvider->loadAllPages();
}
void PagesWithNotesModel::_addPage(int pageIndex)
{
if (m_documentProvider == nullptr)
......
// SPDX-FileCopyrightText: 2022 - 2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef PAGESWITHNOTESMODEL_H
......@@ -29,6 +29,9 @@ public:
void setNewData(BaseDocument *newData);
public slots:
void preLoadAllNotes();
private slots:
void _addPage(int pageIndex);
......
// SPDX-FileCopyrightText: 2022 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#include <QMutexLocker>
......@@ -9,9 +9,11 @@
QMutex PagePreloader::m_mutex;
PagePreloader::PagePreloader(QSharedPointer<PdfDocument> document, int pageIndex) :
PagePreloader::PagePreloader(QSharedPointer<PdfDocument> document, int pageIndex, int countPages) :
m_document(document),
m_pageIndex(pageIndex)
m_pageIndex(pageIndex),
m_count(countPages),
m_isCanceled(false)
{ }
PagePreloader::~PagePreloader() = default;
......@@ -23,11 +25,22 @@ void PagePreloader::run()
return;
}
QMutexLocker lock(&m_mutex);
auto pageFuture = m_document->page(m_pageIndex);
pageFuture.waitForFinished();
if (pageFuture.isFinished() && !pageFuture.isCanceled())
emit done(m_pageIndex, BaseDocument::PageLoadStatus::Success);
else
emit done(m_pageIndex, BaseDocument::PageLoadStatus::Fail);
for (int i = 0; i < m_count; i++) {
if (m_isCanceled)
break;
QMutexLocker lock(&m_mutex);
auto pageFuture = m_document->page(m_pageIndex + i);
pageFuture.waitForFinished();
if (pageFuture.isFinished() && !pageFuture.isCanceled())
emit done(m_pageIndex + i, BaseDocument::PageLoadStatus::Success);
else
emit done(m_pageIndex + i, BaseDocument::PageLoadStatus::Fail);
}
}
void PagePreloader::cancel()
{
m_isCanceled = true;
}
// SPDX-FileCopyrightText: 2022 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef PAGEPRELOADER_H
......@@ -17,10 +17,11 @@ class PagePreloader : public QObject, public QRunnable
Q_OBJECT
public:
PagePreloader(QSharedPointer<PdfDocument> document, int pageIndex);
PagePreloader(QSharedPointer<PdfDocument> document, int pageIndex, int countPages = 1);
~PagePreloader() override;
void run() override;
void cancel();
signals:
void done(int, BaseDocument::PageLoadStatus);
......@@ -28,6 +29,8 @@ signals:
private:
QSharedPointer<PdfDocument> m_document;
int m_pageIndex;
int m_count;
bool m_isCanceled;
static QMutex m_mutex;
};
......
// SPDX-FileCopyrightText: 2022 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#include <amberpdf/pdfdocument.h>
......@@ -12,8 +12,8 @@ PagesSizesLoader::PagesSizesLoader(QSharedPointer<PdfDocument> pdfiumDocument) :
void PagesSizesLoader::run()
{
auto pagesCount = m_pdfiumDocument->pageCount();
m_pageSizes.clear();
for (int i = 0; i < pagesCount; ++i) {
auto sizeFuture = m_pdfiumDocument->pageSize(i);
sizeFuture.waitForFinished();
......
// SPDX-FileCopyrightText: 2022 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#include <QFile>
#include <QFutureWatcher>
#include <QSizeF>
#include <QThreadPool>
#include <QVector>
#include <QPair>
#include <amberpdf/pdfdocument.h>
#include <amberpdf/pdfbookmark.h>
......@@ -23,7 +25,11 @@ PdfDocumentItem::PdfDocumentItem(QObject *parent) : BaseDocument(parent)
m_status = DocumentStatus::Null;
}
PdfDocumentItem::~PdfDocumentItem() = default;
PdfDocumentItem::~PdfDocumentItem()
{
if (m_preloaderAllPage)
m_preloaderAllPage->cancel();
}
QString PdfDocumentItem::path() const
{
......@@ -43,6 +49,55 @@ int PdfDocumentItem::count() const
return m_pdfiumDocument ? m_pdfiumDocument->pageCount() : -1;
}
void PdfDocumentItem::loadAllPages()
{
if (m_preloaderAllPage)
return;
QVector<int> notLoadedPage;
for (int i = 0; i < count(); i++) {
if (!m_loadedPages.contains(i) && !m_pagesInProcess.contains(i))
notLoadedPage.append(i);
}
if (notLoadedPage.empty()) {
return;
}
QVector<QPair<int, int>> rangesToLoad;
int startPage = notLoadedPage[0];
int endPage = startPage;
for (int i = 1; i < notLoadedPage.size(); ++i) {
if (notLoadedPage[i] == endPage + 1) {
endPage = notLoadedPage[i];
} else {
rangesToLoad.push_back(QPair(startPage, endPage));
startPage = endPage = notLoadedPage[i];
}
}
rangesToLoad.push_back(QPair(startPage, endPage));
for (const auto &range : rangesToLoad) {
m_preloaderAllPage = new PagePreloader(m_pdfiumDocument, range.first, range.second - range.first);
connect(m_preloaderAllPage, &PagePreloader::done, this, [&] (int loadedPageIndex, PageLoadStatus loadStatus) {
if (loadStatus == PageLoadStatus::Success) {
if (!m_loadedPages.contains(loadedPageIndex))
m_loadedPages.insert(loadedPageIndex, QSharedPointer<BasePage>(
new PdfPageItem(m_pdfiumDocument->page(loadedPageIndex))));
}
emit pageLoaded(loadedPageIndex, loadStatus);
});
QThreadPool::globalInstance()->start(m_preloaderAllPage, -1);
for (int i = range.first; i < range.second; i++)
m_pagesInProcess.insert(i);
}
}
QSharedPointer<BasePage> PdfDocumentItem::loadPage(int pageIndex)
{
if (pageIndex < 0 || pageIndex >= count())
......
// SPDX-FileCopyrightText: 2022 Open Mobile Platform LLC <community@omp.ru>
// SPDX-FileCopyrightText: 2022-2023 Open Mobile Platform LLC <community@omp.ru>
// SPDX-License-Identifier: BSD-3-Clause
#ifndef PDFDOCUMENTITEM_H
......@@ -9,9 +9,11 @@
#include <QHash>
#include <QSet>
#include <QVector>
#include <QPointer>
#include "basedocument.h"
#include "pdfpageitem.h"
#include "pagepreloader.h"
class PdfPage;
class PdfDocument;
......@@ -26,6 +28,7 @@ public:
QString path() const override;
QSizeF pageSize(int pageNumber) const override;
int count() const override;
void loadAllPages() override;
QSharedPointer<BasePage> loadPage(int pageIndex) override;
void startLoadBookmarks() const override;
QVector<BaseBookmark *> bookmarks() const override;
......@@ -35,14 +38,16 @@ public:
public slots:
void setPath(const QString &path) override;
private:
void onPagePreloaderDone(int loadedPageIndex, PageLoadStatus loadStatus);
private:
QSharedPointer<PdfDocument> m_pdfiumDocument;
QHash<int, QSizeF> m_pageSizes;
QHash<int, QSharedPointer<BasePage>> m_loadedPages;
QSet<int> m_pagesInProcess;
QVector<BaseBookmark *> m_baseBookmarks;
void onPagePreloaderDone(int loadedPageIndex, PageLoadStatus loadStatus);
QPointer<PagePreloader> m_preloaderAllPage;
};
#endif // PDFDOCUMENTITEM_H
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать