1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
SPDX-FileCopyrightText: 2006-2007 Pino Toscano <pino@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "guiutils.h"
// qt/kde includes
#include <KLocalizedString>
#include <KMessageBox>
#include <QApplication>
#include <QFileDialog>
#include <QPainter>
#include <QStandardPaths>
#include <QTextDocument>
// local includes
#include "core/action.h"
#include "core/annotations.h"
#include "core/document.h"
#include <memory>
namespace GuiUtils
{
// TODO: iconName should match AnnotationActionHandler in part/annotationactionhandler.cpp
AnnotationInfo getAnnotationInfo(const Okular::Annotation *ann)
{
Q_ASSERT(ann);
const bool hasComment = !ann->contents().isEmpty();
AnnotationInfo info;
switch (ann->subType()) {
case Okular::Annotation::AText: {
const auto *textAnn = static_cast<const Okular::TextAnnotation *>(ann);
if (textAnn->textType() == Okular::TextAnnotation::Linked) {
info.caption = i18n("Pop-up Note");
info.iconName = QStringLiteral("edit-comment");
} else {
if (textAnn->inplaceIntent() == Okular::TextAnnotation::TypeWriter) {
info.caption = i18n("Typewriter");
info.iconName = QStringLiteral("tool-text");
} else {
info.caption = i18n("Inline Note");
info.iconName = QStringLiteral("note");
}
}
break;
}
case Okular::Annotation::ALine: {
const auto *lineAnn = static_cast<const Okular::LineAnnotation *>(ann);
if (lineAnn->linePoints().count() == 2) {
if (lineAnn->lineEndStyle() == Okular::LineAnnotation::OpenArrow) {
info.caption = hasComment ? i18n("Arrow with Comment") : i18n("Arrow");
info.iconName = QStringLiteral("draw-arrow");
} else {
info.caption = hasComment ? i18n("Straight Line with Comment") : i18n("Straight Line");
info.iconName = QStringLiteral("draw-line");
}
} else {
info.caption = hasComment ? i18n("Polygon with Comment") : i18n("Polygon");
info.iconName = QStringLiteral("draw-polyline");
}
break;
}
case Okular::Annotation::AGeom: {
const auto *geomAnn = static_cast<const Okular::GeomAnnotation *>(ann);
if (geomAnn->geometricalType() == Okular::GeomAnnotation::InscribedSquare) {
info.caption = hasComment ? i18n("Rectangle with Comment") : i18n("Rectangle");
info.iconName = QStringLiteral("draw-rectangle");
} else {
info.caption = hasComment ? i18n("Ellipse with Comment") : i18n("Ellipse");
info.iconName = QStringLiteral("draw-ellipse");
}
break;
}
case Okular::Annotation::AHighlight: {
const auto *highlightAnn = static_cast<const Okular::HighlightAnnotation *>(ann);
switch (highlightAnn->highlightType()) {
case Okular::HighlightAnnotation::Highlight:
info.caption = hasComment ? i18n("Highlight with Comment") : i18n("Highlight");
info.iconName = QStringLiteral("draw-highlight");
break;
case Okular::HighlightAnnotation::Squiggly:
info.caption = hasComment ? i18n("Squiggle with Comment") : i18n("Squiggle");
info.iconName = QStringLiteral("format-text-underline-squiggle");
break;
case Okular::HighlightAnnotation::Underline:
info.caption = hasComment ? i18n("Underline with Comment") : i18n("Underline");
info.iconName = QStringLiteral("format-text-underline");
break;
case Okular::HighlightAnnotation::StrikeOut:
info.caption = hasComment ? i18n("Strike Out with Comment") : i18n("Strike Out");
info.iconName = QStringLiteral("format-text-strikethrough");
break;
}
break;
}
case Okular::Annotation::AStamp:
info.caption = hasComment ? i18n("Stamp with Comment") : i18n("Stamp");
info.iconName = QStringLiteral("tag");
break;
case Okular::Annotation::AInk:
info.caption = hasComment ? i18n("Freehand Line with Comment") : i18n("Freehand Line");
info.iconName = QStringLiteral("draw-freehand");
break;
case Okular::Annotation::ACaret:
info.caption = i18n("Caret");
info.iconName = QStringLiteral("text-cursor");
break;
case Okular::Annotation::AFileAttachment:
info.caption = i18n("File Attachment");
info.iconName = QStringLiteral("mail-attachment");
break;
case Okular::Annotation::ASound:
info.caption = i18n("Sound");
info.iconName = QStringLiteral("audio-x-generic");
break;
case Okular::Annotation::AMovie:
info.caption = i18n("Movie");
info.iconName = QStringLiteral("video-x-generic");
break;
case Okular::Annotation::AScreen:
info.caption = i18nc("Caption for a screen annotation", "Screen");
info.iconName = QStringLiteral("video-display");
break;
case Okular::Annotation::AWidget:
info.caption = i18nc("Caption for a widget annotation", "Widget");
info.iconName = QStringLiteral("preferences-desktop");
break;
case Okular::Annotation::ARichMedia:
info.caption = i18nc("Caption for a rich media annotation", "Rich Media");
info.iconName = QStringLiteral("video-x-generic"); // same as movie
break;
case Okular::Annotation::A_BASE:
// Fall through to default
break;
}
// If no specific info was set, use fallback
if (info.caption.isEmpty()) {
info.caption = i18n("Annotation");
info.iconName = QStringLiteral("okular");
}
return info;
}
QString captionForAnnotation(const Okular::Annotation *ann)
{
Q_ASSERT(ann);
return getAnnotationInfo(ann).caption;
}
QIcon iconForAnnotation(const Okular::Annotation *ann)
{
Q_ASSERT(ann);
AnnotationInfo info = getAnnotationInfo(ann);
return QIcon::fromTheme(info.iconName);
}
QString authorForAnnotation(const Okular::Annotation *ann)
{
Q_ASSERT(ann);
return !ann->author().isEmpty() ? ann->author() : i18nc("Unknown author", "Unknown");
}
QString contentsHtml(const Okular::Annotation *ann)
{
QString text = ann->contents().toHtmlEscaped();
text.replace(QLatin1Char('\n'), QLatin1String("<br>"));
return text;
}
QString prettyToolTip(const Okular::Annotation *ann)
{
Q_ASSERT(ann);
QString author = authorForAnnotation(ann);
QString contents = contentsHtml(ann);
QString tooltip = QStringLiteral("<qt><b>") + i18n("Author: %1", author) + QStringLiteral("</b>");
if (!contents.isEmpty()) {
tooltip += QStringLiteral("<div style=\"font-size: 4px;\"><hr /></div>") + contents;
}
tooltip += QLatin1String("</qt>");
return tooltip;
}
void saveEmbeddedFile(Okular::EmbeddedFile *ef, QWidget *parent)
{
const QString caption = i18n("Where do you want to save %1?", ef->name());
const QString path = QFileDialog::getSaveFileName(parent, caption, ef->name());
if (path.isEmpty()) {
return;
}
QFile targetFile(path);
writeEmbeddedFile(ef, parent, targetFile);
}
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()));
return;
}
target.write(ef->data());
target.close();
}
Okular::Movie *renditionMovieFromScreenAnnotation(const Okular::ScreenAnnotation *annotation)
{
if (!annotation) {
return nullptr;
}
if (annotation->action() && annotation->action()->actionType() == Okular::Action::Rendition) {
Okular::RenditionAction *renditionAction = static_cast<Okular::RenditionAction *>(annotation->action());
return renditionAction->movie();
}
return nullptr;
}
// from Arthur - qt4
static inline int qt_div_255(int x)
{
return (x + (x >> 8) + 0x80) >> 8;
}
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);
}
// iterate over all pixels changing the alpha component value
unsigned int *data = reinterpret_cast<unsigned int *>(grayImage.bits());
unsigned int pixels = grayImage.width() * grayImage.height();
int red = color.red(), green = color.green(), blue = color.blue();
for (unsigned int i = 0; i < pixels; ++i) { // optimize this loop keeping byte order into account
int source = data[i];
int sourceSat = qRed(source);
int newR = qt_div_255(sourceSat * red), newG = qt_div_255(sourceSat * green), newB = qt_div_255(sourceSat * blue);
if (int sourceAlpha = qAlpha(source); sourceAlpha == 255) {
// use destAlpha
data[i] = qRgba(newR, newG, newB, destAlpha);
} else {
// use destAlpha * sourceAlpha product
if (destAlpha < 255) {
sourceAlpha = qt_div_255(destAlpha * sourceAlpha);
}
data[i] = qRgba(newR, newG, newB, sourceAlpha);
}
}
}
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;
}
}
|