Snapper, some code for screenshot tool

This commit is contained in:
Jakub Melka
2020-02-23 18:59:54 +01:00
parent e9bad256dc
commit a27216e752
18 changed files with 636 additions and 58 deletions

View File

@ -56,9 +56,11 @@ SOURCES += \
sources/pdfpattern.cpp \ sources/pdfpattern.cpp \
sources/pdfprogress.cpp \ sources/pdfprogress.cpp \
sources/pdfsecurityhandler.cpp \ sources/pdfsecurityhandler.cpp \
sources/pdfsnapper.cpp \
sources/pdftextlayout.cpp \ sources/pdftextlayout.cpp \
sources/pdfutils.cpp \ sources/pdfutils.cpp \
sources/pdfwidgettool.cpp \ sources/pdfwidgettool.cpp \
sources/pdfwidgetutils.cpp \
sources/pdfxreftable.cpp \ sources/pdfxreftable.cpp \
sources/pdfvisitor.cpp \ sources/pdfvisitor.cpp \
sources/pdfencoding.cpp \ sources/pdfencoding.cpp \
@ -102,8 +104,10 @@ HEADERS += \
sources/pdfpattern.h \ sources/pdfpattern.h \
sources/pdfprogress.h \ sources/pdfprogress.h \
sources/pdfsecurityhandler.h \ sources/pdfsecurityhandler.h \
sources/pdfsnapper.h \
sources/pdftextlayout.h \ sources/pdftextlayout.h \
sources/pdfwidgettool.h \ sources/pdfwidgettool.h \
sources/pdfwidgetutils.h \
sources/pdfxreftable.h \ sources/pdfxreftable.h \
sources/pdfflatmap.h \ sources/pdfflatmap.h \
sources/pdfvisitor.h \ sources/pdfvisitor.h \

View File

