From 69ac4488c21b4065472f7184ea1b7a4570b046d3 Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Wed, 11 Nov 2026 22:01:30 +0100 Subject: [PATCH 1/5] Fix and improve double page mode Loading pages can't cause a force single page in double page mode anymore. --- CHANGELOG.md | 7 +- YACReader/CMakeLists.txt | 2 + YACReader/double_page_layout.cpp | 251 +++++++++++ YACReader/double_page_layout.h | 131 ++++++ YACReader/render.cpp | 411 ++++++++++++----- YACReader/render.h | 33 +- YACReader/viewer.cpp | 7 + common/comic.cpp | 1 + common/comic.h | 2 + tests/CMakeLists.txt | 1 + tests/double_page_layout_test/CMakeLists.txt | 16 + tests/double_page_layout_test/main.cpp | 440 +++++++++++++++++++ 12 files changed, 1181 insertions(+), 121 deletions(-) create mode 100644 YACReader/double_page_layout.cpp create mode 100644 YACReader/double_page_layout.h create mode 100644 tests/double_page_layout_test/CMakeLists.txt create mode 100644 tests/double_page_layout_test/main.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f199ea77..366d9d93a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ Version counting is based on semantic versioning (Major.Feature.Patch) +## 10.5.1 + +### YACReader + +* Fix double page mode when turning pages quickly. It also improves the double page mode behavior. + ## 10.5.0 ### YACReader @@ -56,7 +62,6 @@ Version counting is based on semantic versioning (Major.Feature.Patch) * Fix the apps sometimes getting stuck in fullscreen mode after a restart. YACReaderLibrary no longer starts in fullscreen mode, and YACReader still does when you close it in fullscreen mode, but you can always leave it. - ## 10.4.0 ### YACReader diff --git a/YACReader/CMakeLists.txt b/YACReader/CMakeLists.txt index e87209b97..98669ae1d 100644 --- a/YACReader/CMakeLists.txt +++ b/YACReader/CMakeLists.txt @@ -19,6 +19,8 @@ qt_add_executable(YACReader WIN32 continuous_page_provider.cpp continuous_view_model.h continuous_view_model.cpp + double_page_layout.h + double_page_layout.cpp mouse_handler.h mouse_handler.cpp viewer.h diff --git a/YACReader/double_page_layout.cpp b/YACReader/double_page_layout.cpp new file mode 100644 index 000000000..272006489 --- /dev/null +++ b/YACReader/double_page_layout.cpp @@ -0,0 +1,251 @@ +#include "double_page_layout.h" + +#include + +void DoublePageLayout::reset(int numPages, bool coverIsSinglePage) +{ + pageCount = qMax(0, numPages); + coverAlone = coverIsSinglePage; + shapes.fill(PageShape::Unknown, pageCount); + userAnchor = -1; + keptAnchors.clear(); + dirty = true; +} + +int DoublePageLayout::numPages() const +{ + return pageCount; +} + +void DoublePageLayout::setCoverIsSinglePage(bool coverIsSinglePage) +{ + if (coverAlone != coverIsSinglePage) { + coverAlone = coverIsSinglePage; + dirty = true; + } +} + +bool DoublePageLayout::coverIsSinglePage() const +{ + return coverAlone; +} + +void DoublePageLayout::setPageShape(int page, PageShape shape) +{ + if (page < 0 || page >= pageCount || shapes[page] == shape) { + return; + } + shapes[page] = shape; + dirty = true; +} + +void DoublePageLayout::setAnchor(int page) +{ + userAnchor = (page >= 0 && page < pageCount) ? page : -1; + keptAnchors.clear(); + dirty = true; +} + +void DoublePageLayout::keepView(const View &view) +{ + if (!view.isValid() || view.last >= pageCount || viewStartingAt(view.first) == view) { + return; + } + + keptAnchors.insert(view.first); + if (!view.isDouble() && view.first + 1 < pageCount) { + keptAnchors.insert(view.first + 1); + } + dirty = true; +} + +DoublePageLayout::View DoublePageLayout::viewAt(int page) const +{ + if (page < 0 || page >= pageCount) { + return { }; + } + ensureLayout(); + const int first = viewStarts[page]; + const int last = (first + 1 < pageCount && viewStarts[first + 1] == first) ? first + 1 : first; + return { first, last }; +} + +DoublePageLayout::View DoublePageLayout::viewStartingAt(int page) const +{ + const View view = viewAt(page); + if (!view.isValid() || view.first == page) { + return view; + } + return { page, page }; +} + +DoublePageLayout::View DoublePageLayout::nextView(const View &view) const +{ + if (!view.isValid() || view.last + 1 >= pageCount) { + return { }; + } + return viewAt(view.last + 1); +} + +DoublePageLayout::View DoublePageLayout::previousView(const View &view) const +{ + if (!view.isValid() || view.first <= 0) { + return { }; + } + return viewAt(view.first - 1); +} + +DoublePageLayout::View DoublePageLayout::firstView() const +{ + return viewAt(0); +} + +bool DoublePageLayout::isSinglePage(int page) const +{ + return (page == 0 && coverAlone) || shapes[page] == PageShape::Wide; +} + +void DoublePageLayout::ensureLayout() const +{ + if (!dirty) { + return; + } + dirty = false; + + viewStarts.resize(pageCount); + + auto isAnchor = [this](int page) { + return page == userAnchor || keptAnchors.contains(page); + }; + + int page = 0; + while (page < pageCount) { + if (isSinglePage(page)) { + viewStarts[page] = page; + page++; + continue; + } + + // run of pages that can be paired: [start, end] + const int start = page; + int end = start; + while (end + 1 < pageCount && !isSinglePage(end + 1) && !isAnchor(end + 1)) { + end++; + } + + // an anchor on a page that is alone anyway (the cover, a wide page) does not change the run before it + const bool backwards = end + 1 < pageCount && isAnchor(end + 1) && !isSinglePage(end + 1) && !isAnchor(start); + if (backwards) { + for (int i = end; i >= start; i -= 2) { + if (i - 1 >= start) { + viewStarts[i - 1] = i - 1; + viewStarts[i] = i - 1; + } else { + viewStarts[i] = i; + } + } + } else { + for (int i = start; i <= end; i += 2) { + viewStarts[i] = i; + if (i + 1 <= end) { + viewStarts[i + 1] = i; + } + } + } + + page = end + 1; + } +} + +void DoublePageNavigator::reset(int page) +{ + pageLayout.setAnchor(page); + intent = Intent::None; + intentPage = -1; + lastDisplayedView = { }; +} + +void DoublePageNavigator::setPageShape(int page, DoublePageLayout::PageShape shape) +{ + pageLayout.setPageShape(page, shape); + keepDisplayedView(); +} + +DoublePageNavigator::View DoublePageNavigator::viewFor(int page) const +{ + if (page >= 0 && page < pageLayout.numPages()) { + return pageLayout.viewStartingAt(page); + } + return { page, page }; +} + +int DoublePageNavigator::next(int currentPage) +{ + keepDisplayedView(); + const View view = viewFor(currentPage); + if (!view.isValid() || view.last + 1 >= pageLayout.numPages()) { + return -1; + } + + intent = Intent::Next; + intentPage = view.last + 1; + return resolve(currentPage); +} + +int DoublePageNavigator::previous(int currentPage) +{ + keepDisplayedView(); + const View view = viewFor(currentPage); + if (!view.isValid() || view.first <= 0) { + return -1; + } + + intent = Intent::Previous; + intentPage = view.first - 1; + return resolve(currentPage); +} + +int DoublePageNavigator::last() +{ + if (pageLayout.numPages() <= 0) { + return -1; + } + + pageLayout.setAnchor(-1); + lastDisplayedView = { }; + intent = Intent::Previous; + intentPage = pageLayout.numPages() - 1; + return resolve(-1); +} + +int DoublePageNavigator::resolve(int currentPage) const +{ + if (intent == Intent::None) { + return currentPage; + } + + const View view = pageLayout.viewAt(intentPage); + if (!view.isValid()) { + return currentPage; + } + + // never show again a page of the view the user comes from + if (intent == Intent::Next && view.first < intentPage && lastDisplayedView.contains(view.first)) { + return intentPage; + } + return view.first; +} + +void DoublePageNavigator::viewDisplayed(const View &view) +{ + lastDisplayedView = view; + intent = Intent::None; + intentPage = -1; +} + +void DoublePageNavigator::keepDisplayedView() +{ + if (lastDisplayedView.isValid()) { + pageLayout.keepView(lastDisplayedView); + } +} diff --git a/YACReader/double_page_layout.h b/YACReader/double_page_layout.h new file mode 100644 index 000000000..dee23628c --- /dev/null +++ b/YACReader/double_page_layout.h @@ -0,0 +1,131 @@ +#ifndef DOUBLE_PAGE_LAYOUT_H +#define DOUBLE_PAGE_LAYOUT_H + +#include +#include + +// Groups the pages of a comic in the views of the double page mode. +// +// The layout is a pure function of what is known about the pages, it does not keep any navigation history: +// - the cover is alone when the cover is a single page, +// - a wide page (as seen on screen) is alone, +// - the other pages are paired in order from the start of their run. +// +// The page sizes are not known in advance (an archive is extracted while the user reads), a page with an +// unknown shape is laid out as a portrait page. The shape of a page is set once it is known. +// +// Anchors force a view to start at a page: +// - the anchor set by the user (open at a page, go to a page, manual offset). The run of pages that ends +// right before it is paired backwards from it, so the lone page of an odd run is placed far from the user. +// - the kept views. A view that the user has seen must not change when new page sizes are known. +// +// An anchor on a page that already starts a view (the cover, a wide page) does not change anything. +class DoublePageLayout +{ +public: + enum class PageShape { + Unknown, + Portrait, + Wide + }; + + struct View { + int first = -1; + int last = -1; + + bool isValid() const { return first >= 0 && last >= first; } + bool isDouble() const { return isValid() && last > first; } + bool contains(int page) const { return isValid() && page >= first && page <= last; } + bool operator==(const View &other) const { return first == other.first && last == other.last; } + bool operator!=(const View &other) const { return !(*this == other); } + }; + + // Removes everything: shapes, anchors and kept views. + void reset(int numPages, bool coverIsSinglePage); + + int numPages() const; + + void setCoverIsSinglePage(bool coverIsSinglePage); + bool coverIsSinglePage() const; + + void setPageShape(int page, PageShape shape); + + // The user anchor, -1 removes it. Kept views are removed too, a new anchor is a clean state. + void setAnchor(int page); + + // Makes sure that `view` stays a view of the layout. It adds an anchor only if the layout changed under it. + void keepView(const View &view); + + // The view that contains `page`. + View viewAt(int page) const; + // The view that starts at `page`, the page is shown alone if it is the second page of a pair. + View viewStartingAt(int page) const; + View nextView(const View &view) const; + View previousView(const View &view) const; + View firstView() const; + +private: + bool isSinglePage(int page) const; + void ensureLayout() const; + + int pageCount = 0; + bool coverAlone = true; + QVector shapes; + int userAnchor = -1; + QSet keptAnchors; + + mutable bool dirty = true; + // the first page of the view of each page + mutable QVector viewStarts; +}; + +// The navigation in double page mode on top of a DoublePageLayout. +// +// A page turn (key, mouse wheel, click...) keeps its intent ("the view after page X", "the view before page X") +// until its view is displayed, because new page sizes can change the view of a page while it is loading. +// The last displayed view is kept, so what the user sees (or comes from) never changes. +class DoublePageNavigator +{ +public: + using View = DoublePageLayout::View; + + DoublePageLayout &layout() { return pageLayout; } + const DoublePageLayout &layout() const { return pageLayout; } + + // A clean state: `page` starts a view (open, go to, offset, rotation, settings). + void reset(int page); + + // Sets the shape of a page without changing the last displayed view. + void setPageShape(int page, DoublePageLayout::PageShape shape); + + // The view shown for `page`, it is alone if the layout pairs it with the previous page. + View viewFor(int page) const; + + // The page of the next or previous view, -1 when there is none. + int next(int currentPage); + int previous(int currentPage); + // The page of the last view, laid out like when the comic is read from the cover. + int last(); + + // The page that must be current now, the pending page turn is resolved with what is known now. + int resolve(int currentPage) const; + bool hasPendingPageTurn() const { return intent != Intent::None; } + + void viewDisplayed(const View &view); + +private: + enum class Intent { + None, + Next, + Previous + }; + + void keepDisplayedView(); + + DoublePageLayout pageLayout; + Intent intent = Intent::None; + int intentPage = -1; + View lastDisplayedView; +}; + +#endif // DOUBLE_PAGE_LAYOUT_H diff --git a/YACReader/render.cpp b/YACReader/render.cpp index fd92fce0d..d3d7c8773 100644 --- a/YACReader/render.cpp +++ b/YACReader/render.cpp @@ -5,9 +5,11 @@ #include "yacreader_global_gui.h" #include +#include #include #include #include +#include #include #include #include @@ -27,6 +29,30 @@ inline const T &kClamp(const T &x, const T &low, const T &high) return x; } +namespace { + +// only the header of the image is read, the image is not decoded +QSize imageSizeFromData(const QByteArray &data) +{ + if (data.isEmpty()) { + return { }; + } + QBuffer buffer; + buffer.setData(data); + if (!buffer.open(QIODevice::ReadOnly)) { + return { }; + } + QImageReader reader(&buffer); + return reader.size(); +} + +bool coverIsSinglePageSetting() +{ + return Configuration::getConfiguration().getSettings()->value(COVER_IS_SP, true).toBool(); +} + +} // namespace + inline int changeBrightness(int value, int brightness) { return kClamp(value + brightness * 255 / 100, 0, 255); @@ -369,10 +395,8 @@ void PageRender::run() //----------------------------------------------------------------------------- Render::Render() - : comic(nullptr), doublePage(false), doubleMangaPage(false), currentIndex(0), doublePageViewHistoryPosition(0), numLeftPages(4), numRightPages(4), loadedComic(false), imageRotation(0) + : comic(nullptr), doublePage(false), doubleMangaPage(false), currentIndex(0), numLeftPages(4), numRightPages(4), loadedComic(false), imageRotation(0) { - doublePageViewHistory.append(currentIndex); - int size = numLeftPages + numRightPages + 1; currentPageBufferedIndex = numLeftPages; for (int i = 0; i < size; i++) { @@ -464,10 +488,36 @@ void Render::render() QPixmap *Render::getCurrentPage() { auto page = new QPixmap(); - *page = page->fromImage(*buffer[currentPageBufferedIndex]); + *page = page->fromImage(*buffer[currentPageBufferedIndex + (shownSinglePage() - currentIndex)]); return page; } +// the page that is shown when the current view is not shown as a double page. +// In double page mode it is the second page of the view if the first one cannot be rendered. +int Render::shownSinglePage() const +{ + if (!doublePage || !buffer[currentPageBufferedIndex]->isNull() || buffer[currentPageBufferedIndex + 1]->isNull()) { + return currentIndex; + } + if (!currentDoublePageView().isDouble() || !pageCannotBeRendered(currentIndex)) { + return currentIndex; + } + return currentIndex + 1; +} + +// the page could not be extracted, or its render ended without an image +bool Render::pageCannotBeRendered(int page) const +{ + if (page >= 0 && page < failedPages.size() && failedPages[page]) { + return true; + } + const int index = currentPageBufferedIndex + (page - currentIndex); + if (index < 0 || index >= buffer.size()) { + return false; + } + return buffer[index]->isNull() && pageRenders[index] != nullptr && pageRenders[index]->isFinished(); +} + QPixmap *Render::getCurrentDoublePage(qreal *seamRatio, Qt::Orientation *seamOrientation) { if (currentPageIsDoublePage()) { @@ -597,71 +647,10 @@ const QImage *Render::bufferedImage(int absolutePageIndex) const bool Render::currentPageIsDoublePage() { - if (currentIndex == 0 && Configuration::getConfiguration().getSettings()->value(COVER_IS_SP, true).toBool()) { - return false; - } - if (doublePageHistoryTracksCurrentIndex() && doublePageViewHistoryPosition + 1 < doublePageViewHistory.size() && - currentIndex + 1 >= doublePageViewHistory.at(doublePageViewHistoryPosition + 1)) { - // Keep the second page out of the next view already recorded in history. - return false; - } - if (buffer[currentPageBufferedIndex]->isNull() || buffer[currentPageBufferedIndex + 1]->isNull()) { - return false; - } - if (imageRotation == 0 || imageRotation == 180) { - if (buffer[currentPageBufferedIndex]->height() > buffer[currentPageBufferedIndex]->width() && - buffer[currentPageBufferedIndex + 1]->height() > buffer[currentPageBufferedIndex + 1]->width()) { - return true; - } - } else if (imageRotation == 90 || imageRotation == 270) { - if (buffer[currentPageBufferedIndex]->width() > buffer[currentPageBufferedIndex]->height() && - buffer[currentPageBufferedIndex + 1]->width() > buffer[currentPageBufferedIndex + 1]->height()) { - return true; - } - } - return false; -} - -bool Render::nextPageIsDoublePage() -{ - // this function is not used right now - if (buffer[currentPageBufferedIndex + 2]->isNull() || buffer[currentPageBufferedIndex + 3]->isNull()) { - return false; - } - if (imageRotation == 0 || imageRotation == 180) { - if (buffer[currentPageBufferedIndex + 2]->height() > buffer[currentPageBufferedIndex + 2]->width() && - buffer[currentPageBufferedIndex + 3]->height() > buffer[currentPageBufferedIndex + 3]->width()) { - return true; - } - } else if (imageRotation == 90 || imageRotation == 270) { - if (buffer[currentPageBufferedIndex]->width() > buffer[currentPageBufferedIndex]->height() && - buffer[currentPageBufferedIndex + 1]->width() > buffer[currentPageBufferedIndex + 1]->height()) { - return true; - } - } - return false; -} - -bool Render::previousPageIsDoublePage() -{ - if (currentIndex == 2 && Configuration::getConfiguration().getSettings()->value(COVER_IS_SP, true).toBool()) { - return false; - } - if (buffer[currentPageBufferedIndex - 1]->isNull() || buffer[currentPageBufferedIndex - 2]->isNull()) { + if (!currentDoublePageView().isDouble()) { return false; } - if (imageRotation == 0 || imageRotation == 180) { - if (buffer[currentPageBufferedIndex - 1]->height() > buffer[currentPageBufferedIndex - 1]->width() && - buffer[currentPageBufferedIndex - 2]->height() > buffer[currentPageBufferedIndex - 2]->width()) { - return true; - } - } else if (imageRotation == 90 || imageRotation == 270) { - if (buffer[currentPageBufferedIndex - 1]->width() > buffer[currentPageBufferedIndex - 1]->height() && - buffer[currentPageBufferedIndex - 2]->width() > buffer[currentPageBufferedIndex - 2]->height()) { - return true; - } - } - return false; + return !buffer[currentPageBufferedIndex]->isNull() && !buffer[currentPageBufferedIndex + 1]->isNull(); } void Render::setRotation(int degrees) @@ -683,17 +672,57 @@ void Render::prepareAvailablePage(int page) { emit pageRendered(page); + const int bufferIndex = currentPageBufferedIndex + (page - currentIndex); + if (page >= 0 && page < pageSizes.size() && !pageSizes[page].isValid() && !failedPages[page] && + bufferIndex >= 0 && bufferIndex < buffer.size() && !buffer[bufferIndex]->isNull()) { + // the header of this image did not give its size, the decoded image does (it is already rotated) + QSize size = buffer[bufferIndex]->size(); + if (imageRotation == 90 || imageRotation == 270) { + size.transpose(); + } + const auto viewBefore = currentDoublePageView(); + pageSizes[page] = size; + updatePageShape(page); + refreshDoublePageView(viewBefore); + } + if (!doublePage) { if (currentIndex == page) { emit currentPageReady(); } - } else { - // check for last page in double page mode - if ((currentIndex == page) && (currentIndex + 1) >= (int)comic->numPages()) { - emit currentPageReady(); - } else if ((currentIndex == page && !buffer[currentPageBufferedIndex + 1]->isNull()) || - (currentIndex + 1 == page && !buffer[currentPageBufferedIndex]->isNull())) { - emit currentPageReady(); + } else if (currentDoublePageView().contains(page)) { + showDoublePageViewIfRendered(page); + } +} + +// the view is shown when all its pages are rendered, `renderedPage` is the page that has just been rendered (if any) +void Render::showDoublePageViewIfRendered(int renderedPage) +{ + const auto view = currentDoublePageView(); + for (int viewPage = view.first; viewPage <= view.last; ++viewPage) { + const int index = currentPageBufferedIndex + (viewPage - currentIndex); + // a page that cannot be rendered does not stop the rest of its view + if (viewPage != renderedPage && buffer[index]->isNull() && !pageCannotBeRendered(viewPage)) { + return; + } + } + + doublePageNavigator.viewDisplayed(view); + emit currentPageReady(); +} + +// a failed page is never rendered, so its view is shown when the failure is known +void Render::showDoublePageViewWithFailedPages() +{ + if (!doublePage || !doublePageLayoutIsReady()) { + return; + } + + const auto view = currentDoublePageView(); + for (int viewPage = view.first; viewPage <= view.last; ++viewPage) { + if (viewPage < failedPages.size() && failedPages[viewPage]) { + showDoublePageViewIfRendered(); + return; } } } @@ -773,6 +802,8 @@ void Render::createComic(const QString &path) connect(comic, &Comic::crcErrorFound, this, &Render::crcError, Qt::QueuedConnection); connect(comic, QOverload<>::of(&Comic::errorOpening), this, &Render::reset, Qt::QueuedConnection); connect(comic, QOverload::of(&Comic::imageLoaded), this, &Render::pageRawDataReady, Qt::QueuedConnection); + connect(comic, &Comic::pageFailed, this, &Render::pageFailed, Qt::QueuedConnection); + connect(comic, &Comic::imagesLoaded, this, &Render::allPagesProcessed, Qt::QueuedConnection); connect(comic, QOverload::of(&Comic::imageLoaded), this, QOverload::of(&Render::imageLoaded), Qt::QueuedConnection); connect(comic, &Comic::openAt, this, &Render::renderAt, Qt::QueuedConnection); connect(comic, QOverload::of(&Comic::numPages), this, QOverload::of(&Render::numPages), Qt::QueuedConnection); @@ -836,18 +867,19 @@ void Render::reset() // la página sólo se renderiza, si realmente ha cambiado. void Render::nextPage() { - const int nextPage = doublePage ? nextDoublePageViewIndex() : comic->nextPage(); + if (doublePage) { + nextDoublePageView(); + return; + } + + const int nextPage = comic->nextPage(); // se fuerza renderizado si la página ha cambiado if (currentIndex != nextPage) { - if (doublePage) { - comic->setIndex(nextPage); - } previousIndex = currentIndex; currentIndex = nextPage; update(); emit pageChanged(currentIndex); - } else if (hasLoadedComic() && (doublePage || (unsigned int)currentIndex == numPages() - 1)) { - // in double page mode the view only stays put when it is already the last one + } else if (hasLoadedComic() && ((unsigned int)currentIndex == numPages() - 1)) { emit isLast(); } } @@ -856,13 +888,15 @@ void Render::nextPage() // la página sólo se renderiza, si realmente ha cambiado. void Render::previousPage() { - const int previousPage = doublePage ? previousDoublePageViewIndex() : comic->previousPage(); + if (doublePage) { + previousDoublePageView(); + return; + } + + const int previousPage = comic->previousPage(); // se fuerza renderizado si la página ha cambiado if (currentIndex != previousPage) { - if (doublePage) { - comic->setIndex(previousPage); - } previousIndex = currentIndex; currentIndex = previousPage; update(); @@ -872,7 +906,7 @@ void Render::previousPage() } } -// moves the double page grouping by one page, the new grouping starts a new navigation history +// moves the double page grouping by one page, the page becomes the anchor of a new layout void Render::offsetDoublePage(int offset) { if (!hasLoadedComic()) { @@ -916,6 +950,10 @@ bool Render::hasLoadedComic() void Render::setNumPages(unsigned int numPages) { pagesReady.fill(false, numPages); + pageSizes.fill(QSize(), numPages); + failedPages.fill(false, numPages); + doublePageNavigator.layout().reset(numPages, coverIsSinglePageSetting()); + resetDoublePageNavigation(); } void Render::pageRawDataReady(int page) @@ -923,6 +961,7 @@ void Render::pageRawDataReady(int page) if (!hasLoadedComic()) return; + const auto viewBefore = currentDoublePageView(); pagesEmited.push_back(page); if (pageRenders.size() > 0) { for (int i = 0; i < pagesEmited.size(); i++) { @@ -932,6 +971,7 @@ void Render::pageRawDataReady(int page) } pagesReady[pagesEmited.at(i)] = true; + readPageSize(pagesEmited.at(i)); if (pagesEmited.at(i) == currentIndex) update(); else { @@ -943,6 +983,43 @@ void Render::pageRawDataReady(int page) } pagesEmited.clear(); } + + refreshDoublePageView(viewBefore); +} + +// the pages that could not be extracted are laid out as portrait pages, so their views can be shown +void Render::pageFailed(int page) +{ + if (page < 0 || page >= failedPages.size() || failedPages[page]) { + return; + } + + const auto viewBefore = currentDoublePageView(); + failedPages[page] = true; + updatePageShape(page); + refreshDoublePageView(viewBefore); + showDoublePageViewWithFailedPages(); +} + +void Render::allPagesProcessed() +{ + if (!hasLoadedComic()) { + return; + } + + const auto viewBefore = currentDoublePageView(); + bool newFailedPages = false; + for (int page = 0; page < pagesReady.size() && page < failedPages.size(); ++page) { + if (!pagesReady[page] && !failedPages[page]) { + failedPages[page] = true; + updatePageShape(page); + newFailedPages = true; + } + } + refreshDoublePageView(viewBefore); + if (newFailedPages) { + showDoublePageViewWithFailedPages(); + } } // sólo se renderiza la página, si ha habido un cambio de página @@ -952,16 +1029,33 @@ void Render::goTo(int index) comic->setIndex(index); previousIndex = currentIndex; currentIndex = index; + resetDoublePageNavigation(); update(); emit pageChanged(currentIndex); + } else { + resetDoublePageNavigation(); } +} - resetDoublePageNavigation(); +void Render::goToLastPage() +{ + if (!hasLoadedComic() || numPages() == 0) { + return; + } + + const int lastPage = static_cast(numPages()) - 1; + if (!doublePage || !doublePageLayoutIsReady()) { + goTo(lastPage); + return; + } + + moveToDoublePageView(doublePageNavigator.last()); } void Render::rotateRight() { imageRotation = (imageRotation + 90) % 360; + updateAllPageShapes(); resetDoublePageNavigation(); reload(); } @@ -971,58 +1065,149 @@ void Render::rotateLeft() imageRotation = 270; else imageRotation = imageRotation - 90; + updateAllPageShapes(); resetDoublePageNavigation(); reload(); } +// a clean state: the current page starts a view, nothing seen before is kept void Render::resetDoublePageNavigation() { - doublePageViewHistory.clear(); - doublePageViewHistory.append(currentIndex); - doublePageViewHistoryPosition = 0; + doublePageNavigator.layout().setCoverIsSinglePage(coverIsSinglePageSetting()); + doublePageNavigator.reset(currentIndex); } -bool Render::doublePageHistoryTracksCurrentIndex() const +void Render::updateDoublePageSettings() { - return doublePageViewHistoryPosition < doublePageViewHistory.size() && doublePageViewHistory.at(doublePageViewHistoryPosition) == currentIndex; + if (coverIsSinglePageSetting() == doublePageNavigator.layout().coverIsSinglePage()) { + return; + } + + resetDoublePageNavigation(); + if (comic != nullptr && doublePage) { + update(); + } } -// the views already visited are replayed so going back and forth keeps the same page groupings -int Render::nextDoublePageViewIndex() +// the view shown for the current page, the page is alone if the layout pairs it with the previous one +DoublePageLayout::View Render::currentDoublePageView() const { - if (!doublePageHistoryTracksCurrentIndex()) { - resetDoublePageNavigation(); + return doublePageNavigator.viewFor(currentIndex); +} + +bool Render::doublePageLayoutIsReady() +{ + const int layoutPages = doublePageNavigator.layout().numPages(); + return comic != nullptr && layoutPages > 0 && layoutPages == static_cast(numPages()); +} + +void Render::readPageSize(int page) +{ + if (page < 0 || page >= pageSizes.size() || comic == nullptr || page >= comic->getRawData()->size()) { + return; + } + + pageSizes[page] = imageSizeFromData(comic->getRawData()->at(page)); + updatePageShape(page); +} + +// the shape of a page as it is seen on screen +DoublePageLayout::PageShape Render::pageShapeOnScreen(int page) const +{ + if (failedPages[page]) { + return DoublePageLayout::PageShape::Portrait; + } + if (!pageSizes[page].isValid()) { + return DoublePageLayout::PageShape::Unknown; } - if (doublePageViewHistoryPosition + 1 < doublePageViewHistory.size()) { - return doublePageViewHistory.at(++doublePageViewHistoryPosition); + QSize size = pageSizes[page]; + if (imageRotation == 90 || imageRotation == 270) { + size.transpose(); } + // only pages taller than wide are paired + return size.height() > size.width() ? DoublePageLayout::PageShape::Portrait : DoublePageLayout::PageShape::Wide; +} - const int pageStep = currentPageIsDoublePage() ? 2 : 1; - if (currentIndex + pageStep >= static_cast(comic->numPages())) { - return currentIndex; +void Render::updatePageShape(int page) +{ + if (page < 0 || page >= pageSizes.size()) { + return; } - doublePageViewHistory.append(currentIndex + pageStep); - return doublePageViewHistory.at(++doublePageViewHistoryPosition); + // the view the user sees (or comes from) never changes when new page sizes are known + doublePageNavigator.setPageShape(page, pageShapeOnScreen(page)); } -int Render::previousDoublePageViewIndex() +// for a rotation, the caller starts a clean navigation after it, so no view is kept +void Render::updateAllPageShapes() { - if (!doublePageHistoryTracksCurrentIndex()) { - resetDoublePageNavigation(); + for (int page = 0; page < pageSizes.size(); ++page) { + doublePageNavigator.layout().setPageShape(page, pageShapeOnScreen(page)); + } +} + +void Render::refreshDoublePageView(const DoublePageLayout::View &viewBefore) +{ + if (!doublePage || !doublePageLayoutIsReady()) { + return; + } + + if (doublePageNavigator.hasPendingPageTurn()) { + const int page = doublePageNavigator.resolve(currentIndex); + if (page != currentIndex) { + moveToDoublePageView(page); + return; + } + } + + // e.g. the second page of a pending pair is wide, the first page is now shown alone + if (currentDoublePageView() != viewBefore) { + update(); + } +} + +void Render::moveToDoublePageView(int page) +{ + if (page == currentIndex) { + update(); + return; } - if (doublePageViewHistoryPosition > 0) { - return doublePageViewHistory.at(--doublePageViewHistoryPosition); + comic->setIndex(page); + previousIndex = currentIndex; + currentIndex = page; + update(); + emit pageChanged(currentIndex); +} + +void Render::nextDoublePageView() +{ + if (!hasLoadedComic() || !doublePageLayoutIsReady()) { + return; } - const int pageStep = previousPageIsDoublePage() ? 2 : 1; - const int previousPage = qMax(currentIndex - pageStep, 0); - if (previousPage != currentIndex) { - doublePageViewHistory.prepend(previousPage); + const int page = doublePageNavigator.next(currentIndex); + if (page < 0) { + // the view only stays put when it is already the last one + emit isLast(); + return; + } + moveToDoublePageView(page); +} + +void Render::previousDoublePageView() +{ + if (!hasLoadedComic() || !doublePageLayoutIsReady()) { + return; + } + + const int page = doublePageNavigator.previous(currentIndex); + if (page < 0) { + emit isCover(); + return; } - return previousPage; + moveToDoublePageView(page); } // Actualiza el buffer, añadiendo las imágenes (vacías) necesarias para su posterior renderizado y @@ -1158,7 +1343,7 @@ void Render::doubleMangaPageSwitch() QString Render::getCurrentPagesInformation() { - QString s = QString::number(currentIndex + 1); + QString s = QString::number(shownSinglePage() + 1); if (doublePage && (currentIndex + 1 < (int)comic->numPages())) { if (currentPageIsDoublePage()) { if (doubleMangaPage) diff --git a/YACReader/render.h b/YACReader/render.h index b5014fcf5..e795706d2 100644 --- a/YACReader/render.h +++ b/YACReader/render.h @@ -2,11 +2,13 @@ #define RENDER_H #include "comic.h" +#include "double_page_layout.h" #include #include #include #include +#include #include #include //----------------------------------------------------------------------------- @@ -141,10 +143,10 @@ public slots: QPixmap *getCurrentDoublePage(qreal *seamRatio = nullptr, Qt::Orientation *seamOrientation = nullptr); QPixmap *getCurrentDoubleMangaPage(qreal *seamRatio = nullptr, Qt::Orientation *seamOrientation = nullptr); bool currentPageIsDoublePage(); - bool nextPageIsDoublePage(); - bool previousPageIsDoublePage(); const QImage *bufferedImage(int absolutePageIndex) const; void goTo(int index); + // in double page mode it goes to the last view, so the last pages are paired like when reading to the end + void goToLastPage(); void doublePageSwitch(); void setManga(bool manga); void doubleMangaPageSwitch(); @@ -154,6 +156,10 @@ public slots: void update(); void setNumPages(unsigned int numPages); void pageRawDataReady(int page); + void pageFailed(int page); + void allPagesProcessed(); + // reads the double page settings again (cover as a single page) + void updateDoublePageSettings(); //--comic interface void nextPage(); void previousPage(); @@ -208,8 +214,10 @@ public slots: bool doubleMangaPage; int previousIndex; int currentIndex; - QVector doublePageViewHistory; - int doublePageViewHistoryPosition; + // the view of each page is computed from what is known about the pages, see DoublePageLayout + DoublePageNavigator doublePageNavigator; + QVector pageSizes; + QVector failedPages; // QPixmap * currentPage; int currentPageBufferedIndex; int numLeftPages; @@ -225,9 +233,20 @@ public slots: int imageRotation; QVector filters; void resetDoublePageNavigation(); - int nextDoublePageViewIndex(); - int previousDoublePageViewIndex(); - bool doublePageHistoryTracksCurrentIndex() const; + DoublePageLayout::View currentDoublePageView() const; + bool doublePageLayoutIsReady(); + void readPageSize(int page); + DoublePageLayout::PageShape pageShapeOnScreen(int page) const; + void updatePageShape(int page); + void updateAllPageShapes(); + int shownSinglePage() const; + bool pageCannotBeRendered(int page) const; + void showDoublePageViewIfRendered(int renderedPage = -1); + void showDoublePageViewWithFailedPages(); + void refreshDoublePageView(const DoublePageLayout::View &viewBefore); + void moveToDoublePageView(int page); + void nextDoublePageView(); + void previousDoublePageView(); QMutex mutex; friend class PageRender; diff --git a/YACReader/viewer.cpp b/YACReader/viewer.cpp index 100d40ccd..c7fff8c81 100644 --- a/YACReader/viewer.cpp +++ b/YACReader/viewer.cpp @@ -445,6 +445,12 @@ void Viewer::goToFirstPage() } void Viewer::goToLastPage() { + if (doublePage && !continuousScroll) { + direction = 1; + render->goToLastPage(); + return; + } + goTo(this->render->numPages() - 1); } void Viewer::goTo(unsigned int page) @@ -464,6 +470,7 @@ void Viewer::goTo(unsigned int page) void Viewer::onImageOptionsChanged() { + render->updateDoublePageSettings(); if (continuousScroll) { continuousWidget->invalidateScaledImageCache(); } else { diff --git a/common/comic.cpp b/common/comic.cpp index a340b7030..621d05867 100644 --- a/common/comic.cpp +++ b/common/comic.cpp @@ -496,6 +496,7 @@ void FileComic::crcError(int index) QStringList pageNumbers; for (int page : pages) { pageNumbers.append(QString::number(page + 1)); + emit pageFailed(page); } const QString affectedPages = pageNumbers.isEmpty() ? QString::number(index + 1) : pageNumbers.join(QStringLiteral(", ")); diff --git a/common/comic.h b/common/comic.h index 87469cf65..0f5620023 100644 --- a/common/comic.h +++ b/common/comic.h @@ -110,6 +110,8 @@ public slots: void errorOpening(); void errorOpening(QString); void crcErrorFound(QString); + // the page could not be extracted, its data will never be loaded + void pageFailed(int index); void isBookmark(bool); void bookmarksUpdated(); void isCover(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bbb731f53..20b3eade0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(concurrent_queue_test) add_subdirectory(compressed_archive_test) add_subdirectory(continuous_view_model_test) +add_subdirectory(double_page_layout_test) add_subdirectory(pdf_render_size_test) add_subdirectory(folder_rename_test) add_subdirectory(library_item_actions_test) diff --git a/tests/double_page_layout_test/CMakeLists.txt b/tests/double_page_layout_test/CMakeLists.txt new file mode 100644 index 000000000..9a4341546 --- /dev/null +++ b/tests/double_page_layout_test/CMakeLists.txt @@ -0,0 +1,16 @@ +# Double page layout test + +qt_add_executable(double_page_layout_test + main.cpp + ${PROJECT_SOURCE_DIR}/YACReader/double_page_layout.cpp + ${PROJECT_SOURCE_DIR}/YACReader/double_page_layout.h +) +yacreader_apply_build_options(double_page_layout_test) +target_include_directories(double_page_layout_test PRIVATE + ${PROJECT_SOURCE_DIR}/YACReader +) +target_link_libraries(double_page_layout_test PRIVATE + Qt6::Core + Qt6::Test +) +add_test(NAME double_page_layout_test COMMAND double_page_layout_test) diff --git a/tests/double_page_layout_test/main.cpp b/tests/double_page_layout_test/main.cpp new file mode 100644 index 000000000..7a269a1c5 --- /dev/null +++ b/tests/double_page_layout_test/main.cpp @@ -0,0 +1,440 @@ +#include "double_page_layout.h" + +#include +#include +#include +#include +#include + +#include + +using Shape = DoublePageLayout::PageShape; +using View = DoublePageLayout::View; + +namespace { + +QString viewText(const View &view) +{ + if (view.isDouble()) { + return QStringLiteral("[%1,%2]").arg(view.first + 1).arg(view.last + 1); + } + return QStringLiteral("[%1]").arg(view.first + 1); +} + +// The comic is extracted while the user reads. Page numbers are 0-based, the texts use 1-based numbers. +struct Scenario { + const char *id; + const char *title; + int numPages; + std::vector widePages; // as seen on screen + int openAt; + bool coverIsSinglePage; + std::vector extractionOrder; + double extractionSpeed; // pages extracted per page turn while the key is held, < 0: every size is known at once + int readingTime; // pages extracted while the user looks at a view + std::vector failedPages; // never extracted (CRC error), known as portrait pages when the extraction ends + std::vector actions; + const char *expectedViews; +}; + +// Plays the part of Render: page sizes arrive in extraction order, a view is displayed when all its pages are known. +class Reader +{ +public: + explicit Reader(const Scenario &scenario) + : s(scenario) + { + for (int page : s.widePages) { + wide.insert(page); + } + for (int page : s.failedPages) { + failed.insert(page); + } + + navigator.layout().reset(s.numPages, s.coverIsSinglePage); + if (s.extractionSpeed < 0) { + for (int page = 0; page < s.numPages; ++page) { + if (!failed.contains(page)) { + learn(page); + } + } + learnFailedPages(); + } + navigator.reset(s.openAt); + position = s.openAt; + tryDisplay(); + } + + void run(const QString &action) + { + const QStringList parts = action.split(QLatin1Char(' ')); + const QString kind = parts.at(0); + if (kind.contains(QLatin1Char('*'))) { + const QStringList repeated = kind.split(QLatin1Char('*')); + for (int i = 0; i < repeated.at(1).toInt(); ++i) { + turn(repeated.at(0) == QLatin1String("next"), 1, false); + } + } else if (kind == QLatin1String("next") || kind == QLatin1String("prev")) { + const int count = parts.size() > 1 ? parts.at(1).toInt() : 1; + const bool hold = parts.size() > 2 && parts.at(2) == QLatin1String("hold"); + turn(kind == QLatin1String("next"), count, hold); + } else if (kind == QLatin1String("goto")) { + jumpTo(parts.at(1).toInt() - 1); + } else if (kind == QLatin1String("shift")) { + jumpTo(qBound(0, position + parts.at(1).toInt(), s.numPages - 1)); + } else if (kind == QLatin1String("cover")) { + navigator.layout().setCoverIsSinglePage(parts.at(1) == QLatin1String("on")); + jumpTo(lastShown.isValid() ? lastShown.first : position); + } else if (kind == QLatin1String("last")) { + newEpoch(); + position = navigator.last(); + waitForDisplay(); + } else if (kind == QLatin1String("wait")) { + extract(s.numPages); + tryDisplay(); + } else { + QFAIL(qPrintable(QStringLiteral("unknown action %1").arg(action))); + } + } + + QString shownViews() const { return shown.join(QLatin1Char(' ')); } + QStringList problems; + +private: + struct PageTurn { + bool next = true; + int presses = 0; + }; + + void turn(bool next, int count, bool hold) + { + int presses = 0; + for (int i = 0; i < count; ++i) { + const int page = next ? navigator.next(position) : navigator.previous(position); + if (page >= 0) { + position = page; + presses++; + } + if (hold && s.extractionSpeed >= 0) { + extract(s.extractionSpeed); + tryDisplay(); // a view may flash while the key is held + } + } + if (presses == 0) { + return; + } + pendingTurn = PageTurn { next, presses }; + hasPendingTurn = true; + waitForDisplay(); + } + + void jumpTo(int page) + { + newEpoch(); + position = page; + navigator.reset(page); + waitForDisplay(); + } + + void newEpoch() + { + lastShown = { }; + hasPendingTurn = false; + seenViews.clear(); + } + + void learn(int page) + { + known.insert(page); + navigator.setPageShape(page, wide.contains(page) ? Shape::Wide : Shape::Portrait); + // Render resolves the pending page turn when new sizes are known + if (navigator.hasPendingPageTurn()) { + position = navigator.resolve(position); + } + } + + void learnFailedPages() + { + for (int page : std::as_const(failed)) { + known.insert(page); + navigator.setPageShape(page, Shape::Portrait); + } + } + + void extract(double pages) + { + accumulated += pages; + while (accumulated >= 1 && known.size() < s.numPages) { + for (int page : s.extractionOrder) { + if (!known.contains(page) && !failed.contains(page)) { + learn(page); + break; + } + } + if (known.size() + failed.size() >= s.numPages) { + learnFailedPages(); + } + accumulated -= 1; + } + } + + bool tryDisplay() + { + if (navigator.hasPendingPageTurn()) { + position = navigator.resolve(position); + } + const View view = navigator.viewFor(position); + for (int page = view.first; page <= view.last; ++page) { + if (!known.contains(page)) { + return false; + } + } + navigator.viewDisplayed(view); + if (view == lastShown) { + return true; + } + + if (view.isDouble() && (wide.contains(view.first) || wide.contains(view.last))) { + problems << QStringLiteral("wide page paired in %1").arg(viewText(view)); + } + if (lastShown.isValid() && hasPendingTurn) { + if (view.contains(lastShown.first) || view.contains(lastShown.last)) { + problems << QStringLiteral("duplicated page: %1 -> %2").arg(viewText(lastShown), viewText(view)); + } + if (pendingTurn.presses == 1) { + const bool skipped = pendingTurn.next ? view.first > lastShown.last + 1 : view.last < lastShown.first - 1; + if (skipped) { + problems << QStringLiteral("skipped pages: %1 -> %2").arg(viewText(lastShown), viewText(view)); + } + } + } + for (int page = view.first; page <= view.last; ++page) { + const auto seen = seenViews.constFind(page); + if (seen != seenViews.constEnd() && *seen != viewText(view)) { + problems << QStringLiteral("page %1 was in %2, now in %3").arg(page + 1).arg(*seen, viewText(view)); + } + seenViews.insert(page, viewText(view)); + } + + shown << viewText(view); + lastShown = view; + hasPendingTurn = false; + return true; + } + + void waitForDisplay() + { + int guard = 0; + while (!tryDisplay()) { + extract(1); + if (++guard > 10 * s.numPages) { + problems << QStringLiteral("the view of page %1 is never displayed").arg(position + 1); + return; + } + } + if (s.extractionSpeed >= 0) { + extract(s.readingTime); + } + } + + const Scenario &s; + DoublePageNavigator navigator; + QSet wide; + QSet failed; + QSet known; + double accumulated = 0; + int position = 0; + View lastShown; + PageTurn pendingTurn; + bool hasPendingTurn = false; + QHash seenViews; + QStringList shown; +}; + +// Generated with a simulator of the double page mode (all the cases reviewed for the fix). Each row: id, title, pages, wide pages, +// open at, cover is single page, extraction order, extraction speed, reading time, failed pages, actions, views shown. +const std::vector scenarios = { + { "K1", "All portrait, open at cover, read to end and back", 24, { }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*30", "prev*30" }, "[1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16,17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [16,17] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "K2", "Spread p17, open at cover, read to end and back", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*30", "prev*30" }, "[1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16] [17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "K3", "Spread p18, open at cover, read to end and back", 24, { 17 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*30", "prev*30" }, "[1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16,17] [18] [19,20] [21,22] [23,24] [21,22] [19,20] [18] [16,17] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "K4", "Resume at p16, spread p17, forward to end, back to cover", 24, { 16 }, 15, true, { 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, -1, 2, { }, { "next*30", "prev*30" }, "[16] [17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "K5", "Resume at p15, spread p17, forward, back", 24, { 16 }, 14, true, { 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, -1, 2, { }, { "next*30", "prev*30" }, "[15,16] [17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [17] [15,16] [13,14] [11,12] [9,10] [7,8] [5,6] [3,4] [2] [1]" }, + { "K6", "Resume at p16, spread p17, back first, then forward", 24, { 16 }, 15, true, { 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, -1, 2, { }, { "prev*30", "next*30" }, "[16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16] [17] [18,19] [20,21] [22,23] [24]" }, + { "K7", "Resume at p17 (the spread itself)", 24, { 16 }, 16, true, { 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, -1, 2, { }, { "prev*3", "next*5" }, "[17] [16] [14,15] [12,13] [14,15] [16] [17] [18,19] [20,21]" }, + { "K8", "Resume at p18 (just after the spread)", 24, { 16 }, 17, true, { 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }, -1, 2, { }, { "prev*3", "next*5" }, "[18,19] [17] [16] [14,15] [16] [17] [18,19] [20,21] [22,23]" }, + { "K9", "Two spreads with one page between: p10, p12", 24, { 9, 11 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*30", "prev*30" }, "[1] [2,3] [4,5] [6,7] [8,9] [10] [11] [12] [13,14] [15,16] [17,18] [19,20] [21,22] [23,24] [21,22] [19,20] [17,18] [15,16] [13,14] [12] [11] [10] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "K10", "Two consecutive spreads: p10, p11", 24, { 9, 10 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*30", "prev*30" }, "[1] [2,3] [4,5] [6,7] [8,9] [10] [11] [12,13] [14,15] [16,17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [16,17] [14,15] [12,13] [11] [10] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "K11", "Spread right after cover: p2", 24, { 1 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*4", "prev*4" }, "[1] [2] [3,4] [5,6] [7,8] [5,6] [3,4] [2] [1]" }, + { "K12", "Spread is the last page", 24, { 23 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*30", "prev*3" }, "[1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16,17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19]" }, + { "K13", "Wide cover (p1 is a spread)", 24, { 0 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*4", "prev*4" }, "[1] [2,3] [4,5] [6,7] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "K14", "Cover-is-single OFF, spread p17", 24, { 16 }, 0, false, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*30", "prev*30" }, "[1,2] [3,4] [5,6] [7,8] [9,10] [11,12] [13,14] [15,16] [17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [17] [15,16] [13,14] [11,12] [9,10] [7,8] [5,6] [3,4] [1,2]" }, + { "K15", "Odd page count (23 pages), all portrait", 23, { }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }, -1, 2, { }, { "next*30", "prev*3" }, "[1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16,17] [18,19] [20,21] [22,23] [20,21] [18,19] [16,17]" }, + { "K16", "Back and forth at the spread many times (p16)", 24, { 16 }, 15, true, { 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, -1, 2, { }, { "prev", "next*2", "prev*2", "next*3", "prev*2" }, "[16] [14,15] [16] [17] [16] [14,15] [16] [17] [18,19] [17] [16]" }, + { "K17", "Slider jump p3 -> p20 (spread p17), then back past the spread", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next", "goto 20", "prev*6" }, "[1] [2,3] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11]" }, + { "K18", "Slider jump p3 -> p19 (spread p17), then back", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next", "goto 19", "prev*6" }, "[1] [2,3] [19,20] [18] [17] [16] [14,15] [12,13] [10,11]" }, + { "K19", "Manual shift +1 at p6, read past spread p17, back", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*3", "shift 1", "next*8", "prev*10" }, "[1] [2,3] [4,5] [6,7] [7,8] [9,10] [11,12] [13,14] [15,16] [17] [18,19] [20,21] [22,23] [20,21] [18,19] [17] [15,16] [13,14] [11,12] [9,10] [7,8] [5,6] [3,4]" }, + { "K20", "Manual shift -1 at p6, read on", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*3", "shift -1", "next*4", "prev*4" }, "[1] [2,3] [4,5] [6,7] [5,6] [7,8] [9,10] [11,12] [13,14] [11,12] [9,10] [7,8] [5,6]" }, + { "K21", "Jump to p10, then jump back to p2 (old anchor dropped)", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "goto 10", "next*2", "goto 2", "next*6" }, "[1] [10,11] [12,13] [14,15] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15]" }, + { "S1", "FORUM CASE: all portrait, hold the key, extraction 1 page/press", 24, { }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 1, 2, { }, { "next 20 hold", "prev*30" }, "[24] [22,23] [20,21] [18,19] [16,17] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "S2", "All portrait, hold key, extraction 0.5 page/press", 24, { }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 0.5, 2, { }, { "next 20 hold", "prev*30", "next*30" }, "[24] [22,23] [20,21] [18,19] [16,17] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16,17] [18,19] [20,21] [22,23] [24]" }, + { "S3", "Spread p17, slow extraction, normal reading from cover", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 1, 2, { }, { "next*30", "prev*30" }, "[2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16] [17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "S4", "Spread p17, hold key past the unknown spread, then back", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 0.5, 2, { }, { "next 12 hold", "prev*30" }, "[24] [22,23] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "S5", "Resume p16, spread p17, slow extraction, back first (p1-15 extracted last)", 24, { 16 }, 15, true, { 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 1, 2, { }, { "prev*30", "next*30" }, "[14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1] [2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16] [17] [18,19] [20,21] [22,23] [24]" }, + { "S6", "Resume p16, spread p10 unknown, back past it while it is still unknown", 24, { 9 }, 15, true, { 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 0.5, 0, { }, { "prev*5", "wait all", "next*5", "prev*5" }, "[14,15] [12,13] [11] [10] [8,9] [10] [11] [12,13] [14,15] [16,17] [14,15] [12,13] [11] [10] [8,9]" }, + { "S7", "Resume p16, spread p17 extracted LAST (archive order != page order)", 24, { 16 }, 15, true, { 15, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16 }, 1, 2, { }, { "next*6", "prev*6" }, "[18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [17] [16] [14,15]" }, + { "S8", "Read from cover, spread p17 extracted LAST, quick reading", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 16 }, 2, 0, { }, { "next*30", "prev*30" }, "[2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16] [17] [18,19] [20,21] [22,23] [24] [22,23] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "S9", "Spread p5 extracted last; user reads p6-p12, sees it late", 24, { 4 }, 0, true, { 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 4 }, 3, 0, { }, { "next*6", "wait all", "prev*6" }, "[2,3] [4] [5] [6,7] [8,9] [10,11] [8,9] [6,7] [5] [4] [2,3] [1]" }, + { "S10", "Jump to p20 while p1-19 unknown, spread p17, back", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 1, 2, { }, { "goto 20", "prev*30" }, "[20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "S11", "Hold key across 2 unknown spreads (p9, p17), then read back", 24, { 8, 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 0.5, 2, { }, { "next 14 hold", "prev*30", "next*30" }, "[24] [22,23] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [9] [8] [6,7] [4,5] [2,3] [1] [2,3] [4,5] [6,7] [8] [9] [10,11] [12,13] [14,15] [16] [17] [18,19] [20,21] [22,23] [24]" }, + { "S12", "Hold key back from resume p22, spread p11, unknown", 24, { 10 }, 21, true, { 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, 0.5, 2, { }, { "prev 12 hold", "next*30" }, "[1] [2,3] [4,5] [6,7] [8,9] [10] [11] [12,13] [14,15] [16,17] [18,19] [20,21] [22,23] [24]" }, + { "X1", "Spread p4 extracted LAST; hold key from cover to ~p12, then read on and back", 24, { 3 }, 0, true, { 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 3 }, 1, 0, { }, { "next 6 hold", "next*4", "prev*8" }, "[12,13] [14,15] [16,17] [18,19] [20,21] [18,19] [16,17] [14,15] [12,13] [10,11] [8,9] [6,7] [5]" }, + { "X2", "Same as X1 but user stops at p12 and waits until all is extracted", 24, { 3 }, 0, true, { 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 3 }, 1, 0, { }, { "next 6 hold", "wait all", "next*3", "prev*8" }, "[12,13] [14,15] [16,17] [18,19] [16,17] [14,15] [12,13] [10,11] [8,9] [6,7] [5] [4]" }, + { "X3", "Resume p16; spread p13 extracted last; user goes back slowly", 24, { 12 }, 15, true, { 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 12 }, 1, 0, { }, { "prev*6", "next*6" }, "[14,15] [13] [12] [10,11] [8,9] [6,7] [8,9] [10,11] [12] [13] [14,15] [16,17]" }, + { "X4", "Resume p16; p17 spread extracted last; user presses next at once", 24, { 16 }, 15, true, { 15, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16 }, 1, 0, { }, { "next*3", "prev*4" }, "[18,19] [20,21] [22,23] [20,21] [18,19] [17] [16]" }, + { "X5", "Random-ish: resume p9, spreads p5,p14,p15 out of order, mixed holds", 24, { 4, 13, 14 }, 8, true, { 8, 9, 10, 11, 15, 16, 17, 0, 1, 2, 3, 12, 18, 19, 13, 4, 5, 6, 7, 14, 20, 21, 22, 23 }, 0.7, 0, { }, { "next 3 hold", "next*3", "prev*2", "prev 4 hold", "next*6", "wait all", "prev*12" }, "[15] [16,17] [18,19] [20,21] [18,19] [16,17] [15] [14] [13] [11,12] [13] [14] [15] [16,17] [18,19] [20,21] [18,19] [16,17] [15] [14] [13] [11,12] [9,10] [7,8] [6] [5] [4] [2,3]" }, + { "E2", "CRC error on p9, policy: portrait when extraction ends", 24, { }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 1, 2, { 8 }, { "next*6", "prev*6" }, "[2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "E3", "CRC error on p16, spread p17", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 1, 2, { 15 }, { "next*10", "prev*10" }, "[2,3] [4,5] [6,7] [8,9] [10,11] [12,13] [14,15] [16] [17] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "C1", "Cover single ON -> OFF on [6,7], read on, back to start", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*3", "cover off", "next*8", "prev*14" }, "[1] [2,3] [4,5] [6,7] [6,7] [8,9] [10,11] [12,13] [14,15] [16] [17] [18,19] [20,21] [18,19] [17] [16] [14,15] [12,13] [10,11] [8,9] [6,7] [4,5] [2,3] [1]" }, + { "C2", "Cover single OFF -> ON on [5,6], read on, back to start", 24, { 16 }, 0, false, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*2", "cover on", "next*8", "prev*14" }, "[1,2] [3,4] [5,6] [5,6] [7,8] [9,10] [11,12] [13,14] [15,16] [17] [18,19] [20,21] [18,19] [17] [15,16] [13,14] [11,12] [9,10] [7,8] [5,6] [3,4] [2] [1]" }, + { "C3", "Cover single ON -> OFF while on the cover", 24, { }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "cover off", "next*3", "prev*3" }, "[1] [1,2] [3,4] [5,6] [7,8] [5,6] [3,4] [1,2]" }, + { "L1", "Go to last page, 23 pages (odd), then back", 23, { }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }, -1, 2, { }, { "last", "prev*3" }, "[1] [22,23] [20,21] [18,19] [16,17]" }, + { "L2", "Go to last page, 24 pages, spread p17, then back", 24, { 16 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "last", "prev*6" }, "[1] [24] [22,23] [20,21] [18,19] [17] [16] [14,15]" }, + { "L3", "Go to last page while the end is unknown (slow)", 24, { 22 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 1, 2, { }, { "last", "prev*4" }, "[24] [23] [22] [20,21] [18,19]" }, + { "R1", "Rotated 90: raw landscape pages p2-p24 (portrait on screen), raw portrait p10", 24, { 0, 9 }, 0, true, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, -1, 2, { }, { "next*8", "prev*8" }, "[1] [2,3] [4,5] [6,7] [8,9] [10] [11,12] [13,14] [15,16] [13,14] [11,12] [10] [8,9] [6,7] [4,5] [2,3] [1]" }, +}; + +QList allViews(const DoublePageLayout &layout) +{ + QList views; + for (View view = layout.firstView(); view.isValid(); view = layout.nextView(view)) { + views << view; + } + return views; +} + +QString layoutText(const DoublePageLayout &layout) +{ + QStringList texts; + for (const View &view : allViews(layout)) { + texts << viewText(view); + } + return texts.join(QLatin1Char(' ')); +} + +} // namespace + +class DoublePageLayoutTest : public QObject +{ + Q_OBJECT + +private slots: + void unknownPagesArePortraitPages(); + void widePagesAreAlone(); + void userAnchorPairsTheRunBeforeItBackwards(); + void anchorOnAWidePageChangesNothing(); + void keptViewDoesNotChangeWhenASpreadIsFound(); + void previousAndNextViewsMatch(); + void scenarios_data(); + void scenarios(); +}; + +void DoublePageLayoutTest::unknownPagesArePortraitPages() +{ + DoublePageLayout layout; + layout.reset(8, true); + QCOMPARE(layoutText(layout), QStringLiteral("[1] [2,3] [4,5] [6,7] [8]")); + + layout.setCoverIsSinglePage(false); + QCOMPARE(layoutText(layout), QStringLiteral("[1,2] [3,4] [5,6] [7,8]")); +} + +void DoublePageLayoutTest::widePagesAreAlone() +{ + DoublePageLayout layout; + layout.reset(10, true); + layout.setPageShape(4, Shape::Wide); + QCOMPARE(layoutText(layout), QStringLiteral("[1] [2,3] [4] [5] [6,7] [8,9] [10]")); + + layout.setPageShape(5, Shape::Wide); + QCOMPARE(layoutText(layout), QStringLiteral("[1] [2,3] [4] [5] [6] [7,8] [9,10]")); +} + +void DoublePageLayoutTest::userAnchorPairsTheRunBeforeItBackwards() +{ + DoublePageLayout layout; + layout.reset(10, true); + layout.setAnchor(4); // open at page 5 + QCOMPARE(layoutText(layout), QStringLiteral("[1] [2] [3,4] [5,6] [7,8] [9,10]")); +} + +void DoublePageLayoutTest::anchorOnAWidePageChangesNothing() +{ + DoublePageLayout layout; + layout.reset(10, true); + layout.setPageShape(5, Shape::Wide); + const QString natural = layoutText(layout); + layout.setAnchor(5); + QCOMPARE(layoutText(layout), natural); +} + +void DoublePageLayoutTest::keptViewDoesNotChangeWhenASpreadIsFound() +{ + DoublePageNavigator navigator; + navigator.layout().reset(16, true); + navigator.reset(0); + QCOMPARE(navigator.viewFor(9), View({ 9, 10 })); + navigator.viewDisplayed({ 9, 10 }); + + // page 4 is a spread, the pages after it are paired again, but not the view the user sees + navigator.setPageShape(3, Shape::Wide); + QCOMPARE(navigator.viewFor(9), View({ 9, 10 })); + QCOMPARE(layoutText(navigator.layout()), QStringLiteral("[1] [2,3] [4] [5] [6,7] [8,9] [10,11] [12,13] [14,15] [16]")); +} + +void DoublePageLayoutTest::previousAndNextViewsMatch() +{ + DoublePageLayout layout; + layout.reset(30, true); + for (int page : { 6, 7, 13, 21, 29 }) { + layout.setPageShape(page, Shape::Wide); + } + for (int anchor : { -1, 3, 10, 14, 22 }) { + layout.setAnchor(anchor); + const QList views = allViews(layout); + int expectedFirst = 0; + for (int i = 0; i < views.size(); ++i) { + QCOMPARE(views[i].first, expectedFirst); + expectedFirst = views[i].last + 1; + if (i > 0) { + QCOMPARE(layout.previousView(views[i]), views[i - 1]); + } + } + QCOMPARE(expectedFirst, 30); + } +} + +void DoublePageLayoutTest::scenarios_data() +{ + QTest::addColumn("index"); + for (int i = 0; i < static_cast(::scenarios.size()); ++i) { + QTest::newRow(::scenarios[i].id) << i; + } +} + +void DoublePageLayoutTest::scenarios() +{ + QFETCH(int, index); + const Scenario &scenario = ::scenarios[index]; + + Reader reader(scenario); + for (const char *action : scenario.actions) { + reader.run(QString::fromLatin1(action)); + } + + QVERIFY2(reader.problems.isEmpty(), qPrintable(QStringLiteral("%1: %2").arg(QString::fromLatin1(scenario.title), reader.problems.join(QStringLiteral("; "))))); + QCOMPARE(reader.shownViews(), QString::fromLatin1(scenario.expectedViews)); +} + +QTEST_GUILESS_MAIN(DoublePageLayoutTest) + +#include "main.moc" From 8b62fb3aa50badd2ea23a11dbb40d064225721d2 Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Wed, 11 Nov 2026 22:48:04 +0100 Subject: [PATCH 2/5] Bump version 10.5.1 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index fcc198cb8..555a95a72 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -10.5.0 \ No newline at end of file +10.5.1 \ No newline at end of file From e4f52cbaf5597938a7ba58638c5fd348d1c619c6 Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Fri, 13 Nov 2026 13:19:01 +0100 Subject: [PATCH 3/5] Disable the QML disk cache on Linux --- YACReaderLibrary/main.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/YACReaderLibrary/main.cpp b/YACReaderLibrary/main.cpp index aaac41191..847d5ef2a 100644 --- a/YACReaderLibrary/main.cpp +++ b/YACReaderLibrary/main.cpp @@ -139,6 +139,13 @@ int main(int argc, char **argv) qputenv("QT_D3D_NO_FLIP", "1"); #endif +#ifdef Q_OS_LINUX + // Reproducible builds (Flatpak, Nix, distro packages) give QML files a fixed timestamp, so the + // QML disk cache is never invalidated and stale compiled QML is loaded after updates. + if (!qEnvironmentVariableIsSet("QML_DISABLE_DISK_CACHE") && !qEnvironmentVariableIsSet("QML_FORCE_DISK_CACHE")) + qputenv("QML_DISABLE_DISK_CACHE", "1"); +#endif + QApplication app(argc, argv); app.setApplicationName("YACReaderLibrary"); From 5f452d2e2e037dd7afd1be244cdfc237d504848d Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Fri, 13 Nov 2026 13:41:36 +0100 Subject: [PATCH 4/5] Show the search menu button next to the clear button so "save as smart list" actually works --- custom_widgets/yacreader_search_line_edit.cpp | 34 ++++++++++++++----- custom_widgets/yacreader_search_line_edit.h | 3 ++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/custom_widgets/yacreader_search_line_edit.cpp b/custom_widgets/yacreader_search_line_edit.cpp index 0def7ac89..b29e23de9 100644 --- a/custom_widgets/yacreader_search_line_edit.cpp +++ b/custom_widgets/yacreader_search_line_edit.cpp @@ -6,8 +6,12 @@ #include #include +namespace { +const int menuButtonSpacing = 2; +} + YACReaderSearchLineEdit::YACReaderSearchLineEdit(QWidget *parent) - : QLineEdit(parent), paddingLeft(0), paddingRight(0) + : QLineEdit(parent), paddingLeft(0), paddingRight(0), basePaddingRight(0) { clearButton = new QToolButton(this); menuButton = new QToolButton(this); @@ -30,7 +34,8 @@ YACReaderSearchLineEdit::YACReaderSearchLineEdit(QWidget *parent) int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); paddingLeft = 15 + frameWidth + 6 + 5; - paddingRight = 12 + frameWidth + 10; + basePaddingRight = 12 + frameWidth + 10; + paddingRight = basePaddingRight; QSize msz = minimumSizeHint(); setMinimumSize(qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2), @@ -49,7 +54,11 @@ YACReaderSearchLineEdit::YACReaderSearchLineEdit(QWidget *parent) void YACReaderSearchLineEdit::setSearchMenu(QMenu *menu) { menuButton->setMenu(menu); - menuButton->setVisible(menu != nullptr && QLineEdit::text().isEmpty()); + menuButton->setVisible(menu != nullptr); + + paddingRight = basePaddingRight + (menu != nullptr ? menuButton->width() + menuButtonSpacing : 0); + applyTheme(theme); + positionButtons(); } void YACReaderSearchLineEdit::applyTheme(const Theme &theme) @@ -100,23 +109,30 @@ const QString YACReaderSearchLineEdit::text() } void YACReaderSearchLineEdit::resizeEvent(QResizeEvent *) +{ + positionButtons(); + + QSize szl = searchLabel->sizeHint(); + searchLabel->move(8, (rect().bottom() + 2 - szl.height()) / 2); +} + +void YACReaderSearchLineEdit::positionButtons() { const QSize clearSize = clearButton->sizeHint(); const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); const int marginRight = style()->pixelMetric(QStyle::PM_LayoutRightMargin); - const int menuX = rect().right() - frameWidth - menuButton->width() - marginRight - 6; + const int rightEdge = rect().right() - frameWidth - marginRight - 6; + const int menuX = rightEdge - menuButton->width(); menuButton->move(menuX, (height() - menuButton->height()) / 2); - clearButton->move(rect().right() - frameWidth - clearSize.width() - marginRight - 6, - (rect().bottom() + 2 - clearSize.height()) / 2); - QSize szl = searchLabel->sizeHint(); - searchLabel->move(8, (rect().bottom() + 2 - szl.height()) / 2); + const int clearRight = menuButton->menu() != nullptr ? menuX - menuButtonSpacing : rightEdge; + clearButton->move(clearRight - clearSize.width(), + (rect().bottom() + 2 - clearSize.height()) / 2); } void YACReaderSearchLineEdit::updateCloseButton(const QString &text) { clearButton->setVisible(!text.isEmpty()); - menuButton->setVisible(menuButton->menu() != nullptr && text.isEmpty()); } void YACReaderSearchLineEdit::processText(const QString &text) diff --git a/custom_widgets/yacreader_search_line_edit.h b/custom_widgets/yacreader_search_line_edit.h index 92c220e33..7b3a2e65c 100644 --- a/custom_widgets/yacreader_search_line_edit.h +++ b/custom_widgets/yacreader_search_line_edit.h @@ -36,8 +36,11 @@ private slots: QToolButton *menuButton; QLabel *searchLabel; + void positionButtons(); + int paddingLeft; int paddingRight; + int basePaddingRight; }; #endif // YACREADER_SEARCH_LINE_EDIT_H From 05dfbd03e18fc5f98a90ae8a92172e92b222b879 Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Fri, 13 Nov 2026 16:34:04 +0100 Subject: [PATCH 5/5] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 366d9d93a..27ca6ef26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ Version counting is based on semantic versioning (Major.Feature.Patch) * Fix double page mode when turning pages quickly. It also improves the double page mode behavior. +### YACReaderLibrary + +* Show the search menu button next to the clear button so "save as smart list" actually works (Windows & Linux). +* Don't use the QML cache on Linux systems that have the risk to use a stale cache causing white content screens (which are actualy failures at runtime). + ## 10.5.0 ### YACReader