##// END OF EJS Templates
Handles tooltip for a data source item
Alexandre Leroux -
r319:4b46cbe03729
parent child
Show More
@@ -59,6 +59,9 public:
59 59 */
60 60 QVariant data(const QString &key) const noexcept;
61 61
62 /// Gets all data
63 const QHash<QString, QVariant> &data() const noexcept;
64
62 65 bool isRoot() const noexcept;
63 66
64 67 QString name() const noexcept;
@@ -70,6 +70,11 QVariant DataSourceItem::data(const QString &key) const noexcept
70 70 return impl->m_Data.value(key);
71 71 }
72 72
73 const QHash<QString, QVariant> &DataSourceItem::data() const noexcept
74 {
75 return impl->m_Data;
76 }
77
73 78 bool DataSourceItem::isRoot() const noexcept
74 79 {
75 80 return impl->m_Parent == nullptr;
@@ -41,6 +41,54 QIcon itemIcon(const DataSourceItem *dataSource)
41 41 return QIcon{};
42 42 }
43 43
44 /// @return the tooltip text for a variant. The text depends on whether the data is a simple variant
45 /// or a list of variants
46 QString tooltipValue(const QVariant &variant) noexcept
47 {
48 // If the variant is a list of variants, the text of the tooltip is of the form: {val1, val2,
49 // ...}
50 if (variant.canConvert<QVariantList>()) {
51 auto valueString = QStringLiteral("{");
52
53 auto variantList = variant.value<QVariantList>();
54 for (auto it = variantList.cbegin(), end = variantList.cend(); it != end; ++it) {
55 valueString.append(it->toString());
56
57 if (std::distance(it, end) != 1) {
58 valueString.append(", ");
59 }
60 }
61
62 valueString.append(QStringLiteral("}"));
63
64 return valueString;
65 }
66 else {
67 return variant.toString();
68 }
69 }
70
71 QString itemTooltip(const DataSourceItem *dataSource) noexcept
72 {
73 // The tooltip displays all item's data
74 if (dataSource) {
75 auto result = QString{};
76
77 const auto &data = dataSource->data();
78 for (auto it = data.cbegin(), end = data.cend(); it != end; ++it) {
79 result.append(QString{"<b>%1:</b> %2<br/>"}.arg(it.key(), tooltipValue(it.value())));
80 }
81
82 return result;
83 }
84 else {
85 qCCritical(LOG_DataSourceTreeWidgetItem())
86 << QObject::tr("Can't set data source tooltip : the data source is null");
87
88 return QString{};
89 }
90 }
91
44 92 } // namespace
45 93
46 94 struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
@@ -63,8 +111,9 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const Da
63 111 : QTreeWidgetItem{parent, type},
64 112 impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)}
65 113 {
66 // Sets the icon depending on the data source
114 // Sets the icon and the tooltip depending on the data source
67 115 setIcon(0, itemIcon(impl->m_Data));
116 setToolTip(0, itemTooltip(impl->m_Data));
68 117
69 118 // Generates tree actions based on the item actions
70 119 auto createTreeAction = [this, &parent](const auto &itemAction) {
General Comments 0
You need to be logged in to leave comments. Login now