@ -402,6 +402,7 @@ PDFPrecompiledPageGenerator::PDFPrecompiledPageGenerator(PDFPrecompiledPage* pre
m_precompiledPage(precompiledPage) m_precompiledPage(precompiledPage)
{ {
m_precompiledPage->setPaperColor(cms->getPaperColor()); m_precompiledPage->setPaperColor(cms->getPaperColor());
m_precompiledPage->getSnapInfo()->addPageMediaBox(page->getRotatedMediaBox());
} }
void PDFPrecompiledPageGenerator::performPathPainting(const QPainterPath& path, bool stroke, bool fill, bool text, Qt::FillRule fillRule) void PDFPrecompiledPageGenerator::performPathPainting(const QPainterPath& path, bool stroke, bool fill, bool text, Qt::FillRule fillRule)
@ -428,6 +429,17 @@ void PDFPrecompiledPageGenerator::performImagePainting(const QImage& image)
return; return;
} }
// Add snap info for image to the snapper
QMatrix matrix = getCurrentWorldMatrix();
PDFSnapInfo* snapInfo = m_precompiledPage->getSnapInfo();
snapInfo->addImage({
matrix.map(QPointF(0.0, 0.0)),
matrix.map(QPointF(1.0, 0.0)),
matrix.map(QPointF(1.0, 1.0)),
matrix.map(QPointF(0.0, 1.0)),
matrix.map(QPointF(0.5, 0.5)),
});
if (isTransparencyGroupActive()) if (isTransparencyGroupActive())
{ {
PDFReal alpha = getEffectiveFillingAlpha(); PDFReal alpha = getEffectiveFillingAlpha();

View File

@ -23,6 +23,7 @@
#include "pdfrenderer.h" #include "pdfrenderer.h"
#include "pdfpagecontentprocessor.h" #include "pdfpagecontentprocessor.h"
#include "pdftextlayout.h" #include "pdftextlayout.h"
#include "pdfsnapper.h"
#include <QPen> #include <QPen>
#include <QBrush> #include <QBrush>
@ -223,6 +224,9 @@ public:
QColor getPaperColor() const { return m_paperColor; } QColor getPaperColor() const { return m_paperColor; }
void setPaperColor(QColor paperColor) { m_paperColor = paperColor; } void setPaperColor(QColor paperColor) { m_paperColor = paperColor; }
PDFSnapInfo* getSnapInfo() { return &m_snapInfo; }
const PDFSnapInfo* getSnapInfo() const { return &m_snapInfo; }
private: private:
struct PathPaintData struct PathPaintData
{ {
@ -291,6 +295,7 @@ private:
std::vector<QMatrix> m_matrices; std::vector<QMatrix> m_matrices;
std::vector<QPainter::CompositionMode> m_compositionModes; std::vector<QPainter::CompositionMode> m_compositionModes;
QList<PDFRenderError> m_errors; QList<PDFRenderError> m_errors;
PDFSnapInfo m_snapInfo;
}; };
/// Processor, which processes PDF's page commands and writes them to the precompiled page. /// Processor, which processes PDF's page commands and writes them to the precompiled page.

View File

@ -0,0 +1,129 @@
// Copyright (C) 2020 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 "pdfsnapper.h"
#include "pdfcompiler.h"
#include "pdfwidgetutils.h"
#include <QPainter>
namespace pdf
{
void PDFSnapInfo::addPageMediaBox(const QRectF& mediaBox)
{
QPointF tl = mediaBox.topLeft();
QPointF tr = mediaBox.topRight();
QPointF bl = mediaBox.bottomLeft();
QPointF br = mediaBox.bottomRight();
QPointF center = mediaBox.center();
m_snapPoints.insert(m_snapPoints.cend(), {
SnapPoint(SnapType::PageCorner, tl ),
SnapPoint(SnapType::PageCorner, tr ),
SnapPoint(SnapType::PageCorner, bl ),
SnapPoint(SnapType::PageCorner, br ),
SnapPoint(SnapType::PageCenter, center)
});
addLine(tl, tr);
addLine(tr, br);
addLine(br, bl);
addLine(tl, bl);
}
void PDFSnapInfo::addImage(const std::array<QPointF, 5>& points)
{
m_snapPoints.insert(m_snapPoints.cend(), {
SnapPoint(SnapType::ImageCorner, points[0]),
SnapPoint(SnapType::ImageCorner, points[1]),
SnapPoint(SnapType::ImageCorner, points[2]),
SnapPoint(SnapType::ImageCorner, points[3]),
SnapPoint(SnapType::ImageCenter, points[4])
});
for (size_t i = 0; i < 4; ++i)
{
addLine(points[i], points[(i + 1) % 4]);
}
}
void PDFSnapInfo::addLine(const QPointF& start, const QPointF& end)
{
QLineF line(start, end);
m_snapPoints.emplace_back(SnapType::LineCenter, line.center());
m_snapLines.emplace_back(line);
}
PDFSnapper::PDFSnapper()
{
}
void PDFSnapper::drawSnapPoints(QPainter* painter, PDFInteger pageIndex, const PDFPrecompiledPage* compiledPage, const QMatrix& pagePointToDevicePointMatrix) const
{
if (m_currentPage != -1 && m_currentPage != pageIndex)
{
// We are drawing only snap points, which are on current page
return;
}
Q_ASSERT(painter);
Q_ASSERT(compiledPage);
const PDFSnapInfo* snapInfo = (m_currentPage != pageIndex) ? compiledPage->getSnapInfo() : &m_currentPageSnapInfo;
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
QPen pen = painter->pen();
pen.setCapStyle(Qt::RoundCap);
pen.setWidth(PDFWidgetUtils::scaleDPI_x(painter->device(), 10));
for (const PDFSnapInfo::SnapPoint& snapPoint : snapInfo->getSnapPoints())
{
QColor color = pen.color();
QColor newColor = color;
switch (snapPoint.type)
{
case SnapType::PageCorner:
newColor = Qt::blue;
break;
case SnapType::GeneratedLineProjection:
newColor = Qt::green;
break;
default:
newColor = Qt::red;
break;
}
if (color != newColor)
{
pen.setColor(newColor);
painter->setPen(pen);
}
QPoint point = pagePointToDevicePointMatrix.map(snapPoint.point).toPoint();
painter->drawPoint(point);
}
painter->restore();
}
} // namespace pdf

View File

@ -0,0 +1,110 @@
// Copyright (C) 2020 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 PDFSNAPPER_H
#define PDFSNAPPER_H
#include "pdfglobal.h"
#include <array>
class QPainter;
namespace pdf
{
class PDFPrecompiledPage;
enum class SnapType
{
Invalid,
PageCorner, ///< Corner of the page media box
ImageCorner, ///< Corner of image
PageCenter, ///< Center of page media box
ImageCenter, ///< Center of image
LineCenter, ///< Center of line
GeneratedLineProjection ///< Generated point to line projections
};
/// Contain informations for snap points in the pdf page. Snap points
/// can be for example image centers, rectangle corners, line start/end
/// points, page boundary boxes etc. All coordinates are in page coordinates.
class PDFSnapInfo
{
public:
explicit inline PDFSnapInfo() = default;
struct SnapPoint
{
explicit inline constexpr SnapPoint() = default;
explicit inline constexpr SnapPoint(SnapType type, QPointF point) :
type(type),
point(point)
{
}
SnapType type = SnapType::Invalid;
QPointF point;
};
/// Adds page media box. Media box must be in page coordinates.
/// \param mediaBox Media box
void addPageMediaBox(const QRectF& mediaBox);
/// Adds image box. Because it is not guaranteed, that it will be rectangle, five
/// points are defined - four corners and center.
/// \param points Four corner points in clockwise order, fifth point is center,
/// all in page coordinates.
void addImage(const std::array<QPointF, 5>& points);
/// Adds line and line center points
/// \param start Start point of line, in page coordinates
/// \param end End point of line, in page coordinates
void addLine(const QPointF& start, const QPointF& end);
/// Returns snap points
const std::vector<SnapPoint>& getSnapPoints() const { return m_snapPoints; }
private:
std::vector<SnapPoint> m_snapPoints;
std::vector<QLineF> m_snapLines;
};
/// Snap engine, which handles snapping of points on the page.
class PDFSnapper
{
public:
PDFSnapper();
/// Draws snapping points onto the page
/// \param painter Painter
/// \param pageIndex Page index
/// \param compiledPage Compiled page
/// \param pagePointToDevicePointMatrix Matrix mapping page space to device point space
void drawSnapPoints(QPainter* painter,
pdf::PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
const QMatrix& pagePointToDevicePointMatrix) const;
private:
PDFSnapInfo m_currentPageSnapInfo;
PDFInteger m_currentPage = -1;
};
} // namespace pdf
#endif // PDFSNAPPER_H

View File

@ -59,18 +59,6 @@ PDFWidgetTool::~PDFWidgetTool()
} }
void PDFWidgetTool::drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix) const
{
for (PDFWidgetTool* tool : m_toolStack)
{
tool->drawPage(painter, pageIndex, compiledPage, layoutGetter, pagePointToDevicePointMatrix);
}
}
void PDFWidgetTool::setDocument(const PDFDocument* document) void PDFWidgetTool::setDocument(const PDFDocument* document)
{ {
if (m_document != document) if (m_document != document)
@ -78,6 +66,12 @@ void PDFWidgetTool::setDocument(const PDFDocument* document)
// We must turn off the tool, if we are changing the document // We must turn off the tool, if we are changing the document
setActive(false); setActive(false);
m_document = document; m_document = document;
for (PDFWidgetTool* tool : m_toolStack)
{
tool->setDocument(document);
}
updateActions(); updateActions();
} }
} }
@ -107,37 +101,61 @@ void PDFWidgetTool::setActive(bool active)
void PDFWidgetTool::keyPressEvent(QWidget* widget, QKeyEvent* event) void PDFWidgetTool::keyPressEvent(QWidget* widget, QKeyEvent* event)
{ {
Q_UNUSED(widget); if (PDFWidgetTool* tool = getTopToolstackTool())
Q_UNUSED(event); {
tool->keyPressEvent(widget, event);
}
} }
void PDFWidgetTool::mousePressEvent(QWidget* widget, QMouseEvent* event) void PDFWidgetTool::mousePressEvent(QWidget* widget, QMouseEvent* event)
{ {
Q_UNUSED(widget); if (PDFWidgetTool* tool = getTopToolstackTool())
Q_UNUSED(event); {
tool->mousePressEvent(widget, event);
}
} }
void PDFWidgetTool::mouseReleaseEvent(QWidget* widget, QMouseEvent* event) void PDFWidgetTool::mouseReleaseEvent(QWidget* widget, QMouseEvent* event)
{ {
Q_UNUSED(widget); if (PDFWidgetTool* tool = getTopToolstackTool())
Q_UNUSED(event); {
tool->mouseReleaseEvent(widget, event);
}
} }
void PDFWidgetTool::mouseMoveEvent(QWidget* widget, QMouseEvent* event) void PDFWidgetTool::mouseMoveEvent(QWidget* widget, QMouseEvent* event)
{ {
Q_UNUSED(widget); if (PDFWidgetTool* tool = getTopToolstackTool())
Q_UNUSED(event); {
tool->mouseMoveEvent(widget, event);
}
} }
void PDFWidgetTool::wheelEvent(QWidget* widget, QWheelEvent* event) void PDFWidgetTool::wheelEvent(QWidget* widget, QWheelEvent* event)
{ {
Q_UNUSED(widget); if (PDFWidgetTool* tool = getTopToolstackTool())
Q_UNUSED(event); {
tool->wheelEvent(widget, event);
}
}
const std::optional<QCursor>& PDFWidgetTool::getCursor() const
{
// If we have active subtool, return its mouse cursor
if (PDFWidgetTool* tool = getTopToolstackTool())
{
return tool->getCursor();
}
return m_cursor;
} }
void PDFWidgetTool::setActiveImpl(bool active) void PDFWidgetTool::setActiveImpl(bool active)
{ {
Q_UNUSED(active); for (PDFWidgetTool* tool : m_toolStack)
{
tool->setActive(active);
}
} }
void PDFWidgetTool::updateActions() void PDFWidgetTool::updateActions()
@ -149,6 +167,26 @@ void PDFWidgetTool::updateActions()
} }
} }
PDFWidgetTool* PDFWidgetTool::getTopToolstackTool() const
{
if (!m_toolStack.empty())
{
return m_toolStack.back();
}
return nullptr;
}
void PDFWidgetTool::addTool(PDFWidgetTool* tool)
{
m_toolStack.push_back(tool);
}
void PDFWidgetTool::removeTool()
{
m_toolStack.pop_back();
}
PDFFindTextTool::PDFFindTextTool(PDFDrawWidgetProxy* proxy, QAction* prevAction, QAction* nextAction, QObject* parent, QWidget* parentDialog) : PDFFindTextTool::PDFFindTextTool(PDFDrawWidgetProxy* proxy, QAction* prevAction, QAction* nextAction, QObject* parent, QWidget* parentDialog) :
BaseClass(proxy, parent), BaseClass(proxy, parent),
m_prevAction(prevAction), m_prevAction(prevAction),
@ -192,6 +230,8 @@ void PDFFindTextTool::clearResults()
void PDFFindTextTool::setActiveImpl(bool active) void PDFFindTextTool::setActiveImpl(bool active)
{ {
BaseClass::setActiveImpl(active);
if (active) if (active)
{ {
Q_ASSERT(!m_dialog); Q_ASSERT(!m_dialog);
@ -561,6 +601,8 @@ void PDFSelectTextTool::mouseMoveEvent(QWidget* widget, QMouseEvent* event)
void PDFSelectTextTool::setActiveImpl(bool active) void PDFSelectTextTool::setActiveImpl(bool active)
{ {
BaseClass::setActiveImpl(active);
if (active) if (active)
{ {
pdf::PDFAsynchronousTextLayoutCompiler* compiler = getProxy()->getTextLayoutCompiler(); pdf::PDFAsynchronousTextLayoutCompiler* compiler = getProxy()->getTextLayoutCompiler();
@ -665,6 +707,7 @@ PDFToolManager::PDFToolManager(PDFDrawWidgetProxy* proxy, Actions actions, QObje
m_predefinedTools[FindTextTool] = new PDFFindTextTool(proxy, actions.findPrevAction, actions.findNextAction, this, parentDialog); m_predefinedTools[FindTextTool] = new PDFFindTextTool(proxy, actions.findPrevAction, actions.findNextAction, this, parentDialog);
m_predefinedTools[SelectTextTool] = new PDFSelectTextTool(proxy, actions.selectTextToolAction, actions.copyTextAction, actions.selectAllAction, actions.deselectAction, this); m_predefinedTools[SelectTextTool] = new PDFSelectTextTool(proxy, actions.selectTextToolAction, actions.copyTextAction, actions.selectAllAction, actions.deselectAction, this);
m_predefinedTools[MagnifierTool] = new PDFMagnifierTool(proxy, actions.magnifierAction, this); m_predefinedTools[MagnifierTool] = new PDFMagnifierTool(proxy, actions.magnifierAction, this);
m_predefinedTools[ScreenshotTool] = new PDFScreenshotTool(proxy, actions.screenshotToolAction, this);
for (PDFWidgetTool* tool : m_predefinedTools) for (PDFWidgetTool* tool : m_predefinedTools)
{ {
@ -903,4 +946,71 @@ void PDFMagnifierTool::setMagnifierSize(int magnifierSize)
m_magnifierSize = magnifierSize; m_magnifierSize = magnifierSize;
} }
PDFPickTool::PDFPickTool(PDFDrawWidgetProxy* proxy, PDFPickTool::Mode mode, QObject* parent) :
BaseClass(proxy, parent),
m_mode(mode)
{
setCursor(Qt::BlankCursor);
}
void PDFPickTool::drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix) const
{
Q_UNUSED(layoutGetter);
m_snapper.drawSnapPoints(painter, pageIndex, compiledPage, pagePointToDevicePointMatrix);
}
void PDFPickTool::drawPostRendering(QPainter* painter, QRect rect) const
{
QPoint hleft = m_mousePosition;
QPoint hright = m_mousePosition;
QPoint vtop = m_mousePosition;
QPoint vbottom = m_mousePosition;
hleft.setX(0);
hright.setX(rect.width());
vtop.setY(0);
vbottom.setY(rect.height());
painter->setPen(Qt::black);
painter->drawLine(hleft, hright);
painter->drawLine(vtop, vbottom);
}
void PDFPickTool::mousePressEvent(QWidget* widget, QMouseEvent* event)
{
Q_UNUSED(widget);
event->accept();
}
void PDFPickTool::mouseReleaseEvent(QWidget* widget, QMouseEvent* event)
{
Q_UNUSED(widget);
event->accept();
}
void PDFPickTool::mouseMoveEvent(QWidget* widget, QMouseEvent* event)
{
Q_UNUSED(widget);
event->accept();
QPoint mousePos = event->pos();
if (m_mousePosition != mousePos)
{
m_mousePosition = mousePos;
getProxy()->repaintNeeded();
}
}
PDFScreenshotTool::PDFScreenshotTool(PDFDrawWidgetProxy* proxy, QAction* action, QObject* parent) :
BaseClass(proxy, action, parent),
m_pickTool(nullptr)
{
m_pickTool = new PDFPickTool(proxy, PDFPickTool::Mode::Rectangles, this);
addTool(m_pickTool);
}
} // namespace pdf } // namespace pdf

View File

@ -20,6 +20,7 @@
#include "pdfdrawspacecontroller.h" #include "pdfdrawspacecontroller.h"
#include "pdftextlayout.h" #include "pdftextlayout.h"
#include "pdfsnapper.h"
#include <QDialog> #include <QDialog>
#include <QCursor> #include <QCursor>
@ -44,12 +45,6 @@ public:
explicit PDFWidgetTool(PDFDrawWidgetProxy* proxy, QAction* action, QObject* parent); explicit PDFWidgetTool(PDFDrawWidgetProxy* proxy, QAction* action, QObject* parent);
virtual ~PDFWidgetTool(); virtual ~PDFWidgetTool();
virtual void drawPage(QPainter* painter,
PDFInteger pageIndex,
const PDFPrecompiledPage* compiledPage,
PDFTextLayoutGetter& layoutGetter,
const QMatrix& pagePointToDevicePointMatrix) const override;
/// Sets document, shuts down the tool, if it is active, and document /// Sets document, shuts down the tool, if it is active, and document
/// is changing. /// is changing.
/// \param document Document /// \param document Document
@ -93,7 +88,7 @@ public:
/// Returns actual cursor defined by the tool. Cursor can be undefined, /// Returns actual cursor defined by the tool. Cursor can be undefined,
/// in this case, optional will be set to nullopt. /// in this case, optional will be set to nullopt.
const std::optional<QCursor>& getCursor() const { return m_cursor; } const std::optional<QCursor>& getCursor() const;
signals: signals:
void toolActivityChanged(bool active); void toolActivityChanged(bool active);
@ -102,12 +97,19 @@ protected:
virtual void setActiveImpl(bool active); virtual void setActiveImpl(bool active);
virtual void updateActions(); virtual void updateActions();
/// Returns currently active tool from toolstack,
/// or nullptr, if no active tool from toolstack exists.
PDFWidgetTool* getTopToolstackTool() const;
const PDFDocument* getDocument() const { return m_document; } const PDFDocument* getDocument() const { return m_document; }
PDFDrawWidgetProxy* getProxy() const { return m_proxy; } PDFDrawWidgetProxy* getProxy() const { return m_proxy; }
inline void setCursor(QCursor cursor) { m_cursor = qMove(cursor); } inline void setCursor(QCursor cursor) { m_cursor = qMove(cursor); }
inline void unsetCursor() { m_cursor = std::nullopt; } inline void unsetCursor() { m_cursor = std::nullopt; }
void addTool(PDFWidgetTool* tool);
void removeTool();
private: private:
bool m_active; bool m_active;
const PDFDocument* m_document; const PDFDocument* m_document;
@ -271,6 +273,60 @@ private:
PDFReal m_magnifierZoom; PDFReal m_magnifierZoom;
}; };
/// Tools for picking various items on page - points, rectangles, images etc.
class PDFFORQTLIBSHARED_EXPORT PDFPickTool : public PDFWidgetTool
{
Q_OBJECT
private:
using BaseClass = PDFWidgetTool;
public:
enum class Mode
{
Points, ///< Pick points
Rectangles, ///< Pick rectangles
Images ///< Pick images
};
/// Constructs new picking tool
/// \param proxy Draw widget proxy
/// \param mode Picking mode
/// \param parent Parent object
explicit PDFPickTool(PDFDrawWidgetProxy* proxy, Mode mode, QObject* parent);
virtual void drawPage(QPainter* painter, PDFInteger pageIndex, const PDFPrecompiledPage* compiledPage, PDFTextLayoutGetter& layoutGetter, const QMatrix& pagePointToDevicePointMatrix) const override;
virtual void drawPostRendering(QPainter* painter, QRect rect) const override;
virtual void mousePressEvent(QWidget* widget, QMouseEvent* event) override;
virtual void mouseReleaseEvent(QWidget* widget, QMouseEvent* event) override;
virtual void mouseMoveEvent(QWidget* widget, QMouseEvent* event) override;
private:
Mode m_mode;
PDFSnapper m_snapper;
QPoint m_mousePosition;
};
/// Tool that makes screenshot of page area and copies it to the clipboard
class PDFFORQTLIBSHARED_EXPORT PDFScreenshotTool : public PDFWidgetTool
{
Q_OBJECT
private:
using BaseClass = PDFWidgetTool;
public:
/// Constructs new screenshot tool
/// \param proxy Draw widget proxy
/// \param action Tool activation action
/// \param parent Parent object
explicit PDFScreenshotTool(PDFDrawWidgetProxy* proxy, QAction* action, QObject* parent);
private:
PDFPickTool* m_pickTool;
};
/// Manager used for managing tools, their activity, availability /// Manager used for managing tools, their activity, availability
/// and other settings. It also defines a predefined set of tools, /// and other settings. It also defines a predefined set of tools,
/// available for various purposes (text searching, magnifier tool etc.) /// available for various purposes (text searching, magnifier tool etc.)
@ -291,6 +347,7 @@ public:
QAction* deselectAction = nullptr; QAction* deselectAction = nullptr;
QAction* copyTextAction = nullptr; QAction* copyTextAction = nullptr;
QAction* magnifierAction = nullptr; QAction* magnifierAction = nullptr;
QAction* screenshotToolAction = nullptr;
}; };
/// Construct new text search tool /// Construct new text search tool
@ -309,6 +366,7 @@ public:
FindTextTool, FindTextTool,
SelectTextTool, SelectTextTool,
MagnifierTool, MagnifierTool,
ScreenshotTool,
ToolEnd ToolEnd
}; };

View File

@ -25,27 +25,27 @@ int qt_default_dpi_x() { return 96; }
int qt_default_dpi_y() { return 96; } int qt_default_dpi_y() { return 96; }
#endif #endif
namespace pdfviewer namespace pdf
{ {
int PDFWidgetUtils::getPixelSize(QWidget* widget, pdf::PDFReal sizeMM) int PDFWidgetUtils::getPixelSize(QPaintDevice* device, pdf::PDFReal sizeMM)
{ {
const int width = widget->width(); const int width = device->width();
const int height = widget->height(); const int height = device->height();
if (width > height) if (width > height)
{ {
return pdf::PDFReal(width) * sizeMM / pdf::PDFReal(widget->widthMM()); return PDFReal(width) * sizeMM / PDFReal(device->widthMM());
} }
else else
{ {
return pdf::PDFReal(height) * sizeMM / pdf::PDFReal(widget->heightMM()); return PDFReal(height) * sizeMM / PDFReal(device->heightMM());
} }
} }
int PDFWidgetUtils::scaleDPI_x(QWidget* widget, int unscaledSize) int PDFWidgetUtils::scaleDPI_x(QPaintDevice* device, int unscaledSize)
{ {
const double logicalDPI_x = widget->logicalDpiX(); const double logicalDPI_x = device->logicalDpiX();
const double defaultDPI_x = qt_default_dpi_x(); const double defaultDPI_x = qt_default_dpi_x();
return (logicalDPI_x / defaultDPI_x) * unscaledSize; return (logicalDPI_x / defaultDPI_x) * unscaledSize;
} }
@ -63,7 +63,7 @@ void PDFWidgetUtils::scaleWidget(QWidget* widget, QSize unscaledSize)
widget->resize(width, height); widget->resize(width, height);
} }
QSize PDFWidgetUtils::scaleDPI(QWidget* widget, QSize unscaledSize) QSize PDFWidgetUtils::scaleDPI(QPaintDevice* widget, QSize unscaledSize)
{ {
const double logicalDPI_x = widget->logicalDpiX(); const double logicalDPI_x = widget->logicalDpiX();
const double logicalDPI_y = widget->logicalDpiY(); const double logicalDPI_y = widget->logicalDpiY();
@ -76,4 +76,4 @@ QSize PDFWidgetUtils::scaleDPI(QWidget* widget, QSize unscaledSize)
return QSize(width, height); return QSize(width, height);
} }
} // namespace pdfviewer } // namespace pdf

