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
19 : KoItemToolTip(widget)
20{
21}
22
26
27QTextDocument *NodeToolTip::createDocument(const QModelIndex &index)
28{
29 QTextDocument *doc = new QTextDocument(this);
30
31 int size = 250*devicePixelRatioF();
32 QImage thumb = index.data(int(KisNodeModel::BeginThumbnailRole) + size).value<QImage>();
33 thumb.setDevicePixelRatio(devicePixelRatioF());
34 doc->addResource(QTextDocument::ImageResource, QUrl("data:thumbnail"), thumb);
35
36 QString name = index.data(Qt::DisplayRole).toString();
38 QString rows;
39 // Note: Can't use <nobr> due to https://bugreports.qt.io/browse/QTBUG-1135
40 // It breaks randomly with CJK. Works fine with `white-space:pre` though.
41 const QString row = QString("<tr><td align=\"right\"><p style=\"white-space:pre\">%1:</p></td><td align=\"left\">%2</td></tr>");
42 QString value;
43 for(int i = 0, n = properties.count(); i < n; ++i) {
44 if (properties[i].id == KisLayerPropertiesIcons::layerError.id() ||
45 properties[i].id == KisLayerPropertiesIcons::layerColorSpaceMismatch.id()) continue;
46
47 if (properties[i].isMutable)
48 value = properties[i].state.toBool() ? i18n("Yes") : i18n("No");
49 else
50 value = properties[i].state.toString();
51
52 rows.append(row.arg(properties[i].name).arg(value));
53 }
54
55 QString dropReason = index.data(KisNodeModel::DropReasonRole).toString();
56
57 if (!dropReason.isEmpty()) {
58 dropReason = QString("<p align=\"center\"><b>%1</b></p>").arg(dropReason);
59 }
60
61 QString errorMessage;
62 {
63 auto addWarningProperty = [&] (const QString &propertyId) {
64 auto it = std::find_if(properties.begin(), properties.end(),
66 propertyId));
67
68 if (it != properties.end()) {
69 doc->addResource(QTextDocument::ImageResource, QUrl(QString("data:symbol_%1").arg(it->id)), it->onIcon.pixmap(QSize(32,32)).toImage());
70 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);
71 }
72 };
73
74 addWarningProperty(KisLayerPropertiesIcons::layerError.id());
76 }
77
78 rows = QString("<table>%1</table>").arg(rows);
79
80 const QString image = QString("<table border=\"1\"><tr><td><img src=\"data:thumbnail\"></td></tr></table>");
81 const QString body = QString("<h3 align=\"center\">%1</h3>").arg(name)
82 + errorMessage
83 + dropReason
84 + QString("<p><table><tr><td>%1</td><td>%2</td></tr></table></p>").arg(image).arg(rows);
85 const QString html = QString("<html><body>%1</body></html>").arg(body);
86
87 doc->setHtml(html);
88
89 const int margin = 16;
90 doc->setTextWidth(qMin(doc->size().width() + 2 * margin, qreal(600.0)));
91
92 doc->setDocumentMargin(margin);
93 doc->setUseDesignMetrics(true);
94
95 return doc;
96}
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(QWidget *widget)
~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