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; }

View File

@ -100,6 +100,8 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
m_pageNumberSpinBox = new QSpinBox(this);
m_pageNumberLabel = new QLabel(this);
m_pageNumberSpinBox->setFixedWidth(adjustDpiX(80));
m_pageNumberSpinBox->setAlignment(Qt::AlignCenter);
connect(m_pageNumberSpinBox, &QSpinBox::editingFinished, this, &PDFViewerMainWindow::onPageNumberSpinboxEditingFinished);
// Page control
ui->mainToolBar->addSeparator();
@ -121,6 +123,8 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
m_pageZoomSpinBox->setDecimals(2);
m_pageZoomSpinBox->setSuffix(tr("%"));
m_pageZoomSpinBox->setFixedWidth(adjustDpiX(80));
m_pageZoomSpinBox->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
connect(m_pageZoomSpinBox, &QDoubleSpinBox::editingFinished, this, &PDFViewerMainWindow::onPageZoomSpinboxEditingFinished);
ui->mainToolBar->addWidget(m_pageZoomSpinBox);
connect(ui->actionZoom_In, &QAction::triggered, this, [this] { m_pdfWidget->getDrawWidgetProxy()->performOperation(pdf::PDFDrawWidgetProxy::ZoomIn); });
@ -208,6 +212,36 @@ void PDFViewerMainWindow::onPageLayoutChanged()
updatePageLayoutActions();
}
void PDFViewerMainWindow::onPageNumberSpinboxEditingFinished()
{
if (m_isLoadingUI)
{
return;
}
if (m_pageNumberSpinBox->hasFocus())
{
m_pdfWidget->setFocus();
}
m_pdfWidget->getDrawWidgetProxy()->goToPage(m_pageNumberSpinBox->value() - 1);
}
void PDFViewerMainWindow::onPageZoomSpinboxEditingFinished()
{
if (m_isLoadingUI)
{
return;
}
if (m_pageZoomSpinBox->hasFocus())
{
m_pdfWidget->setFocus();
}
m_pdfWidget->getDrawWidgetProxy()->zoom(m_pageZoomSpinBox->value() / 100.0);
}
void PDFViewerMainWindow::readSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
@ -324,6 +358,14 @@ void PDFViewerMainWindow::updateUI(bool fullUpdate)
m_pageNumberLabel->setText(QString());
}
}
else
{
std::vector<pdf::PDFInteger> currentPages = m_pdfWidget->getDrawWidget()->getCurrentPages();
if (!currentPages.empty())
{
m_pageNumberSpinBox->setValue(currentPages.front() + 1);
}
}
m_pageZoomSpinBox->setValue(m_pdfWidget->getDrawWidgetProxy()->getZoom() * 100);
}

View File

@ -74,6 +74,8 @@ private:
void onPageRenderingErrorsChanged(pdf::PDFInteger pageIndex, int errorsCount);
void onDrawSpaceChanged();
void onPageLayoutChanged();
void onPageNumberSpinboxEditingFinished();
void onPageZoomSpinboxEditingFinished();
void readSettings();
void writeSettings();