PDF4QT/PdfForQtLib/sources/pdfdrawspacecontroller.h

346 lines
12 KiB
C
Raw Normal View History

2019-01-20 17:55:06 +01:00
// Copyright (C) 2019 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/>.
#ifndef PDFDRAWSPACECONTROLLER_H
#define PDFDRAWSPACECONTROLLER_H
2019-02-02 18:10:00 +01:00
#include "pdfglobal.h"
2019-01-20 17:55:06 +01:00
#include "pdfdocument.h"
2019-02-24 19:42:00 +01:00
#include "pdfrenderer.h"
2019-04-12 19:17:19 +02:00
#include "pdffont.h"
2019-01-20 17:55:06 +01:00
#include <QRectF>
#include <QObject>
#include <QMarginsF>
2019-01-27 17:55:22 +01:00
class QPainter;
class QScrollBar;
2019-01-20 17:55:06 +01:00
namespace pdf
{
2019-01-27 17:55:22 +01:00
class PDFWidget;
2019-09-01 18:26:52 +02:00
class IDrawWidget;
2019-01-20 17:55:06 +01:00
/// This class controls draw space - page layout. Pages are divided into blocks
/// each block can contain one or multiple pages. Units are in milimeters.
/// Pages are layouted in zoom-independent mode.
class PDFDrawSpaceController : public QObject
{
Q_OBJECT
public:
explicit PDFDrawSpaceController(QObject* parent);
2019-04-12 19:17:19 +02:00
virtual ~PDFDrawSpaceController() override;
2019-01-20 17:55:06 +01:00
2019-01-27 17:55:22 +01:00
/// Sets the document and recalculates the draw space. Document can be nullptr,
/// in that case, draw space is cleared. Optional content activity can be nullptr,
/// in that case, no content is suppressed.
2019-01-27 17:55:22 +01:00
/// \param document Document
/// \param optionalContentActivity Optional content activity
void setDocument(const PDFDocument* document, const PDFOptionalContentActivity* optionalContentActivity);
2019-01-20 17:55:06 +01:00
2019-01-27 17:55:22 +01:00
/// Sets the page layout. Page layout can be one of the PDF's page layouts.
/// \param pageLayout Page layout
void setPageLayout(PageLayout pageLayout);
2019-01-20 17:55:06 +01:00
2019-01-27 17:55:22 +01:00
/// Returns the page layout
PageLayout getPageLayout() const { return m_pageLayoutMode; }
/// Returns the block count
size_t getBlockCount() const { return m_blockItems.size(); }
/// Return the bounding rectangle of the block. If block doesn't exist,
/// then invalid rectangle is returned (no exception is thrown).
/// \param blockIndex Index of the block
QRectF getBlockBoundingRectangle(size_t blockIndex) const;
2019-01-20 17:55:06 +01:00
/// Represents layouted page. This structure contains index of the block, index of the
/// page and page rectangle, in which the page is contained.
struct LayoutItem
{
constexpr inline explicit LayoutItem() : blockIndex(-1), pageIndex(-1), pageRotation(PageRotation::None) { }
constexpr inline explicit LayoutItem(PDFInteger blockIndex, PDFInteger pageIndex, PageRotation rotation, const QRectF& pageRectMM) :
blockIndex(blockIndex), pageIndex(pageIndex), pageRotation(rotation), pageRectMM(pageRectMM) { }
PDFInteger blockIndex;
PDFInteger pageIndex;
PageRotation pageRotation;
QRectF pageRectMM;
};
using LayoutItems = std::vector<LayoutItem>;
2019-01-27 17:55:22 +01:00
/// Returns the layout items for desired block. If block doesn't exist,
/// then empty array is returned.
/// \param blockIndex Index of the block
LayoutItems getLayoutItems(size_t blockIndex) const;
2019-02-09 18:40:56 +01:00
/// Returns the document
const PDFDocument* getDocument() const { return m_document; }
2019-04-12 19:17:19 +02:00
/// Returns the font cache
const PDFFontCache* getFontCache() const { return &m_fontCache; }
/// Returns optional content activity
const PDFOptionalContentActivity* getOptionalContentActivity() const { return m_optionalContentActivity; }
2019-01-27 17:55:22 +01:00
signals:
void drawSpaceChanged();
void repaintNeeded();
2019-01-27 17:55:22 +01:00
private:
/// Recalculates the draw space. Preserves setted page rotation.
void recalculate();
/// Clears the draw space. Emits signal if desired.
void clear(bool emitSignal);
2019-01-20 17:55:06 +01:00
/// Represents data for the single block. Contains block size in milimeters.
struct LayoutBlock
{
constexpr inline explicit LayoutBlock() = default;
constexpr inline explicit LayoutBlock(const QRectF& blockRectMM) : blockRectMM(blockRectMM) { }
QRectF blockRectMM;
};
using BlockItems = std::vector<LayoutBlock>;
2019-04-12 19:17:19 +02:00
static constexpr size_t FONT_CACHE_LIMIT = 32;
static constexpr size_t REALIZED_FONT_CACHE_LIMIT = 128;
2019-01-20 17:55:06 +01:00
const PDFDocument* m_document;
const PDFOptionalContentActivity* m_optionalContentActivity;
2019-01-20 17:55:06 +01:00
PageLayout m_pageLayoutMode;
LayoutItems m_layoutItems;
BlockItems m_blockItems;
PDFReal m_verticalSpacingMM;
PDFReal m_horizontalSpacingMM;
2019-04-12 19:17:19 +02:00
/// Font cache
PDFFontCache m_fontCache;
2019-01-20 17:55:06 +01:00
};
2019-01-27 17:55:22 +01:00
/// This is a proxy class to draw space controller using widget. We have two spaces, pixel space
/// (on the controlled widget) and device space (device is draw space controller).
2019-02-02 18:10:00 +01:00
class PDFFORQTLIBSHARED_EXPORT PDFDrawWidgetProxy : public QObject
2019-01-27 17:55:22 +01:00
{
Q_OBJECT
public:
explicit PDFDrawWidgetProxy(QObject* parent);
2019-04-12 19:17:19 +02:00
virtual ~PDFDrawWidgetProxy() override;
2019-01-27 17:55:22 +01:00
/// Sets the document and updates the draw space. Document can be nullptr,
/// in that case, draw space is cleared. Optional content activity can be nullptr,
/// in that case, no content is suppressed.
2019-01-27 17:55:22 +01:00
/// \param document Document
/// \param optionalContentActivity Optional content activity
void setDocument(const PDFDocument* document, const PDFOptionalContentActivity* optionalContentActivity);
2019-01-27 17:55:22 +01:00
void init(PDFWidget* widget);
/// Updates the draw space area
void update();
/// Draws the actually visible pages on the painter using the rectangle.
/// Rectangle is space in the widget, which is used for painting the PDF.
/// \param painter Painter to paint the PDF pages
/// \param rect Rectangle in which the content is painted
void draw(QPainter* painter, QRect rect);
2019-02-02 18:10:00 +01:00
enum Operation
{
NavigateDocumentStart,
NavigateDocumentEnd,
NavigateNextPage,
NavigatePreviousPage,
NavigateNextStep,
NavigatePreviousStep
};
/// Performs the desired operation (for example navigation).
/// \param operation Operation to be performed
void performOperation(Operation operation);
/// Scrolls by pixels, if it is possible. If it is not possible to scroll,
2019-05-04 18:22:40 +02:00
/// then nothing happens. Returns pixel offset, by which view camera was moved.
2019-02-02 18:10:00 +01:00
/// \param offset Offset in pixels
2019-05-04 18:22:40 +02:00
QPoint scrollByPixels(QPoint offset);
2019-02-02 18:10:00 +01:00
/// Sets the zoom. Tries to preserve current offsets (so the current visible
/// area will be visible after the zoom).
/// \param zoom New zoom
void zoom(PDFReal zoom);
/// 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; }
2019-02-03 13:57:37 +01:00
/// Sets the page layout. Page layout can be one of the PDF's page layouts.
/// \param pageLayout Page layout
void setPageLayout(PageLayout pageLayout);
/// Returns the page layout
PageLayout getPageLayout() const { return m_controller->getPageLayout(); }
2019-02-24 19:42:00 +01:00
/// Returns pages, which are intersecting rectangle (even partially)
/// \param rect Rectangle to test
std::vector<PDFInteger> getPagesIntersectingRect(QRect rect) const;
2019-05-04 18:22:40 +02:00
/// Returns bounding box of pages, which are intersecting rectangle (even partially)
/// \param rect Rectangle to test
QRect getPagesIntersectingRectBoundingBox(QRect rect) const;
/// Returns true, if we are in the block mode (multiple blocks with separate pages),
/// or continuous mode (single block with continuous list of separated pages).
bool isBlockMode() const;
2019-02-02 18:10:00 +01:00
static constexpr PDFReal ZOOM_STEP = 1.2;
2019-07-06 15:55:37 +02:00
PDFRenderer::Features getFeatures() const;
void setFeatures(PDFRenderer::Features features);
2019-09-28 18:26:31 +02:00
void setPreferredMeshResolutionRatio(PDFReal ratio);
void setMinimalMeshResolutionRatio(PDFReal ratio);
void setColorTolerance(PDFReal colorTolerance);
2019-07-06 15:55:37 +02:00
2019-01-27 17:55:22 +01:00
signals:
void drawSpaceChanged();
2019-02-03 13:57:37 +01:00
void pageLayoutChanged();
2019-02-24 19:42:00 +01:00
void renderingError(PDFInteger pageIndex, const QList<PDFRenderError>& errors);
void repaintNeeded();
2019-01-27 17:55:22 +01:00
private:
struct LayoutItem
{
constexpr inline explicit LayoutItem() : pageIndex(-1), pageRotation(PageRotation::None) { }
constexpr inline explicit LayoutItem(PDFInteger pageIndex, PageRotation rotation, const QRect& pageRect) :
pageIndex(pageIndex), pageRotation(rotation), pageRect(pageRect) { }
PDFInteger pageIndex;
PageRotation pageRotation;
QRect pageRect;
};
struct Layout
{
inline void clear()
{
items.clear();
blockRect = QRect();
}
std::vector<LayoutItem> items;
QRect blockRect;
};
static constexpr size_t INVALID_BLOCK_INDEX = std::numeric_limits<size_t>::max();
// Minimal/maximal zoom is from 8% to 6400 %, according to the PDF 1.7 Reference,
// Appendix C.
static constexpr PDFReal MIN_ZOOM = 8.0 / 100.0;
static constexpr PDFReal MAX_ZOOM = 6400.0 / 100.0;
/// Converts rectangle from device space to the pixel space
QRectF fromDeviceSpace(const QRectF& rect) const;
2019-02-02 18:10:00 +01:00
void onHorizontalScrollbarValueChanged(int value);
void onVerticalScrollbarValueChanged(int value);
void setHorizontalOffset(int value);
void setVerticalOffset(int value);
void setBlockIndex(int index);
void updateHorizontalScrollbarFromOffset();
void updateVerticalScrollbarFromOffset();
template<typename T>
struct Range
{
constexpr inline Range() : min(T()), max(T()) { }
constexpr inline Range(T value) : min(value), max(value) { }
constexpr inline Range(T min, T max) : min(min), max(max) { }
T min;
T max;
constexpr inline T bound(T value) { return qBound(min, value, max); }
};
2019-01-27 17:55:22 +01:00
/// Flag, disables the update
bool m_updateDisabled;
/// Current block (in the draw space controller)
size_t m_currentBlock;
/// Number of pixels (fractional) per milimeter (unit is pixel/mm) of the screen,
/// so, size of the area in milimeters can be computed as pixelCount * m_pixelPerMM [mm].
PDFReal m_pixelPerMM;
/// 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 m_zoom;
/// Converts pixel to device space units (mm) using zoom
PDFReal m_pixelToDeviceSpaceUnit;
/// Converts device space units (mm) to real pixels using zoom
PDFReal m_deviceSpaceUnitToPixel;
/// Actual vertical offset of the draw space area in the widget (so block will be drawn
/// with this vertical offset)
PDFInteger m_verticalOffset;
2019-02-02 18:10:00 +01:00
/// Range of vertical offset
Range<PDFInteger> m_verticalOffsetRange;
2019-01-27 17:55:22 +01:00
/// Actual horizontal offset of the draw space area in the widget (so block will be drawn
/// with this horizontal offset)
PDFInteger m_horizontalOffset;
2019-02-02 18:10:00 +01:00
/// Range for horizontal offset
Range<PDFInteger> m_horizontalOffsetRange;
2019-01-27 17:55:22 +01:00
/// Draw space controller
PDFDrawSpaceController* m_controller;
/// Controlled draw widget (proxy is for this widget)
2019-09-08 11:13:59 +02:00
PDFWidget* m_widget;
2019-01-27 17:55:22 +01:00
/// Vertical scrollbar
QScrollBar* m_verticalScrollbar;
/// Horizontal scrollbar
QScrollBar* m_horizontalScrollbar;
/// Current page layout
Layout m_layout;
2019-07-06 15:55:37 +02:00
/// Renderer features
PDFRenderer::Features m_features;
2019-09-28 18:26:31 +02:00
/// Mesh quality settings
PDFMeshQualitySettings m_meshQualitySettings;
2019-01-27 17:55:22 +01:00
};
2019-01-20 17:55:06 +01:00
} // namespace pdf
#endif // PDFDRAWSPACECONTROLLER_H