Krita Source Code Documentation
Loading...
Searching...
No Matches
KisIconToolTip.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 1999 Carsten Pfeiffer (pfeiffer@kde.org)
3 * SPDX-FileCopyrightText: 2002 Igor Jansen (rm@kde.org)
4 * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "KisIconToolTip.h"
10
11#include <QTextDocument>
12#include <QUrl>
13#include <QModelIndex>
14#include <QPainter>
15
16#include <KisResourceModel.h>
18#include <klocalizedstring.h>
19
21
25
29
34
36{
37 if (value) {
39 } else {
40 m_checkersPainter.reset();
41 }
42}
43
44QTextDocument *KisIconToolTip::createDocument(const QModelIndex &index)
45{
46 QTextDocument *doc = new QTextDocument(this);
47
48 QImage thumb = index.data(Qt::DecorationRole).value<QImage>();
49 if (thumb.isNull()) {
50 thumb = index.data(Qt::UserRole + KisAbstractResourceModel::Thumbnail).value<QImage>();
51 }
52
53 if (!m_fixedToolTipThumbnailSize.isEmpty() && !thumb.isNull()) {
54 int pixelSize = 48; // // let's say, 48x48?
55 if (!thumb.isNull()) {
56 // affects mostly gradients, which are very long but only 1px tall
57 Qt::AspectRatioMode aspectRatioMode = thumb.height() == 1 ? Qt::IgnoreAspectRatio : Qt::KeepAspectRatio;
58 // this allows the pixel patterns to be displayed correctly,
59 // while the presets (which have 200x200 thumbnails) will still be pretty
60 // Fast Transformation == Nearest Neighbour
61 Qt::TransformationMode transformationMode = (thumb.width() < pixelSize || thumb.height() < pixelSize) ? Qt::FastTransformation : Qt::SmoothTransformation;
62
65 * devicePixelRatioF(),
66 aspectRatioMode,
67 transformationMode);
68 }
69 }
70
72 QImage image(thumb.size(), QImage::Format_ARGB32);
73
74 {
75 QPainter gc(&image);
76 m_checkersPainter->paint(gc, thumb.rect());
77 gc.drawImage(QPoint(), thumb);
78 }
79
80 thumb = image;
81 }
82
83 thumb.setDevicePixelRatio(devicePixelRatioF());
84 doc->addResource(QTextDocument::ImageResource, QUrl("data:thumbnail"), thumb);
85
86 QString name = index.data(Qt::DisplayRole).toString();
87 QString presetDisplayName = index.data(Qt::UserRole + KisAbstractResourceModel::Name).toString().replace("_", " ");
88 //This is to ensure that the other uses of this class don't get an empty string, while resource management should get a nice string.
89 if (!presetDisplayName.isEmpty()) {
90 name = presetDisplayName;
91 }
92
93 QString translatedName = index.data(Qt::UserRole + KisAbstractResourceModel::Tooltip).toString().replace("_", " ");
94
95 QString tagsRow;
96 QString tagsData = index.data(Qt::UserRole + KisAbstractResourceModel::Tags).toStringList().join(", ");
97 if (!tagsData.isEmpty()) {
98 const QString list = QString("<ul style=\"list-style-type: none; margin: 0px;\">%1</ul> ").arg(tagsData);
99 tagsRow = QString("<tr><td>%1:</td><td style=\"text-align: right;\">%2</td></tr>").arg(i18n("Tags"), list);
100 }
101
102 const QString brokenReason = index.data(Qt::UserRole + KisAbstractResourceModel::BrokenStatusMessage).toString();
103 const QString brokenRow =
104 !brokenReason.isEmpty()
105 ? QString("<tr><td colspan=\"2\"><b>%1</b></td></tr>"
106 "<tr><td colspan=\"2\">%2</td></tr>")
107 .arg(i18n("Resource is broken!"), brokenReason)
108 : QString();
109
110 QString location = index.data(Qt::UserRole + KisAbstractResourceModel::Location).toString();
111 if (location.isEmpty()) {
112 location = i18nc("a placeholder name for the default storage of resources", "resource folder");
113 }
114
115 const QString locationRow = QString("<tr><td>%1:</td><td style=\"text-align: right;\">%2</td></tr>").arg(i18n("Location"), location);
116
117 const QString footerTable = QString("<p><table>%1%2%3</table></p>").arg(brokenRow).arg(tagsRow).arg(locationRow);
118
119 const QString image = QString("<center><img src=\"data:thumbnail\"></center>");
120 QString body = QString("<h3 align=\"center\">%1</h3>%2%3").arg(name, image, footerTable);
121 if (translatedName != name) {
122 body = QString("<h3 align=\"center\">%1</h3><h4 align=\"center\">%2</h4>%3%4").arg(name, translatedName, image, footerTable);
123 }
124 const QString html = QString("<html><body>%1</body></html>").arg(body);
125
126 doc->setHtml(html);
127
128 const int margin = 16;
129 doc->setTextWidth(qMin(doc->size().width() + 2 * margin, qreal(500.0)));
130 doc->setDocumentMargin(margin);
131 doc->setUseDesignMetrics(true);
132
133 return doc;
134}
float value(const T *src, size_t ch)
void setFixedToolTipThumbnailSize(const QSize &size)
QScopedPointer< KoCheckerBoardPainter > m_checkersPainter
void setToolTipShouldRenderCheckers(bool value)
~KisIconToolTip() override
QSize m_fixedToolTipThumbnailSize
QTextDocument * createDocument(const QModelIndex &index) override
static KisResourceThumbnailCache * instance()
QImage getImage(const QModelIndex &index, const QSize size=QSize(-1, -1), Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode=Qt::FastTransformation)
QPersistentModelIndex index