View File

@ -22,19 +22,20 @@
#include <QWidget> #include <QWidget>
namespace pdfviewer namespace pdf
{ {
class PDFWidgetUtils class PDFFORQTLIBSHARED_EXPORT PDFWidgetUtils
{ {
public: public:
PDFWidgetUtils() = delete; PDFWidgetUtils() = delete;
/// Converts size in MM to pixel size /// Converts size in MM to pixel size
static int getPixelSize(QWidget* widget, pdf::PDFReal sizeMM); static int getPixelSize(QPaintDevice* device, pdf::PDFReal sizeMM);
/// Scale horizontal DPI value /// Scale horizontal DPI value
static int scaleDPI_x(QWidget* widget, int unscaledSize); /// \param device Paint device to obtain logical DPI for scaling
static int scaleDPI_x(QPaintDevice* device, int unscaledSize);
/// Scales widget based on DPI /// Scales widget based on DPI
/// \param widget Widget to be scaled /// \param widget Widget to be scaled
@ -42,11 +43,11 @@ public:
static void scaleWidget(QWidget* widget, QSize unscaledSize); static void scaleWidget(QWidget* widget, QSize unscaledSize);
/// Scales size based on DPI /// Scales size based on DPI
/// \param widget Widget, from which we get DPI /// \param device Paint device to obtain logical DPI for scaling
/// \param unscaledSize Unscaled size /// \param unscaledSize Unscaled size
static QSize scaleDPI(QWidget* widget, QSize unscaledSize); static QSize scaleDPI(QPaintDevice* widget, QSize unscaledSize);
}; };
} // namespace pdfviewer } // namespace pdf
#endif // PDFWIDGETUTILS_H #endif // PDFWIDGETUTILS_H

View File

@ -44,8 +44,7 @@ SOURCES += \
pdftexttospeech.cpp \ pdftexttospeech.cpp \
pdfviewermainwindow.cpp \ pdfviewermainwindow.cpp \
pdfviewersettings.cpp \ pdfviewersettings.cpp \
pdfviewersettingsdialog.cpp \ pdfviewersettingsdialog.cpp
pdfwidgetutils.cpp
HEADERS += \ HEADERS += \
pdfaboutdialog.h \ pdfaboutdialog.h \
@ -58,8 +57,7 @@ HEADERS += \
pdftexttospeech.h \ pdftexttospeech.h \
pdfviewermainwindow.h \ pdfviewermainwindow.h \
pdfviewersettings.h \ pdfviewersettings.h \
pdfviewersettingsdialog.h \ pdfviewersettingsdialog.h
pdfwidgetutils.h
FORMS += \ FORMS += \
pdfaboutdialog.ui \ pdfaboutdialog.ui \

View File

@ -48,7 +48,7 @@ PDFAboutDialog::PDFAboutDialog(QWidget* parent) :
ui->tableWidget->setItem(i, 3, new QTableWidgetItem(info.url)); ui->tableWidget->setItem(i, 3, new QTableWidgetItem(info.url));
} }
PDFWidgetUtils::scaleWidget(this, QSize(750, 600)); pdf::PDFWidgetUtils::scaleWidget(this, QSize(750, 600));
} }
PDFAboutDialog::~PDFAboutDialog() PDFAboutDialog::~PDFAboutDialog()

