2021-05-24 07:25:56 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2006-2007 Pino Toscano <pino@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2006-12-27 23:56:38 +00:00
|
|
|
|
2007-12-24 00:54:21 +00:00
|
|
|
#include "guiutils.h"
|
2007-04-19 18:30:20 +00:00
|
|
|
|
2006-12-27 23:56:38 +00:00
|
|
|
// qt/kde includes
|
2014-10-19 13:20:30 +00:00
|
|
|
#include <KLocalizedString>
|
2016-07-21 19:53:54 +00:00
|
|
|
#include <KMessageBox>
|
2020-09-03 11:02:13 +00:00
|
|
|
#include <QApplication>
|
2016-07-21 19:53:54 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QTextDocument>
|
2006-12-27 23:56:38 +00:00
|
|
|
|
|
|
|
// local includes
|
2012-09-27 12:14:03 +00:00
|
|
|
#include "core/action.h"
|
2006-12-27 23:56:38 +00:00
|
|
|
#include "core/annotations.h"
|
2008-04-14 22:31:28 +00:00
|
|
|
#include "core/document.h"
|
2006-12-27 23:56:38 +00:00
|
|
|
|
2008-11-15 23:27:27 +00:00
|
|
|
#include <memory>
|
2016-07-21 19:53:54 +00:00
|
|
|
|
2007-12-24 00:54:21 +00:00
|
|
|
namespace GuiUtils
|
|
|
|
{
|
2007-09-08 14:44:21 +00:00
|
|
|
QString captionForAnnotation(const Okular::Annotation *ann)
|
2006-12-27 23:56:38 +00:00
|
|
|
{
|
2007-10-03 22:53:27 +00:00
|
|
|
Q_ASSERT(ann);
|
2006-12-27 23:56:38 +00:00
|
|
|
|
2018-04-20 17:38:08 +00:00
|
|
|
const bool hasComment = !ann->contents().isEmpty();
|
|
|
|
|
2006-12-27 23:56:38 +00:00
|
|
|
QString ret;
|
|
|
|
switch (ann->subType()) {
|
|
|
|
case Okular::Annotation::AText:
|
|
|
|
if (((Okular::TextAnnotation *)ann)->textType() == Okular::TextAnnotation::Linked) {
|
2013-05-20 12:52:47 +00:00
|
|
|
ret = i18n("Pop-up Note");
|
2020-07-10 22:15:05 +00:00
|
|
|
} else {
|
Add typewriter annotation tool
Summary:
Typewriter is originally specified by the PDF reference as special FreeText annotation, where Intent=FreeTextTypewriter. It features opaque letters on transparent background, so that users can fill non interactive forms. Herewith typewriter is implemented natively for PDF, and there's also an Okular specific implementation for other document types. The added tool reuses the inline note UI.
This work was done during GSoC 2018. See https://community.kde.org/GSoC/2018/StatusReports/DileepSankhla for details.
FEATURE: 353401
Test Plan:
- okularpartrc is generated (if not yet existing) with typewriter as 10th tool
- typewriter tool is also available in Annotation Tools -> Add, Typ "Typewriter"
- selecting the tool and left click into document opens inline note input dialog
- finishing creates an annotation similar to inline note, but with transparent background
- saving into PDF results in /Subtype FreeText /IT /FreeTextTypeWriter
- saving typewriter into archive stores color with alpha channel = 0x00
- opening annotated archive works, if archive was created with old Okular, and opened in patched Okular
- opening annotated archive works, if archive was created with patched Okular, and opened in old Okular
Reviewers: sander
Reviewed By: sander
Subscribers: ngraham, sander, okular-devel
Tags: #okular
Differential Revision: https://phabricator.kde.org/D15204
2018-09-21 20:10:04 +00:00
|
|
|
if (((Okular::TextAnnotation *)ann)->inplaceIntent() == Okular::TextAnnotation::TypeWriter) {
|
|
|
|
ret = i18n("Typewriter");
|
2006-12-27 23:56:38 +00:00
|
|
|
} else {
|
Add typewriter annotation tool
Summary:
Typewriter is originally specified by the PDF reference as special FreeText annotation, where Intent=FreeTextTypewriter. It features opaque letters on transparent background, so that users can fill non interactive forms. Herewith typewriter is implemented natively for PDF, and there's also an Okular specific implementation for other document types. The added tool reuses the inline note UI.
This work was done during GSoC 2018. See https://community.kde.org/GSoC/2018/StatusReports/DileepSankhla for details.
FEATURE: 353401
Test Plan:
- okularpartrc is generated (if not yet existing) with typewriter as 10th tool
- typewriter tool is also available in Annotation Tools -> Add, Typ "Typewriter"
- selecting the tool and left click into document opens inline note input dialog
- finishing creates an annotation similar to inline note, but with transparent background
- saving into PDF results in /Subtype FreeText /IT /FreeTextTypeWriter
- saving typewriter into archive stores color with alpha channel = 0x00
- opening annotated archive works, if archive was created with old Okular, and opened in patched Okular
- opening annotated archive works, if archive was created with patched Okular, and opened in old Okular
Reviewers: sander
Reviewed By: sander
Subscribers: ngraham, sander, okular-devel
Tags: #okular
Differential Revision: https://phabricator.kde.org/D15204
2018-09-21 20:10:04 +00:00
|
|
|
ret = i18n("Inline Note");
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
Add typewriter annotation tool
Summary:
Typewriter is originally specified by the PDF reference as special FreeText annotation, where Intent=FreeTextTypewriter. It features opaque letters on transparent background, so that users can fill non interactive forms. Herewith typewriter is implemented natively for PDF, and there's also an Okular specific implementation for other document types. The added tool reuses the inline note UI.
This work was done during GSoC 2018. See https://community.kde.org/GSoC/2018/StatusReports/DileepSankhla for details.
FEATURE: 353401
Test Plan:
- okularpartrc is generated (if not yet existing) with typewriter as 10th tool
- typewriter tool is also available in Annotation Tools -> Add, Typ "Typewriter"
- selecting the tool and left click into document opens inline note input dialog
- finishing creates an annotation similar to inline note, but with transparent background
- saving into PDF results in /Subtype FreeText /IT /FreeTextTypeWriter
- saving typewriter into archive stores color with alpha channel = 0x00
- opening annotated archive works, if archive was created with old Okular, and opened in patched Okular
- opening annotated archive works, if archive was created with patched Okular, and opened in old Okular
Reviewers: sander
Reviewed By: sander
Subscribers: ngraham, sander, okular-devel
Tags: #okular
Differential Revision: https://phabricator.kde.org/D15204
2018-09-21 20:10:04 +00:00
|
|
|
}
|
2006-12-27 23:56:38 +00:00
|
|
|
break;
|
|
|
|
case Okular::Annotation::ALine:
|
2013-04-19 19:12:34 +00:00
|
|
|
if (((Okular::LineAnnotation *)ann)->linePoints().count() == 2) {
|
2018-04-20 17:38:08 +00:00
|
|
|
ret = hasComment ? i18n("Straight Line with Comment") : i18n("Straight Line");
|
2013-04-20 08:53:04 +00:00
|
|
|
} else {
|
2018-04-20 17:38:08 +00:00
|
|
|
ret = hasComment ? i18n("Polygon with Comment") : i18n("Polygon");
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2013-04-20 08:53:04 +00:00
|
|
|
break;
|
|
|
|
case Okular::Annotation::AGeom:
|
2018-04-20 17:38:08 +00:00
|
|
|
ret = hasComment ? i18n("Geometry with Comment") : i18n("Geometry");
|
2006-12-27 23:56:38 +00:00
|
|
|
break;
|
2008-04-13 16:42:14 +00:00
|
|
|
case Okular::Annotation::AHighlight:
|
2008-04-13 20:04:08 +00:00
|
|
|
switch (((Okular::HighlightAnnotation *)ann)->highlightType()) {
|
2008-08-23 00:09:59 +00:00
|
|
|
case Okular::HighlightAnnotation::Highlight:
|
2012-08-16 09:53:58 +00:00
|
|
|
ret = hasComment ? i18n("Highlight with Comment") : i18n("Highlight");
|
|
|
|
break;
|
|
|
|
case Okular::HighlightAnnotation::Squiggly:
|
|
|
|
ret = hasComment ? i18n("Squiggle with Comment") : i18n("Squiggle");
|
|
|
|
break;
|
2015-08-04 09:11:29 +00:00
|
|
|
case Okular::HighlightAnnotation::Underline:
|
|
|
|
ret = hasComment ? i18n("Underline with Comment") : i18n("Underline");
|
|
|
|
break;
|
2006-12-27 23:56:38 +00:00
|
|
|
case Okular::HighlightAnnotation::StrikeOut:
|
|
|
|
ret = hasComment ? i18n("Strike Out with Comment") : i18n("Strike Out");
|
|
|
|
break;
|
2020-07-10 22:15:05 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-12-27 23:56:38 +00:00
|
|
|
case Okular::Annotation::AStamp:
|
2018-04-20 17:38:08 +00:00
|
|
|
ret = hasComment ? i18n("Stamp with Comment") : i18n("Stamp");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2006-12-27 23:56:38 +00:00
|
|
|
case Okular::Annotation::AInk:
|
2018-04-20 17:38:08 +00:00
|
|
|
ret = hasComment ? i18n("Freehand Line with Comment") : i18n("Freehand Line");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2008-04-13 16:42:14 +00:00
|
|
|
case Okular::Annotation::ACaret:
|
|
|
|
ret = i18n("Caret");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2008-04-13 16:42:14 +00:00
|
|
|
case Okular::Annotation::AFileAttachment:
|
|
|
|
ret = i18n("File Attachment");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2006-12-27 23:56:38 +00:00
|
|
|
case Okular::Annotation::ASound:
|
2008-04-13 20:04:08 +00:00
|
|
|
ret = i18n("Sound");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2006-12-27 23:56:38 +00:00
|
|
|
case Okular::Annotation::AMovie:
|
2008-08-23 00:09:59 +00:00
|
|
|
ret = i18n("Movie");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2012-08-16 09:53:58 +00:00
|
|
|
case Okular::Annotation::AScreen:
|
|
|
|
ret = i18nc("Caption for a screen annotation", "Screen");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2008-04-13 16:42:14 +00:00
|
|
|
case Okular::Annotation::AWidget:
|
2012-08-16 09:53:58 +00:00
|
|
|
ret = i18nc("Caption for a widget annotation", "Widget");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2008-04-13 16:42:14 +00:00
|
|
|
case Okular::Annotation::ARichMedia:
|
2015-08-04 09:11:29 +00:00
|
|
|
ret = i18nc("Caption for a rich media annotation", "Rich Media");
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2012-08-16 09:53:58 +00:00
|
|
|
case Okular::Annotation::A_BASE:
|
2020-07-10 22:15:05 +00:00
|
|
|
break;
|
2006-12-27 23:56:38 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-08 14:44:21 +00:00
|
|
|
QString authorForAnnotation(const Okular::Annotation *ann)
|
2007-09-08 14:41:40 +00:00
|
|
|
{
|
2007-10-03 22:53:27 +00:00
|
|
|
Q_ASSERT(ann);
|
2007-09-08 14:41:40 +00:00
|
|
|
|
|
|
|
return !ann->author().isEmpty() ? ann->author() : i18nc("Unknown author", "Unknown");
|
|
|
|
}
|
|
|
|
|
2007-09-08 14:44:21 +00:00
|
|
|
QString contentsHtml(const Okular::Annotation *ann)
|
2006-12-28 00:40:09 +00:00
|
|
|
{
|
2014-10-01 05:27:09 +00:00
|
|
|
QString text = ann->contents().toHtmlEscaped();
|
2016-07-11 20:05:18 +00:00
|
|
|
text.replace(QLatin1Char('\n'), QLatin1String("<br>"));
|
2008-04-03 09:52:05 +00:00
|
|
|
return text;
|
2006-12-28 00:40:09 +00:00
|
|
|
}
|
|
|
|
|
2007-09-08 14:44:21 +00:00
|
|
|
QString prettyToolTip(const Okular::Annotation *ann)
|
2007-09-08 14:41:40 +00:00
|
|
|
{
|
2007-10-03 22:53:27 +00:00
|
|
|
Q_ASSERT(ann);
|
2007-09-08 14:41:40 +00:00
|
|
|
|
|
|
|
QString author = authorForAnnotation(ann);
|
|
|
|
QString contents = contentsHtml(ann);
|
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
QString tooltip = QStringLiteral("<qt><b>") + i18n("Author: %1", author) + QStringLiteral("</b>");
|
2007-09-08 14:41:40 +00:00
|
|
|
if (!contents.isEmpty()) {
|
2015-10-29 12:37:11 +00:00
|
|
|
tooltip += QStringLiteral("<div style=\"font-size: 4px;\"><hr /></div>") + contents;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2007-09-08 14:41:40 +00:00
|
|
|
|
2015-10-29 12:37:11 +00:00
|
|
|
tooltip += QLatin1String("</qt>");
|
2007-09-08 14:41:40 +00:00
|
|
|
|
|
|
|
return tooltip;
|
|
|
|
}
|
|
|
|
|
2008-04-14 22:31:28 +00:00
|
|
|
void saveEmbeddedFile(Okular::EmbeddedFile *ef, QWidget *parent)
|
|
|
|
{
|
|
|
|
const QString caption = i18n("Where do you want to save %1?", ef->name());
|
2016-01-30 15:50:11 +00:00
|
|
|
const QString path = QFileDialog::getSaveFileName(parent, caption, ef->name());
|
2008-04-14 22:31:28 +00:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
return;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2016-03-23 00:58:38 +00:00
|
|
|
QFile targetFile(path);
|
|
|
|
writeEmbeddedFile(ef, parent, targetFile);
|
|
|
|
}
|
2008-04-14 22:31:28 +00:00
|
|
|
|
2016-03-23 00:58:38 +00:00
|
|
|
void writeEmbeddedFile(Okular::EmbeddedFile *ef, QWidget *parent, QFile &target)
|
|
|
|
{
|
|
|
|
if (!target.open(QIODevice::WriteOnly)) {
|
|
|
|
KMessageBox::error(parent, i18n("Could not open \"%1\" for writing. File was not saved.", target.fileName()));
|
2009-12-26 20:47:58 +00:00
|
|
|
return;
|
2008-04-14 22:31:28 +00:00
|
|
|
}
|
2016-03-23 00:58:38 +00:00
|
|
|
target.write(ef->data());
|
|
|
|
target.close();
|
2008-04-14 22:31:28 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 12:14:03 +00:00
|
|
|
Okular::Movie *renditionMovieFromScreenAnnotation(const Okular::ScreenAnnotation *annotation)
|
|
|
|
{
|
|
|
|
if (!annotation) {
|
2017-09-05 21:27:18 +00:00
|
|
|
return nullptr;
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-09-27 12:14:03 +00:00
|
|
|
|
|
|
|
if (annotation->action() && annotation->action()->actionType() == Okular::Action::Rendition) {
|
|
|
|
Okular::RenditionAction *renditionAction = static_cast<Okular::RenditionAction *>(annotation->action());
|
|
|
|
return renditionAction->movie();
|
|
|
|
}
|
|
|
|
|
2017-09-05 21:27:18 +00:00
|
|
|
return nullptr;
|
2012-09-27 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 10:19:21 +00:00
|
|
|
// from Arthur - qt4
|
2013-05-18 20:23:20 +00:00
|
|
|
static inline int qt_div_255(int x)
|
|
|
|
{
|
|
|
|
return (x + (x >> 8) + 0x80) >> 8;
|
|
|
|
}
|
2012-06-12 10:19:21 +00:00
|
|
|
|
|
|
|
void colorizeImage(QImage &grayImage, const QColor &color, unsigned int destAlpha)
|
|
|
|
{
|
|
|
|
// Make sure that the image is Format_ARGB32_Premultiplied
|
|
|
|
if (grayImage.format() != QImage::Format_ARGB32_Premultiplied) {
|
|
|
|
grayImage = grayImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-06-12 10:19:21 +00:00
|
|
|
|
|
|
|
// iterate over all pixels changing the alpha component value
|
2020-02-19 15:46:23 +00:00
|
|
|
unsigned int *data = reinterpret_cast<unsigned int *>(grayImage.bits());
|
2012-06-12 10:19:21 +00:00
|
|
|
unsigned int pixels = grayImage.width() * grayImage.height();
|
|
|
|
int red = color.red(), green = color.green(), blue = color.blue();
|
|
|
|
|
|
|
|
int source, sourceSat, sourceAlpha;
|
|
|
|
for (unsigned int i = 0; i < pixels; ++i) { // optimize this loop keeping byte order into account
|
|
|
|
source = data[i];
|
|
|
|
sourceSat = qRed(source);
|
|
|
|
int newR = qt_div_255(sourceSat * red), newG = qt_div_255(sourceSat * green), newB = qt_div_255(sourceSat * blue);
|
|
|
|
if ((sourceAlpha = qAlpha(source)) == 255) {
|
|
|
|
// use destAlpha
|
|
|
|
data[i] = qRgba(newR, newG, newB, destAlpha);
|
|
|
|
} else {
|
|
|
|
// use destAlpha * sourceAlpha product
|
|
|
|
if (destAlpha < 255) {
|
|
|
|
sourceAlpha = qt_div_255(destAlpha * sourceAlpha);
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
2012-06-12 10:19:21 +00:00
|
|
|
data[i] = qRgba(newR, newG, newB, sourceAlpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:02:13 +00:00
|
|
|
QIcon createColorIcon(const QList<QColor> &colors, const QIcon &background, ColorIconFlags flags)
|
|
|
|
{
|
|
|
|
QIcon colorIcon;
|
|
|
|
|
|
|
|
// Create a pixmap for each common icon size.
|
|
|
|
for (int size : {16, 22, 24, 32, 48}) {
|
|
|
|
QPixmap pixmap(QSize(size, size) * qApp->devicePixelRatio());
|
|
|
|
pixmap.setDevicePixelRatio(qApp->devicePixelRatio());
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
// Configure hairlines for visualization of outline or transparency (visualizeTransparent):
|
|
|
|
painter.setPen(QPen(qApp->palette().color(QPalette::Active, QPalette::WindowText), 0));
|
|
|
|
painter.setBrush(Qt::NoBrush);
|
|
|
|
|
|
|
|
if (background.isNull()) {
|
|
|
|
// Full-size color rectangles.
|
|
|
|
// Draw rectangles left to right:
|
|
|
|
for (int i = 0; i < colors.count(); ++i) {
|
|
|
|
if (!colors.at(i).isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QRect rect(QPoint(size * i / colors.count(), 0), QPoint(size * (i + 1) / colors.count(), size));
|
|
|
|
if ((flags & VisualizeTransparent) && (colors.at(i) == Qt::transparent)) {
|
|
|
|
painter.drawLine(rect.topLeft(), rect.bottomRight());
|
|
|
|
painter.drawLine(rect.bottomLeft(), rect.topRight());
|
|
|
|
} else {
|
|
|
|
painter.fillRect(rect, colors.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw hairline outline:
|
|
|
|
// To get the hairline on the outermost pixels, we shrink the rectangle by a half pixel on each edge.
|
|
|
|
const qreal halfPixelWidth = 0.5 / pixmap.devicePixelRatio();
|
|
|
|
painter.drawRect(QRectF(QPointF(halfPixelWidth, halfPixelWidth), QPointF(qreal(size) - halfPixelWidth, qreal(size) - halfPixelWidth)));
|
|
|
|
} else {
|
|
|
|
// Lower 25% color rectangles.
|
|
|
|
// Draw background icon:
|
|
|
|
background.paint(&painter, QRect(QPoint(0, 0), QSize(size, size)));
|
|
|
|
|
|
|
|
// Draw rectangles left to right:
|
|
|
|
for (int i = 0; i < colors.count(); ++i) {
|
|
|
|
if (!colors.at(i).isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QRect rect(QPoint(size * i / colors.count(), size * 3 / 4), QPoint(size * (i + 1) / colors.count(), size));
|
|
|
|
if ((flags & VisualizeTransparent) && (colors.at(i) == Qt::transparent)) {
|
|
|
|
painter.drawLine(rect.topLeft(), rect.bottomRight());
|
|
|
|
painter.drawLine(rect.bottomLeft(), rect.topRight());
|
|
|
|
} else {
|
|
|
|
painter.fillRect(rect, colors.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.end();
|
|
|
|
colorIcon.addPixmap(pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return colorIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon createOpacityIcon(qreal opacity)
|
|
|
|
{
|
|
|
|
QIcon opacityIcon;
|
|
|
|
|
|
|
|
// Create a pixmap for each common icon size.
|
|
|
|
for (int size : {16, 22, 24, 32, 48}) {
|
|
|
|
QPixmap pixmap(QSize(size, size) * qApp->devicePixelRatio());
|
|
|
|
pixmap.setDevicePixelRatio(qApp->devicePixelRatio());
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
painter.setBrush(qApp->palette().color(QPalette::Active, QPalette::WindowText));
|
|
|
|
|
|
|
|
// Checkerboard pattern
|
|
|
|
painter.drawRect(QRectF(QPoint(0, 0), QPoint(size, size) / 2));
|
|
|
|
painter.drawRect(QRectF(QPoint(size, size) / 2, QPoint(size, size)));
|
|
|
|
|
|
|
|
// Opacity
|
|
|
|
painter.setOpacity(opacity);
|
|
|
|
painter.drawRect(QRect(QPoint(0, 0), QPoint(size, size)));
|
|
|
|
|
|
|
|
painter.end();
|
|
|
|
opacityIcon.addPixmap(pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return opacityIcon;
|
|
|
|
}
|
|
|
|
|
2007-09-08 14:44:21 +00:00
|
|
|
}
|