From 4b46cbe0372906086426b01a6386a39b3924cb01 2017-07-05 09:04:17 From: Alexandre Leroux Date: 2017-07-05 09:04:17 Subject: [PATCH] Handles tooltip for a data source item --- diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h index d72683f..58bd71e 100644 --- a/core/include/DataSource/DataSourceItem.h +++ b/core/include/DataSource/DataSourceItem.h @@ -59,6 +59,9 @@ public: */ QVariant data(const QString &key) const noexcept; + /// Gets all data + const QHash &data() const noexcept; + bool isRoot() const noexcept; QString name() const noexcept; diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp index 0144f3b..7f52669 100644 --- a/core/src/DataSource/DataSourceItem.cpp +++ b/core/src/DataSource/DataSourceItem.cpp @@ -70,6 +70,11 @@ QVariant DataSourceItem::data(const QString &key) const noexcept return impl->m_Data.value(key); } +const QHash &DataSourceItem::data() const noexcept +{ + return impl->m_Data; +} + bool DataSourceItem::isRoot() const noexcept { return impl->m_Parent == nullptr; diff --git a/gui/src/DataSource/DataSourceTreeWidgetItem.cpp b/gui/src/DataSource/DataSourceTreeWidgetItem.cpp index e558105..22a3cde 100644 --- a/gui/src/DataSource/DataSourceTreeWidgetItem.cpp +++ b/gui/src/DataSource/DataSourceTreeWidgetItem.cpp @@ -41,6 +41,54 @@ QIcon itemIcon(const DataSourceItem *dataSource) return QIcon{}; } +/// @return the tooltip text for a variant. The text depends on whether the data is a simple variant +/// or a list of variants +QString tooltipValue(const QVariant &variant) noexcept +{ + // If the variant is a list of variants, the text of the tooltip is of the form: {val1, val2, + // ...} + if (variant.canConvert()) { + auto valueString = QStringLiteral("{"); + + auto variantList = variant.value(); + for (auto it = variantList.cbegin(), end = variantList.cend(); it != end; ++it) { + valueString.append(it->toString()); + + if (std::distance(it, end) != 1) { + valueString.append(", "); + } + } + + valueString.append(QStringLiteral("}")); + + return valueString; + } + else { + return variant.toString(); + } +} + +QString itemTooltip(const DataSourceItem *dataSource) noexcept +{ + // The tooltip displays all item's data + if (dataSource) { + auto result = QString{}; + + const auto &data = dataSource->data(); + for (auto it = data.cbegin(), end = data.cend(); it != end; ++it) { + result.append(QString{"%1: %2
"}.arg(it.key(), tooltipValue(it.value()))); + } + + return result; + } + else { + qCCritical(LOG_DataSourceTreeWidgetItem()) + << QObject::tr("Can't set data source tooltip : the data source is null"); + + return QString{}; + } +} + } // namespace struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { @@ -63,8 +111,9 @@ DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const Da : QTreeWidgetItem{parent, type}, impl{spimpl::make_unique_impl(data)} { - // Sets the icon depending on the data source + // Sets the icon and the tooltip depending on the data source setIcon(0, itemIcon(impl->m_Data)); + setToolTip(0, itemTooltip(impl->m_Data)); // Generates tree actions based on the item actions auto createTreeAction = [this, &parent](const auto &itemAction) {