View File

@ -48,13 +48,13 @@ PDFDocumentPropertiesDialog::PDFDocumentPropertiesDialog(const pdf::PDFDocument*
initializeFonts(document); initializeFonts(document);
initializeDisplayAndPrintSettings(document); initializeDisplayAndPrintSettings(document);
const int minimumSectionSize = PDFWidgetUtils::scaleDPI_x(this, 300); const int minimumSectionSize = pdf::PDFWidgetUtils::scaleDPI_x(this, 300);
for (QTreeWidget* widget : findChildren<QTreeWidget*>(QString(), Qt::FindChildrenRecursively)) for (QTreeWidget* widget : findChildren<QTreeWidget*>(QString(), Qt::FindChildrenRecursively))
{ {
widget->header()->setMinimumSectionSize(minimumSectionSize); widget->header()->setMinimumSectionSize(minimumSectionSize);
} }
PDFWidgetUtils::scaleWidget(this, QSize(750, 600)); pdf::PDFWidgetUtils::scaleWidget(this, QSize(750, 600));
} }
PDFDocumentPropertiesDialog::~PDFDocumentPropertiesDialog() PDFDocumentPropertiesDialog::~PDFDocumentPropertiesDialog()

View File

@ -41,5 +41,6 @@
<file>resources/play.svg</file> <file>resources/play.svg</file>
<file>resources/stop.svg</file> <file>resources/stop.svg</file>
<file>resources/magnifier.svg</file> <file>resources/magnifier.svg</file>
<file>resources/screenshot-tool.svg</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -80,7 +80,7 @@ PDFRenderToImagesDialog::PDFRenderToImagesDialog(const pdf::PDFDocument* documen
loadImageWriterSettings(); loadImageWriterSettings();
loadImageExportSettings(); loadImageExportSettings();
PDFWidgetUtils::scaleWidget(this, QSize(1000, 600)); pdf::PDFWidgetUtils::scaleWidget(this, QSize(1000, 600));
} }
PDFRenderToImagesDialog::~PDFRenderToImagesDialog() PDFRenderToImagesDialog::~PDFRenderToImagesDialog()

