Krita Source Code Documentation
Loading...
Searching...
No Matches
NodeToolTip.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2006 Gábor Lehel <illissius@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "NodeToolTip.h"
7#include "kis_node_model.h"
8
9#include <QImage>
10#include <QModelIndex>
11#include <QTextDocument>
12#include <QUrl>
13#include <klocalizedstring.h>
14
15#include <kis_base_node.h>
17
21
25
26QTextDocument *NodeToolTip::createDocument(const QModelIndex &index)
27{
28 QTextDocument *doc = new QTextDocument(this);
29
30 int size = 250*devicePixelRatioF();
31 QImage thumb = index.data(int(KisNodeModel::BeginThumbnailRole) + size).value<QImage>();
32 thumb.setDevicePixelRatio(devicePixelRatioF());
33 doc->addResource(QTextDocument::ImageResource, QUrl("data:thumbnail"), thumb);
34
35 QString name = index.data(Qt::DisplayRole).toString();
37 QString rows;
38 // Note: Can't use <nobr> due to https://bugreports.qt.io/browse/QTBUG-1135
39 // It breaks randomly with CJK. Works fine with `white-space:pre` though.
40 const QString row = QString("<tr><td align=\"right\"><p style=\"white-space:pre\">%1:</p></td><td align=\"left\">%2</td></tr>");
41 QString value;
42 for(int i = 0, n = properties.count(); i < n; ++i) {
43 if (properties[i].id == KisLayerPropertiesIcons::layerError.id() ||
44 properties[i].id == KisLayerPropertiesIcons::layerColorSpaceMismatch.id()) continue;
45
46 if (properties[i].isMutable)
47 value = properties[i].state.toBool() ? i18n("Yes") : i18n("No");
48 else
49 value = properties[i].state.toString();
50
51 rows.append(row.arg(properties[i].name).arg(value));
52 }
53
54 QString dropReason = index.data(KisNodeModel::DropReasonRole).toString();
55
56 if (!dropReason.isEmpty()) {
57 dropReason = QString("<p align=\"center\"><b>%1</b></p>").arg(dropReason);
58 }
59
60 QString errorMessage;
61 {
62 auto addWarningProperty = [&] (const QString &propertyId) {
63 auto it = std::find_if(properties.begin(), properties.end(),
65 propertyId));
66
67 if (it != properties.end()) {
68 doc->addResource(QTextDocument::ImageResource, QUrl(QString("data:symbol_%1").arg(it->id)), it->onIcon.pixmap(QSize(32,32)).toImage());
69 errorMessage += QString("<table align=\"center\" border=\"0\"><tr valign=\"middle\"><td align=\"right\"><img src=\"data:symbol_%2\"></td><td align=\"left\"><b>%1</b></td></tr></table>").arg(it->state.toString(), it->id);
70 }
71 };
72
73 addWarningProperty(KisLayerPropertiesIcons::layerError.id());
75 }
76
77 rows = QString("<table>%1</table>").arg(rows);
78
79 const QString image = QString("<table border=\"1\"><tr><td><img src=\"data:thumbnail\"></td></tr></table>");
80 const QString body = QString("<h3 align=\"center\">%1</h3>").arg(name)
81 + errorMessage
82 + dropReason
83 + QString("<p><table><tr><td>%1</td><td>%2</td></tr></table></p>").arg(image).arg(rows);
84 const QString html = QString("<html><body>%1</body></html>").arg(body);
85
86 doc->setHtml(html);
87
88 const int margin = 16;
89 doc->setTextWidth(qMin(doc->size().width() + 2 * margin, qreal(600.0)));
90
91 doc->setDocumentMargin(margin);
92 doc->setUseDesignMetrics(true);
93
94 return doc;
95}
float value(const T *src, size_t ch)
@ PropertiesRole
A list of properties the part has.
QString id() const
Definition KoID.cpp:63
QPersistentModelIndex index
QTextDocument * createDocument(const QModelIndex &index) override
~NodeToolTip() override
auto mem_equal_to(MemTypeNoRef Class::*ptr, MemType &&value)
mem_equal_to is an unary functor that compares a member of the object to a given value
Definition KisMpl.h:233