2019-01-27 17:55:22 +01:00
|
|
|
// Copyright (C) 2018 Jakub Melka
|
|
|
|
//
|
|
|
|
// This file is part of PdfForQt.
|
|
|
|
//
|
|
|
|
// PdfForQt is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// PdfForQt is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "pdfdrawwidget.h"
|
|
|
|
#include "pdfdrawspacecontroller.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QKeyEvent>
|
2019-02-02 18:10:00 +01:00
|
|
|
#include <QApplication>
|
2019-01-27 17:55:22 +01:00
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
|
|
|
PDFWidget::PDFWidget(QWidget* parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
m_drawWidget(nullptr),
|
|
|
|
m_horizontalScrollBar(nullptr),
|
|
|
|
m_verticalScrollBar(nullptr),
|
|
|
|
m_proxy(nullptr)
|
|
|
|
{
|
|
|
|
m_drawWidget = new PDFDrawWidget(this, this);
|
|
|
|
m_horizontalScrollBar = new QScrollBar(Qt::Horizontal, this);
|
|
|
|
m_verticalScrollBar = new QScrollBar(Qt::Vertical, this);
|
|
|
|
|
|
|
|
QGridLayout* layout = new QGridLayout(this);
|
|
|
|
layout->setSpacing(0);
|
|
|
|
layout->addWidget(m_drawWidget, 0, 0);
|
|
|
|
layout->addWidget(m_horizontalScrollBar, 1, 0);
|
|
|
|
layout->addWidget(m_verticalScrollBar, 0, 1);
|
|
|
|
layout->setMargin(0);
|
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
setFocusProxy(m_drawWidget);
|
|
|
|
|
|
|
|
m_proxy = new PDFDrawWidgetProxy(this);
|
|
|
|
m_proxy->init(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFWidget::~PDFWidget()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFWidget::setDocument(const PDFDocument* document)
|
|
|
|
{
|
|
|
|
m_proxy->setDocument(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFDrawWidget::PDFDrawWidget(PDFWidget* widget, QWidget* parent) :
|
|
|
|
QWidget(parent),
|
2019-02-02 18:10:00 +01:00
|
|
|
m_widget(widget),
|
|
|
|
m_mouseOperation(MouseOperation::None)
|
2019-01-27 17:55:22 +01:00
|
|
|
{
|
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFDrawWidget::~PDFDrawWidget()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize PDFDrawWidget::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(200, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFDrawWidget::paintEvent(QPaintEvent* event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
m_widget->getDrawWidgetProxy()->draw(&painter, this->rect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFDrawWidget::resizeEvent(QResizeEvent* event)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
|
|
|
|
m_widget->getDrawWidgetProxy()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFDrawWidget::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
|
|
|
QScrollBar* verticalScrollbar = m_widget->getVerticalScrollbar();
|
|
|
|
|
|
|
|
// Vertical navigation
|
|
|
|
if (verticalScrollbar->isVisible())
|
|
|
|
{
|
2019-02-02 18:10:00 +01:00
|
|
|
constexpr std::pair<QKeySequence::StandardKey, PDFDrawWidgetProxy::Operation> keyToOperations[] =
|
2019-01-27 17:55:22 +01:00
|
|
|
{
|
2019-02-02 18:10:00 +01:00
|
|
|
{ QKeySequence::MoveToStartOfDocument, PDFDrawWidgetProxy::NavigateDocumentStart },
|
|
|
|
{ QKeySequence::MoveToEndOfDocument, PDFDrawWidgetProxy::NavigateDocumentEnd },
|
|
|
|
{ QKeySequence::MoveToNextPage, PDFDrawWidgetProxy::NavigateNextPage },
|
|
|
|
{ QKeySequence::MoveToPreviousPage, PDFDrawWidgetProxy::NavigatePreviousPage },
|
|
|
|
{ QKeySequence::MoveToNextLine, PDFDrawWidgetProxy::NavigateNextStep },
|
|
|
|
{ QKeySequence::MoveToPreviousLine, PDFDrawWidgetProxy::NavigatePreviousStep }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const std::pair<QKeySequence::StandardKey, PDFDrawWidgetProxy::Operation>& keyToOperation : keyToOperations)
|
|
|
|
{
|
|
|
|
if (event->matches(keyToOperation.first))
|
|
|
|
{
|
|
|
|
m_widget->getDrawWidgetProxy()->performOperation(keyToOperation.second);
|
|
|
|
}
|
2019-01-27 17:55:22 +01:00
|
|
|
}
|
2019-02-02 18:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFDrawWidget::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
m_mouseOperation = MouseOperation::Translate;
|
|
|
|
m_lastMousePosition = event->pos();
|
|
|
|
setCursor(Qt::ClosedHandCursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFDrawWidget::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
performMouseOperation(event->pos());
|
|
|
|
|
|
|
|
switch (m_mouseOperation)
|
|
|
|
{
|
|
|
|
case MouseOperation::None:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MouseOperation::Translate:
|
2019-01-27 17:55:22 +01:00
|
|
|
{
|
2019-02-02 18:10:00 +01:00
|
|
|
m_mouseOperation = MouseOperation::None;
|
|
|
|
unsetCursor();
|
|
|
|
break;
|
2019-01-27 17:55:22 +01:00
|
|
|
}
|
2019-02-02 18:10:00 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFDrawWidget::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
performMouseOperation(event->pos());
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFDrawWidget::wheelEvent(QWheelEvent* event)
|
|
|
|
{
|
|
|
|
Qt::KeyboardModifiers keyboardModifiers = QApplication::keyboardModifiers();
|
|
|
|
|
|
|
|
if (keyboardModifiers.testFlag(Qt::ControlModifier))
|
|
|
|
{
|
|
|
|
// Zoom in/Zoom out
|
|
|
|
const int angleDeltaY = event->angleDelta().y();
|
|
|
|
const PDFReal zoom = m_widget->getDrawWidgetProxy()->getZoom();
|
|
|
|
const PDFReal zoomStep = std::pow(PDFDrawWidgetProxy::ZOOM_STEP, static_cast<PDFReal>(angleDeltaY) / static_cast<PDFReal>(QWheelEvent::DefaultDeltasPerStep));
|
|
|
|
const PDFReal newZoom = zoom * zoomStep;
|
|
|
|
m_widget->getDrawWidgetProxy()->zoom(newZoom);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Move Up/Down. Angle is negative, if wheel is scrolled down. First we try to scroll by pixel delta.
|
|
|
|
// Otherwise we compute scroll using angle.
|
|
|
|
QPoint scrollByPixels = event->pixelDelta();
|
|
|
|
if (scrollByPixels.isNull())
|
2019-01-27 17:55:22 +01:00
|
|
|
{
|
2019-02-02 18:10:00 +01:00
|
|
|
const QPoint angleDelta = event->angleDelta();
|
|
|
|
const bool shiftModifier = keyboardModifiers.testFlag(Qt::ShiftModifier);
|
|
|
|
const int stepVertical = shiftModifier ? m_widget->getVerticalScrollbar()->pageStep() : m_widget->getVerticalScrollbar()->singleStep();
|
|
|
|
const int stepHorizontal = shiftModifier ? m_widget->getHorizontalScrollbar()->pageStep() : m_widget->getHorizontalScrollbar()->singleStep();
|
|
|
|
|
|
|
|
const int scrollVertical = stepVertical * static_cast<PDFReal>(angleDelta.y()) / static_cast<PDFReal>(QWheelEvent::DefaultDeltasPerStep);
|
|
|
|
const int scrollHorizontal = stepHorizontal * static_cast<PDFReal>(angleDelta.x()) / static_cast<PDFReal>(QWheelEvent::DefaultDeltasPerStep);
|
|
|
|
|
|
|
|
scrollByPixels = QPoint(scrollHorizontal, scrollVertical);
|
2019-01-27 17:55:22 +01:00
|
|
|
}
|
2019-02-02 18:10:00 +01:00
|
|
|
|
|
|
|
m_widget->getDrawWidgetProxy()->scrollByPixels(scrollByPixels);
|
2019-01-27 17:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
2019-02-02 18:10:00 +01:00
|
|
|
void PDFDrawWidget::performMouseOperation(QPoint currentMousePosition)
|
|
|
|
{
|
|
|
|
switch (m_mouseOperation)
|
|
|
|
{
|
|
|
|
case MouseOperation::None:
|
|
|
|
// No operation performed
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MouseOperation::Translate:
|
|
|
|
{
|
|
|
|
QPoint difference = currentMousePosition - m_lastMousePosition;
|
|
|
|
m_widget->getDrawWidgetProxy()->scrollByPixels(difference);
|
|
|
|
m_lastMousePosition = currentMousePosition;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:55:22 +01:00
|
|
|
} // namespace pdf
|