View File

@ -356,7 +356,7 @@ void PDFSidebarWidget::onOutlineItemClicked(const QModelIndex& index)
void PDFSidebarWidget::onThumbnailsSizeChanged(int size) void PDFSidebarWidget::onThumbnailsSizeChanged(int size)
{ {
const int thumbnailsSize = PDFWidgetUtils::getPixelSize(this, size * 10.0); const int thumbnailsSize = pdf::PDFWidgetUtils::getPixelSize(this, size * 10.0);
Q_ASSERT(thumbnailsSize > 0); Q_ASSERT(thumbnailsSize > 0);
m_thumbnailsModel->setThumbnailsSize(thumbnailsSize); m_thumbnailsModel->setThumbnailsSize(thumbnailsSize);
} }

View File

@ -94,7 +94,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) :
ui->setupUi(this); ui->setupUi(this);
// Initialize toolbar icon size // Initialize toolbar icon size
QSize iconSize = PDFWidgetUtils::scaleDPI(this, QSize(24, 24)); QSize iconSize = pdf::PDFWidgetUtils::scaleDPI(this, QSize(24, 24));
ui->mainToolBar->setIconSize(iconSize); ui->mainToolBar->setIconSize(iconSize);
// Initialize task bar progress // Initialize task bar progress
@ -190,6 +190,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) :
// Tools // Tools
ui->mainToolBar->addAction(ui->actionSelectText); ui->mainToolBar->addAction(ui->actionSelectText);
ui->mainToolBar->addAction(ui->actionMagnifier); ui->mainToolBar->addAction(ui->actionMagnifier);
ui->mainToolBar->addAction(ui->actionScreenshot);
connect(ui->actionZoom_In, &QAction::triggered, this, [this] { m_pdfWidget->getDrawWidgetProxy()->performOperation(pdf::PDFDrawWidgetProxy::ZoomIn); }); connect(ui->actionZoom_In, &QAction::triggered, this, [this] { m_pdfWidget->getDrawWidgetProxy()->performOperation(pdf::PDFDrawWidgetProxy::ZoomIn); });
connect(ui->actionZoom_Out, &QAction::triggered, this, [this] { m_pdfWidget->getDrawWidgetProxy()->performOperation(pdf::PDFDrawWidgetProxy::ZoomOut); }); connect(ui->actionZoom_Out, &QAction::triggered, this, [this] { m_pdfWidget->getDrawWidgetProxy()->performOperation(pdf::PDFDrawWidgetProxy::ZoomOut); });
@ -253,6 +254,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) :
actions.deselectAction = ui->actionDeselectText; actions.deselectAction = ui->actionDeselectText;
actions.copyTextAction = ui->actionCopyText; actions.copyTextAction = ui->actionCopyText;
actions.magnifierAction = ui->actionMagnifier; actions.magnifierAction = ui->actionMagnifier;
actions.screenshotToolAction = ui->actionScreenshot;
m_toolManager = new pdf::PDFToolManager(m_pdfWidget->getDrawWidgetProxy(), actions, this, this); m_toolManager = new pdf::PDFToolManager(m_pdfWidget->getDrawWidgetProxy(), actions, this, this);
m_pdfWidget->setToolManager(m_toolManager); m_pdfWidget->setToolManager(m_toolManager);
updateMagnifierToolSettings(); updateMagnifierToolSettings();
@ -1129,7 +1131,7 @@ void PDFViewerMainWindow::on_actionRendering_Errors_triggered()
void PDFViewerMainWindow::updateMagnifierToolSettings() void PDFViewerMainWindow::updateMagnifierToolSettings()
{ {
pdf::PDFMagnifierTool* magnifierTool = m_toolManager->getMagnifierTool(); pdf::PDFMagnifierTool* magnifierTool = m_toolManager->getMagnifierTool();
magnifierTool->setMagnifierSize(PDFWidgetUtils::scaleDPI_x(this, m_settings->getSettings().m_magnifierSize)); magnifierTool->setMagnifierSize(pdf::PDFWidgetUtils::scaleDPI_x(this, m_settings->getSettings().m_magnifierSize));
magnifierTool->setMagnifierZoom(m_settings->getSettings().m_magnifierZoom); magnifierTool->setMagnifierZoom(m_settings->getSettings().m_magnifierZoom);
} }

