@@ -59,6 +59,9 public: | |||||
59 | */ |
|
59 | */ | |
60 | QVariant data(const QString &key) const noexcept; |
|
60 | QVariant data(const QString &key) const noexcept; | |
61 |
|
61 | |||
|
62 | /// Gets all data | |||
|
63 | const QHash<QString, QVariant> &data() const noexcept; | |||
|
64 | ||||
62 | bool isRoot() const noexcept; |
|
65 | bool isRoot() const noexcept; | |
63 |
|
66 | |||
64 | QString name() const noexcept; |
|
67 | QString name() const noexcept; |
@@ -70,6 +70,11 QVariant DataSourceItem::data(const QString &key) const noexcept | |||||
70 | return impl->m_Data.value(key); |
|
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 | bool DataSourceItem::isRoot() const noexcept |
|
78 | bool DataSourceItem::isRoot() const noexcept | |
74 | { |
|
79 | { | |
75 | return impl->m_Parent == nullptr; |
|
80 | return impl->m_Parent == nullptr; |
@@ -41,6 +41,54 QIcon itemIcon(const DataSourceItem *dataSource) | |||||
41 | return QIcon{}; |
|
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 | } // namespace |
|
92 | } // namespace | |
45 |
|
93 | |||
46 | struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { |
|
94 | struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { | |
@@ -63,8 +111,9 DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const Da | |||||
63 | : QTreeWidgetItem{parent, type}, |
|
111 | : QTreeWidgetItem{parent, type}, | |
64 | impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)} |
|
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 | setIcon(0, itemIcon(impl->m_Data)); |
|
115 | setIcon(0, itemIcon(impl->m_Data)); | |
|
116 | setToolTip(0, itemTooltip(impl->m_Data)); | |||
68 |
|
117 | |||
69 | // Generates tree actions based on the item actions |
|
118 | // Generates tree actions based on the item actions | |
70 | auto createTreeAction = [this, &parent](const auto &itemAction) { |
|
119 | auto createTreeAction = [this, &parent](const auto &itemAction) { |
General Comments 0
You need to be logged in to leave comments.
Login now