Advanced tools - create sticky note (beginning)

This commit is contained in:
Jakub Melka
2020-12-03 19:58:50 +01:00
parent 0c92648e2a
commit 4d0c1e0ab1
8 changed files with 319 additions and 30 deletions

View File

@@ -0,0 +1,65 @@
// 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 "pdfadvancedtools.h"
#include <QActionGroup>
namespace pdf
{
PDFCreateStickyNoteTool::PDFCreateStickyNoteTool(PDFDrawWidgetProxy* proxy, PDFToolManager* toolManager, QActionGroup* actionGroup, QObject* parent) :
BaseClass(proxy, parent),
m_toolManager(toolManager),
m_actionGroup(actionGroup),
m_pickTool(nullptr)
{
m_pickTool = new PDFPickTool(proxy, PDFPickTool::Mode::Points, this);
addTool(m_pickTool);
connect(m_pickTool, &PDFPickTool::pointPicked, this, &PDFCreateStickyNoteTool::onPointPicked);
connect(m_actionGroup, &QActionGroup::triggered, this, &PDFCreateStickyNoteTool::onActionTriggered);
updateActions();
}
void PDFCreateStickyNoteTool::updateActions()
{
BaseClass::updateActions();
if (m_actionGroup)
{
const bool isEnabled = getDocument() && getDocument()->getStorage().getSecurityHandler()->isAllowed(PDFSecurityHandler::Permission::Modify);
m_actionGroup->setEnabled(isEnabled);
if (!isActive() && m_actionGroup->checkedAction())
{
m_actionGroup->checkedAction()->setChecked(false);
}
}
}
void PDFCreateStickyNoteTool::onActionTriggered(QAction* action)
{
setActive(action && action->isChecked());
}
void PDFCreateStickyNoteTool::onPointPicked(PDFInteger pageIndex, QPointF pagePoint)
{
}
} // namespace pdf

View File

@@ -0,0 +1,56 @@
// 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 PDFADVANCEDTOOLS_H
#define PDFADVANCEDTOOLS_H
#include "pdfwidgettool.h"
#include "pdfannotation.h"
class QActionGroup;
namespace pdf
{
/// Tool that creates 'sticky note' annotations. Multiple types of sticky
/// notes are available, user can select a type of sticky note. When
/// user select a point, popup window appears and user can enter a text.
class PDFFORQTLIBSHARED_EXPORT PDFCreateStickyNoteTool : public PDFWidgetTool
{
Q_OBJECT
private:
using BaseClass = PDFWidgetTool;
public:
explicit PDFCreateStickyNoteTool(PDFDrawWidgetProxy* proxy, PDFToolManager* toolManager, QActionGroup* actionGroup, QObject* parent);
protected:
virtual void updateActions() override;
private:
void onActionTriggered(QAction* action);
void onPointPicked(PDFInteger pageIndex, QPointF pagePoint);
PDFToolManager* m_toolManager;
QActionGroup* m_actionGroup;
PDFPickTool* m_pickTool;
};
} // namespace pdf
#endif // PDFADVANCEDTOOLS_H

View File

@@ -1960,35 +1960,7 @@ void PDFTextAnnotation::draw(AnnotationDrawParameters& parameters) const
QFont font = QApplication::font();
font.setPixelSize(16.0);
QString text = "?";
if (m_iconName == "Comment")
{
text = QString::fromUtf16(u"\U0001F4AC");
}
else if (m_iconName == "Help")
{
text = "?";
}
else if (m_iconName == "Insert")
{
text = QString::fromUtf16(u"\u2380");
}
else if (m_iconName == "Key")
{
text = QString::fromUtf16(u"\U0001F511");
}
else if (m_iconName == "NewParagraph")
{
text = QString::fromUtf16(u"\u2606");
}
else if (m_iconName == "Note")
{
text = QString::fromUtf16(u"\u266A");
}
else if (m_iconName == "Paragraph")
{
text = QString::fromUtf16(u"\u00B6");
}
QString text = getTextForIcon(m_iconName);
QPainterPath textPath;
textPath.addText(0.0, 0.0, font, text);
@@ -2006,6 +1978,71 @@ PDFTextAnnotation::Flags PDFTextAnnotation::getEffectiveFlags() const
return getFlags() | NoZoom | NoRotate;
}
QIcon PDFTextAnnotation::createIcon(QString key, QSize size)
{
QIcon icon;
QPixmap pixmap(size);
pixmap.fill(Qt::transparent);
QRect rectangle(QPoint(0, 0), size);
rectangle.adjust(1, 1, -1, -1);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
QString text = getTextForIcon(key);
QFont font = QApplication::font();
font.setPixelSize(size.height() * 0.75);
QPainterPath textPath;
textPath.addText(0.0, 0.0, font, text);
QRectF textBoundingRect = textPath.boundingRect();
QPointF offset = rectangle.center() - textBoundingRect.center();
textPath.translate(offset);
painter.fillPath(textPath, QBrush(Qt::black, Qt::SolidPattern));
painter.end();
icon.addPixmap(qMove(pixmap));
return icon;
}
QString PDFTextAnnotation::getTextForIcon(const QString& key)
{
QString text = "?";
if (key == "Comment")
{
text = QString::fromUtf16(u"\U0001F4AC");
}
else if (key == "Help")
{
text = "?";
}
else if (key == "Insert")
{
text = QString::fromUtf16(u"\u2380");
}
else if (key == "Key")
{
text = QString::fromUtf16(u"\U0001F511");
}
else if (key == "NewParagraph")
{
text = QString::fromUtf16(u"\u2606");
}
else if (key == "Note")
{
text = QString::fromUtf16(u"\u266A");
}
else if (key == "Paragraph")
{
text = QString::fromUtf16(u"\u00B6");
}
return text;
}
void PDFLineAnnotation::draw(AnnotationDrawParameters& parameters) const
{
QLineF line = getLine();

View File

@@ -742,7 +742,7 @@ enum class TextAnnotationIcon
/// as if flag NoZoom and NoRotate were set). When this annotation is opened,
/// it displays popup window containing the text of the note, font and size
/// is implementation dependent by viewer application.
class PDFTextAnnotation : public PDFMarkupAnnotation
class PDFFORQTLIBSHARED_EXPORT PDFTextAnnotation : public PDFMarkupAnnotation
{
public:
inline explicit PDFTextAnnotation() = default;
@@ -757,9 +757,13 @@ public:
const QString& getState() const { return m_state; }
const QString& getStateModel() const { return m_stateModel; }
static QIcon createIcon(QString key, QSize size);
private:
friend static PDFAnnotationPtr PDFAnnotation::parse(const PDFObjectStorage* storage, PDFObjectReference reference);
static QString getTextForIcon(const QString& key);
bool m_open = false;
QByteArray m_iconName;
QString m_state;