View File

@ -87,6 +87,7 @@
<string>Tools</string> <string>Tools</string>
</property> </property>
<addaction name="actionMagnifier"/> <addaction name="actionMagnifier"/>
<addaction name="actionScreenshot"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionRendering_Errors"/> <addaction name="actionRendering_Errors"/>
<addaction name="separator"/> <addaction name="separator"/>
@ -473,6 +474,18 @@
<string>Magnifier Tool</string> <string>Magnifier Tool</string>
</property> </property>
</action> </action>
<action name="actionScreenshot">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="pdfforqtviewer.qrc">
<normaloff>:/resources/screenshot-tool.svg</normaloff>:/resources/screenshot-tool.svg</iconset>
</property>
<property name="text">
<string>Screenshot</string>
</property>
</action>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources> <resources>

View File

@ -0,0 +1,135 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="30mm"
height="30mm"
viewBox="0 0 30 30"
version="1.1"
id="svg5291"
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
sodipodi:docname="screenshot-tool.svg">
<defs
id="defs5285">
<inkscape:perspective
sodipodi:type="inkscape:persp3d"
inkscape:vp_x="0 : 15 : 1"
inkscape:vp_y="0 : 1000 : 0"
inkscape:vp_z="30 : 15 : 1"
inkscape:persp3d-origin="15 : 10 : 1"
id="perspective5921" />
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.35355338"
inkscape:cx="813.01344"
inkscape:cy="-622.0065"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="3840"
inkscape:window-height="2035"
inkscape:window-x="-13"
inkscape:window-y="-13"
inkscape:window-maximized="1" />
<metadata
id="metadata5288">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
<dc:creator>
<cc:Agent>
<dc:title>Jakub Melka</dc:title>
</cc:Agent>
</dc:creator>
<cc:license
rdf:resource="http://creativecommons.org/licenses/by-sa/4.0/" />
</cc:Work>
<cc:License
rdf:about="http://creativecommons.org/licenses/by-sa/4.0/">
<cc:permits
rdf:resource="http://creativecommons.org/ns#Reproduction" />
<cc:permits
rdf:resource="http://creativecommons.org/ns#Distribution" />
<cc:requires
rdf:resource="http://creativecommons.org/ns#Notice" />
<cc:requires
rdf:resource="http://creativecommons.org/ns#Attribution" />
<cc:permits
rdf:resource="http://creativecommons.org/ns#DerivativeWorks" />
<cc:requires
rdf:resource="http://creativecommons.org/ns#ShareAlike" />
</cc:License>
</rdf:RDF>
</metadata>
<g
inkscape:label="Vrstva 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-267)">
<flowRoot
xml:space="preserve"
id="flowRoot5913"
style="font-style:normal;font-weight:normal;font-size:40px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"><flowRegion
id="flowRegion5915"><rect
id="rect5917"
width="129.22377"
height="91.747108"
x="-13.788582"
y="-33.515606" /></flowRegion><flowPara
id="flowPara5919" /></flowRoot> <flowRoot
xml:space="preserve"
id="flowRoot843"
style="font-style:italic;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:40px;line-height:1.25;font-family:sans-serif;-inkscape-font-specification:'sans-serif Italic';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none"><flowRegion
id="flowRegion845"><rect
id="rect847"
width="159.09903"
height="129.04698"
x="-37.123108"
y="-35.460152" /></flowRegion><flowPara
id="flowPara849" /></flowRoot> <rect
style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:1.39999998;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
id="rect851"
width="24.789249"
height="16.884754"
x="2.5958552"
y="275.43802" />
<rect
style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:1.78122473;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
id="rect853"
width="2.7173724"
height="1.2168522"
x="4.1575332"
y="273.02496" />
<ellipse
style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:1.39999998;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
id="path855"
cx="14.84974"
cy="283.91141"
rx="5.589323"
ry="5.4322267" />
<ellipse
style="fill:none;fill-opacity:1;stroke:#000000;stroke-width:1.39999998;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;paint-order:stroke fill markers"
id="path857"
cx="14.896935"
cy="283.81024"
rx="3.0401912"
ry="2.9934189" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 5.0 KiB