Page navigation and zoom

This commit is contained in:
Jakub Melka
2019-11-09 15:11:57 +01:00
parent e9dff4d747
commit eab366b90d
4 changed files with 95 additions and 0 deletions

View File

@@ -93,6 +93,27 @@ PDFDrawSpaceController::LayoutItems PDFDrawSpaceController::getLayoutItems(size_
return result;
}
PDFDrawSpaceController::LayoutItem PDFDrawSpaceController::getLayoutItemForPage(PDFInteger pageIndex) const
{
LayoutItem result;
if (pageIndex >= 0 && pageIndex < static_cast<PDFInteger>(m_layoutItems.size()) && m_layoutItems[pageIndex].pageIndex == pageIndex)
{
result = m_layoutItems[pageIndex];
}
if (!result.isValid())
{
auto it = std::find_if(m_layoutItems.cbegin(), m_layoutItems.cend(), [pageIndex](const LayoutItem& item) { return item.pageIndex == pageIndex; });
if (it != m_layoutItems.cend())
{
result = *it;
}
}
return result;
}
void PDFDrawSpaceController::recalculate()
{
if (!m_document)
@@ -713,6 +734,25 @@ void PDFDrawWidgetProxy::zoom(PDFReal zoom)
}
}
void PDFDrawWidgetProxy::goToPage(PDFInteger pageIndex)
{
PDFDrawSpaceController::LayoutItem layoutItem = m_controller->getLayoutItemForPage(pageIndex);
if (layoutItem.isValid())
{
// We have found our page, navigate onto it
if (isBlockMode())
{
setBlockIndex(layoutItem.blockIndex);
}
else
{
QRect rect = fromDeviceSpace(layoutItem.pageRectMM).toRect();
setVerticalOffset(-rect.top() - m_layout.blockRect.top());
}
}
}
void PDFDrawWidgetProxy::setPageLayout(PageLayout pageLayout)
{
if (getPageLayout() != pageLayout)

View File

@@ -76,6 +76,8 @@ public:
constexpr inline explicit LayoutItem(PDFInteger blockIndex, PDFInteger pageIndex, PageRotation rotation, const QRectF& pageRectMM) :
blockIndex(blockIndex), pageIndex(pageIndex), pageRotation(rotation), pageRectMM(pageRectMM) { }
bool isValid() const { return pageIndex != -1; }
PDFInteger blockIndex;
PDFInteger pageIndex;
PageRotation pageRotation;
@@ -89,6 +91,11 @@ public:
/// \param blockIndex Index of the block
LayoutItems getLayoutItems(size_t blockIndex) const;
/// Returns layout for single page. If page index is invalid,
/// or page layout cannot be found, then invalid layout item is returned.
/// \param pageIndex Page index
LayoutItem getLayoutItemForPage(PDFInteger pageIndex) const;
/// Returns the document
const PDFDocument* getDocument() const { return m_document; }
@@ -190,6 +197,10 @@ public:
/// \param zoom New zoom
void zoom(PDFReal zoom);
/// Go to the specified page
/// \param pageIndex Page to scroll to
void goToPage(PDFInteger pageIndex);
/// Returns current zoom from widget space to device space. So, for example 2.00 corresponds to 200% zoom,
/// and each 1 cm of widget area corresponds to 0.5 cm of the device space area.
PDFReal getZoom() const { return m_zoom; }