##// END OF EJS Templates
Defines actions for a data source items...
Defines actions for a data source items DataSourceItemAction is an object that will be connected to a QAction. When the QAction will be triggered, the DataSourceItemAction will be executed

File last commit:

r142:11579fae1cc2
r144:7a9a79ed3b02
Show More
DataSourceTreeWidgetItem.cpp
87 lines | 2.6 KiB | text/x-c | CppLexer
/ gui / src / DataSource / DataSourceTreeWidgetItem.cpp
#include <DataSource/DataSourceItem.h>
#include <DataSource/DataSourceTreeWidgetItem.h>
#include <SqpApplication.h>
#include <QAction>
Q_LOGGING_CATEGORY(LOG_DataSourceTreeWidgetItem, "DataSourceTreeWidgetItem")
namespace {
QIcon itemIcon(const DataSourceItem *dataSource)
{
if (dataSource) {
auto dataSourceType = dataSource->type();
switch (dataSourceType) {
case DataSourceItemType::NODE:
return sqpApp->style()->standardIcon(QStyle::SP_DirIcon);
case DataSourceItemType::PRODUCT:
return sqpApp->style()->standardIcon(QStyle::SP_FileIcon);
default:
// No action
break;
}
qCWarning(LOG_DataSourceTreeWidgetItem())
<< QObject::tr("Can't set data source icon : unknown data source type");
}
else {
qCWarning(LOG_DataSourceTreeWidgetItem())
<< QObject::tr("Can't set data source icon : the data source is null");
}
// Default cases
return QIcon{};
}
} // namespace
struct DataSourceTreeWidgetItem::DataSourceTreeWidgetItemPrivate {
explicit DataSourceTreeWidgetItemPrivate(const DataSourceItem *data) : m_Data{data} {}
/// Model used to retrieve data source information
const DataSourceItem *m_Data;
/// Actions associated to the item. The parent of the item (QTreeWidget) takes the ownership of
/// the actions
QList<QAction *> m_Actions;
};
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)}
{
// Sets the icon depending on the data source
setIcon(0, itemIcon(impl->m_Data));
/// @todo ALX : generate actions based on the DataSourceItem (model)
}
QVariant DataSourceTreeWidgetItem::data(int column, int role) const
{
if (role == Qt::DisplayRole) {
return (impl->m_Data) ? impl->m_Data->data(column) : QVariant{};
}
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);
}
}
QList<QAction *> DataSourceTreeWidgetItem::actions() const noexcept
{
return impl->m_Actions;
}