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
23 : KoItemToolTip(parent)
24{
25}
26
30
35
37{
38 if (value) {
40 } else {
41 m_checkersPainter.reset();
42 }
43}
44
45QTextDocument *KisIconToolTip::createDocument(const QModelIndex &index)
46{
47 QTextDocument *doc = new QTextDocument(this);
48
49 QImage thumb = index.data(Qt::DecorationRole).value<QImage>();
50 if (thumb.isNull()) {
51 thumb = index.data(Qt::UserRole + KisAbstractResourceModel::Thumbnail).value<QImage>();
52 }
53
54 if (!m_fixedToolTipThumbnailSize.isEmpty() && !thumb.isNull()) {
55 int pixelSize = 48; // // let's say, 48x48?
56 if (!thumb.isNull()) {
57 // affects mostly gradients, which are very long but only 1px tall
58 Qt::AspectRatioMode aspectRatioMode = thumb.height() == 1 ? Qt::IgnoreAspectRatio : Qt::KeepAspectRatio;
59 // this allows the pixel patterns to be displayed correctly,
60 // while the presets (which have 200x200 thumbnails) will still be pretty
61 // Fast Transformation == Nearest Neighbour
62 Qt::TransformationMode transformationMode = (thumb.width() < pixelSize || thumb.height() < pixelSize) ? Qt::FastTransformation : Qt::SmoothTransformation;
63
66 * devicePixelRatioF(),
67 aspectRatioMode,
68 transformationMode);
69 }
70 }
71
73 QImage image(thumb.size(), QImage::Format_ARGB32);
74
75 {
76 QPainter gc(&image);
77 m_checkersPainter->paint(gc, thumb.rect());
78 gc.drawImage(QPoint(), thumb);
79 }
80
81 thumb = image;
82 }
83
84 thumb.setDevicePixelRatio(devicePixelRatioF());
85 doc->addResource(QTextDocument::ImageResource, QUrl("data:thumbnail"), thumb);
86
87 QString name = index.data(Qt::DisplayRole).toString();
88 QString presetDisplayName = index.data(Qt::UserRole + KisAbstractResourceModel::Name).toString().replace("_", " ");
89 //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.
90 if (!presetDisplayName.isEmpty()) {
91 name = presetDisplayName;
92 }
93
94 QString translatedName = index.data(Qt::UserRole + KisAbstractResourceModel::Tooltip).toString().replace("_", " ");
95
96 QString tagsRow;
97 QString tagsData = index.data(Qt::UserRole + KisAbstractResourceModel::Tags).toStringList().join(", ");
98 if (!tagsData.isEmpty()) {
99 const QString list = QString("<ul style=\"list-style-type: none; margin: 0px;\">%1</ul> ").arg(tagsData);
100 tagsRow = QString("<tr><td>%1:</td><td style=\"text-align: right;\">%2</td></tr>").arg(i18n("Tags"), list);
101 }
102
103 const QString brokenReason = index.data(Qt::UserRole + KisAbstractResourceModel::BrokenStatusMessage).toString();
104 const QString brokenRow =
105 !brokenReason.isEmpty()
106 ? QString("<tr><td colspan=\"2\"><b>%1</b></td></tr>"
107 "<tr><td colspan=\"2\">%2</td></tr>")
108 .arg(i18n("Resource is broken!"), brokenReason)
109 : QString();
110
111 QString location = index.data(Qt::UserRole + KisAbstractResourceModel::Location).toString();
112 if (location.isEmpty()) {
113 location = i18nc("a placeholder name for the default storage of resources", "resource folder");
114 }
115
116 const QString locationRow = QString("<tr><td>%1:</td><td style=\"text-align: right;\">%2</td></tr>").arg(i18n("Location"), location);
117
118 const QString footerTable = QString("<p><table>%1%2%3</table></p>").arg(brokenRow).arg(tagsRow).arg(locationRow);
119
120 const QString image = QString("<center><img src=\"data:thumbnail\"></center>");
121 QString body = QString("<h3 align=\"center\">%1</h3>%2%3").arg(name, image, footerTable);
122 if (translatedName != name) {
123 body = QString("<h3 align=\"center\">%1</h3><h4 align=\"center\">%2</h4>%3%4").arg(name, translatedName, image, footerTable);
124 }
125 const QString html = QString("<html><body>%1</body></html>").arg(body);
126
127 doc->setHtml(html);
128
129 const int margin = 16;
130 doc->setTextWidth(qMin(doc->size().width() + 2 * margin, qreal(500.0)));
131 doc->setDocumentMargin(margin);
132 doc->setUseDesignMetrics(true);
133
134 return doc;
135}
float value(const T *src, size_t ch)
KisIconToolTip(QWidget *parent)
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