DataSourceTreeWidgetItem.cpp
185 lines
| 5.7 KiB
| text/x-c
|
CppLexer
|
r80 | #include <DataSource/DataSourceItem.h> | ||
|
r145 | #include <DataSource/DataSourceItemAction.h> | ||
|
r80 | #include <DataSource/DataSourceTreeWidgetItem.h> | ||
|
r142 | #include <QAction> | ||
|
r80 | Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem") | ||
|
r81 | namespace { | ||
|
r343 | // Column indexes | ||
const auto NAME_COLUMN = 0; | ||||
|
r81 | QIcon itemIcon(const DataSourceItem *dataSource) | ||
{ | ||||
if (dataSource) { | ||||
auto dataSourceType = dataSource->type(); | ||||
switch (dataSourceType) { | ||||
|
r345 | case DataSourceItemType::NODE: { | ||
return dataSource->isRoot() ? QIcon{":/icones/dataSourceRoot.png"} | ||||
: QIcon{":/icones/dataSourceNode.png"}; | ||||
} | ||||
|
r81 | case DataSourceItemType::PRODUCT: | ||
|
r344 | return QIcon{":/icones/dataSourceProduct.png"}; | ||
case DataSourceItemType::COMPONENT: | ||||
return QIcon{":/icones/dataSourceComponent.png"}; | ||||
|
r81 | default: | ||
// No action | ||||
break; | ||||
} | ||||
|
r93 | |||
qCWarning(LOG_DataSourceTreeWidgetItem()) | ||||
<< QObject::tr("Can't set data source icon : unknown data source type"); | ||||
} | ||||
else { | ||||
|
r345 | qCCritical(LOG_DataSourceTreeWidgetItem()) | ||
|
r93 | << QObject::tr("Can't set data source icon : the data source is null"); | ||
|
r81 | } | ||
// Default cases | ||||
return QIcon{}; | ||||
} | ||||
|
r346 | /// @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<QVariantList>()) { | ||||
auto valueString = QStringLiteral("{"); | ||||
auto variantList = variant.value<QVariantList>(); | ||||
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{"<b>%1:</b> %2<br/>"}.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{}; | ||||
} | ||||
} | ||||
|
r81 | } // namespace | ||
|
r80 | struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate { | ||
explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {} | ||||
/// Model used to retrieve data source information | ||||
const DataSourceItem *m_Data; | ||||
|
r142 | /// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of | ||
/// the actions | ||||
QList<QAction *> m_Actions; | ||||
|
r80 | }; | ||
DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(const DataSourceItem *data, int type) | ||||
: DataSourceTreeWidgetItem{nullptr, data, type} | ||||
{ | ||||
} | ||||
DataSourceTreeWidgetItem::DataSourceTreeWidgetItem(QTreeWidget *parent, const DataSourceItem *data, | ||||
int type) | ||||
: QTreeWidgetItem{parent, type}, | ||||
impl{spimpl::make_unique_impl<DataSourceTreeWidgetItemPrivate>(data)} | ||||
{ | ||||
|
r346 | // Sets the icon and the tooltip depending on the data source | ||
|
r81 | setIcon(0, itemIcon(impl->m_Data)); | ||
|
r346 | setToolTip(0, itemTooltip(impl->m_Data)); | ||
|
r142 | |||
|
r145 | // Generates tree actions based on the item actions | ||
auto createTreeAction = [this, &parent](const auto &itemAction) { | ||||
auto treeAction = new QAction{itemAction->name(), parent}; | ||||
// Executes item action when tree action is triggered | ||||
QObject::connect(treeAction, &QAction::triggered, itemAction, | ||||
&DataSourceItemAction::execute); | ||||
return treeAction; | ||||
}; | ||||
auto itemActions = impl->m_Data->actions(); | ||||
std::transform(std::cbegin(itemActions), std::cend(itemActions), | ||||
std::back_inserter(impl->m_Actions), createTreeAction); | ||||
r862 | ||||
// Sets the flags of the items | ||||
auto flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; | ||||
if (data->type() == DataSourceItemType::COMPONENT | ||||
|| data->type() == DataSourceItemType::PRODUCT) { | ||||
flags |= Qt::ItemIsDragEnabled; | ||||
} | ||||
setFlags(flags); | ||||
|
r80 | } | ||
|
r477 | const DataSourceItem *DataSourceTreeWidgetItem::data() const | ||
{ | ||||
return impl->m_Data; | ||||
} | ||||
|
r80 | QVariant DataSourceTreeWidgetItem::data(int column, int role) const | ||
{ | ||||
if (role == Qt::DisplayRole) { | ||||
|
r343 | if (impl->m_Data) { | ||
switch (column) { | ||||
case NAME_COLUMN: | ||||
return impl->m_Data->name(); | ||||
default: | ||||
// No action | ||||
break; | ||||
} | ||||
qCWarning(LOG_DataSourceTreeWidgetItem()) | ||||
<< QObject::tr("Can't get data (unknown column %1)").arg(column); | ||||
} | ||||
else { | ||||
qCCritical(LOG_DataSourceTreeWidgetItem()) << QObject::tr("Can't get data (null item)"); | ||||
} | ||||
return QVariant{}; | ||||
|
r80 | } | ||
else { | ||||
return QTreeWidgetItem::data(column, role); | ||||
} | ||||
} | ||||
void DataSourceTreeWidgetItem::setData(int column, int role, const QVariant &value) | ||||
{ | ||||
// Data can't be changed by edition | ||||
if (role != Qt::EditRole) { | ||||
QTreeWidgetItem::setData(column, role, value); | ||||
} | ||||
} | ||||
|
r142 | |||
QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept | ||||
{ | ||||
return impl->m_Actions; | ||||
} | ||||